Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 39089c8

Browse files
authored
docs: add no-useless-computed-key examples with object patterns (#19109)
1 parent 895c60f commit 39089c8

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

docs/src/rules/no-useless-computed-key.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ var a = { [0]: 0 };
3434
var a = { ['x']: 0 };
3535
var a = { ['x']() {} };
3636

37+
var { [0]: a } = obj;
38+
var { ['x']: a } = obj;
39+
3740
class Foo {
3841
["foo"] = "bar";
3942

@@ -63,6 +66,9 @@ var a = { x() {} };
6366
var c = { a: 0 };
6467
var c = { '0+1,234': 0 };
6568

69+
var { 0: a } = obj;
70+
var { 'x': a } = obj;
71+
6672
class Foo {
6773
"foo" = "bar";
6874

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

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,15 @@ ruleTester.run("no-useless-computed-key", rule, {
2424
"({ [x]: 0 });",
2525
"({ a: 0, [b](){} })",
2626
"({ ['__proto__']: [] })",
27+
"var { 'a': foo } = obj",
28+
"var { [a]: b } = obj;",
29+
"var { a } = obj;",
30+
"var { a: a } = obj;",
31+
"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+
2736
{ code: "class Foo { a() {} }", options: [{ enforceForClassMembers: true }] },
2837
{ code: "class Foo { 'a'() {} }", options: [{ enforceForClassMembers: true }] },
2938
{ code: "class Foo { [x]() {} }", options: [{ enforceForClassMembers: true }] },
@@ -64,6 +73,14 @@ ruleTester.run("no-useless-computed-key", rule, {
6473
data: { property: "'0'" },
6574
type: "Property"
6675
}]
76+
}, {
77+
code: "var { ['0']: a } = obj",
78+
output: "var { '0': a } = obj",
79+
errors: [{
80+
messageId: "unnecessarilyComputedProperty",
81+
data: { property: "'0'" },
82+
type: "Property"
83+
}]
6784
}, {
6885
code: "({ ['0+1,234']: 0 })",
6986
output: "({ '0+1,234': 0 })",
@@ -80,6 +97,14 @@ ruleTester.run("no-useless-computed-key", rule, {
8097
data: { property: "0" },
8198
type: "Property"
8299
}]
100+
}, {
101+
code: "var { [0]: a } = obj",
102+
output: "var { 0: a } = obj",
103+
errors: [{
104+
messageId: "unnecessarilyComputedProperty",
105+
data: { property: "0" },
106+
type: "Property"
107+
}]
83108
}, {
84109
code: "({ ['x']: 0 })",
85110
output: "({ 'x': 0 })",
@@ -88,6 +113,14 @@ ruleTester.run("no-useless-computed-key", rule, {
88113
data: { property: "'x'" },
89114
type: "Property"
90115
}]
116+
}, {
117+
code: "var { ['x']: a } = obj",
118+
output: "var { 'x': a } = obj",
119+
errors: [{
120+
messageId: "unnecessarilyComputedProperty",
121+
data: { property: "'x'" },
122+
type: "Property"
123+
}]
91124
}, {
92125
code: "({ ['x']() {} })",
93126
output: "({ 'x'() {} })",
@@ -120,6 +153,14 @@ ruleTester.run("no-useless-computed-key", rule, {
120153
data: { property: "'x'" },
121154
type: "Property"
122155
}]
156+
}, {
157+
code: "var { [('x')]: a } = obj",
158+
output: "var { 'x': a } = obj",
159+
errors: [{
160+
messageId: "unnecessarilyComputedProperty",
161+
data: { property: "'x'" },
162+
type: "Property"
163+
}]
123164
}, {
124165
code: "({ *['x']() {} })",
125166
output: "({ *'x'() {} })",

0 commit comments

Comments
 (0)