summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorShobhit Chittora <chittorashobhit@gmail.com>2017-12-17 16:11:34 +0530
committerMyles Borins <mylesborins@google.com>2018-02-12 19:28:26 -0500
commit6187aec242cbfc964aa87872cdc4dc53c6486479 (patch)
treeba83f226bdc8e37450d721ed87117f6a364f67b9
parent038e52627fddd331361fa73f848bd3ad3f9c7cf7 (diff)
downloadnode-new-6187aec242cbfc964aa87872cdc4dc53c6486479.tar.gz
tools: autofixer for lowercase-name-for-primitive
PR-URL: https://github.com/nodejs/node/pull/17715 Refs: https://github.com/nodejs/node/issues/16636 Reviewed-By: Anatoli Papirovski <apapirovski@mac.com> Reviewed-By: Weijia Wang <starkwang@126.com>
-rw-r--r--test/parallel/test-eslint-lowercase-name-for-primitive.js22
-rw-r--r--tools/eslint-rules/lowercase-name-for-primitive.js21
2 files changed, 31 insertions, 12 deletions
diff --git a/test/parallel/test-eslint-lowercase-name-for-primitive.js b/test/parallel/test-eslint-lowercase-name-for-primitive.js
index f4a2ac7c3b..3eb0d83834 100644
--- a/test/parallel/test-eslint-lowercase-name-for-primitive.js
+++ b/test/parallel/test-eslint-lowercase-name-for-primitive.js
@@ -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\']) '
}
]
});
diff --git a/tools/eslint-rules/lowercase-name-for-primitive.js b/tools/eslint-rules/lowercase-name-for-primitive.js
index cb603b62c0..d3a5243c37 100644
--- a/tools/eslint-rules/lowercase-name-for-primitive.js
+++ b/tools/eslint-rules/lowercase-name-for-primitive.js
@@ -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 {