|
| 1 | +import { readFile, readdir } from "node:fs/promises"; |
| 2 | +import { dirname, extname, relative, resolve } from "node:path"; |
| 3 | +import ts from "typescript"; |
| 4 | + |
| 5 | +const sourceRoot = resolve(process.cwd(), "src"); |
| 6 | +const sourceExtensions = new Set([ |
| 7 | + ".ts", |
| 8 | + ".tsx", |
| 9 | + ".js", |
| 10 | + ".jsx", |
| 11 | + ".mts", |
| 12 | + ".mjs", |
| 13 | + ".cts", |
| 14 | + ".cjs", |
| 15 | +]); |
| 16 | + |
| 17 | +async function listSourceFiles(directory) { |
| 18 | + const entries = await readdir(directory, { withFileTypes: true }); |
| 19 | + const files = []; |
| 20 | + |
| 21 | + for (const entry of entries) { |
| 22 | + const filePath = resolve(directory, entry.name); |
| 23 | + if (entry.isDirectory()) { |
| 24 | + files.push(...(await listSourceFiles(filePath))); |
| 25 | + } else if (sourceExtensions.has(extname(entry.name))) { |
| 26 | + files.push(filePath); |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + return files; |
| 31 | +} |
| 32 | + |
| 33 | +function isStringLiteral(node) { |
| 34 | + return node && ts.isStringLiteral(node); |
| 35 | +} |
| 36 | + |
| 37 | +function collectModuleSpecifiers(sourceFile) { |
| 38 | + const specifiers = []; |
| 39 | + |
| 40 | + const addSpecifier = (node) => { |
| 41 | + if (isStringLiteral(node)) { |
| 42 | + specifiers.push({ |
| 43 | + line: |
| 44 | + sourceFile.getLineAndCharacterOfPosition(node.getStart(sourceFile)) |
| 45 | + .line + 1, |
| 46 | + value: node.text, |
| 47 | + }); |
| 48 | + } |
| 49 | + }; |
| 50 | + |
| 51 | + const visit = (node) => { |
| 52 | + if (ts.isImportDeclaration(node) || ts.isExportDeclaration(node)) { |
| 53 | + addSpecifier(node.moduleSpecifier); |
| 54 | + } else if (ts.isImportEqualsDeclaration(node)) { |
| 55 | + const reference = node.moduleReference; |
| 56 | + if (ts.isExternalModuleReference(reference)) { |
| 57 | + addSpecifier(reference.expression); |
| 58 | + } |
| 59 | + } else if (ts.isCallExpression(node) && node.arguments.length === 1) { |
| 60 | + const expression = node.expression; |
| 61 | + const isDynamicImport = expression.kind === ts.SyntaxKind.ImportKeyword; |
| 62 | + const isRequireCall = |
| 63 | + ts.isIdentifier(expression) && expression.text === "require"; |
| 64 | + const isVitestModuleCall = |
| 65 | + ts.isPropertyAccessExpression(expression) && |
| 66 | + (expression.name.text === "importActual" || |
| 67 | + expression.name.text === "importMock"); |
| 68 | + |
| 69 | + if (isDynamicImport || isRequireCall || isVitestModuleCall) { |
| 70 | + addSpecifier(node.arguments[0]); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + ts.forEachChild(node, visit); |
| 75 | + }; |
| 76 | + |
| 77 | + visit(sourceFile); |
| 78 | + return specifiers; |
| 79 | +} |
| 80 | + |
| 81 | +function getLongRelativeImport(filePath, specifier) { |
| 82 | + const parentTraversal = specifier.match(/^(?:\.\.\/)+/u)?.[0] ?? ""; |
| 83 | + const depth = (parentTraversal.match(/\.\.\//gu) ?? []).length; |
| 84 | + if (depth < 2) return null; |
| 85 | + |
| 86 | + const sourceRelativePath = relative(process.cwd(), filePath); |
| 87 | + const targetRelativePath = relative( |
| 88 | + process.cwd(), |
| 89 | + resolve(dirname(filePath), specifier), |
| 90 | + ).replaceAll("\\", "/"); |
| 91 | + |
| 92 | + if (targetRelativePath !== "src" && !targetRelativePath.startsWith("src/")) { |
| 93 | + return null; |
| 94 | + } |
| 95 | + |
| 96 | + return { |
| 97 | + file: sourceRelativePath.replaceAll("\\", "/"), |
| 98 | + specifier, |
| 99 | + }; |
| 100 | +} |
| 101 | + |
| 102 | +async function findViolations() { |
| 103 | + const violations = []; |
| 104 | + |
| 105 | + for (const filePath of await listSourceFiles(sourceRoot)) { |
| 106 | + const source = await readFile(filePath, "utf8"); |
| 107 | + const extension = extname(filePath); |
| 108 | + const scriptKind = |
| 109 | + extension === ".tsx" |
| 110 | + ? ts.ScriptKind.TSX |
| 111 | + : extension === ".jsx" |
| 112 | + ? ts.ScriptKind.JSX |
| 113 | + : [".js", ".mjs", ".cjs"].includes(extension) |
| 114 | + ? ts.ScriptKind.JS |
| 115 | + : ts.ScriptKind.TS; |
| 116 | + const sourceFile = ts.createSourceFile( |
| 117 | + filePath, |
| 118 | + source, |
| 119 | + ts.ScriptTarget.Latest, |
| 120 | + true, |
| 121 | + scriptKind, |
| 122 | + ); |
| 123 | + |
| 124 | + for (const specifier of collectModuleSpecifiers(sourceFile)) { |
| 125 | + const violation = getLongRelativeImport(filePath, specifier.value); |
| 126 | + if (violation) { |
| 127 | + violations.push({ ...violation, line: specifier.line }); |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + return violations.sort( |
| 133 | + (a, b) => a.file.localeCompare(b.file) || a.line - b.line, |
| 134 | + ); |
| 135 | +} |
| 136 | + |
| 137 | +const violations = await findViolations(); |
| 138 | + |
| 139 | +if (violations.length > 0) { |
| 140 | + console.error( |
| 141 | + `Found ${violations.length} src-internal long relative import${ |
| 142 | + violations.length === 1 ? "" : "s" |
| 143 | + }: |
| 144 | +${violations |
| 145 | + .map(({ file, line, specifier }) => `- ${file}:${line} ${specifier}`) |
| 146 | + .join("\n")}`, |
| 147 | + ); |
| 148 | + process.exit(1); |
| 149 | +} |
| 150 | + |
| 151 | +console.log("No src-internal long relative imports found."); |
0 commit comments