Skip to content

Commit

Permalink
Fix: dot-notation false positive with private identifier (refs #14857) (
Browse files Browse the repository at this point in the history
#14898)

* Fix: dot-notation false positive with private identifier (refs #14857)

* Add example in docs
  • Loading branch information
mdjermanovic committed Aug 10, 2021
1 parent 8c35066 commit ccb9a91
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
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

0 comments on commit ccb9a91

Please sign in to comment.