Skip to content

Commit

Permalink
Fix: Do not strip underscores before checking against allow (fixes es…
Browse files Browse the repository at this point in the history
  • Loading branch information
lukeapage committed Oct 21, 2018
1 parent 58ff359 commit f4d4408
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions lib/rules/camelcase.js
Expand Up @@ -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
Expand All @@ -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);
}

Expand All @@ -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);
}
Expand All @@ -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)) {
Expand All @@ -191,20 +192,20 @@ 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);
}

// Check if it's an import specifier
} 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);
}
}
Expand Down

0 comments on commit f4d4408

Please sign in to comment.