oxc-project/oxc · warning · OxcDiagnostic

Expected longform method syntax for string literal keys.

Error message

Expected longform method syntax for string literal keys.

What it means

Diagnostic from `object-shorthand` when the `avoidQuotes` option is enabled and a shorthand method uses a quoted string-literal key (e.g. `{ "foo"() {} }`). The option exists because quoted keys in shorthand position hurt readability and diff tooling, so the rule asks for longform method syntax (`"foo": function () {}`) instead. Fired from the apply-to-methods branch when the key is a string literal.

Source

Thrown at crates/oxc_linter/src/rules/eslint/object_shorthand.rs:30

use oxc_ast_visit::{Visit, walk};
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_span::{GetSpan, Span};
use serde::Deserialize;

use crate::{
    AstNode,
    context::LintContext,
    rule::{Rule, TupleRuleConfig},
    utils::deserialize_regex_option,
};

fn expected_all_properties_shorthanded(span: Span) -> OxcDiagnostic {
    OxcDiagnostic::warn("Expected shorthand for all properties.").with_label(span)
}

fn expected_literal_method_longform(span: Span) -> OxcDiagnostic {
    OxcDiagnostic::warn("Expected longform method syntax for string literal keys.").with_label(span)
}

fn expected_property_shorthand(span: Span) -> OxcDiagnostic {
    OxcDiagnostic::warn("Expected property shorthand.").with_label(span)
}

fn expected_property_longform(span: Span) -> OxcDiagnostic {
    OxcDiagnostic::warn("Expected longform property syntax.").with_label(span)
}

fn expected_method_shorthand(span: Span) -> OxcDiagnostic {
    OxcDiagnostic::warn("Expected method shorthand.").with_label(span)
}

fn expected_method_longform(span: Span) -> OxcDiagnostic {
    OxcDiagnostic::warn("Expected longform method syntax.").with_label(span)
}

View on GitHub (pinned to a3d33dda7c)

Solutions

  1. Write the method in longform: `{ "foo": function () {} }`.
  2. If the key is identifier-safe, drop the quotes and keep shorthand: `{ foo() {} }`.
  3. Use a computed key only when the key is genuinely dynamic.
  4. Let `oxlint --fix` convert the shorthand to longform automatically.

Example fix

// before
const o = {
  "fetch data"() { return 1; },
};

// after
const o = {
  "fetch data": function () { return 1; },
};
Defensive patterns

Strategy: validation

Validate before calling

const hasQuotedShorthandMethod = /\{\s*['"][^'"]+['"]\s*\(/.test(objectLiteralSource);

Prevention

When it happens

Trigger: Config `"object-shorthand": ["error", "always", { "avoidQuotes": true }]` and code like `const o = { "some key"() { return 1; } }` or `{ 'foo'() {} }`. Only method shorthands with quoted keys are reported; unquoted shorthand methods are fine.

Common situations: Teams adopting avoidQuotes to keep JSON-like property style; keys generated with spaces or reserved words where authors reached for quote-plus-shorthand syntax.

Related errors


AI-assisted analysis of oxc-project/oxc@a3d33dda7c (2026-08-20). Data as JSON: /api/errors/94b58defdbcf064e. Report an issue: GitHub.