-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsrc_transform.js
398 lines (372 loc) · 14.2 KB
/
src_transform.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
import UglifyJS from './uglify.js';
export default class BabelsbergSrcTransform {
isAlways(node) {
return ((node instanceof UglifyJS.AST_LabeledStatement) &&
(node.label.name === 'always') &&
(node.body instanceof UglifyJS.AST_BlockStatement));
}
isStay(node) {
return ((node instanceof UglifyJS.AST_LabeledStatement) &&
(node.label.name === 'stay') &&
(node.body instanceof UglifyJS.AST_BlockStatement));
}
isRule(node) {
if ((node instanceof UglifyJS.AST_Label) &&
node.name === 'rule') {
this.__ruleLabelSeen = node;
return true;
} else if (this.__ruleLabelSeen &&
node instanceof UglifyJS.AST_String) {
return true;
} else if ((node instanceof UglifyJS.AST_LabeledStatement) &&
(node.label.name === 'rule') &&
(node.body instanceof UglifyJS.AST_BlockStatement)) {
return true;
} else if ((node instanceof UglifyJS.AST_LabeledStatement) &&
(node.body.body instanceof UglifyJS.AST_SimpleStatement) &&
(node.body.body.body instanceof UglifyJS.AST_Call) &&
(node.body.body.body.expression instanceof UglifyJS.AST_Dot) &&
(node.body.body.body.expression.property === 'rule') &&
(node.body.body.body.expression.expression.name === 'bbb')) {
// rule label with string that was transformed... remove the label
this.__ruleLabelRemove = true;
return true;
}
this.__ruleLabelSeen = null;
return false;
}
isOnce(node) {
return ((node instanceof UglifyJS.AST_LabeledStatement) &&
(node.label.name === 'once') &&
(node.body instanceof UglifyJS.AST_BlockStatement));
}
isTrigger(node) {
return ((node instanceof UglifyJS.AST_Call) &&
(node.expression instanceof UglifyJS.AST_SymbolRef) &&
(node.expression.name === 'when'));
}
ensureThisToSelfIn(ast) {
var tr = new UglifyJS.TreeTransformer(function(node) {
if (node instanceof UglifyJS.AST_This) {
return new UglifyJS.AST_SymbolRef({
start: node.start,
end: node.end,
name: '_$_self'
});
}
}, null);
ast.transform(tr);
}
hasContextInArgs(constraintNode) {
if (constraintNode.args.length == 2) {
if (!(constraintNode.args[0] instanceof UglifyJS.AST_Object)) {
throw new SyntaxError(
"first argument of call to `always' must be an object"
);
}
return constraintNode.args[0].properties.some(function(ea) {
return ea.key === 'ctx';
});
} else {
return false;
}
}
createContextFor(ast, constraintNode) {
var enclosed = ast.enclosed,
self = this,
lastArg = constraintNode.args[constraintNode.args.length - 1];
if (lastArg instanceof UglifyJS.AST_Function) {
enclosed = lastArg.enclosed || [];
enclosed = enclosed.filter(function(ea) {
// reject all that
// 1. are not declared (var) BEFORE the always
// 2. are first referenced (globals, specials, etc) AFTER the always
return !((ea.init && (ea.init.start.pos > constraintNode.start.pos)) ||
(ea.orig && ea.orig[0] &&
(ea.orig[0].start.pos > constraintNode.end.pos)));
});
enclosed.push({name: '_$_self'}); // always include this
}
var ctx = new UglifyJS.AST_Object({
start: constraintNode.start,
end: constraintNode.end,
properties: enclosed.map(function(ea) {
return new UglifyJS.AST_ObjectKeyVal({
start: constraintNode.start,
end: constraintNode.end,
key: ea.name,
value: self.contextMap(ea.name)
});
})
});
var ctxkeyval = new UglifyJS.AST_ObjectKeyVal({
start: constraintNode.start,
end: constraintNode.end,
key: 'ctx',
value: ctx
});
if (constraintNode.args.length == 2) {
constraintNode.args[0].properties.push(ctxkeyval);
} else {
constraintNode.args.unshift(new UglifyJS.AST_Object({
start: constraintNode.start,
end: constraintNode.end,
properties: [ctxkeyval]
}));
}
}
ensureContextFor(ast, constraintNode) {
if (!this.hasContextInArgs(constraintNode)) {
this.createContextFor(ast, constraintNode);
}
}
getContextTransformerFor(ast) {
var self = this;
return new UglifyJS.TreeTransformer(null, function(node) {
if (self.isAlways(node)) {
return self.transformConstraint(ast, node, 'always');
} else if (self.isOnce(node)) {
return self.transformConstraint(ast, node, 'once');
} else if (self.isTrigger(node)) {
return self.transformConstraint(ast, node, 'when');
} else if (self.isStay(node)) {
return self.transformConstraint(ast, node, 'stay');
} else if (self.isRule(node)) {
var newNode = self.createRuleFor(node);
self.isTransformed = true;
return newNode;
}
});
}
transformConstraint(ast, node, name) {
var newNode = this.createCallFor(ast, node, name);
this.isTransformed = true;
return newNode;
}
transform(code) {
var ast = UglifyJS.parse(code);
ast.figure_out_scope();
var transformedAst = ast.transform(this.getContextTransformerFor(ast)),
stream = UglifyJS.OutputStream({beautify: true, comments: true});
if (this.isTransformed) {
transformedAst.print(stream);
return stream.toString();
} else {
return code;
}
}
transformAddScript(code) {
var ast = UglifyJS.parse(code);
ast.figure_out_scope(),
transformed = false;
var transformedAst = ast.transform(new UglifyJS.TreeTransformer(
null,
function(node) {
if (node instanceof UglifyJS.AST_Call &&
node.expression instanceof UglifyJS.AST_Dot &&
node.expression.property === 'addScript' &&
node.expression.expression instanceof UglifyJS.AST_This) {
if(node.args.length !== 1) throw 'Assertion error';
node.args.push(new UglifyJS.AST_String({
value: code.slice(node.args[0].start.pos, node.args[0].end.endpos)
}));
transformed = true;
return node;
}
})),
stream = UglifyJS.OutputStream({beautify: true, comments: true});
if (transformed) {
transformedAst.print(stream);
return stream.toString();
} else {
return code;
}
}
ensureReturnIn(body) {
var lastStatement = body[body.length - 1];
if (!(lastStatement.body instanceof UglifyJS.AST_Return)) {
body[body.length - 1] = new UglifyJS.AST_Return({
start: lastStatement.start,
end: lastStatement.end,
value: lastStatement
});
}
}
extractArgumentsFrom(constraintNode) {
var body = constraintNode.body.body,
newBody = [],
args = [],
extraArgs = [],
store;
newBody = body.filter(function(ea) {
if (ea instanceof UglifyJS.AST_LabeledStatement) {
if (!(ea.body instanceof UglifyJS.AST_SimpleStatement)) {
throw new SyntaxError(
"Labeled arguments in `always:' have to be simple statements"
);
}
if (ea.label.name == 'store' || ea.label.name == 'name') {
store = new UglifyJS.AST_Assign({
start: ea.start,
end: ea.end,
right: undefined /* filled later */,
operator: '=',
left: ea.body.body
});
} else {
extraArgs.push(new UglifyJS.AST_ObjectKeyVal({
start: ea.start,
end: ea.end,
key: ea.label.name,
value: ea.body.body
}));
}
return false;
} else {
return true;
}
});
if (extraArgs) {
args.push(new UglifyJS.AST_Object({
start: constraintNode.start,
end: constraintNode.end,
properties: extraArgs
}));
}
return {body: newBody, args: args, store: store};
}
createCallFor(ast, constraintNode, methodName) {
var body, args, store, enclosed,
self = this;
if (constraintNode instanceof UglifyJS.AST_LabeledStatement) {
var splitBodyAndArgs = this.extractArgumentsFrom(constraintNode);
body = splitBodyAndArgs.body;
args = splitBodyAndArgs.args;
store = splitBodyAndArgs.store;
enclosed = constraintNode.label.scope.enclosed;
} else if (constraintNode instanceof UglifyJS.AST_Call) {
var nodeArgs = constraintNode.args,
funcArg = nodeArgs[nodeArgs.length - 1];
if (!(funcArg instanceof UglifyJS.AST_Function)) {
throw new SyntaxError(
'Last argument to ' +
constraintNode.expression.name +
' must be a function'
);
}
body = funcArg.body;
args = nodeArgs.slice(0, nodeArgs.length - 1);
enclosed = funcArg.enclosed;
} else {
throw new SyntaxError("Don't know what to do with " + constraintNode);
}
this.ensureReturnIn(body);
body.forEach(function(ea) {
self.ensureThisToSelfIn(ea);
});
var call = new UglifyJS.AST_Call({
start: constraintNode.start,
end: constraintNode.end,
expression: new UglifyJS.AST_Dot({
start: constraintNode.start,
end: constraintNode.end,
property: methodName,
expression: new UglifyJS.AST_SymbolRef({
start: constraintNode.start,
end: constraintNode.end,
name: 'bbb'
})
}),
args: args.concat([new UglifyJS.AST_Function({
start: body.start,
end: body.end,
body: body,
enclosed: enclosed,
argnames: []
})])
});
this.ensureContextFor(ast, call);
var newBody;
if (store) {
store.right = call;
newBody = store;
} else {
newBody = call;
}
if (constraintNode instanceof UglifyJS.AST_Statement) {
return new UglifyJS.AST_SimpleStatement({
start: constraintNode.start,
end: constraintNode.end,
body: newBody
});
} else {
return newBody;
}
}
createRuleFor(ruleNode) {
// remove label
if (ruleNode instanceof UglifyJS.AST_Label) return ruleNode;
var stringNode;
if (ruleNode instanceof UglifyJS.AST_String) {
stringNode = ruleNode;
stringNode.value = stringNode.value.replace(/\|\s*-/mg, ':-');
ruleNode = this.__ruleLabelSeen;
delete this.__ruleLabelSeen;
} else if (this.__ruleLabelRemove) {
delete this.__ruleLabelRemove;
return ruleNode.body.body;
} else {
// ruleNode instanceof UglifyJS.AST_LabeledStatement
var stream = UglifyJS.OutputStream({beautify: true, comments: true});
ruleNode.body.print(stream);
stringNode = new UglifyJS.AST_String({
start: ruleNode.body.start,
end: ruleNode.body.end,
value: stream.toString().
replace(/\|\s*-/mg, ':-').
replace(/^{\s*/, '').
replace(/\s*}\s*$/, '').
replace(/\s*;\s*$/, '')
});
}
return new UglifyJS.AST_SimpleStatement({
start: ruleNode.start,
end: ruleNode.end,
body: new UglifyJS.AST_Call({
start: ruleNode.start,
end: ruleNode.end,
expression: new UglifyJS.AST_Dot({
start: ruleNode.start,
end: ruleNode.end,
property: 'rule',
expression: new UglifyJS.AST_SymbolRef({
start: ruleNode.start,
end: ruleNode.end,
name: 'bbb'
})
}),
args: [stringNode]
})
});
}
contextMap(name) {
// map some custom shortnames to bbb functions
if (name === '_$_self') {
return new UglifyJS.AST_Binary({
operator: '||',
left: new UglifyJS.AST_Dot({
expression: new UglifyJS.AST_This({}),
property: 'doitContext'
}),
right: new UglifyJS.AST_This({})
});
}
if (name === 'ro') {
name = 'bbb.readonly';
}
if (name === 'system') {
name = 'bbb.system()';
}
return new UglifyJS.AST_SymbolRef({name: name});
}
}