Skip to content
Permalink
Browse files
Fix: prefer-destructuring PrivateIdentifier false positive (refs #14857
…) (#14897)
  • Loading branch information
mdjermanovic committed Aug 10, 2021
1 parent ccb9a91 commit cbc43daad2ea229fb15a9198efd2bc2721dfb75f
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 3 deletions.
@@ -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
@@ -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;
}

@@ -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: [

0 comments on commit cbc43da

Please sign in to comment.