oxc-project/oxc · warning · OxcDiagnostic

Expected method shorthand.

Error message

Expected method shorthand.

What it means

Diagnostic from `object-shorthand` with apply-to-methods on (the default `always`/`methods` ShorthandType). A method is written longform as a function-valued property, `{ a: function () {} }`, and ES6 method shorthand `{ a() {} }` is expected. Oxc flags ObjectProperty nodes whose value is a plain Function/ArrowExpression under the methods branch.

Source

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

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)
}

fn unexpected_mix(span: Span) -> OxcDiagnostic {
    OxcDiagnostic::warn("Unexpected mix of shorthand and non-shorthand properties.")
        .with_label(span)
}

#[derive(Debug, Default, Clone)]
pub struct ObjectShorthand(Box<ObjectShorthandConfig>);

#[derive(Debug, Default, Clone, JsonSchema, Deserialize)]
#[serde(default)]
pub struct ObjectShorthandTupleConfig(ShorthandType, ObjectShorthandOptions);

View on GitHub (pinned to a3d33dda7c)

Solutions

  1. Convert to method shorthand: `{ start: function () {} }` -> `{ start() {} }`.
  2. Set `methodsIgnorePattern` for names that must stay function properties (e.g. generators used as values).
  3. Use `oxlint --fix` for a bulk rewrite.
  4. If arrow semantics (`this` binding) matter, keep arrows but as `start: () => {}` only if configured to allow them, or refactor to methods.

Example fix

// before
const api = {
  start: function () { return 1; },
};

// after
const api = {
  start() { return 1; },
};
Defensive patterns

Strategy: validation

Validate before calling

const hasLongformMethod = /:\s*(function|\([^)]*\)\s*=>)/.test(objectLiteralSource);

Prevention

When it happens

Trigger: `const server = { start: function () {...}, stop: function () {...} };` or `{ handler: (e) => {} }` under method checks. Options `avoidExplicitReturnArrows` and `methodsIgnorePattern` (a regex exempting method names) refine what is reported; arrow-function properties may be treated separately from function expressions.

Common situations: ES5-era object module patterns (`var api = { init: function () {} }`) being linted with modern defaults; bundling legacy libraries into a linted monorepo.

Related errors


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