-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
409 lines (337 loc) · 11.1 KB
/
index.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
399
400
401
402
403
404
405
406
407
408
409
/*jslint node: true, nomen: true*/
'use strict';
var estraverse = require('estraverse'),
rocamboleToken = require('rocambole-token'),
braces = require('esformatter-braces').nodeBefore,
ctx,
Context;
Context = function Context() {
this.constructor.apply(this, arguments);
};
Context.prototype.constructor = function Context(node, parent) {
this.node(node);
this.parent(parent);
this._firstVars = null;
this._declaredVars = [];
if (this.node().params) {
this.node().params.forEach(function (param) {
this.declareVar({
id: {
type: 'Identifier',
name: param.name
}
});
}, this);
}
};
// Object setter/getter
Context.prototype.node = function (node) {
if (arguments.length === 1) {
this._node = node;
}
return this._node;
};
Context.prototype.parent = function parent(node) {
if (arguments.length === 1) {
this._parent = node;
}
return this._parent;
};
Context.prototype.body = function body() {
return this.node().type === 'Program' ? this.node().body : this.node().body.body;
};
Context.prototype.firstVars = function firstVars(node) {
if (arguments.length === 1) {
this._firstVars = node;
}
return this._firstVars;
};
// Booelean getter
Context.prototype.isBlock = function isBlock(node) {
return node.type === 'Program' || node.type === 'BlockStatement';
};
// Booelean getter
Context.prototype.isScopeBlock = function isScopeBlock(node) {
return node.type === 'Program' || node.type === 'FunctionDeclaration' || node.type === 'FunctionExpression';
};
Context.prototype.isScopeBlockChild = function isScopeBlockChild(node) {
if (node.parent.type === 'Program') {
return true;
}
if (node.parent.type === 'BlockStatement' && node.parent.parent === this.node()) {
return true;
}
return false;
};
Context.prototype.isFor = function isFor(node) {
return node.type === 'ForStatement';
};
Context.prototype.isForin = function isFor(node) {
return node.type === 'ForInStatement';
};
Context.prototype.isVars = function isVars(node) {
return node.type === 'VariableDeclaration';
};
Context.prototype.isFirstVars = function isFirstVars(node) {
return this._firstVars === node;
};
Context.prototype.isVarDeclared = function isVarDeclared(varname) {
return this._declaredVars.indexOf(varname) !== -1;
};
// Methods
Context.prototype.declareVar = function declareVar(node) {
if (!this.isVarDeclared(node.id.name)) {
this._declaredVars.push(node.id.name);
}
};
Context.prototype.createFirstVars = function () {
var startToken, endToken;
// Tokens for variable list
startToken = rocamboleToken.after(this.node().body.startToken, {
type: 'Keyword',
value: 'var'
});
endToken = rocamboleToken.after(startToken, {
type: 'Punctuator',
value: ';'
});
// Create a variable declarations node as the first one
this.firstVars({
type: 'VariableDeclaration',
declarations: [],
kind: 'var',
parent: this.node(),
prev: undefined,
next: undefined,
depth: this.body().depth + 1,
startToken: startToken,
endToken: endToken
});
// Set references
this.firstVars().next = this.body()[0];
this.body()[0].prev = this.firstVars();
this.body().unshift(this.firstVars());
};
Context.prototype.addToFirstVars = function addToFirstVars(node) {
// Skip if it's already declared
if (this.isVarDeclared(node.id.name)) {
return;
}
var firstVars = this.firstVars(),
declarations = firstVars.declarations,
declarationLast = declarations[declarations.length - 1],
varNode,
idNode,
idToken,
separatorToken;
if (declarationLast) {
// Comma token for variable list
separatorToken = rocamboleToken.after(declarationLast.endToken, {
type: 'Punctuator',
value: ','
});
// Comma token for variable list
separatorToken = rocamboleToken.after(separatorToken, {
type: 'LineBreak',
value: '\n'
});
separatorToken = rocamboleToken.after(separatorToken, {
type: 'WhiteSpace',
value: ' '
});
} else {
separatorToken = rocamboleToken.after(firstVars.startToken, {
type: 'WhiteSpace',
value: ' '
});
}
// Id token for variable identifier
idToken = rocamboleToken.after(separatorToken, {
type: 'Identifier',
value: node.id.name
});
// The variable id node
idNode = {
type: 'Identifier',
name: node.id.name,
parent: undefined,
prev: undefined,
next: undefined,
depth: firstVars.depth + 2,
startToken: idToken,
endToken: idToken
};
// The variable declarator node wrapping the identifier
varNode = {
type: 'VariableDeclarator',
id: idNode,
init: null,
parent: undefined,
prev: undefined,
next: undefined,
depth: firstVars.depth + 1,
startToken: idToken,
endToken: idToken
};
if (declarationLast) {
// Set reference on last declaration
declarationLast.next = varNode;
varNode.prev = declarationLast;
}
// Set parent reference
idNode.parent = varNode;
varNode.parent = firstVars;
// Add the declaration node to the declarations list
declarations.push(varNode);
// Add to declared variables list
this.declareVar(node);
};
Context.prototype.declaratorToAssignment = function declaratorToAssignment(node) {
var vars = node.parent,
commaToken;
if (this.isForin(vars.parent)) {
return;
}
if (this.isFor(vars.parent)) {
// Convert to assignment
node.type = 'AssignmentExpression';
node.operator = '=';
node.left = node.id;
node.right = node.init;
delete node.id;
delete node.init;
delete node.range;
} else {
// Convert to assignment
node.type = 'AssignmentExpression';
node.operator = '=';
node.left = node.id;
node.right = node.init;
delete node.id;
delete node.init;
delete node.range;
// Convert `,` to `;`
commaToken = rocamboleToken.findNextNonEmpty(node.endToken);
if (commaToken.value === ',') {
commaToken.value = ';';
}
}
};
Context.prototype.declarationUnwrap = function declarationUnwrap(node) {
// Ensure block before unwrap declarations
if (!this.isBlock(node.parent)) {
braces(node.parent);
}
var declarations = node.declarations.filter(function (assignment) {
return assignment.right ? true : false;
}),
declarationFirst = declarations[0],
declarationLast = declarations[declarations.length - 1],
parent = node.parent.type === 'SwitchCase' ? node.parent.consequent : node.parent.body,
index = parent.indexOf(node),
args;
if (declarations.length) {
// Set new start references
declarationFirst.startToken.prev = node.startToken.prev;
node.startToken.prev.next = declarationFirst.startToken;
// Set new end reference
declarationLast.endToken.next = node.endToken.next;
if (node.endToken.next) {
node.endToken.next.prev = declarationLast.endToken;
}
// Set end token `;`
if ((rocamboleToken.findNextNonEmpty(declarationLast.endToken) || {}).value !== ';') {
rocamboleToken.after(declarationLast.endToken, {
type: 'Punctuator',
value: ';'
});
}
} else {
node.startToken.prev.next = node.endToken.next;
node.endToken.next.prev = node.startToken.prev;
declarations = [];
}
// Set new parent and level for each assignment
declarations.forEach(function (declaration) {
declaration.depth = declaration.depth - 1;
declaration.parent = parent;
});
// Replace declarations with assignments
args = [].concat(declarations);
args.unshift(1);
args.unshift(index);
parent.splice.apply(parent, args);
};
Context.prototype.declarationToIdentifier = function declarationToIdentifier(node) {
var prevToken = rocamboleToken.findPrevNonEmpty(node.startToken),
forin = node.parent;
// Set identifier as left
forin.left = node.declarations.shift().id;
// Update references
forin.left.startToken.prev = prevToken;
prevToken.next = forin.left.startToken;
};
Context.prototype.declarationToSequence = function declarationToSequence(node) {
var prevToken = node.startToken.prev,
startToken = rocamboleToken.findNextNonEmpty(node.startToken);
prevToken.next = startToken;
startToken.prev = prevToken;
node.type = 'SequenceExpression';
node.expressions = node.declarations;
node.startToken = startToken;
delete node.declarations;
delete node.kind;
delete node.range;
};
// Static method
Context.traverse = function traverse() {
var ctx = new Context({});
return {
enter: function (node) {
if (ctx.isScopeBlock(node)) {
// Enter the current scope block and create a new scope context
ctx = new Context(node, ctx);
} else if (ctx.isVars(node)) {
// For variable declarations
if (!ctx.firstVars()) {
// Init first variable declarations
if (ctx.isScopeBlockChild(node)) {
// Set current as first one if it's direct child (not in a for statement for example)
ctx.firstVars(node);
} else {
// Create one
ctx.createFirstVars();
}
}
if (ctx.isFirstVars(node)) {
// Set variables as declared if declaration is first one
node.declarations.forEach(ctx.declareVar, ctx);
} else {
// Add declarations to first declarations
node.declarations.forEach(ctx.addToFirstVars, ctx);
// Add declarations to first declarations
node.declarations.forEach(ctx.declaratorToAssignment, ctx);
if (ctx.isFor(node.parent)) {
ctx.declarationToSequence(node);
} else if (ctx.isForin(node.parent)) {
ctx.declarationToIdentifier(node);
} else {
ctx.declarationUnwrap(node);
}
}
}
},
leave: function (node) {
if (ctx.isScopeBlock(node)) {
// Leave the current scope block and go to parent scope context
ctx = ctx.parent();
}
}
};
};
// Export the plugin
module.exports = {
transformBefore: function (ast) {
estraverse.traverse(ast, Context.traverse());
}
};