forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrequire-common-first.js
83 lines (72 loc) · 2.23 KB
/
require-common-first.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
/**
* @fileoverview Require `common` module first in our tests.
*/
'use strict';
const path = require('path');
const { isRequireCall, isString } = require('./rules-utils.js');
//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
module.exports = {
create(context) {
const requiredModule = 'common';
const isESM = context.parserOptions.sourceType === 'module';
const foundModules = [];
/**
* Function to check if the path is a module and return its name.
* @param {string} str The path to check
* @returns {string} module name
*/
function getModuleName(str) {
if (str === '../common/index.mjs') {
return 'common';
}
return path.basename(str);
}
/**
* Function to check if a node has an argument that is a module and
* return its name.
* @param {ASTNode} node The node to check
* @returns {undefined | string} module name or undefined
*/
function getModuleNameFromCall(node) {
// Node has arguments and first argument is string
if (node.arguments.length && isString(node.arguments[0])) {
return getModuleName(node.arguments[0].value.trim());
}
return undefined;
}
const rules = {
'Program:exit'(node) {
// The common module should be loaded in the first place.
const notLoadedFirst = foundModules.indexOf(requiredModule) !== 0;
if (notLoadedFirst) {
context.report(
node,
'Mandatory module "{{moduleName}}" must be loaded ' +
'before any other modules.',
{ moduleName: requiredModule },
);
}
},
};
if (isESM) {
rules.ImportDeclaration = (node) => {
const moduleName = getModuleName(node.source.value);
if (moduleName) {
foundModules.push(moduleName);
}
};
} else {
rules.CallExpression = (node) => {
if (isRequireCall(node)) {
const moduleName = getModuleNameFromCall(node);
if (moduleName) {
foundModules.push(moduleName);
}
}
};
}
return rules;
},
};