diff --git a/lib/rules/id-match.js b/lib/rules/id-match.js index b924a8e81cd..4c9f4351088 100644 --- a/lib/rules/id-match.js +++ b/lib/rules/id-match.js @@ -42,12 +42,9 @@ module.exports = { regexp = new RegExp(pattern); var options = context.options[1] || {}, - properties = options.properties, + properties = !!options.properties, onlyDeclarations = !!options.onlyDeclarations; - // cast to boolean and default to false - properties = !!properties; - /** * Checks if a string matches the provided pattern * @param {String} name The string to check. @@ -88,38 +85,35 @@ module.exports = { Identifier: function(node) { var name = node.name, - effectiveParent = (node.parent.type === "MemberExpression") ? node.parent.parent : node.parent; + parent = node.parent, + effectiveParent = (parent.type === "MemberExpression") ? parent.parent : parent; - // MemberExpressions get special rules - if (node.parent.type === "MemberExpression") { + if (parent.type === "MemberExpression") { - // return early if properties is false if (!properties) { return; } // Always check object names - if (node.parent.object.type === "Identifier" && - node.parent.object.name === node.name) { + if (parent.object.type === "Identifier" && + parent.object.name === name) { if (isInvalid(name)) { report(node); } - // Report AssignmentExpressions only if they are the left side of the assignment + // Report AssignmentExpressions only if they are the left side of the assignment } else if (effectiveParent.type === "AssignmentExpression" && (effectiveParent.right.type !== "MemberExpression" || effectiveParent.left.type === "MemberExpression" && - effectiveParent.left.property.name === node.name)) { + effectiveParent.left.property.name === name)) { if (isInvalid(name)) { report(node); } } - // Properties have their own rules - } else if (node.parent.type === "Property") { + } else if (parent.type === "Property") { - // return early if properties is false - if (!properties) { + if (!properties || parent.key.name !== name) { return; } @@ -127,7 +121,6 @@ module.exports = { report(node); } - // Report anything that is a match and not a CallExpression } else { var isDeclaration = effectiveParent.type === "FunctionDeclaration" || effectiveParent.type === "VariableDeclarator"; diff --git a/tests/lib/rules/id-match.js b/tests/lib/rules/id-match.js index 10883f98c8f..23679b75d92 100644 --- a/tests/lib/rules/id-match.js +++ b/tests/lib/rules/id-match.js @@ -105,6 +105,16 @@ ruleTester.run("id-match", rule, { code: "var myArray = new Array(); var myDate = new Date();", options: ["^[a-z$]+([A-Z][a-z]+)*$"] }, + { + code: "var x = obj._foo;", + options: ["^[^_]+$"] + }, + { + code: "var obj = {key: no_under}", + options: ["^[^_]+$", { + properties: true + }] + }, { code: "var o = {key: 1}", options: ["^[^_]+$", { @@ -135,10 +145,6 @@ ruleTester.run("id-match", rule, { properties: false }] }, - { - code: "var x = obj._foo;", - options: ["^[^_]+$"] - }, { code: "var x = obj._foo2;", options: ["^[^_]+$", {