UNPKG

ts-transformer-properties-rename

Version:

A TypeScript custom transformer which renames internal properties of the project

103 lines (102 loc) 4.66 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ExportsSymbolTree = void 0; const ts = require("typescript"); const typescript_helpers_1 = require("./typescript-helpers"); class ExportsSymbolTree { constructor(program, entrySourceFiles) { this.exportsTree = new Map(); this.program = program; this.computeTreeForExports(entrySourceFiles); } isSymbolAccessibleFromExports(symbol) { symbol = this.getActualSymbol(symbol); let result = false; this.exportsTree.forEach((set) => { result = result || set.has(symbol); }); return result; } computeTreeForExports(entrySourceFiles) { this.exportsTree.clear(); const typeChecker = this.program.getTypeChecker(); for (const filePath of entrySourceFiles) { const sourceFile = this.program.getSourceFile(filePath); if (sourceFile === undefined) { throw new Error(`Cannot find source file ${filePath}`); } const entrySourceFile = typeChecker.getSymbolAtLocation(sourceFile); if (entrySourceFile === undefined) { // if a source file doesn't have any export - then it doesn't have a symbol as well // so just skip it here continue; } for (const entryExportSymbol of (0, typescript_helpers_1.getExportsForSourceFile)(typeChecker, entrySourceFile)) { const exportSymbolsSet = new Set(); this.exportsTree.set(entryExportSymbol, exportSymbolsSet); for (const exportDeclaration of (0, typescript_helpers_1.getDeclarationsForSymbol)(entryExportSymbol)) { this.computeTreeForChildren(exportSymbolsSet, exportDeclaration, new Set()); } } } } computeTreeForChildren(targetSymbolsSet, node, visitedSymbols) { // it's similar to handling ts.Block node - both Block and variable's initializer are part of _implementation_ // and we don't care about that implementation at all - we just only need to worry it's definition // for functions it is arguments and return type // for variables - the type of a variable if (ts.isVariableDeclaration(node)) { const typeChecker = this.program.getTypeChecker(); const variableType = typeChecker.getTypeAtLocation(node); const variableTypeSymbol = variableType.getSymbol(); if (variableTypeSymbol !== undefined) { targetSymbolsSet.add(variableTypeSymbol); } return; } ts.forEachChild(node, (childNode) => this.computeTreeForNode(targetSymbolsSet, childNode, visitedSymbols)); } computeTreeForNode(targetSymbolsSet, node, visitedSymbols) { if (ts.isVariableStatement(node)) { for (const varDeclaration of node.declarationList.declarations) { this.computeTreeForNode(targetSymbolsSet, varDeclaration, visitedSymbols); } return; } // eslint-disable-next-line deprecation/deprecation if (node.kind === ts.SyntaxKind.JSDocComment || ts.isBlock(node)) { return; } if ((0, typescript_helpers_1.isClassMember)(node) && (0, typescript_helpers_1.hasPrivateKeyword)(node)) { return; } if (ts.isIdentifier(node)) { const symbol = this.getSymbol(node); if (symbol === null) { return; } if (visitedSymbols.has(symbol)) { return; } visitedSymbols.add(symbol); for (const childSymbol of (0, typescript_helpers_1.splitTransientSymbol)(symbol, this.program.getTypeChecker())) { targetSymbolsSet.add(childSymbol); for (const exportDeclaration of (0, typescript_helpers_1.getDeclarationsForSymbol)(childSymbol)) { this.computeTreeForChildren(targetSymbolsSet, exportDeclaration, visitedSymbols); } } } this.computeTreeForChildren(targetSymbolsSet, node, visitedSymbols); } getSymbol(node) { const nodeSymbol = this.program.getTypeChecker().getSymbolAtLocation(node); if (nodeSymbol === undefined) { return null; } return this.getActualSymbol(nodeSymbol); } getActualSymbol(symbol) { return (0, typescript_helpers_1.getActualSymbol)(symbol, this.program.getTypeChecker()); } } exports.ExportsSymbolTree = ExportsSymbolTree;