From 39089c80a7af09494fce86a6574bf012cbe46d10 Mon Sep 17 00:00:00 2001 From: Milos Djermanovic Date: Wed, 13 Nov 2024 07:23:12 +0100 Subject: [PATCH] docs: add `no-useless-computed-key` examples with object patterns (#19109) --- docs/src/rules/no-useless-computed-key.md | 6 ++++ tests/lib/rules/no-useless-computed-key.js | 41 ++++++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/docs/src/rules/no-useless-computed-key.md b/docs/src/rules/no-useless-computed-key.md index 1a77b39bf050..947f7180489b 100644 --- a/docs/src/rules/no-useless-computed-key.md +++ b/docs/src/rules/no-useless-computed-key.md @@ -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"; @@ -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"; diff --git a/tests/lib/rules/no-useless-computed-key.js b/tests/lib/rules/no-useless-computed-key.js index 52765a03453b..70460f4e0b50 100644 --- a/tests/lib/rules/no-useless-computed-key.js +++ b/tests/lib/rules/no-useless-computed-key.js @@ -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 }] }, @@ -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 })", @@ -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 })", @@ -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'() {} })", @@ -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'() {} })",