-
Notifications
You must be signed in to change notification settings - Fork 6
/
transform.ts
188 lines (173 loc) · 5.02 KB
/
transform.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// tslint:disable:no-console
import * as ts from "typescript";
const REACT_REGEX = /['"]react['"]/;
/**
* Check if node is a prologue directive (e.g "use strict")
* @param node node to check
* @returns {boolean} true if it is, false otherwise
*/
function isNotPrologueDirective(node: ts.Node): boolean {
return (
!ts.isExpressionStatement(node) || !ts.isStringLiteral(node.expression)
);
}
/**
* Check if this node is a import react node
*
* @param {ts.Node} node node
* @param {ts.SourceFile} sf source file to get text from
* @returns {boolean} true if it is, false otherwise
*/
function isReactImport(node: ts.Node, sf: ts.SourceFile): boolean {
return (
ts.isImportDeclaration(node) &&
REACT_REGEX.test(node.moduleSpecifier.getText(sf))
);
}
/**
* Check if an attribute is mutable (having non-primitive values)
*
* @param {ts.JsxAttributeLike} attr attribute
* @returns {boolean} true if mutable, false otherwise
*/
function isMutableProp(attr: ts.JsxAttributeLike): boolean {
// {...props} spread operator's definitely mutable
if (ts.isJsxSpreadAttribute(attr)) {
return true;
}
const { initializer } = attr;
// cases like <button enabled />
if (!initializer) {
return false;
}
// foo="bar"
if (ts.isStringLiteral(initializer)) {
return false;
}
if (ts.isJsxExpression(initializer)) {
const { expression } = initializer;
if (
// foo={true}
expression.kind === ts.SyntaxKind.TrueKeyword ||
// foo={false}
expression.kind === ts.SyntaxKind.FalseKeyword ||
// foo={1}
ts.isNumericLiteral(expression) ||
// foo={"asd"}
ts.isStringLiteral(expression)
) {
return false;
}
}
return true;
}
/**
* Check if element is considered a constant element
*
* @param {ts.Node} el element to check
* @returns {boolean}
*/
function isConstantElement(
el: ts.Node
): el is ts.JsxSelfClosingElement & boolean {
// We only handle self-closing el for now
// e.g: <img src="foo"/>
// TODO: We can support immutable children but later
if (!ts.isJsxSelfClosingElement(el)) {
return false;
}
// No attributes, e.g <br/>
return (
!el.attributes ||
!el.attributes.properties ||
!el.attributes.properties.length ||
// no mutable prop
!el.attributes.properties.find(isMutableProp)
);
}
/**
* Visit nodes recursively and try to determine if node's
* considered a constant node.
* NOTE: This modifies hoistedVariables inline
*
* @param {ts.TransformationContext} ctx transformation context
* @param {HoistedVariables} hoistedVariables hoistedVariables to populate
* @returns {ts.Visitor}
*/
function constantElementVisitor(
ctx: ts.TransformationContext,
hoistedVariables: ts.VariableStatement[]
): ts.Visitor {
const visitor: ts.Visitor = node => {
if (isConstantElement(node)) {
const variable = ts.createUniqueName("hoisted_constant_element");
const statement = ts.createVariableStatement(
undefined,
ts.createVariableDeclarationList([
ts.createVariableDeclaration(variable, undefined, node)
])
);
// Store the variable assignement to hoist later
hoistedVariables.push(statement);
// Replace <foo /> with {hoisted_constant_element_1}
// TODO: Figure out case like `return <foo />
return ts.createJsxExpression(undefined, variable);
}
return ts.visitEachChild(node, visitor, ctx);
};
return visitor;
}
function visitSourceFile(
ctx: ts.TransformationContext,
sf: ts.SourceFile,
opts?: Opts
): ts.SourceFile {
/**
* Find the 1st node that we can inject hoisted variable. This means:
* 1. Pass the prologue directive
* 2. Pass shebang (not a node)
* 3. Pass top level comments (not a node)
* 4. Pass React import (bc hoisted var uses React)
*/
const firstHoistableNodeIndex = sf.statements.findIndex(
node => isNotPrologueDirective(node) && isReactImport(node, sf)
);
// Can't find where to hoist
if (!~firstHoistableNodeIndex) {
return sf;
}
const hoistedVariables: ts.VariableStatement[] = [];
const elVisitor = constantElementVisitor(ctx, hoistedVariables);
// We assume we only care about nodes after React import
const transformedStatements = sf.statements
.slice(firstHoistableNodeIndex + 1, sf.statements.length)
.map(node => ts.visitNode(node, elVisitor));
if (opts.verbose) {
console.log(
`Hoisting ${hoistedVariables.length} elements in ${sf.fileName}:`
);
hoistedVariables.forEach(n =>
console.log(n.declarationList.declarations[0].initializer.getText(sf))
);
}
return ts.updateSourceFileNode(
sf,
ts.setTextRange(
ts.createNodeArray([
...sf.statements.slice(0, firstHoistableNodeIndex + 1),
// Inject hoisted variables
...hoistedVariables,
...transformedStatements
]),
sf.statements
)
);
}
export interface Opts {
verbose?: boolean;
}
export function transform(
opts: Opts = {}
): ts.TransformerFactory<ts.SourceFile> {
return ctx => sf => visitSourceFile(ctx, sf, opts);
}