Skip to content

Commit

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

PR-URL: #27680
Reviewed-By: Richard Lau <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
Reviewed-By: Rich Trott <[email protected]>
Reviewed-By: Yongsheng Zhang <[email protected]>
  • Loading branch information
cjihrig committed May 15, 2019
1 parent 53bef42 commit 03d4353
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 24 deletions.
6 changes: 2 additions & 4 deletions tools/eslint-rules/no-duplicate-requires.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
*/
'use strict';

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

//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
Expand All @@ -13,10 +15,6 @@ function isString(node) {
return node && node.type === 'Literal' && typeof node.value === 'string';
}

function isRequireCall(node) {
return node.callee.type === 'Identifier' && node.callee.name === 'require';
}

function isTopLevel(node) {
do {
if (node.type === 'FunctionDeclaration' ||
Expand Down
10 changes: 1 addition & 9 deletions tools/eslint-rules/require-common-first.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
'use strict';

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

//------------------------------------------------------------------------------
// Rule Definition
Expand All @@ -23,15 +24,6 @@ module.exports = function(context) {
return node && node.type === 'Literal' && typeof node.value === 'string';
}

/**
* Function to check if a node is a require call.
* @param {ASTNode} node The node to check.
* @returns {boolean} If the node is a require call.
*/
function isRequireCall(node) {
return node.callee.type === 'Identifier' && node.callee.name === 'require';
}

/**
* Function to check if the path is a module and return its name.
* @param {String} str The path to check
Expand Down
11 changes: 2 additions & 9 deletions tools/eslint-rules/required-modules.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
*/
'use strict';

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

//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
Expand Down Expand Up @@ -32,15 +34,6 @@ module.exports = function(context) {
return node && node.type === 'Literal' && typeof node.value === 'string';
}

/**
* Function to check if a node is a require call.
* @param {ASTNode} node The node to check.
* @returns {boolean} If the node is a require call.
*/
function isRequireCall(node) {
return node.callee.type === 'Identifier' && node.callee.name === 'require';
}

/**
* Function to check if the path is a required module and return its name.
* @param {String} str The path to check
Expand Down
9 changes: 7 additions & 2 deletions tools/eslint-rules/rules-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
*/
'use strict';

function isRequireCall(node) {
return node.callee.type === 'Identifier' && node.callee.name === 'require';
}
module.exports.isRequireCall = isRequireCall;

module.exports.isDefiningError = function(node) {
return node.expression &&
node.expression.type === 'CallExpression' &&
Expand All @@ -16,7 +21,7 @@ module.exports.isDefiningError = function(node) {
* require calls.
*/
module.exports.isRequired = function(node, modules) {
return node.callee.name === 'require' && node.arguments.length !== 0 &&
return isRequireCall(node) && node.arguments.length !== 0 &&
modules.includes(node.arguments[0].value);
};

Expand All @@ -26,7 +31,7 @@ module.exports.isRequired = function(node, modules) {
*/
const commonModuleRegExp = new RegExp(/^(\.\.\/)*common(\.js)?$/);
module.exports.isCommonModule = function(node) {
return node.callee.name === 'require' &&
return isRequireCall(node) &&
node.arguments.length !== 0 &&
commonModuleRegExp.test(node.arguments[0].value);
};
Expand Down

0 comments on commit 03d4353

Please sign in to comment.