Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: prefer-destructuring PrivateIdentifier false positive (refs #14857) #14897

Merged
merged 1 commit into from Aug 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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