diff --git a/src/walk/3-modify-get.ts b/src/walk/3-modify-get.ts index 1e3ab14..ff8d6fe 100644 --- a/src/walk/3-modify-get.ts +++ b/src/walk/3-modify-get.ts @@ -1,18 +1,14 @@ import ts from "typescript"; -import { DependencyGraph } from "./graph"; import { globalTypeChecker, globalContext } from "."; -import { hashSymbol, hashNode } from "./utils"; +import { hashSymbolOrNode } from "./utils"; export const handleGet = ( node: ts.CallExpression, transformList: Map ) => { // hash and move type argument to argument - const symbol = globalTypeChecker - .getTypeAtLocation(node.typeArguments![0]) - .getSymbol(); const argument = ts.factory.createStringLiteral( - symbol ? hashSymbol(symbol) : hashNode(node.typeArguments![0]) + hashSymbolOrNode(node.typeArguments![0]) ); const newNode = ts.factory.updateCallExpression( node, @@ -38,12 +34,7 @@ export const getFactoryDependencies = (factory: ts.Expression) => { .getSymbol(); if (symbol && symbol === getSymbol) { const classOrInterface = node.typeArguments![0]; - const symbol = globalTypeChecker - .getTypeAtLocation(classOrInterface) - .getSymbol(); - const hash = symbol - ? hashSymbol(symbol) - : hashNode(node.typeArguments![0]); + const hash = hashSymbolOrNode(node.typeArguments![0]); dependencies.push(hash); } } diff --git a/src/walk/source-templates.ts b/src/walk/source-templates.ts index 061e2a5..1fb7306 100644 --- a/src/walk/source-templates.ts +++ b/src/walk/source-templates.ts @@ -1,6 +1,6 @@ import ts from "typescript"; import { globalContext, globalTypeChecker } from "."; -import { hashNode, hashSymbol } from "./utils"; +import { hashSymbolOrNode } from "./utils"; export const defaultFactoryTemplate = ( className: string, @@ -132,10 +132,7 @@ export const factoryWrapperTemplate = (factory: ts.Expression) => { .getSymbol(); if (symbol && symbol === getSymbol) { const classOrInterface = node.typeArguments![0]; - const classOrInterfaceSymbol = globalTypeChecker.getTypeAtLocation(classOrInterface).getSymbol() - const hash = classOrInterfaceSymbol - ? hashSymbol(classOrInterfaceSymbol) - : hashNode(classOrInterface); + const hash = hashSymbolOrNode(classOrInterface); const newNode = ts.factory.createCallExpression( ts.factory.createIdentifier("get"), undefined, diff --git a/src/walk/utils.ts b/src/walk/utils.ts index 1a70c93..3891601 100644 --- a/src/walk/utils.ts +++ b/src/walk/utils.ts @@ -35,3 +35,9 @@ export const hashNode = (node: ts.Node) => { return hash; }; + +export const hashSymbolOrNode = (node: ts.Node) => { + const type = globalTypeChecker.getTypeAtLocation(node); + const symbol = type.getSymbol(); + return symbol ? hashSymbol(symbol) : hashNode(node); +}