From ccb9a9138acd63457e004630475495954c1be6f4 Mon Sep 17 00:00:00 2001 From: Milos Djermanovic Date: Tue, 10 Aug 2021 02:46:23 +0200 Subject: [PATCH] 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 --- docs/rules/dot-notation.md | 13 +++++++++++++ lib/rules/dot-notation.js | 1 + tests/lib/rules/dot-notation.js | 7 ++++++- 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/docs/rules/dot-notation.md b/docs/rules/dot-notation.md index a13049a3f3f..26c77756c4b 100644 --- a/docs/rules/dot-notation.md +++ b/docs/rules/dot-notation.md @@ -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. diff --git a/lib/rules/dot-notation.js b/lib/rules/dot-notation.js index 3aa9f3110f5..20434f3594c 100644 --- a/lib/rules/dot-notation.js +++ b/lib/rules/dot-notation.js @@ -141,6 +141,7 @@ module.exports = { if ( !allowKeywords && !node.computed && + node.property.type === "Identifier" && keywords.indexOf(String(node.property.name)) !== -1 ) { context.report({ diff --git a/tests/lib/rules/dot-notation.js b/tests/lib/rules/dot-notation.js index fed94c128cf..249faf80e8f 100644 --- a/tests/lib/rules/dot-notation.js +++ b/tests/lib/rules/dot-notation.js @@ -59,7 +59,12 @@ ruleTester.run("dot-notation", rule, { "a[void 0];", "a[b()];", { code: "a[/(?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: [ {