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: dot-notation false positive with private identifier (refs #14857) #14898

Merged
merged 2 commits 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
13 changes: 13 additions & 0 deletions docs/rules/dot-notation.md
Expand Up @@ -46,6 +46,19 @@ var foo = { "class": "CS 101" }
var x = foo["class"]; // Property name is a reserved word, square-bracket notation required
```

Examples of additional **correct** code for the `{ "allowKeywords": false }` option:

```js
/*eslint dot-notation: ["error", { "allowKeywords": false }]*/

class C {
#in;
foo() {
this.#in; // Dot notation is required for private identifiers
}
}
```

### allowPattern

For example, when preparing data to be sent to an external API, it is often required to use property names that include underscores. If the `camelcase` rule is in effect, these [snake case](https://en.wikipedia.org/wiki/Snake_case) properties would not be allowed. By providing an `allowPattern` to the `dot-notation` rule, these snake case properties can be accessed with bracket notation.
Expand Down
1 change: 1 addition & 0 deletions lib/rules/dot-notation.js
Expand Up @@ -141,6 +141,7 @@ module.exports = {
if (
!allowKeywords &&
!node.computed &&
node.property.type === "Identifier" &&
keywords.indexOf(String(node.property.name)) !== -1
) {
context.report({
Expand Down
7 changes: 6 additions & 1 deletion tests/lib/rules/dot-notation.js
Expand Up @@ -59,7 +59,12 @@ ruleTester.run("dot-notation", rule, {
"a[void 0];",
"a[b()];",
{ code: "a[/(?<zero>0)/];", parserOptions: { ecmaVersion: 2018 } },
{ code: "class C { foo() { this['#a'] } }", parserOptions: { ecmaVersion: 2022 } }
{ code: "class C { foo() { this['#a'] } }", parserOptions: { ecmaVersion: 2022 } },
{
code: "class C { #in; foo() { this.#in; } }",
options: [{ allowKeywords: false }],
parserOptions: { ecmaVersion: 2022 }
}
],
invalid: [
{
Expand Down