2019-05-12 07:11:13 +00:00
|
|
|
/**
|
|
|
|
* @fileoverview Require `common` module first in our tests.
|
|
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const path = require('path');
|
2019-05-15 19:29:56 +00:00
|
|
|
const { isRequireCall, isString } = require('./rules-utils.js');
|
2019-05-12 07:11:13 +00:00
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// Rule Definition
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
2023-02-03 09:55:29 +00:00
|
|
|
module.exports = {
|
|
|
|
create(context) {
|
|
|
|
const requiredModule = 'common';
|
2024-05-23 19:45:18 +00:00
|
|
|
const isESM = context.languageOptions.sourceType === 'module';
|
2023-02-03 09:55:29 +00:00
|
|
|
const foundModules = [];
|
2019-05-12 07:11:13 +00:00
|
|
|
|
2023-02-03 09:55:29 +00:00
|
|
|
/**
|
|
|
|
* 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 === '../common/index.mjs') {
|
|
|
|
return 'common';
|
|
|
|
}
|
2019-05-12 07:11:13 +00:00
|
|
|
|
2023-02-03 09:55:29 +00:00
|
|
|
return path.basename(str);
|
|
|
|
}
|
2019-05-12 07:11:13 +00:00
|
|
|
|
2023-02-03 09:55:29 +00:00
|
|
|
/**
|
|
|
|
* 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) {
|
2019-05-12 07:11:13 +00:00
|
|
|
// Node has arguments and first argument is string
|
2023-02-03 09:55:29 +00:00
|
|
|
if (node.arguments.length && isString(node.arguments[0])) {
|
|
|
|
return getModuleName(node.arguments[0].value.trim());
|
|
|
|
}
|
2019-05-12 07:11:13 +00:00
|
|
|
|
2023-02-03 09:55:29 +00:00
|
|
|
return undefined;
|
|
|
|
}
|
2019-05-12 07:11:13 +00:00
|
|
|
|
2023-02-03 09:55:29 +00:00
|
|
|
const rules = {
|
|
|
|
'Program:exit'(node) {
|
2019-05-12 07:11:13 +00:00
|
|
|
// The common module should be loaded in the first place.
|
2023-02-03 09:55:29 +00:00
|
|
|
const notLoadedFirst = foundModules.indexOf(requiredModule) !== 0;
|
|
|
|
if (notLoadedFirst) {
|
|
|
|
context.report(
|
|
|
|
node,
|
|
|
|
'Mandatory module "{{moduleName}}" must be loaded ' +
|
2019-05-12 07:11:13 +00:00
|
|
|
'before any other modules.',
|
2023-02-03 09:55:29 +00:00
|
|
|
{ moduleName: requiredModule },
|
|
|
|
);
|
|
|
|
}
|
|
|
|
},
|
2019-05-12 07:11:13 +00:00
|
|
|
};
|
|
|
|
|
2023-02-03 09:55:29 +00:00
|
|
|
if (isESM) {
|
|
|
|
rules.ImportDeclaration = (node) => {
|
|
|
|
const moduleName = getModuleName(node.source.value);
|
2019-05-12 07:11:13 +00:00
|
|
|
if (moduleName) {
|
|
|
|
foundModules.push(moduleName);
|
|
|
|
}
|
2023-02-03 09:55:29 +00:00
|
|
|
};
|
|
|
|
} else {
|
|
|
|
rules.CallExpression = (node) => {
|
|
|
|
if (isRequireCall(node)) {
|
|
|
|
const moduleName = getModuleNameFromCall(node);
|
|
|
|
|
|
|
|
if (moduleName) {
|
|
|
|
foundModules.push(moduleName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2019-05-12 07:11:13 +00:00
|
|
|
|
2023-02-03 09:55:29 +00:00
|
|
|
return rules;
|
|
|
|
},
|
2019-05-12 07:11:13 +00:00
|
|
|
};
|