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 isString() in lint rules #27719

Closed
wants to merge 1 commit into from
Closed
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: 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