Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tools: DRY isRequireCall() in lint rules #27680

Merged
merged 1 commit into from
May 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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