2021年我们有--explainFiles
如果您想更仔细地检查您的代码库(例如区分仅类型导入和运行时导入),您可以使用 Typescript API。可能性是无限的,但也许下面的这些部分可以帮助您找到一个好的方向(可能有错误):
import * as ts from "typescript";
interface FoundReference {
typeOnly: boolean;
relativePathReference: boolean;
referencingPath: string;
referencedSpecifier: string;
}
const specifierRelativeFile = /^\..*(?<!\.(less|svg|png|woff))$/;
const specifierNodeModule = /^[^\.]/;
const diveDeeper = (path: string, node: ts.Node, found: FoundReference[]) =>
Promise.all(node.getChildren().map(n => findAllReferencesNode(path, n, found)));
const findAllReferencesNode = async (path: string, node: ts.Node, found: FoundReference[]) => {
switch (node.kind) {
case ts.SyntaxKind.ExportDeclaration:
const exportDeclaration = node as ts.ExportDeclaration;
if (exportDeclaration.moduleSpecifier) {
const specifier = (exportDeclaration.moduleSpecifier as ts.StringLiteral).text;
if (specifier) {
if (specifierRelativeFile.test(specifier)) {
found.push({
typeOnly: exportDeclaration.isTypeOnly,
relativePathReference: true,
referencingPath: path,
referencedSpecifier: specifier
});
} else if (specifierNodeModule.test(specifier)) {
found.push({
typeOnly: exportDeclaration.isTypeOnly,
relativePathReference: false,
referencingPath: path,
referencedSpecifier: specifier
});
}
}
}
break;
case ts.SyntaxKind.ImportDeclaration:
const importDeclaration = node as ts.ImportDeclaration;
const importClause = importDeclaration.importClause;
const specifier = (importDeclaration.moduleSpecifier as ts.StringLiteral).text;
if (specifier) {
if (specifierRelativeFile.test(specifier)) {
found.push({
typeOnly: (!!importClause && !importClause.isTypeOnly),
relativePathReference: true,
referencingPath: path,
referencedSpecifier: specifier
});
} else if (specifierNodeModule.test(specifier)) {
found.push({
typeOnly: (!!importClause && !importClause.isTypeOnly),
relativePathReference: false,
referencingPath: path,
referencedSpecifier: specifier
});
}
}
break;
case ts.SyntaxKind.CallExpression:
const callExpression = node as ts.CallExpression;
if ((callExpression.expression.kind === ts.SyntaxKind.ImportKeyword ||
(callExpression.expression.kind === ts.SyntaxKind.Identifier &&
callExpression.expression.getText() === "require")) &&
callExpression.arguments[0]?.kind === ts.SyntaxKind.StringLiteral) {
const specifier = (callExpression.arguments[0] as ts.StringLiteral).text;
if (specifierRelativeFile.test(specifier)) {
found.push({
typeOnly: false,
relativePathReference: true,
referencingPath: path,
referencedSpecifier: specifier
});
} else if (specifierNodeModule.test(specifier)) {
found.push({
typeOnly: false,
relativePathReference: false,
referencingPath: path,
referencedSpecifier: specifier
});
} else {
await diveDeeper(path, node, found);
}
} else {
await diveDeeper(path, node, found);
}
break;
default:
await diveDeeper(path, node, found);
break;
}
}
const path = "example.ts";
const source = `
import foo from "./foo";
import * as bar from "./bar";
import { buzz } from "./fizz/buzz";
export foo from "./foo";
export * as bar from "./bar";
export { buzz } from "./fizz/buzz";
const whatever = require("whatever");
const stuff = async () => {
require("whatever");
const x = await import("xyz");
}
`
const rootNode = ts.createSourceFile(
path,
source,
ts.ScriptTarget.Latest,
/*setParentNodes */ true
);
const found: FoundReference[] = [];
findAllReferencesNode(path, rootNode, found)
.then(() => {
console.log(found);
});
[
{
"typeOnly": true,
"relativePathReference": true,
"referencingPath": "example.ts",
"referencedSpecifier": "./foo"
},
{
"typeOnly": true,
"relativePathReference": true,
"referencingPath": "example.ts",
"referencedSpecifier": "./bar"
},
{
"typeOnly": true,
"relativePathReference": true,
"referencingPath": "example.ts",
"referencedSpecifier": "./fizz/buzz"
},
{
"typeOnly": false,
"relativePathReference": true,
"referencingPath": "example.ts",
"referencedSpecifier": "./bar"
},
{
"typeOnly": false,
"relativePathReference": true,
"referencingPath": "example.ts",
"referencedSpecifier": "./fizz/buzz"
},
{
"typeOnly": false,
"relativePathReference": false,
"referencingPath": "example.ts",
"referencedSpecifier": "whatever"
},
{
"typeOnly": false,
"relativePathReference": false,
"referencingPath": "example.ts",
"referencedSpecifier": "whatever"
},
{
"typeOnly": false,
"relativePathReference": false,
"referencingPath": "example.ts",
"referencedSpecifier": "xyz"
}
]
一旦你有了referencedSpecifier,你需要一些基本的逻辑来将它解析到下一个路径,并用下一个解析的文件重复你的探索,递归。