Skip to content

Commit

Permalink
Fix: Do not strip underscores in camelcase allow (fixes #11000) (#11001)
Browse files Browse the repository at this point in the history
* Fix: Do not strip underscores before checking against allow (fixes #11000)

* Code review comment - test added
  • Loading branch information
lukeapage authored and platinumazure committed Nov 28, 2018
1 parent a675c89 commit c0a80d0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
19 changes: 10 additions & 9 deletions lib/rules/camelcase.js
Expand Up @@ -132,9 +132,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 @@ -151,11 +152,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 @@ -167,7 +168,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 @@ -179,7 +180,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 @@ -193,20 +194,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
4 changes: 4 additions & 0 deletions tests/lib/rules/camelcase.js
Expand Up @@ -193,6 +193,10 @@ ruleTester.run("camelcase", rule, {
{
code: "user_id = 0;",
options: [{ allow: ["_id$"] }]
},
{
code: "__option_foo__ = 0;",
options: [{ allow: ["__option_foo__"] }]
}
],
invalid: [
Expand Down

0 comments on commit c0a80d0

Please sign in to comment.