From 01b302d87d0374cabb4df7a646322e3c95945f10 Mon Sep 17 00:00:00 2001 From: Milos Djermanovic Date: Fri, 6 Aug 2021 23:35:09 +0200 Subject: [PATCH 1/2] Fix: dot-notation false positive with private identifier (refs #14857) --- lib/rules/dot-notation.js | 1 + tests/lib/rules/dot-notation.js | 7 ++++++- 2 files changed, 7 insertions(+), 1 deletion(-) 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: [ { From c076dea60deda4538b7d3933f7dbdd976a4072c6 Mon Sep 17 00:00:00 2001 From: Milos Djermanovic Date: Sat, 7 Aug 2021 21:44:28 +0200 Subject: [PATCH 2/2] Add example in docs --- docs/rules/dot-notation.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) 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.