forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequired-modules.js
115 lines (99 loc) · 3.14 KB
/
required-modules.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/**
* @fileoverview Require usage of specified node modules.
* @author Rich Trott
*/
'use strict';
const path = require('path');
//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
module.exports = function(context) {
// Trim required module names
const requiredModules = context.options;
const isESM = context.parserOptions.sourceType === 'module';
const foundModules = [];
// 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';
}
/**
* 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 value = path.basename(str);
// Check if value is in required modules array
return requiredModules.indexOf(value) !== -1 ? value : undefined;
}
/**
* 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) {
// Node has arguments and first argument is string
if (node.arguments.length && isString(node.arguments[0])) {
return getRequiredModuleName(node.arguments[0].value.trim());
}
return undefined;
}
const rules = {
'Program:exit'(node) {
if (foundModules.length < requiredModules.length) {
const missingModules = requiredModules.filter(
(module) => foundModules.indexOf(module) === -1
);
missingModules.forEach((moduleName) => {
context.report(
node,
'Mandatory module "{{moduleName}}" must be loaded.',
{ moduleName: moduleName }
);
});
}
}
};
if (isESM) {
rules.ImportDeclaration = (node) => {
const requiredModuleName = getRequiredModuleName(node.source.value);
if (requiredModuleName) {
foundModules.push(requiredModuleName);
}
};
} else {
rules.CallExpression = (node) => {
if (isRequireCall(node)) {
const requiredModuleName = getRequiredModuleNameFromCall(node);
if (requiredModuleName) {
foundModules.push(requiredModuleName);
}
}
};
}
return rules;
};
module.exports.schema = {
'type': 'array',
'additionalItems': {
'type': 'string'
},
'uniqueItems': true
};