Skip to content

Commit 0e16b35

Browse files
cjihrigtargos
authored andcommitted
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: #27719 Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Masashi Hirano <[email protected]> Reviewed-By: Yongsheng Zhang <[email protected]> Reviewed-By: Rich Trott <[email protected]>
1 parent 74feb0b commit 0e16b35

File tree

4 files changed

+7
-25
lines changed

4 files changed

+7
-25
lines changed

tools/eslint-rules/no-duplicate-requires.js

+1-5
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,13 @@
44
*/
55
'use strict';
66

7-
const { isRequireCall } = require('./rules-utils.js');
7+
const { isRequireCall, isString } = require('./rules-utils.js');
88

99
//------------------------------------------------------------------------------
1010
// Rule Definition
1111
//------------------------------------------------------------------------------
1212

1313

14-
function isString(node) {
15-
return node && node.type === 'Literal' && typeof node.value === 'string';
16-
}
17-
1814
function isTopLevel(node) {
1915
do {
2016
if (node.type === 'FunctionDeclaration' ||

tools/eslint-rules/require-common-first.js

+1-10
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
'use strict';
55

66
const path = require('path');
7-
const { isRequireCall } = require('./rules-utils.js');
7+
const { isRequireCall, isString } = require('./rules-utils.js');
88

99
//------------------------------------------------------------------------------
1010
// Rule Definition
@@ -15,15 +15,6 @@ module.exports = function(context) {
1515
const isESM = context.parserOptions.sourceType === 'module';
1616
const foundModules = [];
1717

18-
/**
19-
* Function to check if a node is a string literal.
20-
* @param {ASTNode} node The node to check.
21-
* @returns {boolean} If the node is a string literal.
22-
*/
23-
function isString(node) {
24-
return node && node.type === 'Literal' && typeof node.value === 'string';
25-
}
26-
2718
/**
2819
* Function to check if the path is a module and return its name.
2920
* @param {String} str The path to check

tools/eslint-rules/required-modules.js

+1-10
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*/
55
'use strict';
66

7-
const { isRequireCall } = require('./rules-utils.js');
7+
const { isRequireCall, isString } = require('./rules-utils.js');
88

99
//------------------------------------------------------------------------------
1010
// Rule Definition
@@ -25,15 +25,6 @@ module.exports = function(context) {
2525
return {};
2626
}
2727

28-
/**
29-
* Function to check if a node is a string literal.
30-
* @param {ASTNode} node The node to check.
31-
* @returns {boolean} If the node is a string literal.
32-
*/
33-
function isString(node) {
34-
return node && node.type === 'Literal' && typeof node.value === 'string';
35-
}
36-
3728
/**
3829
* Function to check if the path is a required module and return its name.
3930
* @param {String} str The path to check

tools/eslint-rules/rules-utils.js

+4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ function isRequireCall(node) {
88
}
99
module.exports.isRequireCall = isRequireCall;
1010

11+
module.exports.isString = function(node) {
12+
return node && node.type === 'Literal' && typeof node.value === 'string';
13+
};
14+
1115
module.exports.isDefiningError = function(node) {
1216
return node.expression &&
1317
node.expression.type === 'CallExpression' &&

0 commit comments

Comments
 (0)