{"record":{"id":"31f98642ec41890f","repo":"oxc-project/oxc","slug":"unnecessarily-computed-property-key-found","errorCode":null,"errorMessage":"Unnecessarily computed property `{key}` found.","messagePattern":"Unnecessarily computed property `(.+?)` found\\.","errorType":"validation","errorClass":"OxcDiagnostic","httpStatus":null,"severity":"warning","filePath":"crates/oxc_linter/src/rules/eslint/no_useless_computed_key.rs","lineNumber":21,"sourceCode":"use oxc_macros::declare_oxc_lint;\nuse oxc_span::{GetSpan, Span};\nuse oxc_str::Str;\nuse schemars::JsonSchema;\nuse serde::Deserialize;\nuse serde_json::Value;\n\nuse crate::{\n    AstNode,\n    context::LintContext,\n    rule::{DefaultRuleConfig, Rule},\n    utils::pad_fix_with_token_boundary,\n};\n\nfn no_useless_computed_key_diagnostic(span: Span, raw: Option<Str>) -> OxcDiagnostic {\n    // false positive, if we remove the closure, `borrowed data escapes outside of function `raw` escapes the function body here`\n    #[expect(clippy::redundant_closure)]\n    let key = raw.unwrap_or_else(|| Str::empty());\n    OxcDiagnostic::warn(format!(\"Unnecessarily computed property `{key}` found.\"))\n        .with_help(\"Replace the computed property with a plain identifier or string literal\")\n        .with_label(span)\n}\n\n#[derive(Debug, Clone, JsonSchema, Deserialize)]\n#[serde(rename_all = \"camelCase\", default, deny_unknown_fields)]\npub struct NoUselessComputedKey {\n    /// The `enforceForClassMembers` option controls whether the rule applies to\n    /// class members (methods and properties).\n    ///\n    /// Examples of **correct** code for this rule with the `{ \"enforceForClassMembers\": false }` option:\n    /// ```js\n    /// class SomeClass {\n    ///     [\"foo\"] = \"bar\";\n    ///     [42] = \"baz\";\n    ///     get ['b']() {}\n    ///     set ['c'](value) {}\n    ///     static [\"foo\"] = \"bar\";","sourceCodeStart":3,"sourceCodeEnd":39,"githubUrl":"https://github.com/oxc-project/oxc/blob/e1e7af627c8843ab64044ed466b128fcc21a035b/crates/oxc_linter/src/rules/eslint/no_useless_computed_key.rs#L3-L39","documentation":"Diagnostic from the `no-useless-computed-key` rule. An object literal or class member uses computed key syntax `{[\"key\"]: value}` where the expression is a plain string/number literal, so the brackets add nothing over the direct key syntax. Oxc reports the span and, per the diagnostic builder, uses the literal (or an empty Str fallback) in the message.","triggerScenarios":"Writing `{ [\"foo\"]: 1 }`, `{ [42]: 'x' }`, or `class A { [\"bar\"]() {} }`. The rule also honors the `enforceForClassMembers` option (default applies to object literals; set it to cover class members). Dynamic keys like `{ [Symbol.iterator]: fn }` or `{ [prefix + 'x']: 1 }` are not flagged.","commonSituations":"Code generated from templates or JSON-to-JS converters that always emit computed keys; refactors that removed a dynamic expression but left the brackets; copy-paste from code that legitimately used symbols.","solutions":["Drop the brackets and quotes for identifier-safe keys: `{ [\"foo\"]: 1 }` -> `{ foo: 1 }`.","For numeric keys use plain `{ 42: 'x' }`.","For keys needing quotes (spaces, reserved words) write `{ 'foo bar': 1 }` without brackets.","Let `oxlint --fix` apply the suggested replacement (the rule pads fixes with token boundaries)."],"exampleFix":"// before\nconst obj = { [\"foo\"]: 1, ['bar']: 2 };\n\n// after\nconst obj = { foo: 1, bar: 2 };","handlingStrategy":"validation","validationCode":"const hasStaticComputedKey = /\\[\\s*(['\"][^'\"]+['\"]|\\d+)\\s*\\]/.test(sourceSnippet);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Use bracket keys only for genuinely dynamic expressions or symbols.","Write plain keys for static strings and numbers.","Enable the autofix so templates that emit computed keys get cleaned automatically."],"tags":["lint","eslint","oxlint","object-literal","computed-key"],"backgroundTag":"no-useless-computed-key","analyzedSha":"e1e7af627c8843ab64044ed466b128fcc21a035b","analyzedAt":"2026-08-20T07:01:07.079Z","contentChangedAt":"2026-08-20T07:01:07.079Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}