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 4 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
22 changes: 15 additions & 7 deletions test/parallel/test-eslint-lowercase-name-for-primitive.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,28 @@ 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' }]
code: `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.

@shobhitchittora Could you change these backquote to singlequote? For example:

{
  code: 'new errors.TypeError(\'ERR_INVALID_ARG_TYPE\', \'a\', ' +
        '\'Number\''
}

In addition, you can use this command to check if your change will pass CI:

node test/parallel/test-eslint-lowercase-name-for-primitive.js 

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done and started a build.

errors: [{ message: 'primitive should use lowercase: Number' }],
output: `new errors.TypeError('ERR_INVALID_ARG_TYPE', 'a',
'number')`
},
{
code: 'new errors.TypeError("ERR_INVALID_ARG_TYPE", "a", "STRING")',
errors: [{ message: 'primitive should use lowercase: STRING' }]
code: `new errors.TypeError('ERR_INVALID_ARG_TYPE', 'a',
'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"])',
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'])`
}
]
});
21 changes: 16 additions & 5 deletions tools/eslint-rules/lowercase-name-for-primitive.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,33 @@ module.exports = function(context) {

switch (names.type) {
case 'Literal':
checkName(node, names.value);
checkName(names);
break;
case 'ArrayExpression':
names.elements.forEach((name) => {
checkName(node, name.value);
checkName(name);
});
break;
}
}

function checkName(node, name) {
function checkName(node) {
const name = node.value;
const lowercaseName = name.toLowerCase();
if (primitives.includes(lowercaseName) && !primitives.includes(name)) {
if (name !== lowercaseName && primitives.includes(lowercaseName)) {
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