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

tools: auto fix custom eslint rule for lowercase-name-for-primitive.js #17715

Next Next commit
tools: auto fix custom eslint rule for lowercase-name-for-primitive.js
1. Fixer for lowercase-name-for-primitive.js
2. Modified the node passed to fix it.

Refs : #16636
  • Loading branch information
shobhitchittora committed Dec 17, 2017
commit a389548f5081c7632387de84b39e9952780ab585
18 changes: 14 additions & 4 deletions tools/eslint-rules/lowercase-name-for-primitive.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ module.exports = function(context) {

switch (names.type) {
case 'Literal':
checkName(node, names.value);
checkName(names, names.value);
break;
case 'ArrayExpression':
names.elements.forEach((name) => {
checkName(node, name.value);
names.elements.forEach((name, index) => {
checkName(names.elements[index], name.value);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couldn't this just be name instead of names.elements[index]? Pretty sure we can also get rid of the second argument here and above.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍🏻

});
break;
}
Expand All @@ -36,8 +36,18 @@ module.exports = function(context) {
const lowercaseName = name.toLowerCase();
if (primitives.includes(lowercaseName) && !primitives.includes(name)) {
Copy link
Contributor

@apapirovski apapirovski Dec 18, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

name could just be replaced with node.value now.

Also, pretty sure !primitives.includes(name) could be simplified to name !== lowercaseName (and it could be put as the first condition so the includes only runs if necessary).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Made the changes.

const msg = `primitive should use lowercase: ${name}`;
context.report(node, msg);
context.report({
node,
message: msg,
fix: (fixer) => {
return fixer.replaceText(
node,
`'${lowercaseName}'`
);
}
});
}

}

return {
Expand Down