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

Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions test/parallel/test-eslint-lowercase-name-for-primitive.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,23 @@ new RuleTester().run('lowercase-name-for-primitive', rule, {
invalid: [
{
code: 'new errors.TypeError("ERR_INVALID_ARG_TYPE", "a", "Number")',
errors: [{ message: 'primitive should use lowercase: Number' }]
errors: [{ message: 'primitive should use lowercase: Number' }],
output: 'new errors.TypeError("ERR_INVALID_ARG_TYPE", "a", "number")'
Copy link
Contributor

Choose a reason for hiding this comment

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

CI failed due to a small problem:
The input code and expected output are using double quotes ", but the autofixer will change it to single quotes, which makes the actual output cannot match the expected output.

Copy link
Contributor

Choose a reason for hiding this comment

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

@shobhitchittora Could you change these double quotes in code and output to using single quotes?

Copy link
Contributor Author

@shobhitchittora shobhitchittora Dec 25, 2017

Choose a reason for hiding this comment

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

@starkwang pushed a fix. How did you check the problem in Jenkins that the output is not correct?

Copy link
Contributor

Choose a reason for hiding this comment

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

I modified some codes in eslint (eslint/lib/testers/rule-tester.js) to expose the error message.
You can see eslint/eslint#9769 for more detail. 😉

},
{
code: 'new errors.TypeError("ERR_INVALID_ARG_TYPE", "a", "STRING")',
errors: [{ message: 'primitive should use lowercase: STRING' }]
errors: [{ message: 'primitive should use lowercase: STRING' }],
output: 'new errors.TypeError("ERR_INVALID_ARG_TYPE", "a", "string")'
},
{
code: 'new errors.TypeError("ERR_INVALID_ARG_TYPE", "a",' +
'["String", "Number"])',
errors: [
{ message: 'primitive should use lowercase: String' },
{ message: 'primitive should use lowercase: Number' }
]
],
output: 'new errors.TypeError("ERR_INVALID_ARG_TYPE", "a",' +
'["string", "number"])'
}
]
});
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
Member

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
Member

@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