Skip to content

Commit bdec50e

Browse files
authored
feat: fix no-useless-computed-key false negative with __proto__ (#19123)
1 parent 09bc2a8 commit bdec50e

2 files changed

Lines changed: 12 additions & 5 deletions

File tree

lib/rules/no-useless-computed-key.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,10 @@ function hasUselessComputedKey(node) {
5858

5959
switch (node.type) {
6060
case "Property":
61-
return value !== "__proto__";
61+
if (node.parent.type === "ObjectExpression") {
62+
return value !== "__proto__";
63+
}
64+
return true;
6265

6366
case "PropertyDefinition":
6467
if (node.static) {

tests/lib/rules/no-useless-computed-key.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,6 @@ ruleTester.run("no-useless-computed-key", rule, {
2929
"var { a } = obj;",
3030
"var { a: a } = obj;",
3131
"var { a: b } = obj;",
32-
33-
// ['__proto__'] is useless computed key in object patterns, but the rule doesn't report it for backwards compatibility since it's frozen
34-
"var { ['__proto__']: a } = obj",
35-
3632
{ code: "class Foo { a() {} }", options: [{ enforceForClassMembers: true }] },
3733
{ code: "class Foo { 'a'() {} }", options: [{ enforceForClassMembers: true }] },
3834
{ code: "class Foo { [x]() {} }", options: [{ enforceForClassMembers: true }] },
@@ -121,6 +117,14 @@ ruleTester.run("no-useless-computed-key", rule, {
121117
data: { property: "'x'" },
122118
type: "Property"
123119
}]
120+
}, {
121+
code: "var { ['__proto__']: a } = obj",
122+
output: "var { '__proto__': a } = obj",
123+
errors: [{
124+
messageId: "unnecessarilyComputedProperty",
125+
data: { property: "'__proto__'" },
126+
type: "Property"
127+
}]
124128
}, {
125129
code: "({ ['x']() {} })",
126130
output: "({ 'x'() {} })",

0 commit comments

Comments
 (0)