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>
This commit is contained in:
cjihrig 2019-05-15 15:29:56 -04:00 committed by Rich Trott
parent abe82110b7
commit af83b7963f
4 changed files with 7 additions and 25 deletions

View File

@ -4,17 +4,13 @@
*/ */
'use strict'; 'use strict';
const { isRequireCall } = require('./rules-utils.js'); const { isRequireCall, isString } = require('./rules-utils.js');
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// Rule Definition // Rule Definition
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
function isString(node) {
return node && node.type === 'Literal' && typeof node.value === 'string';
}
function isTopLevel(node) { function isTopLevel(node) {
do { do {
if (node.type === 'FunctionDeclaration' || if (node.type === 'FunctionDeclaration' ||

View File

@ -4,7 +4,7 @@
'use strict'; 'use strict';
const path = require('path'); const path = require('path');
const { isRequireCall } = require('./rules-utils.js'); const { isRequireCall, isString } = require('./rules-utils.js');
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// Rule Definition // Rule Definition
@ -15,15 +15,6 @@ module.exports = function(context) {
const isESM = context.parserOptions.sourceType === 'module'; const isESM = context.parserOptions.sourceType === 'module';
const foundModules = []; 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. * Function to check if the path is a module and return its name.
* @param {String} str The path to check * @param {String} str The path to check

View File

@ -4,7 +4,7 @@
*/ */
'use strict'; 'use strict';
const { isRequireCall } = require('./rules-utils.js'); const { isRequireCall, isString } = require('./rules-utils.js');
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// Rule Definition // Rule Definition
@ -25,15 +25,6 @@ module.exports = function(context) {
return {}; return {};
} }
/**
* 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. * Function to check if the path is a required module and return its name.
* @param {String} str The path to check * @param {String} str The path to check

View File

@ -8,6 +8,10 @@ function isRequireCall(node) {
} }
module.exports.isRequireCall = isRequireCall; module.exports.isRequireCall = isRequireCall;
module.exports.isString = function(node) {
return node && node.type === 'Literal' && typeof node.value === 'string';
};
module.exports.isDefiningError = function(node) { module.exports.isDefiningError = function(node) {
return node.expression && return node.expression &&
node.expression.type === 'CallExpression' && node.expression.type === 'CallExpression' &&