Skip to content

Commit

Permalink
docs: add no-useless-computed-key examples with object patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
mdjermanovic committed Nov 6, 2024
1 parent 4f08332 commit 74f313f
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
6 changes: 6 additions & 0 deletions docs/src/rules/no-useless-computed-key.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ var a = { [0]: 0 };
var a = { ['x']: 0 };
var a = { ['x']() {} };

var { [0]: a } = obj;
var { ['x']: a } = obj;

class Foo {
["foo"] = "bar";

Expand Down Expand Up @@ -63,6 +66,9 @@ var a = { x() {} };
var c = { a: 0 };
var c = { '0+1,234': 0 };

var { 0: a } = obj;
var { 'x': a } = obj;

class Foo {
"foo" = "bar";

Expand Down
41 changes: 41 additions & 0 deletions tests/lib/rules/no-useless-computed-key.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@ ruleTester.run("no-useless-computed-key", rule, {
"({ [x]: 0 });",
"({ a: 0, [b](){} })",
"({ ['__proto__']: [] })",
"var { 'a': foo } = obj",
"var { [a]: b } = obj;",
"var { a } = obj;",
"var { a: a } = obj;",
"var { a: b } = obj;",

// ['__proto__'] is useless computed key in object patterns, but the rule doesn't report it for backwards compatibility since it's frozen
"var { ['__proto__']: a } = obj",

{ code: "class Foo { a() {} }", options: [{ enforceForClassMembers: true }] },
{ code: "class Foo { 'a'() {} }", options: [{ enforceForClassMembers: true }] },
{ code: "class Foo { [x]() {} }", options: [{ enforceForClassMembers: true }] },
Expand Down Expand Up @@ -64,6 +73,14 @@ ruleTester.run("no-useless-computed-key", rule, {
data: { property: "'0'" },
type: "Property"
}]
}, {
code: "var { ['0']: a } = obj",
output: "var { '0': a } = obj",
errors: [{
messageId: "unnecessarilyComputedProperty",
data: { property: "'0'" },
type: "Property"
}]
}, {
code: "({ ['0+1,234']: 0 })",
output: "({ '0+1,234': 0 })",
Expand All @@ -80,6 +97,14 @@ ruleTester.run("no-useless-computed-key", rule, {
data: { property: "0" },
type: "Property"
}]
}, {
code: "var { [0]: a } = obj",
output: "var { 0: a } = obj",
errors: [{
messageId: "unnecessarilyComputedProperty",
data: { property: "0" },
type: "Property"
}]
}, {
code: "({ ['x']: 0 })",
output: "({ 'x': 0 })",
Expand All @@ -88,6 +113,14 @@ ruleTester.run("no-useless-computed-key", rule, {
data: { property: "'x'" },
type: "Property"
}]
}, {
code: "var { ['x']: a } = obj",
output: "var { 'x': a } = obj",
errors: [{
messageId: "unnecessarilyComputedProperty",
data: { property: "'x'" },
type: "Property"
}]
}, {
code: "({ ['x']() {} })",
output: "({ 'x'() {} })",
Expand Down Expand Up @@ -120,6 +153,14 @@ ruleTester.run("no-useless-computed-key", rule, {
data: { property: "'x'" },
type: "Property"
}]
}, {
code: "var { [('x')]: a } = obj",
output: "var { 'x': a } = obj",
errors: [{
messageId: "unnecessarilyComputedProperty",
data: { property: "'x'" },
type: "Property"
}]
}, {
code: "({ *['x']() {} })",
output: "({ *'x'() {} })",
Expand Down

0 comments on commit 74f313f

Please sign in to comment.