-
Notifications
You must be signed in to change notification settings - Fork 0
/
transformer.ts
112 lines (93 loc) · 4.64 KB
/
transformer.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import ts from 'typescript';
export interface RenamePrivateOptions {
privatePrefix?: string,
}
export type RenameFunction = (identifier: string) => string;
type Handler<T extends ts.Node> = (node: T, visitor: ts.Visitor, context: ts.TransformationContext) => T;
export const RenamePrivateTransformer = (fn?: RenameFunction) => (program: ts.Program, config?: RenamePrivateOptions) => {
const typeChecker = program.getTypeChecker();
const transformerFactory: ts.TransformerFactory<ts.SourceFile> = context => sourceFile => {
const visitor = (node: ts.Node): ts.Node => {
if (ts.isIdentifier(node)) {
return handleIdentifier(node, visitor, context);
} else if (ts.isElementAccessExpression(node)) {
return handleElementAccessExpression(node, visitor, context);
} else if (ts.isBindingElement(node)) {
return handleBindingElement(node, visitor, context);
} else {
return ts.visitEachChild(node, visitor, context);
}
};
return ts.visitNode(sourceFile, visitor);
};
const handleIdentifier: Handler<ts.Identifier> = (node, visitor, context) => {
const symbol = typeChecker.getSymbolAtLocation(node);
if (!isPrivateSymbol(symbol)) return node;
return context.factory.createIdentifier(rename(symbol));
};
const handleElementAccessExpression: Handler<ts.ElementAccessExpression> = (node, visitor, context) => {
let symbol: ts.Symbol | undefined;
if (ts.isStringLiteral(node.argumentExpression)) {
// this['privateField']
symbol = typeChecker.getSymbolAtLocation(node.argumentExpression);
} else {
const type = typeChecker.getTypeAtLocation(node.argumentExpression);
if (!type.isStringLiteral()) {
// can't infer the symbol or if it is private
return ts.visitEachChild(node, visitor, context);
}
// this[x] where x has a string literal type
const parentType = typeChecker.getTypeAtLocation(node.expression);
symbol = parentType.getProperty(type.value);
}
// must recurse on node.expression for nested accesses regardless of the symbol
const index = isPrivateSymbol(symbol) ? context.factory.createStringLiteral(rename(symbol)) : node.argumentExpression;
if (ts.isElementAccessChain(node)) {
return context.factory.createElementAccessChain(
ts.visitNode(node.expression, visitor),
node.questionDotToken,
index
);
} else {
return context.factory.createElementAccessExpression(
ts.visitNode(node.expression, visitor),
index
);
}
};
const handleBindingElement: Handler<ts.BindingElement> = (node, visitor, context) => {
// check object, not array, binding
if (!ts.isObjectBindingPattern(node.parent)) return ts.visitEachChild(node, visitor, context);
let symbol: ts.Symbol | undefined;
if (node.propertyName === undefined) {
// const {name} = this;
if (!ts.isIdentifier(node.name)) {
// node.name should always be an identifier as a nested binding doesn't make sense in an object binding
// pattern without a property name first - recurse anyway to be safe
return ts.visitEachChild(node, visitor, context);
}
const parentType = typeChecker.getTypeAtLocation(node.parent);
symbol = parentType.getProperty(ts.idText(node.name));
} else {
// const {propertyName: name} = this;
symbol = typeChecker.getSymbolAtLocation(node.propertyName);
}
// must recurse on node.name for nested bindings and node.initializer regardless of the symbol
return context.factory.createBindingElement(
node.dotDotDotToken,
isPrivateSymbol(symbol) ? rename(symbol) : node.propertyName,
ts.visitNode(node.name, visitor),
ts.visitNode(node.initializer, visitor)
);
};
function isPrivateSymbol(symbol?: ts.Symbol): symbol is ts.Symbol {
return symbol?.valueDeclaration?.modifiers !== undefined
&& symbol.valueDeclaration.modifiers.some(x => x.kind === ts.SyntaxKind.PrivateKeyword);
}
function rename(symbol: ts.Symbol): string {
const name = ts.symbolName(symbol);
return fn === undefined ? (config?.privatePrefix ?? '__private_') + name : fn(name);
}
return transformerFactory;
};
export default RenamePrivateTransformer();