Skip to content

Commit 1330e50

Browse files
committed
Added caches to getting symbol visibility type operation
1 parent 8afefce commit 1330e50

File tree

1 file changed

+24
-12
lines changed

1 file changed

+24
-12
lines changed

src/transformer.ts

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@ const defaultOptions: RenameOptions = {
5454
publicJSDocTag: 'public',
5555
};
5656

57+
const enum VisibilityType {
58+
Internal,
59+
Private,
60+
External,
61+
}
62+
5763
// tslint:disable-next-line:no-default-export
5864
export default function propertiesRenameTransformer(program: ts.Program, config?: Partial<RenameOptions>): ts.TransformerFactory<ts.SourceFile> {
5965
return createTransformerFactory(program, config);
@@ -64,6 +70,13 @@ function createTransformerFactory(program: ts.Program, options?: Partial<RenameO
6470
const typeChecker = program.getTypeChecker();
6571
const exportsSymbolTree = new ExportsSymbolTree(program, fullOptions.entrySourceFiles);
6672

73+
const cache = new Map<ts.Symbol, VisibilityType>();
74+
75+
function putToCache(nodeSymbol: ts.Symbol, val: VisibilityType): VisibilityType {
76+
cache.set(nodeSymbol, val);
77+
return val;
78+
}
79+
6780
return (context: ts.TransformationContext) => {
6881
function transformNodeAndChildren(node: ts.SourceFile, context: ts.TransformationContext): ts.SourceFile;
6982
function transformNodeAndChildren(node: ts.Node, context: ts.TransformationContext): ts.Node;
@@ -263,12 +276,6 @@ function createTransformerFactory(program: ts.Program, options?: Partial<RenameO
263276
return getActualSymbol(symbol);
264277
}
265278

266-
const enum VisibilityType {
267-
Internal,
268-
Private,
269-
External,
270-
}
271-
272279
// tslint:disable-next-line:cyclomatic-complexity
273280
function isTypePropertyExternal(type: ts.Type, typePropertyName: string): boolean {
274281
if (type.flags & ts.TypeFlags.Object) {
@@ -375,26 +382,31 @@ function createTransformerFactory(program: ts.Program, options?: Partial<RenameO
375382
function getSymbolVisibilityType(nodeSymbol: ts.Symbol): VisibilityType {
376383
nodeSymbol = getActualSymbol(nodeSymbol);
377384

385+
const cachedValue = cache.get(nodeSymbol);
386+
if (cachedValue !== undefined) {
387+
return cachedValue;
388+
}
389+
378390
const symbolDeclarations = getDeclarationsForSymbol(nodeSymbol);
379391
if (symbolDeclarations.some(isDeclarationFromExternals)) {
380-
return VisibilityType.External;
392+
return putToCache(nodeSymbol, VisibilityType.External);
381393
}
382394

383395
if (isPrivateClassMember(nodeSymbol)) {
384-
return VisibilityType.Private;
396+
return putToCache(nodeSymbol, VisibilityType.Private);
385397
}
386398

387399
if (nodeSymbol.escapedName === 'prototype') {
388400
// accessing to prototype
389-
return VisibilityType.External;
401+
return putToCache(nodeSymbol, VisibilityType.External);
390402
}
391403

392404
if (fullOptions.publicJSDocTag.length !== 0) {
393405
for (const declaration of symbolDeclarations) {
394406
let currentNode: ts.Node = declaration;
395407
while (!ts.isSourceFile(currentNode)) {
396408
if (getNodeJSDocComment(currentNode).includes(`@${fullOptions.publicJSDocTag}`)) {
397-
return VisibilityType.External;
409+
return putToCache(nodeSymbol, VisibilityType.External);
398410
}
399411

400412
currentNode = currentNode.parent;
@@ -406,13 +418,13 @@ function createTransformerFactory(program: ts.Program, options?: Partial<RenameO
406418
let symbol: TSSymbolInternal | undefined = nodeSymbol as TSSymbolInternal;
407419
while (symbol !== undefined) {
408420
if (exportsSymbolTree.isSymbolAccessibleFromExports(symbol)) {
409-
return VisibilityType.External;
421+
return putToCache(nodeSymbol, VisibilityType.External);
410422
}
411423

412424
symbol = symbol.parent;
413425
}
414426

415-
return VisibilityType.Internal;
427+
return putToCache(nodeSymbol, VisibilityType.Internal);
416428
}
417429

418430
function getShorthandObjectBindingElementSymbol(element: ts.BindingElement): ts.Symbol | null {

0 commit comments

Comments
 (0)