From 3ad8cf14f5bf40b8ddca482843ed3f79601069da Mon Sep 17 00:00:00 2001 From: Weijia Wang <381152119@qq.com> Date: Sat, 9 Dec 2017 22:22:39 +0800 Subject: [PATCH] tools: add lowercase-name-for-primitive eslint rule MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Primitives should use lowercase in error message. Refs: https://github.com/nodejs/node/pull/16401 PR-URL: https://github.com/nodejs/node/pull/17568 Reviewed-By: Anna Henningsen Reviewed-By: Michaƫl Zasso Reviewed-By: Ruben Bridgewater Reviewed-By: James M Snell --- lib/.eslintrc.yaml | 1 + ...est-eslint-lowercase-name-for-primitive.js | 41 ++++++++++++++++ .../lowercase-name-for-primitive.js | 49 +++++++++++++++++++ 3 files changed, 91 insertions(+) create mode 100644 test/parallel/test-eslint-lowercase-name-for-primitive.js create mode 100644 tools/eslint-rules/lowercase-name-for-primitive.js diff --git a/lib/.eslintrc.yaml b/lib/.eslintrc.yaml index 24f54e682636ee..aaebadca125814 100644 --- a/lib/.eslintrc.yaml +++ b/lib/.eslintrc.yaml @@ -3,3 +3,4 @@ rules: require-buffer: error buffer-constructor: error no-let-in-for-declaration: error + lowercase-name-for-primitive: error diff --git a/test/parallel/test-eslint-lowercase-name-for-primitive.js b/test/parallel/test-eslint-lowercase-name-for-primitive.js new file mode 100644 index 00000000000000..f4a2ac7c3b8307 --- /dev/null +++ b/test/parallel/test-eslint-lowercase-name-for-primitive.js @@ -0,0 +1,41 @@ +'use strict'; + +require('../common'); + +const RuleTester = require('../../tools/eslint').RuleTester; +const rule = require('../../tools/eslint-rules/lowercase-name-for-primitive'); + +const valid = [ + 'string', + 'number', + 'boolean', + 'null', + 'undefined' +]; + +new RuleTester().run('lowercase-name-for-primitive', rule, { + valid: [ + 'new errors.TypeError("ERR_INVALID_ARG_TYPE", "a", ["string", "number"])', + ...valid.map((name) => + `new errors.TypeError("ERR_INVALID_ARG_TYPE", "name", "${name}")` + ) + ], + 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", "STRING")', + errors: [{ message: 'primitive should use lowercase: 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' } + ] + } + ] +}); diff --git a/tools/eslint-rules/lowercase-name-for-primitive.js b/tools/eslint-rules/lowercase-name-for-primitive.js new file mode 100644 index 00000000000000..bdf10328f3f96a --- /dev/null +++ b/tools/eslint-rules/lowercase-name-for-primitive.js @@ -0,0 +1,49 @@ +/** + * @fileoverview Check that TypeError[ERR_INVALID_ARG_TYPE] uses + * lowercase for primitive types + * @author Weijia Wang + */ +'use strict'; + +//------------------------------------------------------------------------------ +// Rule Definition +//------------------------------------------------------------------------------ + +const primitives = [ + 'number', 'string', 'boolean', 'null', 'undefined' +]; + +module.exports = function(context) { + return { + NewExpression(node) { + if ( + node.callee.property && + node.callee.property.name === 'TypeError' && + node.arguments[0].value === 'ERR_INVALID_ARG_TYPE' + ) { + checkNamesArgument(node.arguments[2]); + } + + function checkNamesArgument(names) { + switch (names.type) { + case 'Literal': + checkName(names.value); + break; + case 'ArrayExpression': + names.elements.forEach((name) => { + checkName(name.value); + }); + break; + } + } + + function checkName(name) { + const lowercaseName = name.toLowerCase(); + if (primitives.includes(lowercaseName) && !primitives.includes(name)) { + const msg = `primitive should use lowercase: ${name}`; + context.report(node, msg); + } + } + } + }; +};