2015-10-02 04:38:16 +00:00
|
|
|
/**
|
|
|
|
* @fileoverview Require usage of specified node modules.
|
|
|
|
* @author Rich Trott
|
|
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
|
2019-05-15 19:29:56 +00:00
|
|
|
const { isRequireCall, isString } = require('./rules-utils.js');
|
2019-05-13 18:50:55 +00:00
|
|
|
|
2015-10-02 04:38:16 +00:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// Rule Definition
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
2023-02-03 09:55:29 +00:00
|
|
|
module.exports = {
|
|
|
|
meta: { schema: [{
|
|
|
|
'type': 'object',
|
|
|
|
'additionalProperties': {
|
|
|
|
'type': 'string',
|
|
|
|
},
|
|
|
|
}] },
|
|
|
|
create(context) {
|
2019-03-22 02:44:26 +00:00
|
|
|
// Trim required module names
|
2023-02-03 09:55:29 +00:00
|
|
|
const options = context.options[0];
|
|
|
|
const requiredModules = options ? Object.keys(options).map((x) => {
|
|
|
|
return [ x, new RegExp(options[x]) ];
|
|
|
|
}) : [];
|
2024-05-23 19:45:18 +00:00
|
|
|
const isESM = context.languageOptions.sourceType === 'module';
|
2015-10-02 04:38:16 +00:00
|
|
|
|
2023-02-03 09:55:29 +00:00
|
|
|
const foundModules = [];
|
2015-10-02 04:38:16 +00:00
|
|
|
|
2023-02-03 09:55:29 +00:00
|
|
|
// If no modules are required we don't need to check the CallExpressions
|
|
|
|
if (requiredModules.length === 0) {
|
|
|
|
return {};
|
|
|
|
}
|
2015-10-02 04:38:16 +00:00
|
|
|
|
2023-02-03 09:55:29 +00:00
|
|
|
/**
|
|
|
|
* Function to check if the path is a required module and return its name.
|
|
|
|
* @param {string} str The path to check
|
|
|
|
* @returns {undefined | string} required module name or undefined
|
|
|
|
*/
|
|
|
|
function getRequiredModuleName(str) {
|
|
|
|
const match = requiredModules.find(([, test]) => {
|
|
|
|
return test.test(str);
|
|
|
|
});
|
|
|
|
return match ? match[0] : undefined;
|
|
|
|
}
|
2017-06-06 00:44:56 +00:00
|
|
|
|
2023-02-03 09:55:29 +00:00
|
|
|
/**
|
|
|
|
* Function to check if a node has an argument that is a required module and
|
|
|
|
* return its name.
|
|
|
|
* @param {ASTNode} node The node to check
|
|
|
|
* @returns {undefined | string} required module name or undefined
|
|
|
|
*/
|
|
|
|
function getRequiredModuleNameFromCall(node) {
|
2019-01-21 00:22:27 +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 getRequiredModuleName(node.arguments[0].value.trim());
|
|
|
|
}
|
2015-10-02 04:38:16 +00:00
|
|
|
|
2023-02-03 09:55:29 +00:00
|
|
|
return undefined;
|
|
|
|
}
|
2015-10-02 04:38:16 +00:00
|
|
|
|
2023-02-03 09:55:29 +00:00
|
|
|
const rules = {
|
|
|
|
'Program:exit'(node) {
|
|
|
|
if (foundModules.length < requiredModules.length) {
|
|
|
|
const missingModules = requiredModules.filter(
|
|
|
|
([module]) => foundModules.indexOf(module) === -1,
|
2017-06-18 05:35:39 +00:00
|
|
|
);
|
2023-02-03 09:55:29 +00:00
|
|
|
missingModules.forEach(([moduleName]) => {
|
|
|
|
context.report(
|
|
|
|
node,
|
|
|
|
'Mandatory module "{{moduleName}}" must be loaded.',
|
|
|
|
{ moduleName: moduleName },
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
2017-06-06 00:44:56 +00:00
|
|
|
};
|
|
|
|
|
2023-02-03 09:55:29 +00:00
|
|
|
if (isESM) {
|
|
|
|
rules.ImportDeclaration = (node) => {
|
|
|
|
const requiredModuleName = getRequiredModuleName(node.source.value);
|
2017-06-06 00:44:56 +00:00
|
|
|
if (requiredModuleName) {
|
|
|
|
foundModules.push(requiredModuleName);
|
|
|
|
}
|
2023-02-03 09:55:29 +00:00
|
|
|
};
|
|
|
|
} else {
|
|
|
|
rules.CallExpression = (node) => {
|
|
|
|
if (isRequireCall(node)) {
|
|
|
|
const requiredModuleName = getRequiredModuleNameFromCall(node);
|
2017-06-06 00:44:56 +00:00
|
|
|
|
2023-02-03 09:55:29 +00:00
|
|
|
if (requiredModuleName) {
|
|
|
|
foundModules.push(requiredModuleName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2015-10-02 04:38:16 +00:00
|
|
|
|
2023-02-03 09:55:29 +00:00
|
|
|
return rules;
|
|
|
|
},
|
2015-10-02 04:38:16 +00:00
|
|
|
};
|