Skip to content

Commit

Permalink
tools: decrease code duplication for isString() in lint rules
Browse files Browse the repository at this point in the history
This commit makes isString() a reusable utility
function for core's custom ESLint rules.

PR-URL: #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>
  • Loading branch information
cjihrig authored and targos committed May 18, 2019
1 parent 74feb0b commit 0e16b35
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 25 deletions.
6 changes: 1 addition & 5 deletions tools/eslint-rules/no-duplicate-requires.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' ||
Expand Down
11 changes: 1 addition & 10 deletions tools/eslint-rules/require-common-first.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -15,15 +15,6 @@ module.exports = function(context) {
const isESM = context.parserOptions.sourceType === 'module';
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
Expand Down
11 changes: 1 addition & 10 deletions tools/eslint-rules/required-modules.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/
'use strict';

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

//------------------------------------------------------------------------------
// Rule Definition
Expand All @@ -25,15 +25,6 @@ module.exports = function(context) {
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.
* @param {String} str The path to check
Expand Down
4 changes: 4 additions & 0 deletions tools/eslint-rules/rules-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' &&
Expand Down

0 comments on commit 0e16b35

Please sign in to comment.