Skip to content

Commit

Permalink
tools: autofixer for lowercase-name-for-primitive
Browse files Browse the repository at this point in the history
PR-URL: #17715
Refs: #16636
Reviewed-By: Anatoli Papirovski <[email protected]>
Reviewed-By: Weijia Wang <[email protected]>
  • Loading branch information
shobhitchittora authored and MylesBorins committed Jan 9, 2018
1 parent 9110654 commit 2d07243
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 12 deletions.
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\')',
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

0 comments on commit 2d07243

Please sign in to comment.