Skip to content
Closed
Show file tree
Hide file tree
Changes from 3 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"])'
}
]
});
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