From f4d4408905771b9c993919a6ae788f581a0a398e Mon Sep 17 00:00:00 2001 From: Luke Page Date: Sun, 21 Oct 2018 19:49:19 +0200 Subject: [PATCH] Fix: Do not strip underscores before checking against allow (fixes #11000) --- lib/rules/camelcase.js | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/lib/rules/camelcase.js b/lib/rules/camelcase.js index 41040450f94..232c0ba7ae5 100644 --- a/lib/rules/camelcase.js +++ b/lib/rules/camelcase.js @@ -130,9 +130,10 @@ module.exports = { /* * Leading and trailing underscores are commonly used to flag - * private/protected identifiers, strip them + * private/protected identifiers, strip them before checking if underscored */ - const name = node.name.replace(/^_+|_+$/g, ""), + const name = node.name, + nameIsUnderscored = isUnderscored(name.replace(/^_+|_+$/g, "")), effectiveParent = (node.parent.type === "MemberExpression") ? node.parent.parent : node.parent; // First, we ignore the node if it match the ignore list @@ -149,11 +150,11 @@ module.exports = { } // Always report underscored object names - if (node.parent.object.type === "Identifier" && node.parent.object.name === node.name && isUnderscored(name)) { + if (node.parent.object.type === "Identifier" && node.parent.object.name === node.name && nameIsUnderscored) { report(node); // Report AssignmentExpressions only if they are the left side of the assignment - } else if (effectiveParent.type === "AssignmentExpression" && isUnderscored(name) && (effectiveParent.right.type !== "MemberExpression" || effectiveParent.left.type === "MemberExpression" && effectiveParent.left.property.name === node.name)) { + } else if (effectiveParent.type === "AssignmentExpression" && nameIsUnderscored && (effectiveParent.right.type !== "MemberExpression" || effectiveParent.left.type === "MemberExpression" && effectiveParent.left.property.name === node.name)) { report(node); } @@ -165,7 +166,7 @@ module.exports = { } else if (node.parent.type === "Property" || node.parent.type === "AssignmentPattern") { if (node.parent.parent && node.parent.parent.type === "ObjectPattern") { - if (node.parent.shorthand && node.parent.value.left && isUnderscored(name)) { + if (node.parent.shorthand && node.parent.value.left && nameIsUnderscored) { report(node); } @@ -177,7 +178,7 @@ module.exports = { return; } - const valueIsUnderscored = node.parent.value.name && isUnderscored(name); + const valueIsUnderscored = node.parent.value.name && nameIsUnderscored; // ignore destructuring if the option is set, unless a new identifier is created if (valueIsUnderscored && !(assignmentKeyEqualsValue && ignoreDestructuring)) { @@ -191,7 +192,7 @@ module.exports = { } // don't check right hand side of AssignmentExpression to prevent duplicate warnings - if (isUnderscored(name) && !ALLOWED_PARENT_TYPES.has(effectiveParent.type) && !(node.parent.right === node)) { + if (nameIsUnderscored && !ALLOWED_PARENT_TYPES.has(effectiveParent.type) && !(node.parent.right === node)) { report(node); } @@ -199,12 +200,12 @@ module.exports = { } else if (["ImportSpecifier", "ImportNamespaceSpecifier", "ImportDefaultSpecifier"].indexOf(node.parent.type) >= 0) { // Report only if the local imported identifier is underscored - if (node.parent.local && node.parent.local.name === node.name && isUnderscored(name)) { + if (node.parent.local && node.parent.local.name === node.name && nameIsUnderscored) { report(node); } // Report anything that is underscored that isn't a CallExpression - } else if (isUnderscored(name) && !ALLOWED_PARENT_TYPES.has(effectiveParent.type)) { + } else if (nameIsUnderscored && !ALLOWED_PARENT_TYPES.has(effectiveParent.type)) { report(node); } }