Skip to content

Commit

Permalink
Fix: id-match false positive in property values (fixes eslint#5885) (
Browse files Browse the repository at this point in the history
  • Loading branch information
mikesherov committed Apr 25, 2016
1 parent 51ddd4b commit 1e7a3ef
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 21 deletions.
27 changes: 10 additions & 17 deletions lib/rules/id-match.js
Expand Up @@ -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.
Expand Down Expand Up @@ -88,46 +85,42 @@ 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;
}

if (shouldReport(effectiveParent, name)) {
report(node);
}

// Report anything that is a match and not a CallExpression
} else {
var isDeclaration = effectiveParent.type === "FunctionDeclaration" || effectiveParent.type === "VariableDeclarator";

Expand Down
14 changes: 10 additions & 4 deletions tests/lib/rules/id-match.js
Expand Up @@ -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: ["^[^_]+$", {
Expand Down Expand Up @@ -135,10 +145,6 @@ ruleTester.run("id-match", rule, {
properties: false
}]
},
{
code: "var x = obj._foo;",
options: ["^[^_]+$"]
},
{
code: "var x = obj._foo2;",
options: ["^[^_]+$", {
Expand Down

0 comments on commit 1e7a3ef

Please sign in to comment.