MCPcopy Index your code
hub / github.com/nodejs/node / create

Function create

tools/eslint-rules/required-modules.js:20–99  ·  view source on GitHub ↗
(context)

Source from the content-addressed store, hash-verified

18 },
19 }] },
20 create(context) {
21 // Trim required module names
22 const options = context.options[0];
23 const requiredModules = options ? Object.keys(options).map((x) => {
24 return [ x, new RegExp(options[x]) ];
25 }) : [];
26 const isESM = context.languageOptions.sourceType === 'module';
27
28 const foundModules = [];
29
30 // If no modules are required we don't need to check the CallExpressions
31 if (requiredModules.length === 0) {
32 return {};
33 }
34
35 /**
36 * Function to check if the path is a required module and return its name.
37 * @param {string} str The path to check
38 * @returns {undefined | string} required module name or undefined
39 */
40 function getRequiredModuleName(str) {
41 const match = requiredModules.find(([, test]) => {
42 return test.test(str);
43 });
44 return match ? match[0] : undefined;
45 }
46
47 /**
48 * Function to check if a node has an argument that is a required module and
49 * return its name.
50 * @param {ASTNode} node The node to check
51 * @returns {undefined | string} required module name or undefined
52 */
53 function getRequiredModuleNameFromCall(node) {
54 // Node has arguments and first argument is string
55 if (node.arguments.length && isString(node.arguments[0])) {
56 return getRequiredModuleName(node.arguments[0].value.trim());
57 }
58
59 return undefined;
60 }
61
62 const rules = {
63 'Program:exit'(node) {
64 if (foundModules.length < requiredModules.length) {
65 const missingModules = requiredModules.filter(
66 ([module]) => foundModules.indexOf(module) === -1,
67 );
68 missingModules.forEach(([moduleName]) => {
69 context.report({
70 node: node.body[0] ?? node,
71 message: 'Mandatory module "{{moduleName}}" must be loaded.',
72 data: { moduleName: moduleName },
73 });
74 });
75 }
76 },
77 };

Callers

nothing calls this directly

Calls 6

getRequiredModuleNameFunction · 0.85
isRequireCallFunction · 0.70
mapMethod · 0.65
keysMethod · 0.65
pushMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…