|
| 1 | +const { AST_NODE_TYPES } = require("@typescript-eslint/utils"); |
| 2 | +const { createRule } = require("./utils.cjs"); |
| 3 | +const path = require("path"); |
| 4 | + |
| 5 | +const srcRoot = path.resolve(__dirname, "../../../src"); |
| 6 | + |
| 7 | +const tsNamespaceBarrelRegex = /_namespaces\/ts(?:\.ts|\.js|\.mts|\.mjs|\.cts|\.cjs)?$/; |
| 8 | + |
| 9 | +/** |
| 10 | + * @type {Array<{ name: string; path: string; }>} |
| 11 | + */ |
| 12 | +const modules = [ |
| 13 | + { |
| 14 | + name: "Debug", |
| 15 | + path: "compiler/debug", |
| 16 | + }, |
| 17 | +]; |
| 18 | + |
| 19 | +module.exports = createRule({ |
| 20 | + name: "prefer-direct-import", |
| 21 | + meta: { |
| 22 | + docs: { |
| 23 | + description: ``, |
| 24 | + }, |
| 25 | + messages: { |
| 26 | + importError: `{{ name }} should be imported directly from {{ path }}`, |
| 27 | + }, |
| 28 | + schema: [], |
| 29 | + type: "problem", |
| 30 | + fixable: "code", |
| 31 | + }, |
| 32 | + defaultOptions: [], |
| 33 | + |
| 34 | + create(context) { |
| 35 | + /** |
| 36 | + * @param {string} p |
| 37 | + */ |
| 38 | + function getImportPath(p) { |
| 39 | + let out = path.relative(path.dirname(context.filename), path.join(srcRoot, p)).replace(/\\/g, "/"); |
| 40 | + if (!out.startsWith(".")) out = "./" + out; |
| 41 | + return out; |
| 42 | + } |
| 43 | + |
| 44 | + /** @type {any} */ |
| 45 | + let program; |
| 46 | + let addedImport = false; |
| 47 | + |
| 48 | + return { |
| 49 | + Program: node => { |
| 50 | + program = node; |
| 51 | + }, |
| 52 | + ImportDeclaration: node => { |
| 53 | + if (node.importKind !== "value" || !tsNamespaceBarrelRegex.test(node.source.value)) return; |
| 54 | + |
| 55 | + for (const specifier of node.specifiers) { |
| 56 | + if (specifier.type !== AST_NODE_TYPES.ImportSpecifier || specifier.importKind !== "value") continue; |
| 57 | + |
| 58 | + for (const mod of modules) { |
| 59 | + if (specifier.imported.name !== mod.name) continue; |
| 60 | + |
| 61 | + context.report({ |
| 62 | + messageId: "importError", |
| 63 | + data: { name: mod.name, path: mod.path }, |
| 64 | + node: specifier, |
| 65 | + fix: fixer => { |
| 66 | + const newCode = `import * as ${mod.name} from "${getImportPath(mod.path)}";`; |
| 67 | + const fixes = []; |
| 68 | + if (node.specifiers.length === 1) { |
| 69 | + if (addedImport) { |
| 70 | + fixes.push(fixer.remove(node)); |
| 71 | + } |
| 72 | + else { |
| 73 | + fixes.push(fixer.replaceText(node, newCode)); |
| 74 | + addedImport = true; |
| 75 | + } |
| 76 | + } |
| 77 | + else { |
| 78 | + const comma = context.sourceCode.getTokenAfter(specifier, token => token.value === ","); |
| 79 | + if (!comma) throw new Error("comma is null"); |
| 80 | + const prevNode = context.sourceCode.getTokenBefore(specifier); |
| 81 | + if (!prevNode) throw new Error("prevNode is null"); |
| 82 | + fixes.push( |
| 83 | + fixer.removeRange([prevNode.range[1], specifier.range[0]]), |
| 84 | + fixer.remove(specifier), |
| 85 | + fixer.remove(comma), |
| 86 | + ); |
| 87 | + if (!addedImport) { |
| 88 | + fixes.push(fixer.insertTextBefore(node, newCode + "\r\n")); |
| 89 | + addedImport = true; |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + return fixes; |
| 94 | + }, |
| 95 | + }); |
| 96 | + } |
| 97 | + } |
| 98 | + }, |
| 99 | + MemberExpression: node => { |
| 100 | + if (node.object.type !== AST_NODE_TYPES.Identifier || node.object.name !== "ts") return; |
| 101 | + |
| 102 | + for (const mod of modules) { |
| 103 | + if (node.property.type !== AST_NODE_TYPES.Identifier || node.property.name !== mod.name) continue; |
| 104 | + |
| 105 | + context.report({ |
| 106 | + messageId: "importError", |
| 107 | + data: { name: mod.name, path: mod.path }, |
| 108 | + node, |
| 109 | + fix: fixer => { |
| 110 | + const fixes = [fixer.replaceText(node, mod.name)]; |
| 111 | + |
| 112 | + if (!addedImport) { |
| 113 | + fixes.push(fixer.insertTextBefore(program, `import * as ${mod.name} from "${getImportPath(mod.path)}";\r\n`)); |
| 114 | + addedImport = true; |
| 115 | + } |
| 116 | + |
| 117 | + return fixes; |
| 118 | + }, |
| 119 | + }); |
| 120 | + } |
| 121 | + }, |
| 122 | + }; |
| 123 | + }, |
| 124 | +}); |
0 commit comments