diff --git a/docs/rules/prefer-destructuring.md b/docs/rules/prefer-destructuring.md index 7f18559ed50..64e914fcee4 100644 --- a/docs/rules/prefer-destructuring.md +++ b/docs/rules/prefer-destructuring.md @@ -62,6 +62,17 @@ Examples of **correct** code when `enforceForRenamedProperties` is enabled: var { bar: foo } = object; ``` +Examples of additional **correct** code when `enforceForRenamedProperties` is enabled: + +```javascript +class C { + #x; + foo() { + const bar = this.#x; // private identifiers are not allowed in destructuring + } +} +``` + An example configuration, with the defaults `array` and `object` filled in, looks like this: ```json diff --git a/lib/rules/prefer-destructuring.js b/lib/rules/prefer-destructuring.js index f82bb75c122..f76087e5250 100644 --- a/lib/rules/prefer-destructuring.js +++ b/lib/rules/prefer-destructuring.js @@ -221,7 +221,11 @@ module.exports = { * @returns {void} */ function performCheck(leftNode, rightNode, reportNode) { - if (rightNode.type !== "MemberExpression" || rightNode.object.type === "Super") { + if ( + rightNode.type !== "MemberExpression" || + rightNode.object.type === "Super" || + rightNode.property.type === "PrivateIdentifier" + ) { return; } diff --git a/tests/lib/rules/prefer-destructuring.js b/tests/lib/rules/prefer-destructuring.js index 8f111a1d8c2..7f808a64218 100644 --- a/tests/lib/rules/prefer-destructuring.js +++ b/tests/lib/rules/prefer-destructuring.js @@ -16,7 +16,7 @@ const rule = require("../../../lib/rules/prefer-destructuring"), // Tests //------------------------------------------------------------------------------ -const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 2020 } }); +const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 2022 } }); ruleTester.run("prefer-destructuring", rule, { valid: [ @@ -157,7 +157,40 @@ ruleTester.run("prefer-destructuring", rule, { // Optional chaining "var foo = array?.[0];", // because the fixed code can throw TypeError. - "var foo = object?.foo;" + "var foo = object?.foo;", + + // Private identifiers + "class C { #x; foo() { const x = this.#x; } }", + "class C { #x; foo() { x = this.#x; } }", + "class C { #x; foo(a) { x = a.#x; } }", + { + code: "class C { #x; foo() { const x = this.#x; } }", + options: [{ array: true, object: true }, { enforceForRenamedProperties: true }] + }, + { + code: "class C { #x; foo() { const y = this.#x; } }", + options: [{ array: true, object: true }, { enforceForRenamedProperties: true }] + }, + { + code: "class C { #x; foo() { x = this.#x; } }", + options: [{ array: true, object: true }, { enforceForRenamedProperties: true }] + }, + { + code: "class C { #x; foo() { y = this.#x; } }", + options: [{ array: true, object: true }, { enforceForRenamedProperties: true }] + }, + { + code: "class C { #x; foo(a) { x = a.#x; } }", + options: [{ array: true, object: true }, { enforceForRenamedProperties: true }] + }, + { + code: "class C { #x; foo(a) { y = a.#x; } }", + options: [{ array: true, object: true }, { enforceForRenamedProperties: true }] + }, + { + code: "class C { #x; foo() { x = this.a.#x; } }", + options: [{ array: true, object: true }, { enforceForRenamedProperties: true }] + } ], invalid: [