Skip to content

Commit

Permalink
refactor: introduce hashSymbolOrNode
Browse files Browse the repository at this point in the history
  • Loading branch information
suphon-t authored and msp5382 committed Jan 3, 2024
1 parent a476bba commit 54beb26
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 17 deletions.
15 changes: 3 additions & 12 deletions src/walk/3-modify-get.ts
Original file line number Diff line number Diff line change
@@ -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<ts.Node, ts.Node>
) => {
// 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,
Expand All @@ -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);
}
}
Expand Down
7 changes: 2 additions & 5 deletions src/walk/source-templates.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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,
Expand Down
6 changes: 6 additions & 0 deletions src/walk/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

0 comments on commit 54beb26

Please sign in to comment.