diff options
author | cjihrig <cjihrig@gmail.com> | 2019-05-15 15:29:56 -0400 |
---|---|---|
committer | Rich Trott <rtrott@gmail.com> | 2019-05-17 20:56:26 -0700 |
commit | af83b7963fdffab9a4d9ca9893de517f7213efa8 (patch) | |
tree | 4f974008b199bc79602624bd61a4147b49aaedec /tools/eslint-rules | |
parent | abe82110b78a54a504a104c2da97cbcc7e344e9f (diff) | |
download | node-new-af83b7963fdffab9a4d9ca9893de517f7213efa8.tar.gz |
tools: decrease code duplication for isString() in lint rules
This commit makes isString() a reusable utility
function for core's custom ESLint rules.
PR-URL: https://github.com/nodejs/node/pull/27719
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Masashi Hirano <shisama07@gmail.com>
Reviewed-By: Yongsheng Zhang <zyszys98@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Diffstat (limited to 'tools/eslint-rules')
-rw-r--r-- | tools/eslint-rules/no-duplicate-requires.js | 6 | ||||
-rw-r--r-- | tools/eslint-rules/require-common-first.js | 11 | ||||
-rw-r--r-- | tools/eslint-rules/required-modules.js | 11 | ||||
-rw-r--r-- | tools/eslint-rules/rules-utils.js | 4 |
4 files changed, 7 insertions, 25 deletions
diff --git a/tools/eslint-rules/no-duplicate-requires.js b/tools/eslint-rules/no-duplicate-requires.js index 05a379eba6..8c4b7a4c27 100644 --- a/tools/eslint-rules/no-duplicate-requires.js +++ b/tools/eslint-rules/no-duplicate-requires.js @@ -4,17 +4,13 @@ */ 'use strict'; -const { isRequireCall } = require('./rules-utils.js'); +const { isRequireCall, isString } = require('./rules-utils.js'); //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ -function isString(node) { - return node && node.type === 'Literal' && typeof node.value === 'string'; -} - function isTopLevel(node) { do { if (node.type === 'FunctionDeclaration' || diff --git a/tools/eslint-rules/require-common-first.js b/tools/eslint-rules/require-common-first.js index 2b04a98d9f..4096ee2771 100644 --- a/tools/eslint-rules/require-common-first.js +++ b/tools/eslint-rules/require-common-first.js @@ -4,7 +4,7 @@ 'use strict'; const path = require('path'); -const { isRequireCall } = require('./rules-utils.js'); +const { isRequireCall, isString } = require('./rules-utils.js'); //------------------------------------------------------------------------------ // Rule Definition @@ -16,15 +16,6 @@ module.exports = function(context) { const foundModules = []; /** - * Function to check if a node is a string literal. - * @param {ASTNode} node The node to check. - * @returns {boolean} If the node is a string literal. - */ - function isString(node) { - return node && node.type === 'Literal' && typeof node.value === 'string'; - } - - /** * Function to check if the path is a module and return its name. * @param {String} str The path to check * @returns {String} module name diff --git a/tools/eslint-rules/required-modules.js b/tools/eslint-rules/required-modules.js index 40cb8e46e5..06c998c7f5 100644 --- a/tools/eslint-rules/required-modules.js +++ b/tools/eslint-rules/required-modules.js @@ -4,7 +4,7 @@ */ 'use strict'; -const { isRequireCall } = require('./rules-utils.js'); +const { isRequireCall, isString } = require('./rules-utils.js'); //------------------------------------------------------------------------------ // Rule Definition @@ -26,15 +26,6 @@ module.exports = function(context) { } /** - * Function to check if a node is a string literal. - * @param {ASTNode} node The node to check. - * @returns {boolean} If the node is a string literal. - */ - function isString(node) { - return node && node.type === 'Literal' && typeof node.value === 'string'; - } - - /** * Function to check if the path is a required module and return its name. * @param {String} str The path to check * @returns {undefined|String} required module name or undefined diff --git a/tools/eslint-rules/rules-utils.js b/tools/eslint-rules/rules-utils.js index 713a49bf7a..462ab7c2ff 100644 --- a/tools/eslint-rules/rules-utils.js +++ b/tools/eslint-rules/rules-utils.js @@ -8,6 +8,10 @@ function isRequireCall(node) { } module.exports.isRequireCall = isRequireCall; +module.exports.isString = function(node) { + return node && node.type === 'Literal' && typeof node.value === 'string'; +}; + module.exports.isDefiningError = function(node) { return node.expression && node.expression.type === 'CallExpression' && |