Skip to content

Commit 54beb26

Browse files
suphon-tmsp5382
authored andcommitted
refactor: introduce hashSymbolOrNode
1 parent a476bba commit 54beb26

3 files changed

Lines changed: 11 additions & 17 deletions

File tree

src/walk/3-modify-get.ts

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
11
import ts from "typescript";
2-
import { DependencyGraph } from "./graph";
32
import { globalTypeChecker, globalContext } from ".";
4-
import { hashSymbol, hashNode } from "./utils";
3+
import { hashSymbolOrNode } from "./utils";
54

65
export const handleGet = (
76
node: ts.CallExpression,
87
transformList: Map<ts.Node, ts.Node>
98
) => {
109
// hash and move type argument to argument
11-
const symbol = globalTypeChecker
12-
.getTypeAtLocation(node.typeArguments![0])
13-
.getSymbol();
1410
const argument = ts.factory.createStringLiteral(
15-
symbol ? hashSymbol(symbol) : hashNode(node.typeArguments![0])
11+
hashSymbolOrNode(node.typeArguments![0])
1612
);
1713
const newNode = ts.factory.updateCallExpression(
1814
node,
@@ -38,12 +34,7 @@ export const getFactoryDependencies = (factory: ts.Expression) => {
3834
.getSymbol();
3935
if (symbol && symbol === getSymbol) {
4036
const classOrInterface = node.typeArguments![0];
41-
const symbol = globalTypeChecker
42-
.getTypeAtLocation(classOrInterface)
43-
.getSymbol();
44-
const hash = symbol
45-
? hashSymbol(symbol)
46-
: hashNode(node.typeArguments![0]);
37+
const hash = hashSymbolOrNode(node.typeArguments![0]);
4738
dependencies.push(hash);
4839
}
4940
}

src/walk/source-templates.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import ts from "typescript";
22
import { globalContext, globalTypeChecker } from ".";
3-
import { hashNode, hashSymbol } from "./utils";
3+
import { hashSymbolOrNode } from "./utils";
44

55
export const defaultFactoryTemplate = (
66
className: string,
@@ -132,10 +132,7 @@ export const factoryWrapperTemplate = (factory: ts.Expression) => {
132132
.getSymbol();
133133
if (symbol && symbol === getSymbol) {
134134
const classOrInterface = node.typeArguments![0];
135-
const classOrInterfaceSymbol = globalTypeChecker.getTypeAtLocation(classOrInterface).getSymbol()
136-
const hash = classOrInterfaceSymbol
137-
? hashSymbol(classOrInterfaceSymbol)
138-
: hashNode(classOrInterface);
135+
const hash = hashSymbolOrNode(classOrInterface);
139136
const newNode = ts.factory.createCallExpression(
140137
ts.factory.createIdentifier("get"),
141138
undefined,

src/walk/utils.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,9 @@ export const hashNode = (node: ts.Node) => {
3535

3636
return hash;
3737
};
38+
39+
export const hashSymbolOrNode = (node: ts.Node) => {
40+
const type = globalTypeChecker.getTypeAtLocation(node);
41+
const symbol = type.getSymbol();
42+
return symbol ? hashSymbol(symbol) : hashNode(node);
43+
}

0 commit comments

Comments
 (0)