2015-10-02 06:38:16 +02:00
|
|
|
/**
|
|
|
|
* @fileoverview Require usage of specified node modules.
|
|
|
|
* @author Rich Trott
|
|
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
|
2017-06-19 17:56:04 +02:00
|
|
|
const path = require('path');
|
2015-10-02 06:38:16 +02:00
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// Rule Definition
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
module.exports = function(context) {
|
|
|
|
// trim required module names
|
|
|
|
var requiredModules = context.options;
|
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
|
|
|
|
|
|
|
// if no modules are required we don't need to check the CallExpressions
|
|
|
|
if (requiredModules.length === 0) {
|
|
|
|
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 a node is a require call.
|
|
|
|
* @param {ASTNode} node The node to check.
|
|
|
|
* @returns {boolean} If the node is a require call.
|
|
|
|
*/
|
|
|
|
function isRequireCall(node) {
|
|
|
|
return node.callee.type === 'Identifier' && node.callee.name === 'require';
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
var value = path.basename(str);
|
|
|
|
|
|
|
|
// check if value is in required modules array
|
|
|
|
return requiredModules.indexOf(value) !== -1 ? value : undefined;
|
|
|
|
}
|
|
|
|
|
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) {
|
2015-10-02 06:38:16 +02:00
|
|
|
// node has arguments and first argument is string
|
|
|
|
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) {
|
|
|
|
var missingModules = requiredModules.filter(
|
|
|
|
function(module) {
|
2017-06-19 20:13:30 +02:00
|
|
|
return foundModules.indexOf(module) === -1;
|
2015-10-02 06:38:16 +02:00
|
|
|
}
|
2017-06-18 07:35:39 +02:00
|
|
|
);
|
2015-10-02 06:38:16 +02:00
|
|
|
missingModules.forEach(function(moduleName) {
|
|
|
|
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) => {
|
|
|
|
var requiredModuleName = getRequiredModuleName(node.source.value);
|
|
|
|
if (requiredModuleName) {
|
|
|
|
foundModules.push(requiredModuleName);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
rules.CallExpression = (node) => {
|
|
|
|
if (isRequireCall(node)) {
|
|
|
|
var requiredModuleName = getRequiredModuleNameFromCall(node);
|
|
|
|
|
|
|
|
if (requiredModuleName) {
|
|
|
|
foundModules.push(requiredModuleName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return rules;
|
2015-10-02 06:38:16 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports.schema = {
|
|
|
|
'type': 'array',
|
|
|
|
'additionalItems': {
|
|
|
|
'type': 'string'
|
|
|
|
},
|
|
|
|
'uniqueItems': true
|
|
|
|
};
|