Skip to content

Commit aae0b06

Browse files
committed
perf(utils): cache the source file without any export
1 parent 8829719 commit aae0b06

File tree

2 files changed

+18
-13
lines changed

2 files changed

+18
-13
lines changed

src/transform.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ export default function(program: ts.Program, pluginOptions?: unknown) {
55
return (ctx: ts.TransformationContext) => {
66
return (sourceFile: ts.SourceFile) => {
77
return ts.visitEachChild(sourceFile, visitor, ctx);
8-
98
function visitor(node: ts.Node): ts.Node {
109
if (!ts.isEnumDeclaration(node)) {
1110
return ts.visitEachChild(node, visitor, ctx);
@@ -16,7 +15,9 @@ export default function(program: ts.Program, pluginOptions?: unknown) {
1615
}
1716

1817
if (!hasModifier(node, ts.SyntaxKind.ExportKeyword)) {
19-
if (!getExportedNamesOfSource(program, sourceFile).includes(node.name.text)) {
18+
const exportedNames = getExportedNamesOfSource(program, sourceFile);
19+
20+
if (!exportedNames.includes(node.name.text)) {
2021
return node;
2122
}
2223
}

src/utils.ts

+15-11
Original file line numberDiff line numberDiff line change
@@ -76,22 +76,26 @@ export function hasModifier(node: ts.Node, modifier: ts.SyntaxKind) {
7676
);
7777
}
7878

79-
const cachedMap = new WeakMap<ts.SourceFile, string[]>();
79+
const cachedNames = new WeakMap<ts.SourceFile, string[]>();
8080
export function getExportedNamesOfSource(program: ts.Program, sourceFile: ts.SourceFile) {
81-
const cached = cachedMap.get(sourceFile);
81+
const cached = cachedNames.get(sourceFile);
8282
if (cached) return cached;
8383

8484
const typeChecker = program.getTypeChecker();
8585
const sourceSymbol = typeChecker.getSymbolAtLocation(sourceFile);
86-
if (!sourceSymbol) return [];
86+
let names: string[];
8787

88-
const symbols = typeChecker.getExportsOfModule(sourceSymbol).map(s => {
89-
if (s.flags & ts.SymbolFlags.Alias) {
90-
return typeChecker.getAliasedSymbol(s).name;
91-
}
92-
return s.name;
93-
});
88+
if (sourceSymbol) {
89+
names = typeChecker.getExportsOfModule(sourceSymbol).map(s => {
90+
if (s.flags & ts.SymbolFlags.Alias) {
91+
return typeChecker.getAliasedSymbol(s).name;
92+
}
93+
return s.name;
94+
});
95+
} else {
96+
names = [];
97+
}
9498

95-
cachedMap.set(sourceFile, symbols);
96-
return symbols;
99+
cachedNames.set(sourceFile, names);
100+
return names;
97101
}

0 commit comments

Comments
 (0)