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