Skip to content

Commit

Permalink
Fix: prefer-destructuring PrivateIdentifier false positive (refs #14857
Browse files Browse the repository at this point in the history
…) (#14897)
  • Loading branch information
mdjermanovic committed Aug 10, 2021
1 parent ccb9a91 commit cbc43da
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 3 deletions.
11 changes: 11 additions & 0 deletions docs/rules/prefer-destructuring.md
Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion lib/rules/prefer-destructuring.js
Expand Up @@ -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;
}

Expand Down
37 changes: 35 additions & 2 deletions tests/lib/rules/prefer-destructuring.js
Expand Up @@ -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: [
Expand Down Expand Up @@ -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: [
Expand Down

0 comments on commit cbc43da

Please sign in to comment.