Skip to content
Permalink
Browse files
Fix: dot-notation false positive with private identifier (refs #14857) (
#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 ccb9a9138acd63457e004630475495954c1be6f4
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
@@ -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.
@@ -141,6 +141,7 @@ module.exports = {
if (
!allowKeywords &&
!node.computed &&
node.property.type === "Identifier" &&
keywords.indexOf(String(node.property.name)) !== -1
) {
context.report({
@@ -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: [
{

0 comments on commit ccb9a91

Please sign in to comment.