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
- Convert to method shorthand: `{ start: function () {} }` -> `{ start() {} }`.
- Set `methodsIgnorePattern` for names that must stay function properties (e.g. generators used as values).
- Use `oxlint --fix` for a bulk rewrite.
- 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
- Prefer `{ method() {} }` over `{ method: function () {} }` in modern code.
- Set methodsIgnorePattern when some names must remain plain function properties.
- Run the autofix when onboarding legacy ES5 object modules.
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
- Expected property shorthand.
- Unnecessarily computed property `{key}` found.
- Use the rest parameters instead of `arguments`.
- Use spread operators instead of `.apply()`.
- Expected shorthand for all properties.
AI-assisted analysis of oxc-project/oxc@a3d33dda7c (2026-08-20).
Data as JSON: /api/errors/09bc38b326bb0866.
Report an issue: GitHub.