|
| 1 | +/** |
| 2 | + * SPDX-FileCopyrightText: © 2017 Liferay, Inc. <https://liferay.com> |
| 3 | + * SPDX-License-Identifier: MIT |
| 4 | + */ |
| 5 | + |
| 6 | +const path = require('path'); |
| 7 | + |
| 8 | +function parseImportPath(importPath) { |
| 9 | + const parts = importPath.split('/'); |
| 10 | + |
| 11 | + let moduleName; |
| 12 | + let subpath; |
| 13 | + |
| 14 | + if (importPath.startsWith('@')) { |
| 15 | + moduleName = parts.slice(0, 2).join('/'); |
| 16 | + subpath = parts.slice(2).join('/'); |
| 17 | + } |
| 18 | + else { |
| 19 | + moduleName = parts[0]; |
| 20 | + subpath = parts.slice(1).join('/'); |
| 21 | + } |
| 22 | + |
| 23 | + return {moduleName, subpath}; |
| 24 | +} |
| 25 | + |
| 26 | +function checkImportPath(node, nodeScriptsConfig, context) { |
| 27 | + if ( |
| 28 | + node.source && |
| 29 | + node.source.type === 'Literal' && |
| 30 | + !node.source.value.startsWith('.') && |
| 31 | + !node.source.value.startsWith('/') |
| 32 | + ) { |
| 33 | + const importPath = node.source.value; |
| 34 | + |
| 35 | + const {moduleName, subpath} = parseImportPath(importPath); |
| 36 | + |
| 37 | + if (subpath) { |
| 38 | + const submodules = nodeScriptsConfig.imports[moduleName]; |
| 39 | + |
| 40 | + if (submodules) { |
| 41 | + const match = submodules.some( |
| 42 | + (submodule) => |
| 43 | + path.join(moduleName, submodule) === importPath |
| 44 | + ); |
| 45 | + |
| 46 | + if (!match) { |
| 47 | + context.report({ |
| 48 | + message: `'${moduleName}' does not export submodule: '${subpath}'. Do not import files deeply from another module.`, |
| 49 | + node, |
| 50 | + }); |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +module.exports = { |
| 58 | + create(context) { |
| 59 | + const nodeScriptsConfig = context.options[0]?.nodeScriptsConfig; |
| 60 | + |
| 61 | + if (!nodeScriptsConfig) { |
| 62 | + return {}; |
| 63 | + } |
| 64 | + |
| 65 | + return { |
| 66 | + ExportDefaultDeclaration(node) { |
| 67 | + checkImportPath(node, nodeScriptsConfig, context); |
| 68 | + }, |
| 69 | + ExportNamedDeclaration(node) { |
| 70 | + checkImportPath(node, nodeScriptsConfig, context); |
| 71 | + }, |
| 72 | + ImportDeclaration(node) { |
| 73 | + checkImportPath(node, nodeScriptsConfig, context); |
| 74 | + }, |
| 75 | + }; |
| 76 | + }, |
| 77 | + |
| 78 | + meta: { |
| 79 | + docs: { |
| 80 | + category: 'Best Practices', |
| 81 | + description: 'you cannot import files deeply from another module', |
| 82 | + recommended: false, |
| 83 | + }, |
| 84 | + fixable: null, |
| 85 | + schema: [{type: 'object'}], |
| 86 | + type: 'problem', |
| 87 | + }, |
| 88 | +}; |
0 commit comments