Skip to content

Commit

Permalink
Fix: Check literal type explicitly in dot-notation (#12095)
Browse files Browse the repository at this point in the history
  • Loading branch information
mdjermanovic authored and kaicataldo committed Aug 18, 2019
1 parent 3e5ceca commit 13c3988
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
8 changes: 6 additions & 2 deletions lib/rules/dot-notation.js
Expand Up @@ -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: {
Expand Down Expand Up @@ -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);
}
Expand Down
13 changes: 12 additions & 1 deletion tests/lib/rules/dot-notation.js
Expand Up @@ -57,7 +57,8 @@ ruleTester.run("dot-notation", rule, {
"a.null;",
"a[undefined];",
"a[void 0];",
"a[b()];"
"a[b()];",
{ code: "a[/(?<zero>0)/];", parserOptions: { ecmaVersion: 2018 } }
],
invalid: [
{
Expand All @@ -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;",
Expand Down

0 comments on commit 13c3988

Please sign in to comment.