/
githubmirror
/
node
Обзор
Документация
Войти
/
githubmirror
/
node
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
tools/eslint-rules/require-common-first.js
84 строки
2 KB
Huáng Jùnliàng
tools: bump eslint to v10, babel to v8.0.0-rc.2
03 мар 2026, 20:14
Не верифицирован
03 мар 2026, 20:14
e554661
Код
Авторство
О чём код?
/** * @file Require `common` module first in our tests. */ 'use strict'; const path = require('path'); const { isRequireCall, isString } = require('./rules-utils.js'); //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ module.exports = { create(context) { const requiredModule = 'common'; const isESM = context.languageOptions.sourceType === 'module'; const foundModules = []; /** * Function to check if the path is a module and return its name. * @param {string} str The path to check * @returns {string} module name */ function getModuleName(str) { if (str.startsWith('../') && str.endsWith('/common/index.mjs')) { return 'common'; } return path.basename(str); } /** * Function to check if a node has an argument that is a module and * return its name. * @param {ASTNode} node The node to check * @returns {undefined | string} module name or undefined */ function getModuleNameFromCall(node) { // Node has arguments and first argument is string if (node.arguments.length && isString(node.arguments[0])) { return getModuleName(node.arguments[0].value.trim()); } return undefined; } const rules = { 'Program:exit'(node) { // The common module should be loaded in the first place. const notLoadedFirst = foundModules.indexOf(requiredModule) !== 0; if (notLoadedFirst) { context.report({ node: node.body[0] ?? node, message: 'Mandatory module "{{moduleName}}" must be loaded ' + 'before any other modules.', data: { moduleName: requiredModule }, }); } }, }; if (isESM) { rules.ImportDeclaration = (node) => { const moduleName = getModuleName(node.source.value); if (moduleName) { foundModules.push(moduleName); } }; } else { rules.CallExpression = (node) => { if (isRequireCall(node)) { const moduleName = getModuleNameFromCall(node); if (moduleName) { foundModules.push(moduleName); } } }; } return rules; }, };