Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Do not strip underscores in camelcase allow (fixes #11000) #11001

Merged
merged 2 commits into from Nov 28, 2018
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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