oxc-project/oxc · warning · OxcDiagnostic

Expected shorthand for all properties.

Error message

Expected shorthand for all properties.

What it means

Diagnostic from the `object-shorthand` rule with the `consistent-as-needed` option. Every property in the object could be written shorthand, but at least one is longform, so the rule demands all of them be shorthand for consistency. Oxc's config machinery (ObjectShorthandTupleConfig with ShorthandType ConsistentAsNeeded) drives this branch; the diagnostic labels the offending property span.

Source

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

        ArrowFunctionExpression, Expression, Function, ObjectExpression, ObjectProperty,
        ObjectPropertyKind, PropertyKind,
    },
};
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)
}

View on GitHub (pinned to a3d33dda7c)

Solutions

  1. Convert all value-identical properties to shorthand: `{ x: x, y: y }` -> `{ x, y }`.
  2. Use method shorthand for function values: `{ a: function () {} }` -> `{ a() {} }`.
  3. Run `oxlint --fix` to rewrite the object automatically.
  4. If the longform is intentional (computed or differently-named keys), pick the plain `always` option instead of consistent-as-needed.

Example fix

// before
const x = 1, y = 2;
const o = { x: x, y: y };

// after
const o = { x, y };
Defensive patterns

Strategy: validation

Validate before calling

const hasValueIdenticalPair = /\{\s*[\w$]+\s*:\s*[\w$]+\s*[\},]/.test(objectLiteralSource) &&
  objectLiteralSource.match(/([\w$]+)\s*:\s*([\w$]+)/g)?.every(m => { const [k, v] = m.split(':').map(s => s.trim()); return k !== v; });

Prevention

When it happens

Trigger: `const x = 1, y = 2; const o = { x: x, y };` where every key equals its value-identifier, or `const o = { foo: foo, bar() {} }` under consistent-as-needed. Fires when the whole object is 'shorthand-able' but mixed or all-longform.

Common situations: Enabling consistent-as-needed in shared configs after code was written longform; merging objects during refactors that leave `{ x: x }` pairs behind.

Related errors


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