(context)
| 12 | |
| 13 | module.exports = { |
| 14 | create(context) { |
| 15 | const requiredModule = 'common'; |
| 16 | const isESM = context.languageOptions.sourceType === 'module'; |
| 17 | const foundModules = []; |
| 18 | |
| 19 | /** |
| 20 | * Function to check if the path is a module and return its name. |
| 21 | * @param {string} str The path to check |
| 22 | * @returns {string} module name |
| 23 | */ |
| 24 | function getModuleName(str) { |
| 25 | if (str.startsWith('../') && str.endsWith('/common/index.mjs')) { |
| 26 | return 'common'; |
| 27 | } |
| 28 | |
| 29 | return path.basename(str); |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * Function to check if a node has an argument that is a module and |
| 34 | * return its name. |
| 35 | * @param {ASTNode} node The node to check |
| 36 | * @returns {undefined | string} module name or undefined |
| 37 | */ |
| 38 | function getModuleNameFromCall(node) { |
| 39 | // Node has arguments and first argument is string |
| 40 | if (node.arguments.length && isString(node.arguments[0])) { |
| 41 | return getModuleName(node.arguments[0].value.trim()); |
| 42 | } |
| 43 | |
| 44 | return undefined; |
| 45 | } |
| 46 | |
| 47 | const rules = { |
| 48 | 'Program:exit'(node) { |
| 49 | // The common module should be loaded in the first place. |
| 50 | const notLoadedFirst = foundModules.indexOf(requiredModule) !== 0; |
| 51 | if (notLoadedFirst) { |
| 52 | context.report({ |
| 53 | node: node.body[0] ?? node, |
| 54 | message: |
| 55 | 'Mandatory module "{{moduleName}}" must be loaded ' + |
| 56 | 'before any other modules.', |
| 57 | data: { moduleName: requiredModule }, |
| 58 | }); |
| 59 | } |
| 60 | }, |
| 61 | }; |
| 62 | |
| 63 | if (isESM) { |
| 64 | rules.ImportDeclaration = (node) => { |
| 65 | const moduleName = getModuleName(node.source.value); |
| 66 | if (moduleName) { |
| 67 | foundModules.push(moduleName); |
| 68 | } |
| 69 | }; |
| 70 | } else { |
| 71 | rules.CallExpression = (node) => { |
nothing calls this directly
no test coverage detected
searching dependent graphs…