From 13c3988a4001ae368ea7b6c8d3dd0abfa7c6cf64 Mon Sep 17 00:00:00 2001 From: Milos Djermanovic Date: Sun, 18 Aug 2019 21:45:49 +0200 Subject: [PATCH] Fix: Check literal type explicitly in dot-notation (#12095) --- lib/rules/dot-notation.js | 8 ++++++-- tests/lib/rules/dot-notation.js | 13 ++++++++++++- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/lib/rules/dot-notation.js b/lib/rules/dot-notation.js index 61184ddcd1a..2e8fff8b90e 100644 --- a/lib/rules/dot-notation.js +++ b/lib/rules/dot-notation.js @@ -9,13 +9,16 @@ //------------------------------------------------------------------------------ const astUtils = require("./utils/ast-utils"); +const keywords = require("./utils/keywords"); //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ const validIdentifier = /^[a-zA-Z_$][a-zA-Z0-9_$]*$/u; -const keywords = require("./utils/keywords"); + +// `null` literal must be handled separately. +const literalTypesToCheck = new Set(["string", "boolean"]); module.exports = { meta: { @@ -115,7 +118,8 @@ module.exports = { MemberExpression(node) { if ( node.computed && - node.property.type === "Literal" + node.property.type === "Literal" && + (literalTypesToCheck.has(typeof node.property.value) || astUtils.isNullLiteral(node.property)) ) { checkComputedProperty(node, node.property.value); } diff --git a/tests/lib/rules/dot-notation.js b/tests/lib/rules/dot-notation.js index 490a0956c44..5532ea35870 100644 --- a/tests/lib/rules/dot-notation.js +++ b/tests/lib/rules/dot-notation.js @@ -57,7 +57,8 @@ ruleTester.run("dot-notation", rule, { "a.null;", "a[undefined];", "a[void 0];", - "a[b()];" + "a[b()];", + { code: "a[/(?0)/];", parserOptions: { ecmaVersion: 2018 } } ], invalid: [ { @@ -82,6 +83,16 @@ ruleTester.run("dot-notation", rule, { output: "a.null;", errors: [{ messageId: "useDot", data: { key: "null" } }] }, + { + code: "a[true];", + output: "a.true;", + errors: [{ messageId: "useDot", data: { key: "true" } }] + }, + { + code: "a[false];", + output: "a.false;", + errors: [{ messageId: "useDot", data: { key: "false" } }] + }, { code: "a['b'];", output: "a.b;",