-
Notifications
You must be signed in to change notification settings - Fork 598
/
constant-folding-plugin.js
168 lines (139 loc) Β· 3.95 KB
/
constant-folding-plugin.js
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
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
* @format
*/
'use strict';
import type {Types} from '@babel/types';
export type Visitors = {|
visitor: {|
BinaryExpression: {|exit: (path: any) => void|},
LogicalExpression: {|exit: (path: any) => void|},
Program: {|
enter: (path: any, state: any) => void,
exit: (path: any, state: any) => void,
|},
UnaryExpression: {|exit: (path: any) => void|},
|},
|};
function constantFoldingPlugin(context: {types: Types, ...}): Visitors {
const t = context.types;
const evaluate = function(path: Object) {
const state = {safe: true};
const unsafe = (path: Object, state: Object) => (state.safe = false);
path.traverse(
{
CallExpression: unsafe,
AssignmentExpression: unsafe,
},
state,
);
try {
return state.safe ? path.evaluate() : {confident: false, value: null};
} catch (err) {
return {confident: false, value: null};
}
};
const FunctionDeclaration = {
exit(path: Object, state: Object): void {
const binding =
path.node.id !== null && path.scope.getBinding(path.node.id.name);
if (binding && !binding.referenced) {
state.stripped = true;
path.remove();
}
},
};
const FunctionExpression = {
exit(path: Object, state: Object): void {
const parentPath = path.parentPath;
if (t.isVariableDeclarator(parentPath)) {
const binding = parentPath.scope.getBinding(parentPath.node.id.name);
if (binding && !binding.referenced) {
state.stripped = true;
parentPath.remove();
}
}
},
};
const Conditional = {
exit(path: Object, state: Object): void {
const node = path.node;
const result = evaluate(path.get('test'));
if (result.confident) {
state.stripped = true;
if (result.value || node.alternate) {
path.replaceWith(result.value ? node.consequent : node.alternate);
} else if (!result.value) {
path.remove();
}
}
},
};
const Expression = {
exit(path: Object): void {
const result = evaluate(path);
if (result.confident) {
path.replaceWith(t.valueToNode(result.value));
path.skip();
}
},
};
const LogicalExpression = {
exit(path: Object): void {
const node = path.node;
const result = evaluate(path.get('left'));
if (result.confident) {
const value = result.value;
switch (node.operator) {
case '||':
path.replaceWith(value ? node.left : node.right);
break;
case '&&':
path.replaceWith(value ? node.right : node.left);
break;
case '??':
path.replaceWith(value == null ? node.right : node.left);
break;
}
}
},
};
const Program = {
enter(path: Object, state: Object): void {
state.stripped = false;
},
exit(path: Object, state: Object): void {
path.traverse(
{
ArrowFunctionExpression: FunctionExpression,
ConditionalExpression: Conditional,
FunctionDeclaration,
FunctionExpression,
IfStatement: Conditional,
},
state,
);
if (state.stripped) {
path.scope.crawl();
// Re-traverse all program, if we removed any blocks. Manually re-call
// enter and exit, because traversing a Program node won't call them.
Program.enter(path, state);
path.traverse(visitor);
Program.exit(path, state);
}
},
};
const visitor = {
BinaryExpression: Expression,
LogicalExpression,
Program: {...Program}, // Babel mutates objects passed.
UnaryExpression: Expression,
};
return {visitor};
}
module.exports = constantFoldingPlugin;