1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, '__esModule', { value: true });
|
4 |
|
5 | function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
|
6 |
|
7 | var acorn = require('acorn');
|
8 | var acornJsx = _interopDefault(require('acorn-jsx'));
|
9 | var acornDynamicImport = _interopDefault(require('acorn-dynamic-import'));
|
10 | var MagicString = _interopDefault(require('magic-string'));
|
11 | var rewritePattern = _interopDefault(require('regexpu-core'));
|
12 |
|
13 |
|
14 |
|
15 | function toJSON(node) {
|
16 | var obj = {};
|
17 |
|
18 | Object.keys(node).forEach(function (key) {
|
19 | if (
|
20 | key === 'parent' ||
|
21 | key === 'program' ||
|
22 | key === 'keys' ||
|
23 | key === '__wrapped'
|
24 | )
|
25 | { return; }
|
26 |
|
27 | if (Array.isArray(node[key])) {
|
28 | obj[key] = node[key].map(toJSON);
|
29 | } else if (node[key] && node[key].toJSON) {
|
30 | obj[key] = node[key].toJSON();
|
31 | } else {
|
32 | obj[key] = node[key];
|
33 | }
|
34 | });
|
35 |
|
36 | return obj;
|
37 | }
|
38 |
|
39 | var Node = function Node () {};
|
40 |
|
41 | Node.prototype.ancestor = function ancestor (level) {
|
42 | var node = this;
|
43 | while (level--) {
|
44 | node = node.parent;
|
45 | if (!node) { return null; }
|
46 | }
|
47 |
|
48 | return node;
|
49 | };
|
50 |
|
51 | Node.prototype.contains = function contains (node) {
|
52 | while (node) {
|
53 | if (node === this) { return true; }
|
54 | node = node.parent;
|
55 | }
|
56 |
|
57 | return false;
|
58 | };
|
59 |
|
60 | Node.prototype.findLexicalBoundary = function findLexicalBoundary () {
|
61 | return this.parent.findLexicalBoundary();
|
62 | };
|
63 |
|
64 | Node.prototype.findNearest = function findNearest (type) {
|
65 | if (typeof type === 'string') { type = new RegExp(("^" + type + "$")); }
|
66 | if (type.test(this.type)) { return this; }
|
67 | return this.parent.findNearest(type);
|
68 | };
|
69 |
|
70 | Node.prototype.unparenthesizedParent = function unparenthesizedParent () {
|
71 | var node = this.parent;
|
72 | while (node && node.type === 'ParenthesizedExpression') {
|
73 | node = node.parent;
|
74 | }
|
75 | return node;
|
76 | };
|
77 |
|
78 | Node.prototype.unparenthesize = function unparenthesize () {
|
79 | var node = this;
|
80 | while (node.type === 'ParenthesizedExpression') {
|
81 | node = node.expression;
|
82 | }
|
83 | return node;
|
84 | };
|
85 |
|
86 | Node.prototype.findScope = function findScope (functionScope) {
|
87 | return this.parent.findScope(functionScope);
|
88 | };
|
89 |
|
90 | Node.prototype.getIndentation = function getIndentation () {
|
91 | return this.parent.getIndentation();
|
92 | };
|
93 |
|
94 | Node.prototype.initialise = function initialise (transforms) {
|
95 | for (var i = 0, list = this.keys; i < list.length; i += 1) {
|
96 | var key = list[i];
|
97 |
|
98 | var value = this[key];
|
99 |
|
100 | if (Array.isArray(value)) {
|
101 | value.forEach(function (node) { return node && node.initialise(transforms); });
|
102 | } else if (value && typeof value === 'object') {
|
103 | value.initialise(transforms);
|
104 | }
|
105 | }
|
106 | };
|
107 |
|
108 | Node.prototype.toJSON = function toJSON$1 () {
|
109 | return toJSON(this);
|
110 | };
|
111 |
|
112 | Node.prototype.toString = function toString () {
|
113 | return this.program.magicString.original.slice(this.start, this.end);
|
114 | };
|
115 |
|
116 | Node.prototype.transpile = function transpile (code, transforms) {
|
117 | for (var i = 0, list = this.keys; i < list.length; i += 1) {
|
118 | var key = list[i];
|
119 |
|
120 | var value = this[key];
|
121 |
|
122 | if (Array.isArray(value)) {
|
123 | value.forEach(function (node) { return node && node.transpile(code, transforms); });
|
124 | } else if (value && typeof value === 'object') {
|
125 | value.transpile(code, transforms);
|
126 | }
|
127 | }
|
128 | };
|
129 |
|
130 | function extractNames(node) {
|
131 | var names = [];
|
132 | extractors[node.type](names, node);
|
133 | return names;
|
134 | }
|
135 |
|
136 | var extractors = {
|
137 | Identifier: function Identifier(names, node) {
|
138 | names.push(node);
|
139 | },
|
140 |
|
141 | ObjectPattern: function ObjectPattern(names, node) {
|
142 | for (var i = 0, list = node.properties; i < list.length; i += 1) {
|
143 | var prop = list[i];
|
144 |
|
145 | extractors[prop.type](names, prop);
|
146 | }
|
147 | },
|
148 |
|
149 | Property: function Property(names, node) {
|
150 | extractors[node.value.type](names, node.value);
|
151 | },
|
152 |
|
153 | ArrayPattern: function ArrayPattern(names, node) {
|
154 | for (var i = 0, list = node.elements; i < list.length; i += 1) {
|
155 | var element = list[i];
|
156 |
|
157 | if (element) { extractors[element.type](names, element); }
|
158 | }
|
159 | },
|
160 |
|
161 | RestElement: function RestElement(names, node) {
|
162 | extractors[node.argument.type](names, node.argument);
|
163 | },
|
164 |
|
165 | AssignmentPattern: function AssignmentPattern(names, node) {
|
166 | extractors[node.left.type](names, node.left);
|
167 | }
|
168 | };
|
169 |
|
170 | var reserved = Object.create(null);
|
171 | 'do if in for let new try var case else enum eval null this true void with await break catch class const false super throw while yield delete export import public return static switch typeof default extends finally package private continue debugger function arguments interface protected implements instanceof'
|
172 | .split(' ')
|
173 | .forEach(function (word) { return (reserved[word] = true); });
|
174 |
|
175 | function Scope(options) {
|
176 | options = options || {};
|
177 |
|
178 | this.parent = options.parent;
|
179 | this.isBlockScope = !!options.block;
|
180 | this.createDeclarationCallback = options.declare;
|
181 |
|
182 | var scope = this;
|
183 | while (scope.isBlockScope) { scope = scope.parent; }
|
184 | this.functionScope = scope;
|
185 |
|
186 | this.identifiers = [];
|
187 | this.declarations = Object.create(null);
|
188 | this.references = Object.create(null);
|
189 | this.blockScopedDeclarations = this.isBlockScope ? null : Object.create(null);
|
190 | this.aliases = Object.create(null);
|
191 | }
|
192 |
|
193 | Scope.prototype = {
|
194 | addDeclaration: function addDeclaration(node, kind) {
|
195 | for (var i = 0, list = extractNames(node); i < list.length; i += 1) {
|
196 | var identifier = list[i];
|
197 |
|
198 | var name = identifier.name;
|
199 |
|
200 | var declaration = { name: name, node: identifier, kind: kind, instances: [] };
|
201 | this.declarations[name] = declaration;
|
202 |
|
203 | if (this.isBlockScope) {
|
204 | if (!this.functionScope.blockScopedDeclarations[name])
|
205 | { this.functionScope.blockScopedDeclarations[name] = []; }
|
206 | this.functionScope.blockScopedDeclarations[name].push(declaration);
|
207 | }
|
208 | }
|
209 | },
|
210 |
|
211 | addReference: function addReference(identifier) {
|
212 | if (this.consolidated) {
|
213 | this.consolidateReference(identifier);
|
214 | } else {
|
215 | this.identifiers.push(identifier);
|
216 | }
|
217 | },
|
218 |
|
219 | consolidate: function consolidate() {
|
220 | for (var i = 0; i < this.identifiers.length; i += 1) {
|
221 |
|
222 | var identifier = this.identifiers[i];
|
223 | this.consolidateReference(identifier);
|
224 | }
|
225 |
|
226 | this.consolidated = true;
|
227 | },
|
228 |
|
229 | consolidateReference: function consolidateReference(identifier) {
|
230 | var declaration = this.declarations[identifier.name];
|
231 | if (declaration) {
|
232 | declaration.instances.push(identifier);
|
233 | } else {
|
234 | this.references[identifier.name] = true;
|
235 | if (this.parent) { this.parent.addReference(identifier); }
|
236 | }
|
237 | },
|
238 |
|
239 | contains: function contains(name) {
|
240 | return (
|
241 | this.declarations[name] ||
|
242 | (this.parent ? this.parent.contains(name) : false)
|
243 | );
|
244 | },
|
245 |
|
246 | createIdentifier: function createIdentifier(base) {
|
247 | if (typeof base === 'number') { base = base.toString(); }
|
248 |
|
249 | base = base
|
250 | .replace(/\s/g, '')
|
251 | .replace(/\[([^\]]+)\]/g, '_$1')
|
252 | .replace(/[^a-zA-Z0-9_$]/g, '_')
|
253 | .replace(/_{2,}/, '_');
|
254 |
|
255 | var name = base;
|
256 | var counter = 1;
|
257 |
|
258 | while (
|
259 | this.declarations[name] ||
|
260 | this.references[name] ||
|
261 | this.aliases[name] ||
|
262 | name in reserved
|
263 | ) {
|
264 | name = base + "$" + (counter++);
|
265 | }
|
266 |
|
267 | this.aliases[name] = true;
|
268 | return name;
|
269 | },
|
270 |
|
271 | createDeclaration: function createDeclaration(base) {
|
272 | var id = this.createIdentifier(base);
|
273 | this.createDeclarationCallback(id);
|
274 | return id;
|
275 | },
|
276 |
|
277 | findDeclaration: function findDeclaration(name) {
|
278 | return (
|
279 | this.declarations[name] ||
|
280 | (this.parent && this.parent.findDeclaration(name))
|
281 | );
|
282 | },
|
283 |
|
284 |
|
285 | resolveName: function resolveName(name) {
|
286 | var declaration = this.findDeclaration(name);
|
287 | return declaration ? declaration.name : name;
|
288 | }
|
289 | };
|
290 |
|
291 | function locate(source, index) {
|
292 | var lines = source.split('\n');
|
293 | var len = lines.length;
|
294 |
|
295 | var lineStart = 0;
|
296 | var i;
|
297 |
|
298 | for (i = 0; i < len; i += 1) {
|
299 | var line = lines[i];
|
300 | var lineEnd = lineStart + line.length + 1;
|
301 |
|
302 | if (lineEnd > index) {
|
303 | return { line: i + 1, column: index - lineStart, char: i };
|
304 | }
|
305 |
|
306 | lineStart = lineEnd;
|
307 | }
|
308 |
|
309 | throw new Error('Could not determine location of character');
|
310 | }
|
311 |
|
312 | function pad(num, len) {
|
313 | var result = String(num);
|
314 | return result + repeat(' ', len - result.length);
|
315 | }
|
316 |
|
317 | function repeat(str, times) {
|
318 | var result = '';
|
319 | while (times--) { result += str; }
|
320 | return result;
|
321 | }
|
322 |
|
323 | function getSnippet(source, loc, length) {
|
324 | if ( length === void 0 ) length = 1;
|
325 |
|
326 | var first = Math.max(loc.line - 5, 0);
|
327 | var last = loc.line;
|
328 |
|
329 | var numDigits = String(last).length;
|
330 |
|
331 | var lines = source.split('\n').slice(first, last);
|
332 |
|
333 | var lastLine = lines[lines.length - 1];
|
334 | var offset = lastLine.slice(0, loc.column).replace(/\t/g, ' ').length;
|
335 |
|
336 | var snippet = lines
|
337 | .map(function (line, i) { return ((pad(i + first + 1, numDigits)) + " : " + (line.replace(/\t/g, ' '))); })
|
338 | .join('\n');
|
339 |
|
340 | snippet += '\n' + repeat(' ', numDigits + 3 + offset) + repeat('^', length);
|
341 |
|
342 | return snippet;
|
343 | }
|
344 |
|
345 | var CompileError = (function (Error) {
|
346 | function CompileError(message, node) {
|
347 | Error.call(this, message);
|
348 |
|
349 | this.name = 'CompileError';
|
350 | if (!node) {
|
351 | return;
|
352 | }
|
353 |
|
354 | var source = node.program.magicString.original;
|
355 | var loc = locate(source, node.start);
|
356 |
|
357 | this.message = message + " (" + (loc.line) + ":" + (loc.column) + ")";
|
358 |
|
359 | this.stack = new Error().stack.replace(
|
360 | new RegExp((".+new " + (this.name) + ".+\\n"), 'm'),
|
361 | ''
|
362 | );
|
363 |
|
364 | this.loc = loc;
|
365 | this.snippet = getSnippet(source, loc, node.end - node.start);
|
366 | }
|
367 |
|
368 | if ( Error ) CompileError.__proto__ = Error;
|
369 | CompileError.prototype = Object.create( Error && Error.prototype );
|
370 | CompileError.prototype.constructor = CompileError;
|
371 |
|
372 | CompileError.prototype.toString = function toString () {
|
373 | return ((this.name) + ": " + (this.message) + "\n" + (this.snippet));
|
374 | };
|
375 |
|
376 | CompileError.missingTransform = function missingTransform (feature, transformKey, node, dangerousKey) {
|
377 | if ( dangerousKey === void 0 ) dangerousKey = null;
|
378 |
|
379 | var maybeDangerous = dangerousKey ? (", or `transforms: { " + dangerousKey + ": true }` if you know what you're doing") : '';
|
380 | throw new CompileError(("Transforming " + feature + " is not " + (dangerousKey ? "fully supported" : "implemented") + ". Use `transforms: { " + transformKey + ": false }` to skip transformation and disable this error" + maybeDangerous + "."), node);
|
381 | };
|
382 |
|
383 | return CompileError;
|
384 | }(Error));
|
385 |
|
386 | function findIndex(array, fn) {
|
387 | for (var i = 0; i < array.length; i += 1) {
|
388 | if (fn(array[i], i)) { return i; }
|
389 | }
|
390 |
|
391 | return -1;
|
392 | }
|
393 |
|
394 | var handlers = {
|
395 | Identifier: destructureIdentifier,
|
396 | AssignmentPattern: destructureAssignmentPattern,
|
397 | ArrayPattern: destructureArrayPattern,
|
398 | ObjectPattern: destructureObjectPattern
|
399 | };
|
400 |
|
401 | function destructure(
|
402 | code,
|
403 | createIdentifier,
|
404 | resolveName,
|
405 | node,
|
406 | ref,
|
407 | inline,
|
408 | statementGenerators
|
409 | ) {
|
410 | handlers[node.type](code, createIdentifier, resolveName, node, ref, inline, statementGenerators);
|
411 | }
|
412 |
|
413 | function destructureIdentifier(
|
414 | code,
|
415 | createIdentifier,
|
416 | resolveName,
|
417 | node,
|
418 | ref,
|
419 | inline,
|
420 | statementGenerators
|
421 | ) {
|
422 | statementGenerators.push(function (start, prefix, suffix) {
|
423 | code.overwrite(node.start, node.end, (inline ? prefix : (prefix + "var ")) + resolveName(node) + " = " + ref + suffix);
|
424 | code.move(node.start, node.end, start);
|
425 | });
|
426 | }
|
427 |
|
428 | function destructureMemberExpression(
|
429 | code,
|
430 | createIdentifier,
|
431 | resolveName,
|
432 | node,
|
433 | ref,
|
434 | inline,
|
435 | statementGenerators
|
436 | ) {
|
437 | statementGenerators.push(function (start, prefix, suffix) {
|
438 | code.prependRight(node.start, inline ? prefix : (prefix + "var "));
|
439 | code.appendLeft(node.end, (" = " + ref + suffix));
|
440 | code.move(node.start, node.end, start);
|
441 | });
|
442 | }
|
443 |
|
444 | function destructureAssignmentPattern(
|
445 | code,
|
446 | createIdentifier,
|
447 | resolveName,
|
448 | node,
|
449 | ref,
|
450 | inline,
|
451 | statementGenerators
|
452 | ) {
|
453 | var isIdentifier = node.left.type === 'Identifier';
|
454 | var name = isIdentifier ? node.left.name : ref;
|
455 |
|
456 | if (!inline) {
|
457 | statementGenerators.push(function (start, prefix, suffix) {
|
458 | code.prependRight(
|
459 | node.left.end,
|
460 | (prefix + "if ( " + name + " === void 0 ) " + name)
|
461 | );
|
462 | code.move(node.left.end, node.right.end, start);
|
463 | code.appendLeft(node.right.end, suffix);
|
464 | });
|
465 | }
|
466 |
|
467 | if (!isIdentifier) {
|
468 | destructure(code, createIdentifier, resolveName, node.left, ref, inline, statementGenerators);
|
469 | }
|
470 | }
|
471 |
|
472 | function destructureArrayPattern(
|
473 | code,
|
474 | createIdentifier,
|
475 | resolveName,
|
476 | node,
|
477 | ref,
|
478 | inline,
|
479 | statementGenerators
|
480 | ) {
|
481 | var c = node.start;
|
482 |
|
483 | node.elements.forEach(function (element, i) {
|
484 | if (!element) { return; }
|
485 |
|
486 | if (element.type === 'RestElement') {
|
487 | handleProperty(
|
488 | code,
|
489 | createIdentifier,
|
490 | resolveName,
|
491 | c,
|
492 | element.argument,
|
493 | (ref + ".slice(" + i + ")"),
|
494 | inline,
|
495 | statementGenerators
|
496 | );
|
497 | } else {
|
498 | handleProperty(
|
499 | code,
|
500 | createIdentifier,
|
501 | resolveName,
|
502 | c,
|
503 | element,
|
504 | (ref + "[" + i + "]"),
|
505 | inline,
|
506 | statementGenerators
|
507 | );
|
508 | }
|
509 | c = element.end;
|
510 | });
|
511 |
|
512 | code.remove(c, node.end);
|
513 | }
|
514 |
|
515 | function destructureObjectPattern(
|
516 | code,
|
517 | createIdentifier,
|
518 | resolveName,
|
519 | node,
|
520 | ref,
|
521 | inline,
|
522 | statementGenerators
|
523 | ) {
|
524 | var this$1 = this;
|
525 |
|
526 | var c = node.start;
|
527 |
|
528 | var nonRestKeys = [];
|
529 | node.properties.forEach(function (prop) {
|
530 | var value;
|
531 | var content;
|
532 | if (prop.type === 'Property') {
|
533 | content = prop.value;
|
534 | if (!prop.computed && prop.key.type === 'Identifier') {
|
535 | value = ref + "." + (prop.key.name);
|
536 | nonRestKeys.push(("\"" + (prop.key.name) + "\""));
|
537 | } else if (!prop.computed && prop.key.type === 'Literal') {
|
538 | value = ref + "[" + (prop.key.raw) + "]";
|
539 | nonRestKeys.push(JSON.stringify(String(prop.key.value)));
|
540 | } else {
|
541 | var expr = code.slice(prop.key.start, prop.key.end);
|
542 | value = ref + "[" + expr + "]";
|
543 | nonRestKeys.push(("String(" + expr + ")"));
|
544 | }
|
545 | } else if (prop.type === 'RestElement') {
|
546 | content = prop.argument;
|
547 | value = createIdentifier('rest');
|
548 | statementGenerators.push(function (start, prefix, suffix) {
|
549 | var helper = prop.program.getObjectWithoutPropertiesHelper(code);
|
550 | code.overwrite(
|
551 | prop.start,
|
552 | (c = prop.argument.start),
|
553 | (inline ? prefix : (prefix + "var ")) + value + " = " + helper + "( " + ref + ", [" + (nonRestKeys.join(', ')) + "] )" + suffix
|
554 | );
|
555 | code.move(prop.start, c, start);
|
556 | });
|
557 | } else {
|
558 | throw new CompileError(
|
559 | this$1,
|
560 | ("Unexpected node of type " + (prop.type) + " in object pattern")
|
561 | );
|
562 | }
|
563 | handleProperty(code, createIdentifier, resolveName, c, content, value, inline, statementGenerators);
|
564 | c = prop.end;
|
565 | });
|
566 |
|
567 | code.remove(c, node.end);
|
568 | }
|
569 |
|
570 | function handleProperty(
|
571 | code,
|
572 | createIdentifier,
|
573 | resolveName,
|
574 | c,
|
575 | node,
|
576 | value,
|
577 | inline,
|
578 | statementGenerators
|
579 | ) {
|
580 | switch (node.type) {
|
581 | case 'Identifier': {
|
582 | code.remove(c, node.start);
|
583 | destructureIdentifier(
|
584 | code,
|
585 | createIdentifier,
|
586 | resolveName,
|
587 | node,
|
588 | value,
|
589 | inline,
|
590 | statementGenerators
|
591 | );
|
592 | break;
|
593 | }
|
594 |
|
595 | case 'MemberExpression':
|
596 | code.remove(c, node.start);
|
597 | destructureMemberExpression(
|
598 | code,
|
599 | createIdentifier,
|
600 | resolveName,
|
601 | node,
|
602 | value,
|
603 | true,
|
604 | statementGenerators
|
605 | );
|
606 | break;
|
607 |
|
608 | case 'AssignmentPattern': {
|
609 | var name;
|
610 |
|
611 | var isIdentifier = node.left.type === 'Identifier';
|
612 |
|
613 | if (isIdentifier) {
|
614 | name = resolveName(node.left);
|
615 | } else {
|
616 | name = createIdentifier(value);
|
617 | }
|
618 |
|
619 | statementGenerators.push(function (start, prefix, suffix) {
|
620 | if (inline) {
|
621 | code.prependRight(
|
622 | node.right.start,
|
623 | (name + " = " + value + ", " + name + " = " + name + " === void 0 ? ")
|
624 | );
|
625 | code.appendLeft(node.right.end, (" : " + name + suffix));
|
626 | } else {
|
627 | code.prependRight(
|
628 | node.right.start,
|
629 | (prefix + "var " + name + " = " + value + "; if ( " + name + " === void 0 ) " + name + " = ")
|
630 | );
|
631 | code.appendLeft(node.right.end, suffix);
|
632 | }
|
633 |
|
634 | code.move(node.right.start, node.right.end, start);
|
635 | });
|
636 |
|
637 | if (isIdentifier) {
|
638 | code.remove(c, node.right.start);
|
639 | } else {
|
640 | code.remove(c, node.left.start);
|
641 | code.remove(node.left.end, node.right.start);
|
642 | handleProperty(
|
643 | code,
|
644 | createIdentifier,
|
645 | resolveName,
|
646 | c,
|
647 | node.left,
|
648 | name,
|
649 | inline,
|
650 | statementGenerators
|
651 | );
|
652 | }
|
653 |
|
654 | break;
|
655 | }
|
656 |
|
657 | case 'ObjectPattern': {
|
658 | code.remove(c, (c = node.start));
|
659 |
|
660 | var ref = value;
|
661 | if (node.properties.length > 1) {
|
662 | ref = createIdentifier(value);
|
663 |
|
664 | statementGenerators.push(function (start, prefix, suffix) {
|
665 |
|
666 |
|
667 | code.prependRight(node.start, (inline ? '' : (prefix + "var ")) + ref + " = ");
|
668 | code.overwrite(node.start, (c = node.start + 1), value);
|
669 | code.appendLeft(c, suffix);
|
670 |
|
671 | code.overwrite(
|
672 | node.start,
|
673 | (c = node.start + 1),
|
674 | (inline ? '' : (prefix + "var ")) + ref + " = " + value + suffix
|
675 | );
|
676 | code.move(node.start, c, start);
|
677 | });
|
678 | }
|
679 |
|
680 | destructureObjectPattern(
|
681 | code,
|
682 | createIdentifier,
|
683 | resolveName,
|
684 | node,
|
685 | ref,
|
686 | inline,
|
687 | statementGenerators
|
688 | );
|
689 |
|
690 | break;
|
691 | }
|
692 |
|
693 | case 'ArrayPattern': {
|
694 | code.remove(c, (c = node.start));
|
695 |
|
696 | if (node.elements.filter(Boolean).length > 1) {
|
697 | var ref$1 = createIdentifier(value);
|
698 |
|
699 | statementGenerators.push(function (start, prefix, suffix) {
|
700 | code.prependRight(node.start, (inline ? '' : (prefix + "var ")) + ref$1 + " = ");
|
701 | code.overwrite(node.start, (c = node.start + 1), value, {
|
702 | contentOnly: true
|
703 | });
|
704 | code.appendLeft(c, suffix);
|
705 |
|
706 | code.move(node.start, c, start);
|
707 | });
|
708 |
|
709 | node.elements.forEach(function (element, i) {
|
710 | if (!element) { return; }
|
711 |
|
712 | if (element.type === 'RestElement') {
|
713 | handleProperty(
|
714 | code,
|
715 | createIdentifier,
|
716 | resolveName,
|
717 | c,
|
718 | element.argument,
|
719 | (ref$1 + ".slice(" + i + ")"),
|
720 | inline,
|
721 | statementGenerators
|
722 | );
|
723 | } else {
|
724 | handleProperty(
|
725 | code,
|
726 | createIdentifier,
|
727 | resolveName,
|
728 | c,
|
729 | element,
|
730 | (ref$1 + "[" + i + "]"),
|
731 | inline,
|
732 | statementGenerators
|
733 | );
|
734 | }
|
735 | c = element.end;
|
736 | });
|
737 | } else {
|
738 | var index = findIndex(node.elements, Boolean);
|
739 | var element = node.elements[index];
|
740 | if (element.type === 'RestElement') {
|
741 | handleProperty(
|
742 | code,
|
743 | createIdentifier,
|
744 | resolveName,
|
745 | c,
|
746 | element.argument,
|
747 | (value + ".slice(" + index + ")"),
|
748 | inline,
|
749 | statementGenerators
|
750 | );
|
751 | } else {
|
752 | handleProperty(
|
753 | code,
|
754 | createIdentifier,
|
755 | resolveName,
|
756 | c,
|
757 | element,
|
758 | (value + "[" + index + "]"),
|
759 | inline,
|
760 | statementGenerators
|
761 | );
|
762 | }
|
763 | c = element.end;
|
764 | }
|
765 |
|
766 | code.remove(c, node.end);
|
767 | break;
|
768 | }
|
769 |
|
770 | default: {
|
771 | throw new Error(("Unexpected node type in destructuring (" + (node.type) + ")"));
|
772 | }
|
773 | }
|
774 | }
|
775 |
|
776 | function isUseStrict(node) {
|
777 | if (!node) { return false; }
|
778 | if (node.type !== 'ExpressionStatement') { return false; }
|
779 | if (node.expression.type !== 'Literal') { return false; }
|
780 | return node.expression.value === 'use strict';
|
781 | }
|
782 |
|
783 | var BlockStatement = (function (Node) {
|
784 | function BlockStatement () {
|
785 | Node.apply(this, arguments);
|
786 | }
|
787 |
|
788 | if ( Node ) BlockStatement.__proto__ = Node;
|
789 | BlockStatement.prototype = Object.create( Node && Node.prototype );
|
790 | BlockStatement.prototype.constructor = BlockStatement;
|
791 |
|
792 | BlockStatement.prototype.createScope = function createScope () {
|
793 | var this$1 = this;
|
794 |
|
795 | this.parentIsFunction = /Function/.test(this.parent.type);
|
796 | this.isFunctionBlock = this.parentIsFunction || this.parent.type === 'Root';
|
797 | this.scope = new Scope({
|
798 | block: !this.isFunctionBlock,
|
799 | parent: this.parent.findScope(false),
|
800 | declare: function (id) { return this$1.createdDeclarations.push(id); }
|
801 | });
|
802 |
|
803 | if (this.parentIsFunction) {
|
804 | this.parent.params.forEach(function (node) {
|
805 | this$1.scope.addDeclaration(node, 'param');
|
806 | });
|
807 | }
|
808 | };
|
809 |
|
810 | BlockStatement.prototype.initialise = function initialise (transforms) {
|
811 | this.thisAlias = null;
|
812 | this.argumentsAlias = null;
|
813 | this.defaultParameters = [];
|
814 | this.createdDeclarations = [];
|
815 |
|
816 |
|
817 |
|
818 |
|
819 |
|
820 | if (!this.scope) { this.createScope(); }
|
821 |
|
822 | this.body.forEach(function (node) { return node.initialise(transforms); });
|
823 |
|
824 | this.scope.consolidate();
|
825 | };
|
826 |
|
827 | BlockStatement.prototype.findLexicalBoundary = function findLexicalBoundary () {
|
828 | if (this.type === 'Program') { return this; }
|
829 | if (/^Function/.test(this.parent.type)) { return this; }
|
830 |
|
831 | return this.parent.findLexicalBoundary();
|
832 | };
|
833 |
|
834 | BlockStatement.prototype.findScope = function findScope (functionScope) {
|
835 | if (functionScope && !this.isFunctionBlock)
|
836 | { return this.parent.findScope(functionScope); }
|
837 | return this.scope;
|
838 | };
|
839 |
|
840 | BlockStatement.prototype.getArgumentsAlias = function getArgumentsAlias () {
|
841 | if (!this.argumentsAlias) {
|
842 | this.argumentsAlias = this.scope.createIdentifier('arguments');
|
843 | }
|
844 |
|
845 | return this.argumentsAlias;
|
846 | };
|
847 |
|
848 | BlockStatement.prototype.getArgumentsArrayAlias = function getArgumentsArrayAlias () {
|
849 | if (!this.argumentsArrayAlias) {
|
850 | this.argumentsArrayAlias = this.scope.createIdentifier('argsArray');
|
851 | }
|
852 |
|
853 | return this.argumentsArrayAlias;
|
854 | };
|
855 |
|
856 | BlockStatement.prototype.getThisAlias = function getThisAlias () {
|
857 | if (!this.thisAlias) {
|
858 | this.thisAlias = this.scope.createIdentifier('this');
|
859 | }
|
860 |
|
861 | return this.thisAlias;
|
862 | };
|
863 |
|
864 | BlockStatement.prototype.getIndentation = function getIndentation () {
|
865 | if (this.indentation === undefined) {
|
866 | var source = this.program.magicString.original;
|
867 |
|
868 | var useOuter = this.synthetic || !this.body.length;
|
869 | var c = useOuter ? this.start : this.body[0].start;
|
870 |
|
871 | while (c && source[c] !== '\n') { c -= 1; }
|
872 |
|
873 | this.indentation = '';
|
874 |
|
875 |
|
876 | while (true) {
|
877 | c += 1;
|
878 | var char = source[c];
|
879 |
|
880 | if (char !== ' ' && char !== '\t') { break; }
|
881 |
|
882 | this.indentation += char;
|
883 | }
|
884 |
|
885 | var indentString = this.program.magicString.getIndentString();
|
886 |
|
887 |
|
888 | var parent = this.parent;
|
889 | while (parent) {
|
890 | if (parent.kind === 'constructor' && !parent.parent.parent.superClass) {
|
891 | this.indentation = this.indentation.replace(indentString, '');
|
892 | }
|
893 |
|
894 | parent = parent.parent;
|
895 | }
|
896 |
|
897 | if (useOuter) { this.indentation += indentString; }
|
898 | }
|
899 |
|
900 | return this.indentation;
|
901 | };
|
902 |
|
903 | BlockStatement.prototype.transpile = function transpile (code, transforms) {
|
904 | var this$1 = this;
|
905 |
|
906 | var indentation = this.getIndentation();
|
907 |
|
908 | var introStatementGenerators = [];
|
909 |
|
910 | if (this.argumentsAlias) {
|
911 | introStatementGenerators.push(function (start, prefix, suffix) {
|
912 | var assignment = prefix + "var " + (this$1.argumentsAlias) + " = arguments" + suffix;
|
913 | code.appendLeft(start, assignment);
|
914 | });
|
915 | }
|
916 |
|
917 | if (this.thisAlias) {
|
918 | introStatementGenerators.push(function (start, prefix, suffix) {
|
919 | var assignment = prefix + "var " + (this$1.thisAlias) + " = this" + suffix;
|
920 | code.appendLeft(start, assignment);
|
921 | });
|
922 | }
|
923 |
|
924 | if (this.argumentsArrayAlias) {
|
925 | introStatementGenerators.push(function (start, prefix, suffix) {
|
926 | var i = this$1.scope.createIdentifier('i');
|
927 | var assignment = prefix + "var " + i + " = arguments.length, " + (this$1.argumentsArrayAlias) + " = Array(" + i + ");\n" + indentation + "while ( " + i + "-- ) " + (this$1.argumentsArrayAlias) + "[" + i + "] = arguments[" + i + "]" + suffix;
|
928 | code.appendLeft(start, assignment);
|
929 | });
|
930 | }
|
931 |
|
932 | if (/Function/.test(this.parent.type)) {
|
933 | this.transpileParameters(
|
934 | this.parent.params,
|
935 | code,
|
936 | transforms,
|
937 | indentation,
|
938 | introStatementGenerators
|
939 | );
|
940 | } else if ('CatchClause' === this.parent.type) {
|
941 | this.transpileParameters(
|
942 | [this.parent.param],
|
943 | code,
|
944 | transforms,
|
945 | indentation,
|
946 | introStatementGenerators
|
947 | );
|
948 | }
|
949 |
|
950 | if (transforms.letConst && this.isFunctionBlock) {
|
951 | this.transpileBlockScopedIdentifiers(code);
|
952 | }
|
953 |
|
954 | Node.prototype.transpile.call(this, code, transforms);
|
955 |
|
956 | if (this.createdDeclarations.length) {
|
957 | introStatementGenerators.push(function (start, prefix, suffix) {
|
958 | var assignment = prefix + "var " + (this$1.createdDeclarations.join(', ')) + suffix;
|
959 | code.appendLeft(start, assignment);
|
960 | });
|
961 | }
|
962 |
|
963 | if (this.synthetic) {
|
964 | if (this.parent.type === 'ArrowFunctionExpression') {
|
965 | var expr = this.body[0];
|
966 |
|
967 | if (introStatementGenerators.length) {
|
968 | code
|
969 | .appendLeft(this.start, "{")
|
970 | .prependRight(this.end, ((this.parent.getIndentation()) + "}"));
|
971 |
|
972 | code.prependRight(expr.start, ("\n" + indentation + "return "));
|
973 | code.appendLeft(expr.end, ";\n");
|
974 | } else if (transforms.arrow) {
|
975 | code.prependRight(expr.start, "{ return ");
|
976 | code.appendLeft(expr.end, "; }");
|
977 | }
|
978 | } else if (introStatementGenerators.length) {
|
979 | code.prependRight(this.start, "{").appendLeft(this.end, "}");
|
980 | }
|
981 | }
|
982 |
|
983 | var start;
|
984 | if (isUseStrict(this.body[0])) {
|
985 | start = this.body[0].end;
|
986 | } else if (this.synthetic || this.parent.type === 'Root') {
|
987 | start = this.start;
|
988 | } else {
|
989 | start = this.start + 1;
|
990 | }
|
991 |
|
992 | var prefix = "\n" + indentation;
|
993 | var suffix = ';';
|
994 | introStatementGenerators.forEach(function (fn, i) {
|
995 | if (i === introStatementGenerators.length - 1) { suffix = ";\n"; }
|
996 | fn(start, prefix, suffix);
|
997 | });
|
998 | };
|
999 |
|
1000 | BlockStatement.prototype.transpileParameters = function transpileParameters (params, code, transforms, indentation, introStatementGenerators) {
|
1001 | var this$1 = this;
|
1002 |
|
1003 | params.forEach(function (param) {
|
1004 | if (
|
1005 | param.type === 'AssignmentPattern' &&
|
1006 | param.left.type === 'Identifier'
|
1007 | ) {
|
1008 | if (transforms.defaultParameter) {
|
1009 | introStatementGenerators.push(function (start, prefix, suffix) {
|
1010 | var lhs = prefix + "if ( " + (param.left.name) + " === void 0 ) " + (param.left.name);
|
1011 |
|
1012 | code
|
1013 | .prependRight(param.left.end, lhs)
|
1014 | .move(param.left.end, param.right.end, start)
|
1015 | .appendLeft(param.right.end, suffix);
|
1016 | });
|
1017 | }
|
1018 | } else if (param.type === 'RestElement') {
|
1019 | if (transforms.spreadRest) {
|
1020 | introStatementGenerators.push(function (start, prefix, suffix) {
|
1021 | var penultimateParam = params[params.length - 2];
|
1022 |
|
1023 | if (penultimateParam) {
|
1024 | code.remove(
|
1025 | penultimateParam ? penultimateParam.end : param.start,
|
1026 | param.end
|
1027 | );
|
1028 | } else {
|
1029 | var start$1 = param.start,
|
1030 | end = param.end;
|
1031 |
|
1032 | while (/\s/.test(code.original[start$1 - 1])) { start$1 -= 1; }
|
1033 | while (/\s/.test(code.original[end])) { end += 1; }
|
1034 |
|
1035 | code.remove(start$1, end);
|
1036 | }
|
1037 |
|
1038 | var name = param.argument.name;
|
1039 | var len = this$1.scope.createIdentifier('len');
|
1040 | var count = params.length - 1;
|
1041 |
|
1042 | if (count) {
|
1043 | code.prependRight(
|
1044 | start,
|
1045 | (prefix + "var " + name + " = [], " + len + " = arguments.length - " + count + ";\n" + indentation + "while ( " + len + "-- > 0 ) " + name + "[ " + len + " ] = arguments[ " + len + " + " + count + " ]" + suffix)
|
1046 | );
|
1047 | } else {
|
1048 | code.prependRight(
|
1049 | start,
|
1050 | (prefix + "var " + name + " = [], " + len + " = arguments.length;\n" + indentation + "while ( " + len + "-- ) " + name + "[ " + len + " ] = arguments[ " + len + " ]" + suffix)
|
1051 | );
|
1052 | }
|
1053 | });
|
1054 | }
|
1055 | } else if (param.type !== 'Identifier') {
|
1056 | if (transforms.parameterDestructuring) {
|
1057 | var ref = this$1.scope.createIdentifier('ref');
|
1058 | destructure(
|
1059 | code,
|
1060 | function (id) { return this$1.scope.createIdentifier(id); },
|
1061 | function (ref) {
|
1062 | var name = ref.name;
|
1063 |
|
1064 | return this$1.scope.resolveName(name);
|
1065 | },
|
1066 | param,
|
1067 | ref,
|
1068 | false,
|
1069 | introStatementGenerators
|
1070 | );
|
1071 | code.prependRight(param.start, ref);
|
1072 | }
|
1073 | }
|
1074 | });
|
1075 | };
|
1076 |
|
1077 | BlockStatement.prototype.transpileBlockScopedIdentifiers = function transpileBlockScopedIdentifiers (code) {
|
1078 | var this$1 = this;
|
1079 |
|
1080 | Object.keys(this.scope.blockScopedDeclarations).forEach(function (name) {
|
1081 | var declarations = this$1.scope.blockScopedDeclarations[name];
|
1082 |
|
1083 | for (var i$2 = 0, list$2 = declarations; i$2 < list$2.length; i$2 += 1) {
|
1084 | var declaration = list$2[i$2];
|
1085 |
|
1086 | var cont = false;
|
1087 |
|
1088 | if (declaration.kind === 'for.let') {
|
1089 |
|
1090 | var forStatement = declaration.node.findNearest('ForStatement');
|
1091 |
|
1092 | if (forStatement.shouldRewriteAsFunction) {
|
1093 | var outerAlias = this$1.scope.createIdentifier(name);
|
1094 | var innerAlias = forStatement.reassigned[name]
|
1095 | ? this$1.scope.createIdentifier(name)
|
1096 | : name;
|
1097 |
|
1098 | declaration.name = outerAlias;
|
1099 | code.overwrite(
|
1100 | declaration.node.start,
|
1101 | declaration.node.end,
|
1102 | outerAlias,
|
1103 | { storeName: true }
|
1104 | );
|
1105 |
|
1106 | forStatement.aliases[name] = {
|
1107 | outer: outerAlias,
|
1108 | inner: innerAlias
|
1109 | };
|
1110 |
|
1111 | for (var i = 0, list = declaration.instances; i < list.length; i += 1) {
|
1112 | var identifier = list[i];
|
1113 |
|
1114 | var alias = forStatement.body.contains(identifier)
|
1115 | ? innerAlias
|
1116 | : outerAlias;
|
1117 |
|
1118 | if (name !== alias) {
|
1119 | code.overwrite(identifier.start, identifier.end, alias, {
|
1120 | storeName: true
|
1121 | });
|
1122 | }
|
1123 | }
|
1124 |
|
1125 | cont = true;
|
1126 | }
|
1127 | }
|
1128 |
|
1129 | if (!cont) {
|
1130 | var alias$1 = this$1.scope.createIdentifier(name);
|
1131 |
|
1132 | if (name !== alias$1) {
|
1133 | var declarationParent = declaration.node.parent;
|
1134 | declaration.name = alias$1;
|
1135 | code.overwrite(
|
1136 | declaration.node.start,
|
1137 | declaration.node.end,
|
1138 | alias$1,
|
1139 | { storeName: true }
|
1140 | );
|
1141 | if (declarationParent.type === 'Property' && declarationParent.shorthand) {
|
1142 | declarationParent.shorthand = false;
|
1143 | code.prependLeft(declaration.node.start, (name + ": "));
|
1144 | }
|
1145 |
|
1146 | for (var i$1 = 0, list$1 = declaration.instances; i$1 < list$1.length; i$1 += 1) {
|
1147 | var identifier$1 = list$1[i$1];
|
1148 |
|
1149 | identifier$1.rewritten = true;
|
1150 | var identifierParent = identifier$1.parent;
|
1151 | code.overwrite(identifier$1.start, identifier$1.end, alias$1, {
|
1152 | storeName: true
|
1153 | });
|
1154 | if (identifierParent.type === 'Property' && identifierParent.shorthand) {
|
1155 | identifierParent.shorthand = false;
|
1156 | code.prependLeft(identifier$1.start, (name + ": "));
|
1157 | }
|
1158 | }
|
1159 | }
|
1160 | }
|
1161 | }
|
1162 | });
|
1163 | };
|
1164 |
|
1165 | return BlockStatement;
|
1166 | }(Node));
|
1167 |
|
1168 | function isArguments(node) {
|
1169 | return node.type === 'Identifier' && node.name === 'arguments';
|
1170 | }
|
1171 |
|
1172 | function inlineSpreads(
|
1173 | code,
|
1174 | node,
|
1175 | elements
|
1176 | ) {
|
1177 | var i = elements.length;
|
1178 |
|
1179 | while (i--) {
|
1180 | var element = elements[i];
|
1181 | if (!element || element.type !== 'SpreadElement') {
|
1182 | continue;
|
1183 | }
|
1184 | var argument = element.argument;
|
1185 | if (argument.type !== 'ArrayExpression') {
|
1186 | continue;
|
1187 | }
|
1188 | var subelements = argument.elements;
|
1189 | if (subelements.some(function (subelement) { return subelement === null; })) {
|
1190 |
|
1191 |
|
1192 |
|
1193 |
|
1194 |
|
1195 | continue;
|
1196 | }
|
1197 |
|
1198 | var isLast = i === elements.length - 1;
|
1199 | if (subelements.length === 0) {
|
1200 | code.remove(
|
1201 | isLast && i !== 0
|
1202 | ? elements[i - 1].end
|
1203 | : element.start,
|
1204 | isLast
|
1205 | ? node.end - 1
|
1206 | : elements[i + 1].start);
|
1207 | } else {
|
1208 |
|
1209 |
|
1210 | code.remove(element.start, subelements[0].start);
|
1211 | code.remove(
|
1212 |
|
1213 | subelements[subelements.length - 1].end,
|
1214 |
|
1215 | isLast
|
1216 | ? node.end - 1
|
1217 | : element.end
|
1218 | );
|
1219 | }
|
1220 | elements.splice.apply(elements, [ i, 1 ].concat( subelements ));
|
1221 | i += subelements.length;
|
1222 | }
|
1223 | }
|
1224 |
|
1225 |
|
1226 |
|
1227 |
|
1228 |
|
1229 |
|
1230 |
|
1231 |
|
1232 | function needsParentheses(node) {
|
1233 | switch (node.type) {
|
1234 |
|
1235 |
|
1236 | case 'ArrayExpression':
|
1237 | case 'CallExpression':
|
1238 | case 'Identifier':
|
1239 | case 'ParenthesizedExpression':
|
1240 | case 'ThisExpression':
|
1241 | return false;
|
1242 | default:
|
1243 | return true;
|
1244 | }
|
1245 | }
|
1246 |
|
1247 | function spread(
|
1248 | code,
|
1249 | elements,
|
1250 | start,
|
1251 | argumentsArrayAlias,
|
1252 | isNew
|
1253 | ) {
|
1254 | var i = elements.length;
|
1255 | var firstSpreadIndex = -1;
|
1256 |
|
1257 | while (i--) {
|
1258 | var element$1 = elements[i];
|
1259 | if (element$1 && element$1.type === 'SpreadElement') {
|
1260 | if (isArguments(element$1.argument)) {
|
1261 | code.overwrite(
|
1262 | element$1.argument.start,
|
1263 | element$1.argument.end,
|
1264 | argumentsArrayAlias
|
1265 | );
|
1266 | }
|
1267 |
|
1268 | firstSpreadIndex = i;
|
1269 | }
|
1270 | }
|
1271 |
|
1272 | if (firstSpreadIndex === -1) { return false; }
|
1273 |
|
1274 | if (isNew) {
|
1275 | for (i = 0; i < elements.length; i += 1) {
|
1276 | var element$2 = elements[i];
|
1277 | if (element$2.type === 'SpreadElement') {
|
1278 | code.remove(element$2.start, element$2.argument.start);
|
1279 | } else {
|
1280 | code.prependRight(element$2.start, '[');
|
1281 | code.prependRight(element$2.end, ']');
|
1282 | }
|
1283 | }
|
1284 |
|
1285 | return true;
|
1286 | }
|
1287 |
|
1288 | var element = elements[firstSpreadIndex];
|
1289 | var previousElement = elements[firstSpreadIndex - 1];
|
1290 |
|
1291 | if (!previousElement) {
|
1292 |
|
1293 | var addClosingParen;
|
1294 | if (start !== element.start) {
|
1295 | if ((addClosingParen = needsParentheses(element.argument))) {
|
1296 | code.overwrite(start, element.start, '( ');
|
1297 | } else {
|
1298 | code.remove(start, element.start);
|
1299 | }
|
1300 | } else if (element.parent.type === 'CallExpression') {
|
1301 |
|
1302 |
|
1303 |
|
1304 | addClosingParen = needsParentheses(element.argument);
|
1305 | } else {
|
1306 |
|
1307 | throw new CompileError(
|
1308 | 'Unsupported spread construct, please raise an issue at https://github.com/bublejs/buble/issues',
|
1309 | element
|
1310 | );
|
1311 | }
|
1312 | code.overwrite(element.end, elements[1].start,
|
1313 | addClosingParen ? ' ).concat( ' : '.concat( ');
|
1314 | } else {
|
1315 | code.overwrite(previousElement.end, element.start, ' ].concat( ');
|
1316 | }
|
1317 |
|
1318 | for (i = firstSpreadIndex; i < elements.length; i += 1) {
|
1319 | element = elements[i];
|
1320 |
|
1321 | if (element) {
|
1322 | if (element.type === 'SpreadElement') {
|
1323 | code.remove(element.start, element.argument.start);
|
1324 | } else {
|
1325 | code.appendLeft(element.start, '[');
|
1326 | code.appendLeft(element.end, ']');
|
1327 | }
|
1328 | }
|
1329 | }
|
1330 |
|
1331 | return true;
|
1332 | }
|
1333 |
|
1334 | var ArrayExpression = (function (Node) {
|
1335 | function ArrayExpression () {
|
1336 | Node.apply(this, arguments);
|
1337 | }
|
1338 |
|
1339 | if ( Node ) ArrayExpression.__proto__ = Node;
|
1340 | ArrayExpression.prototype = Object.create( Node && Node.prototype );
|
1341 | ArrayExpression.prototype.constructor = ArrayExpression;
|
1342 |
|
1343 | ArrayExpression.prototype.initialise = function initialise (transforms) {
|
1344 | if (transforms.spreadRest && this.elements.length) {
|
1345 | var lexicalBoundary = this.findLexicalBoundary();
|
1346 |
|
1347 | var i = this.elements.length;
|
1348 | while (i--) {
|
1349 | var element = this.elements[i];
|
1350 | if (
|
1351 | element &&
|
1352 | element.type === 'SpreadElement' &&
|
1353 | isArguments(element.argument)
|
1354 | ) {
|
1355 | this.argumentsArrayAlias = lexicalBoundary.getArgumentsArrayAlias();
|
1356 | }
|
1357 | }
|
1358 | }
|
1359 |
|
1360 | Node.prototype.initialise.call(this, transforms);
|
1361 | };
|
1362 |
|
1363 | ArrayExpression.prototype.transpile = function transpile (code, transforms) {
|
1364 | Node.prototype.transpile.call(this, code, transforms);
|
1365 |
|
1366 | if (transforms.spreadRest) {
|
1367 | inlineSpreads(code, this, this.elements);
|
1368 |
|
1369 | if (this.elements.length) {
|
1370 | var lastElement = this.elements[this.elements.length - 1];
|
1371 | if (
|
1372 | lastElement &&
|
1373 | /\s*,/.test(code.original.slice(lastElement.end, this.end))
|
1374 | ) {
|
1375 | code.overwrite(lastElement.end, this.end - 1, ' ');
|
1376 | }
|
1377 | }
|
1378 |
|
1379 | if (this.elements.length === 1) {
|
1380 | var element = this.elements[0];
|
1381 |
|
1382 | if (element && element.type === 'SpreadElement') {
|
1383 |
|
1384 | if (isArguments(element.argument)) {
|
1385 | code.overwrite(
|
1386 | this.start,
|
1387 | this.end,
|
1388 | ("[].concat( " + (this.argumentsArrayAlias) + " )")
|
1389 | );
|
1390 | } else {
|
1391 | code.overwrite(this.start, element.argument.start, '[].concat( ');
|
1392 | code.overwrite(element.end, this.end, ' )');
|
1393 | }
|
1394 | }
|
1395 | } else {
|
1396 | var hasSpreadElements = spread(
|
1397 | code,
|
1398 | this.elements,
|
1399 | this.start,
|
1400 | this.argumentsArrayAlias
|
1401 | );
|
1402 |
|
1403 | if (hasSpreadElements) {
|
1404 | code.overwrite(this.end - 1, this.end, ')');
|
1405 | }
|
1406 | }
|
1407 | }
|
1408 | };
|
1409 |
|
1410 | return ArrayExpression;
|
1411 | }(Node));
|
1412 |
|
1413 | function removeTrailingComma(code, c) {
|
1414 | while (code.original[c] !== ')') {
|
1415 | if (code.original[c] === ',') {
|
1416 | code.remove(c, c + 1);
|
1417 | return;
|
1418 | }
|
1419 |
|
1420 | if (code.original[c] === '/') {
|
1421 | if (code.original[c + 1] === '/') {
|
1422 | c = code.original.indexOf('\n', c);
|
1423 | } else {
|
1424 | c = code.original.indexOf('*/', c) + 1;
|
1425 | }
|
1426 | }
|
1427 | c += 1;
|
1428 | }
|
1429 | }
|
1430 |
|
1431 | var ArrowFunctionExpression = (function (Node) {
|
1432 | function ArrowFunctionExpression () {
|
1433 | Node.apply(this, arguments);
|
1434 | }
|
1435 |
|
1436 | if ( Node ) ArrowFunctionExpression.__proto__ = Node;
|
1437 | ArrowFunctionExpression.prototype = Object.create( Node && Node.prototype );
|
1438 | ArrowFunctionExpression.prototype.constructor = ArrowFunctionExpression;
|
1439 |
|
1440 | ArrowFunctionExpression.prototype.initialise = function initialise (transforms) {
|
1441 | if (this.async && transforms.asyncAwait) {
|
1442 | CompileError.missingTransform("async arrow functions", "asyncAwait", this);
|
1443 | }
|
1444 | this.body.createScope();
|
1445 | Node.prototype.initialise.call(this, transforms);
|
1446 | };
|
1447 |
|
1448 | ArrowFunctionExpression.prototype.transpile = function transpile (code, transforms) {
|
1449 | var openParensPos = this.start;
|
1450 | for (var end = (this.body || this.params[0]).start - 1; code.original[openParensPos] !== '(' && openParensPos < end;) {
|
1451 | ++openParensPos;
|
1452 | }
|
1453 | if (code.original[openParensPos] !== '(') { openParensPos = -1; }
|
1454 | var naked = openParensPos === -1;
|
1455 |
|
1456 | if (transforms.arrow || this.needsArguments(transforms)) {
|
1457 |
|
1458 | var charIndex = this.body.start;
|
1459 | while (code.original[charIndex] !== '=') {
|
1460 | charIndex -= 1;
|
1461 | }
|
1462 | code.remove(charIndex, this.body.start);
|
1463 |
|
1464 | Node.prototype.transpile.call(this, code, transforms);
|
1465 |
|
1466 |
|
1467 | if (naked) {
|
1468 | code.prependRight(this.params[0].start, '(');
|
1469 | code.appendLeft(this.params[0].end, ')');
|
1470 | }
|
1471 |
|
1472 |
|
1473 | var standalone = this.parent && this.parent.type === 'ExpressionStatement';
|
1474 | var start, text = standalone ? '!' : '';
|
1475 | if (this.async) { text += 'async '; }
|
1476 | text += 'function';
|
1477 | if (!standalone) { text += ' '; }
|
1478 | if (naked) {
|
1479 | start = this.params[0].start;
|
1480 | } else {
|
1481 | start = openParensPos;
|
1482 | }
|
1483 |
|
1484 | if (start > this.start) {
|
1485 | code.overwrite(this.start, start, text);
|
1486 | } else {
|
1487 | code.prependRight(this.start, text);
|
1488 | }
|
1489 | } else {
|
1490 | Node.prototype.transpile.call(this, code, transforms);
|
1491 | }
|
1492 |
|
1493 | if (transforms.trailingFunctionCommas && this.params.length && !naked) {
|
1494 | removeTrailingComma(code, this.params[this.params.length - 1].end);
|
1495 | }
|
1496 | };
|
1497 |
|
1498 |
|
1499 | ArrowFunctionExpression.prototype.needsArguments = function needsArguments (transforms) {
|
1500 | return (
|
1501 | transforms.spreadRest &&
|
1502 | this.params.filter(function (param) { return param.type === 'RestElement'; }).length > 0
|
1503 | );
|
1504 | };
|
1505 |
|
1506 | return ArrowFunctionExpression;
|
1507 | }(Node));
|
1508 |
|
1509 | function checkConst(identifier, scope) {
|
1510 | var declaration = scope.findDeclaration(identifier.name);
|
1511 | if (declaration && declaration.kind === 'const') {
|
1512 | throw new CompileError(((identifier.name) + " is read-only"), identifier);
|
1513 | }
|
1514 | }
|
1515 |
|
1516 | var AssignmentExpression = (function (Node) {
|
1517 | function AssignmentExpression () {
|
1518 | Node.apply(this, arguments);
|
1519 | }
|
1520 |
|
1521 | if ( Node ) AssignmentExpression.__proto__ = Node;
|
1522 | AssignmentExpression.prototype = Object.create( Node && Node.prototype );
|
1523 | AssignmentExpression.prototype.constructor = AssignmentExpression;
|
1524 |
|
1525 | AssignmentExpression.prototype.initialise = function initialise (transforms) {
|
1526 | if (this.left.type === 'Identifier') {
|
1527 | var declaration = this.findScope(false).findDeclaration(this.left.name);
|
1528 |
|
1529 | var statement = declaration && declaration.node.ancestor(3);
|
1530 | if (
|
1531 | statement &&
|
1532 | statement.type === 'ForStatement' &&
|
1533 | statement.body.contains(this)
|
1534 | ) {
|
1535 | statement.reassigned[this.left.name] = true;
|
1536 | }
|
1537 | }
|
1538 |
|
1539 | Node.prototype.initialise.call(this, transforms);
|
1540 | };
|
1541 |
|
1542 | AssignmentExpression.prototype.transpile = function transpile (code, transforms) {
|
1543 | if (this.left.type === 'Identifier') {
|
1544 |
|
1545 |
|
1546 | checkConst(this.left, this.findScope(false));
|
1547 | }
|
1548 |
|
1549 | if (this.operator === '**=' && transforms.exponentiation) {
|
1550 | this.transpileExponentiation(code, transforms);
|
1551 | } else if (/Pattern/.test(this.left.type) && transforms.destructuring) {
|
1552 | this.transpileDestructuring(code);
|
1553 | }
|
1554 |
|
1555 | Node.prototype.transpile.call(this, code, transforms);
|
1556 | };
|
1557 |
|
1558 | AssignmentExpression.prototype.transpileDestructuring = function transpileDestructuring (code) {
|
1559 | var this$1 = this;
|
1560 |
|
1561 | var writeScope = this.findScope(true);
|
1562 | var lookupScope = this.findScope(false);
|
1563 | var assign = writeScope.createDeclaration('assign');
|
1564 | code.appendRight(this.left.end, ("(" + assign));
|
1565 |
|
1566 | code.appendLeft(this.right.end, ', ');
|
1567 | var statementGenerators = [];
|
1568 | destructure(
|
1569 | code,
|
1570 | function (id) { return writeScope.createDeclaration(id); },
|
1571 | function (node) {
|
1572 | var name = lookupScope.resolveName(node.name);
|
1573 | checkConst(node, lookupScope);
|
1574 | return name;
|
1575 | },
|
1576 | this.left,
|
1577 | assign,
|
1578 | true,
|
1579 | statementGenerators
|
1580 | );
|
1581 |
|
1582 | var suffix = ', ';
|
1583 | statementGenerators.forEach(function (fn, j) {
|
1584 | if (j === statementGenerators.length - 1) {
|
1585 | suffix = '';
|
1586 | }
|
1587 |
|
1588 | fn(this$1.end, '', suffix);
|
1589 | });
|
1590 |
|
1591 | if (this.unparenthesizedParent().type === 'ExpressionStatement') {
|
1592 |
|
1593 | code.prependRight(this.end, ")");
|
1594 | } else {
|
1595 |
|
1596 | code.appendRight(this.end, (", " + assign + ")"));
|
1597 | }
|
1598 | };
|
1599 |
|
1600 | AssignmentExpression.prototype.transpileExponentiation = function transpileExponentiation (code) {
|
1601 | var scope = this.findScope(false);
|
1602 |
|
1603 |
|
1604 | var charIndex = this.left.end;
|
1605 | while (code.original[charIndex] !== '*') { charIndex += 1; }
|
1606 | code.remove(charIndex, charIndex + 2);
|
1607 |
|
1608 |
|
1609 |
|
1610 |
|
1611 | var base;
|
1612 |
|
1613 | var left = this.left.unparenthesize();
|
1614 |
|
1615 | if (left.type === 'Identifier') {
|
1616 | base = scope.resolveName(left.name);
|
1617 | } else if (left.type === 'MemberExpression') {
|
1618 | var object;
|
1619 | var needsObjectVar = false;
|
1620 | var property;
|
1621 | var needsPropertyVar = false;
|
1622 |
|
1623 | var statement = this.findNearest(/(?:Statement|Declaration)$/);
|
1624 | var i0 = statement.getIndentation();
|
1625 |
|
1626 | if (left.property.type === 'Identifier') {
|
1627 | property = left.computed
|
1628 | ? scope.resolveName(left.property.name)
|
1629 | : left.property.name;
|
1630 | } else {
|
1631 | property = scope.createDeclaration('property');
|
1632 | needsPropertyVar = true;
|
1633 | }
|
1634 |
|
1635 | if (left.object.type === 'Identifier') {
|
1636 | object = scope.resolveName(left.object.name);
|
1637 | } else {
|
1638 | object = scope.createDeclaration('object');
|
1639 | needsObjectVar = true;
|
1640 | }
|
1641 |
|
1642 | if (left.start === statement.start) {
|
1643 | if (needsObjectVar && needsPropertyVar) {
|
1644 | code.prependRight(statement.start, (object + " = "));
|
1645 | code.overwrite(
|
1646 | left.object.end,
|
1647 | left.property.start,
|
1648 | (";\n" + i0 + property + " = ")
|
1649 | );
|
1650 | code.overwrite(
|
1651 | left.property.end,
|
1652 | left.end,
|
1653 | (";\n" + i0 + object + "[" + property + "]")
|
1654 | );
|
1655 | } else if (needsObjectVar) {
|
1656 | code.prependRight(statement.start, (object + " = "));
|
1657 | code.appendLeft(left.object.end, (";\n" + i0));
|
1658 | code.appendLeft(left.object.end, object);
|
1659 | } else if (needsPropertyVar) {
|
1660 | code.prependRight(left.property.start, (property + " = "));
|
1661 | code.appendLeft(left.property.end, (";\n" + i0));
|
1662 | code.move(left.property.start, left.property.end, this.start);
|
1663 |
|
1664 | code.appendLeft(left.object.end, ("[" + property + "]"));
|
1665 | code.remove(left.object.end, left.property.start);
|
1666 | code.remove(left.property.end, left.end);
|
1667 | }
|
1668 | } else {
|
1669 | if (needsObjectVar && needsPropertyVar) {
|
1670 | code.prependRight(left.start, ("( " + object + " = "));
|
1671 | code.overwrite(
|
1672 | left.object.end,
|
1673 | left.property.start,
|
1674 | (", " + property + " = ")
|
1675 | );
|
1676 | code.overwrite(
|
1677 | left.property.end,
|
1678 | left.end,
|
1679 | (", " + object + "[" + property + "]")
|
1680 | );
|
1681 | } else if (needsObjectVar) {
|
1682 | code.prependRight(left.start, ("( " + object + " = "));
|
1683 | code.appendLeft(left.object.end, (", " + object));
|
1684 | } else if (needsPropertyVar) {
|
1685 | code.prependRight(left.property.start, ("( " + property + " = "));
|
1686 | code.appendLeft(left.property.end, ", ");
|
1687 | code.move(left.property.start, left.property.end, left.start);
|
1688 |
|
1689 | code.overwrite(left.object.end, left.property.start, ("[" + property + "]"));
|
1690 | code.remove(left.property.end, left.end);
|
1691 | }
|
1692 |
|
1693 | if (needsPropertyVar) {
|
1694 | code.appendLeft(this.end, " )");
|
1695 | }
|
1696 | }
|
1697 |
|
1698 | base =
|
1699 | object +
|
1700 | (left.computed || needsPropertyVar ? ("[" + property + "]") : ("." + property));
|
1701 | }
|
1702 |
|
1703 | code.prependRight(this.right.start, ("Math.pow( " + base + ", "));
|
1704 | code.appendLeft(this.right.end, " )");
|
1705 | };
|
1706 |
|
1707 | return AssignmentExpression;
|
1708 | }(Node));
|
1709 |
|
1710 | var AwaitExpression = (function (Node) {
|
1711 | function AwaitExpression () {
|
1712 | Node.apply(this, arguments);
|
1713 | }
|
1714 |
|
1715 | if ( Node ) AwaitExpression.__proto__ = Node;
|
1716 | AwaitExpression.prototype = Object.create( Node && Node.prototype );
|
1717 | AwaitExpression.prototype.constructor = AwaitExpression;
|
1718 |
|
1719 | AwaitExpression.prototype.initialise = function initialise (transforms) {
|
1720 | if (transforms.asyncAwait) {
|
1721 | CompileError.missingTransform("await", "asyncAwait", this);
|
1722 | }
|
1723 | Node.prototype.initialise.call(this, transforms);
|
1724 | };
|
1725 |
|
1726 | return AwaitExpression;
|
1727 | }(Node));
|
1728 |
|
1729 | var BinaryExpression = (function (Node) {
|
1730 | function BinaryExpression () {
|
1731 | Node.apply(this, arguments);
|
1732 | }
|
1733 |
|
1734 | if ( Node ) BinaryExpression.__proto__ = Node;
|
1735 | BinaryExpression.prototype = Object.create( Node && Node.prototype );
|
1736 | BinaryExpression.prototype.constructor = BinaryExpression;
|
1737 |
|
1738 | BinaryExpression.prototype.transpile = function transpile (code, transforms) {
|
1739 | if (this.operator === '**' && transforms.exponentiation) {
|
1740 | code.prependRight(this.start, "Math.pow( ");
|
1741 | code.overwrite(this.left.end, this.right.start, ", ");
|
1742 | code.appendLeft(this.end, " )");
|
1743 | }
|
1744 | Node.prototype.transpile.call(this, code, transforms);
|
1745 | };
|
1746 |
|
1747 | return BinaryExpression;
|
1748 | }(Node));
|
1749 |
|
1750 | var loopStatement = /(?:For(?:In|Of)?|While)Statement/;
|
1751 |
|
1752 | var BreakStatement = (function (Node) {
|
1753 | function BreakStatement () {
|
1754 | Node.apply(this, arguments);
|
1755 | }
|
1756 |
|
1757 | if ( Node ) BreakStatement.__proto__ = Node;
|
1758 | BreakStatement.prototype = Object.create( Node && Node.prototype );
|
1759 | BreakStatement.prototype.constructor = BreakStatement;
|
1760 |
|
1761 | BreakStatement.prototype.initialise = function initialise () {
|
1762 | var loop = this.findNearest(loopStatement);
|
1763 | var switchCase = this.findNearest('SwitchCase');
|
1764 |
|
1765 | if (loop && (!switchCase || loop.depth > switchCase.depth)) {
|
1766 | loop.canBreak = true;
|
1767 | this.loop = loop;
|
1768 | }
|
1769 | };
|
1770 |
|
1771 | BreakStatement.prototype.transpile = function transpile (code) {
|
1772 | if (this.loop && this.loop.shouldRewriteAsFunction) {
|
1773 | if (this.label)
|
1774 | { throw new CompileError(
|
1775 | 'Labels are not currently supported in a loop with locally-scoped variables',
|
1776 | this
|
1777 | ); }
|
1778 | code.overwrite(this.start, this.start + 5, "return 'break'");
|
1779 | }
|
1780 | };
|
1781 |
|
1782 | return BreakStatement;
|
1783 | }(Node));
|
1784 |
|
1785 | var CallExpression = (function (Node) {
|
1786 | function CallExpression () {
|
1787 | Node.apply(this, arguments);
|
1788 | }
|
1789 |
|
1790 | if ( Node ) CallExpression.__proto__ = Node;
|
1791 | CallExpression.prototype = Object.create( Node && Node.prototype );
|
1792 | CallExpression.prototype.constructor = CallExpression;
|
1793 |
|
1794 | CallExpression.prototype.initialise = function initialise (transforms) {
|
1795 | if (transforms.spreadRest && this.arguments.length > 1) {
|
1796 | var lexicalBoundary = this.findLexicalBoundary();
|
1797 |
|
1798 | var i = this.arguments.length;
|
1799 | while (i--) {
|
1800 | var arg = this.arguments[i];
|
1801 | if (arg.type === 'SpreadElement' && isArguments(arg.argument)) {
|
1802 | this.argumentsArrayAlias = lexicalBoundary.getArgumentsArrayAlias();
|
1803 | }
|
1804 | }
|
1805 | }
|
1806 |
|
1807 | Node.prototype.initialise.call(this, transforms);
|
1808 | };
|
1809 |
|
1810 | CallExpression.prototype.transpile = function transpile (code, transforms) {
|
1811 | if (transforms.spreadRest && this.arguments.length) {
|
1812 | inlineSpreads(code, this, this.arguments);
|
1813 |
|
1814 | }
|
1815 |
|
1816 | if (transforms.spreadRest && this.arguments.length) {
|
1817 | var hasSpreadElements = false;
|
1818 | var context;
|
1819 |
|
1820 | var firstArgument = this.arguments[0];
|
1821 |
|
1822 | if (this.arguments.length === 1) {
|
1823 | if (firstArgument.type === 'SpreadElement') {
|
1824 | code.remove(firstArgument.start, firstArgument.argument.start);
|
1825 | hasSpreadElements = true;
|
1826 | }
|
1827 | } else {
|
1828 | hasSpreadElements = spread(
|
1829 | code,
|
1830 | this.arguments,
|
1831 | firstArgument.start,
|
1832 | this.argumentsArrayAlias
|
1833 | );
|
1834 | }
|
1835 |
|
1836 | if (hasSpreadElements) {
|
1837 |
|
1838 |
|
1839 | var _super = null;
|
1840 | if (this.callee.type === 'Super') {
|
1841 | _super = this.callee;
|
1842 | } else if (
|
1843 | this.callee.type === 'MemberExpression' &&
|
1844 | this.callee.object.type === 'Super'
|
1845 | ) {
|
1846 | _super = this.callee.object;
|
1847 | }
|
1848 |
|
1849 | if (!_super && this.callee.type === 'MemberExpression') {
|
1850 | if (this.callee.object.type === 'Identifier') {
|
1851 | context = this.callee.object.name;
|
1852 | } else {
|
1853 | context = this.findScope(true).createDeclaration('ref');
|
1854 | var callExpression = this.callee.object;
|
1855 | code.prependRight(callExpression.start, ("(" + context + " = "));
|
1856 | code.appendLeft(callExpression.end, ")");
|
1857 | }
|
1858 | } else {
|
1859 | context = 'void 0';
|
1860 | }
|
1861 |
|
1862 | code.appendLeft(this.callee.end, '.apply');
|
1863 |
|
1864 | if (_super) {
|
1865 | _super.noCall = true;
|
1866 |
|
1867 | if (this.arguments.length > 1) {
|
1868 | if (firstArgument.type === 'SpreadElement') {
|
1869 | if (needsParentheses(firstArgument.argument)) {
|
1870 | code.prependRight(firstArgument.start, "( ");
|
1871 | }
|
1872 | } else {
|
1873 | code.prependRight(firstArgument.start, "[ ");
|
1874 | }
|
1875 |
|
1876 | code.appendLeft(
|
1877 | this.arguments[this.arguments.length - 1].end,
|
1878 | ' )'
|
1879 | );
|
1880 | }
|
1881 | } else if (this.arguments.length === 1) {
|
1882 | code.prependRight(firstArgument.start, (context + ", "));
|
1883 | } else {
|
1884 | if (firstArgument.type === 'SpreadElement') {
|
1885 | if (needsParentheses(firstArgument.argument)) {
|
1886 | code.appendLeft(firstArgument.start, (context + ", ( "));
|
1887 | } else {
|
1888 | code.appendLeft(firstArgument.start, (context + ", "));
|
1889 | }
|
1890 | } else {
|
1891 | code.appendLeft(firstArgument.start, (context + ", [ "));
|
1892 | }
|
1893 |
|
1894 | code.appendLeft(this.arguments[this.arguments.length - 1].end, ' )');
|
1895 | }
|
1896 | }
|
1897 | }
|
1898 |
|
1899 | if (transforms.trailingFunctionCommas && this.arguments.length) {
|
1900 | removeTrailingComma(code, this.arguments[this.arguments.length - 1].end);
|
1901 | }
|
1902 |
|
1903 | Node.prototype.transpile.call(this, code, transforms);
|
1904 | };
|
1905 |
|
1906 | return CallExpression;
|
1907 | }(Node));
|
1908 |
|
1909 | var CatchClause = (function (Node) {
|
1910 | function CatchClause () {
|
1911 | Node.apply(this, arguments);
|
1912 | }
|
1913 |
|
1914 | if ( Node ) CatchClause.__proto__ = Node;
|
1915 | CatchClause.prototype = Object.create( Node && Node.prototype );
|
1916 | CatchClause.prototype.constructor = CatchClause;
|
1917 |
|
1918 | CatchClause.prototype.initialise = function initialise (transforms) {
|
1919 | var this$1 = this;
|
1920 |
|
1921 | this.createdDeclarations = [];
|
1922 | this.scope = new Scope({
|
1923 | block: true,
|
1924 | parent: this.parent.findScope(false),
|
1925 | declare: function (id) { return this$1.createdDeclarations.push(id); }
|
1926 | });
|
1927 |
|
1928 | this.scope.addDeclaration(this.param, 'catch');
|
1929 |
|
1930 | Node.prototype.initialise.call(this, transforms);
|
1931 | this.scope.consolidate();
|
1932 | };
|
1933 |
|
1934 | CatchClause.prototype.findScope = function findScope (functionScope) {
|
1935 | return functionScope
|
1936 | ? this.parent.findScope(functionScope)
|
1937 | : this.scope;
|
1938 | };
|
1939 |
|
1940 | return CatchClause;
|
1941 | }(Node));
|
1942 |
|
1943 |
|
1944 | var ClassBody = (function (Node) {
|
1945 | function ClassBody () {
|
1946 | Node.apply(this, arguments);
|
1947 | }
|
1948 |
|
1949 | if ( Node ) ClassBody.__proto__ = Node;
|
1950 | ClassBody.prototype = Object.create( Node && Node.prototype );
|
1951 | ClassBody.prototype.constructor = ClassBody;
|
1952 |
|
1953 | ClassBody.prototype.transpile = function transpile (code, transforms, inFunctionExpression, superName) {
|
1954 | var this$1 = this;
|
1955 |
|
1956 | if (transforms.classes) {
|
1957 | var name = this.parent.name;
|
1958 |
|
1959 | var indentStr = code.getIndentString();
|
1960 | var i0 =
|
1961 | this.getIndentation() + (inFunctionExpression ? indentStr : '');
|
1962 | var i1 = i0 + indentStr;
|
1963 |
|
1964 | var constructorIndex = findIndex(
|
1965 | this.body,
|
1966 | function (node) { return node.kind === 'constructor'; }
|
1967 | );
|
1968 | var constructor = this.body[constructorIndex];
|
1969 |
|
1970 | var introBlock = '';
|
1971 | var outroBlock = '';
|
1972 |
|
1973 | if (this.body.length) {
|
1974 | code.remove(this.start, this.body[0].start);
|
1975 | code.remove(this.body[this.body.length - 1].end, this.end);
|
1976 | } else {
|
1977 | code.remove(this.start, this.end);
|
1978 | }
|
1979 |
|
1980 | if (constructor) {
|
1981 | constructor.value.body.isConstructorBody = true;
|
1982 |
|
1983 | var previousMethod = this.body[constructorIndex - 1];
|
1984 | var nextMethod = this.body[constructorIndex + 1];
|
1985 |
|
1986 | // ensure constructor is first
|
1987 | if (constructorIndex > 0) {
|
1988 | code.remove(previousMethod.end, constructor.start);
|
1989 | code.move(
|
1990 | constructor.start,
|
1991 | nextMethod ? nextMethod.start : this.end - 1,
|
1992 | this.body[0].start
|
1993 | );
|
1994 | }
|
1995 |
|
1996 | if (!inFunctionExpression) { code.appendLeft(constructor.end, ';'); }
|
1997 | }
|
1998 |
|
1999 | var namedFunctions =
|
2000 | this.program.options.namedFunctionExpressions !== false;
|
2001 | var namedConstructor =
|
2002 | namedFunctions ||
|
2003 | this.parent.superClass ||
|
2004 | this.parent.type !== 'ClassDeclaration';
|
2005 | if (this.parent.superClass) {
|
2006 | var inheritanceBlock = "if ( " + superName + " ) " + name + ".__proto__ = " + superName + ";\n" + i0 + name + ".prototype = Object.create( " + superName + " && " + superName + ".prototype );\n" + i0 + name + ".prototype.constructor = " + name + ";";
|
2007 |
|
2008 | if (constructor) {
|
2009 | introBlock += "\n\n" + i0 + inheritanceBlock;
|
2010 | } else {
|
2011 | var fn =
|
2012 | "function " + name + " () {" +
|
2013 | (superName
|
2014 | ? ("\n" + i1 + superName + ".apply(this, arguments);\n" + i0 + "}")
|
2015 | : "}") +
|
2016 | (inFunctionExpression ? '' : ';') +
|
2017 | (this.body.length ? ("\n\n" + i0) : '');
|
2018 |
|
2019 | inheritanceBlock = fn + inheritanceBlock;
|
2020 | introBlock += inheritanceBlock + "\n\n" + i0;
|
2021 | }
|
2022 | } else if (!constructor) {
|
2023 | var fn$1 = 'function ' + (namedConstructor ? name + ' ' : '') + '() {}';
|
2024 | if (this.parent.type === 'ClassDeclaration') { fn$1 += ';'; }
|
2025 | if (this.body.length) { fn$1 += "\n\n" + i0; }
|
2026 |
|
2027 | introBlock += fn$1;
|
2028 | }
|
2029 |
|
2030 | var scope = this.findScope(false);
|
2031 |
|
2032 | var prototypeGettersAndSetters = [];
|
2033 | var staticGettersAndSetters = [];
|
2034 | var prototypeAccessors;
|
2035 | var staticAccessors;
|
2036 |
|
2037 | this.body.forEach(function (method, i) {
|
2038 | if ((method.kind === 'get' || method.kind === 'set') && transforms.getterSetter) {
|
2039 | CompileError.missingTransform("getters and setters", "getterSetter", method);
|
2040 | }
|
2041 |
|
2042 | if (method.kind === 'constructor') {
|
2043 | var constructorName = namedConstructor ? ' ' + name : '';
|
2044 | code.overwrite(
|
2045 | method.key.start,
|
2046 | method.key.end,
|
2047 | ("function" + constructorName)
|
2048 | );
|
2049 | return;
|
2050 | }
|
2051 |
|
2052 | if (method.static) {
|
2053 | var len = code.original[method.start + 6] == ' ' ? 7 : 6;
|
2054 | code.remove(method.start, method.start + len);
|
2055 | }
|
2056 |
|
2057 | var isAccessor = method.kind !== 'method';
|
2058 | var lhs;
|
2059 |
|
2060 | var methodName = method.key.name;
|
2061 | if (
|
2062 | reserved[methodName] ||
|
2063 | method.value.body.scope.references[methodName]
|
2064 | ) {
|
2065 | methodName = scope.createIdentifier(methodName);
|
2066 | }
|
2067 |
|
2068 |
|
2069 |
|
2070 | var fake_computed = false;
|
2071 | if (!method.computed && method.key.type === 'Literal') {
|
2072 | fake_computed = true;
|
2073 | method.computed = true;
|
2074 | }
|
2075 |
|
2076 | if (isAccessor) {
|
2077 | if (method.computed) {
|
2078 | throw new Error(
|
2079 | 'Computed accessor properties are not currently supported'
|
2080 | );
|
2081 | }
|
2082 |
|
2083 | code.remove(method.start, method.key.start);
|
2084 |
|
2085 | if (method.static) {
|
2086 | if (!~staticGettersAndSetters.indexOf(method.key.name))
|
2087 | { staticGettersAndSetters.push(method.key.name); }
|
2088 | if (!staticAccessors)
|
2089 | { staticAccessors = scope.createIdentifier('staticAccessors'); }
|
2090 |
|
2091 | lhs = "" + staticAccessors;
|
2092 | } else {
|
2093 | if (!~prototypeGettersAndSetters.indexOf(method.key.name))
|
2094 | { prototypeGettersAndSetters.push(method.key.name); }
|
2095 | if (!prototypeAccessors)
|
2096 | { prototypeAccessors = scope.createIdentifier('prototypeAccessors'); }
|
2097 |
|
2098 | lhs = "" + prototypeAccessors;
|
2099 | }
|
2100 | } else {
|
2101 | lhs = method.static ? ("" + name) : (name + ".prototype");
|
2102 | }
|
2103 |
|
2104 | if (!method.computed) { lhs += '.'; }
|
2105 |
|
2106 | var insertNewlines =
|
2107 | (constructorIndex > 0 && i === constructorIndex + 1) ||
|
2108 | (i === 0 && constructorIndex === this$1.body.length - 1);
|
2109 |
|
2110 | if (insertNewlines) { lhs = "\n\n" + i0 + lhs; }
|
2111 |
|
2112 | var c = method.key.end;
|
2113 | if (method.computed) {
|
2114 | if (fake_computed) {
|
2115 | code.prependRight(method.key.start, '[');
|
2116 | code.appendLeft(method.key.end, ']');
|
2117 | } else {
|
2118 | while (code.original[c] !== ']') { c += 1; }
|
2119 | c += 1;
|
2120 | }
|
2121 | }
|
2122 |
|
2123 | var funcName =
|
2124 | method.computed || isAccessor || !namedFunctions
|
2125 | ? ''
|
2126 | : (methodName + " ");
|
2127 | var rhs =
|
2128 | (isAccessor ? ("." + (method.kind)) : '') +
|
2129 | " = " + (method.value.async ? 'async ' : '') + "function" +
|
2130 | (method.value.generator ? '* ' : ' ') +
|
2131 | funcName;
|
2132 | code.remove(c, method.value.start);
|
2133 | code.prependRight(method.value.start, rhs);
|
2134 | code.appendLeft(method.end, ';');
|
2135 |
|
2136 | if (method.value.generator) { code.remove(method.start, method.key.start); }
|
2137 |
|
2138 | var start = method.key.start;
|
2139 | if (method.computed && !fake_computed) {
|
2140 | while (code.original[start] != '[') {
|
2141 | --start;
|
2142 | }
|
2143 | }
|
2144 | if (method.start < start) {
|
2145 | code.overwrite(method.start, start, lhs);
|
2146 | } else {
|
2147 | code.prependRight(method.start, lhs);
|
2148 | }
|
2149 | });
|
2150 |
|
2151 | if (prototypeGettersAndSetters.length || staticGettersAndSetters.length) {
|
2152 | var intro = [];
|
2153 | var outro = [];
|
2154 |
|
2155 | if (prototypeGettersAndSetters.length) {
|
2156 | intro.push(
|
2157 | ("var " + prototypeAccessors + " = { " + (prototypeGettersAndSetters
|
2158 | .map(function (name) { return (name + ": { configurable: true }"); })
|
2159 | .join(',')) + " };")
|
2160 | );
|
2161 | outro.push(
|
2162 | ("Object.defineProperties( " + name + ".prototype, " + prototypeAccessors + " );")
|
2163 | );
|
2164 | }
|
2165 |
|
2166 | if (staticGettersAndSetters.length) {
|
2167 | intro.push(
|
2168 | ("var " + staticAccessors + " = { " + (staticGettersAndSetters
|
2169 | .map(function (name) { return (name + ": { configurable: true }"); })
|
2170 | .join(',')) + " };")
|
2171 | );
|
2172 | outro.push(("Object.defineProperties( " + name + ", " + staticAccessors + " );"));
|
2173 | }
|
2174 |
|
2175 | if (constructor) { introBlock += "\n\n" + i0; }
|
2176 | introBlock += intro.join(("\n" + i0));
|
2177 | if (!constructor) { introBlock += "\n\n" + i0; }
|
2178 |
|
2179 | outroBlock += "\n\n" + i0 + outro.join(("\n" + i0));
|
2180 | }
|
2181 |
|
2182 | if (constructor) {
|
2183 | code.appendLeft(constructor.end, introBlock);
|
2184 | } else {
|
2185 | code.prependRight(this.start, introBlock);
|
2186 | }
|
2187 |
|
2188 | code.appendLeft(this.end, outroBlock);
|
2189 | }
|
2190 |
|
2191 | Node.prototype.transpile.call(this, code, transforms);
|
2192 | };
|
2193 |
|
2194 | return ClassBody;
|
2195 | }(Node));
|
2196 |
|
2197 |
|
2198 |
|
2199 |
|
2200 |
|
2201 | function deindent(node, code) {
|
2202 | var start = node.start;
|
2203 | var end = node.end;
|
2204 |
|
2205 | var indentStr = code.getIndentString();
|
2206 | var indentStrLen = indentStr.length;
|
2207 | var indentStart = start - indentStrLen;
|
2208 |
|
2209 | if (
|
2210 | !node.program.indentExclusions[indentStart] &&
|
2211 | code.original.slice(indentStart, start) === indentStr
|
2212 | ) {
|
2213 | code.remove(indentStart, start);
|
2214 | }
|
2215 |
|
2216 | var pattern = new RegExp(indentStr + '\\S', 'g');
|
2217 | var slice = code.original.slice(start, end);
|
2218 | var match;
|
2219 |
|
2220 | while ((match = pattern.exec(slice))) {
|
2221 | var removeStart = start + match.index;
|
2222 | if (!node.program.indentExclusions[removeStart]) {
|
2223 | code.remove(removeStart, removeStart + indentStrLen);
|
2224 | }
|
2225 | }
|
2226 | }
|
2227 |
|
2228 | var ClassDeclaration = (function (Node) {
|
2229 | function ClassDeclaration () {
|
2230 | Node.apply(this, arguments);
|
2231 | }
|
2232 |
|
2233 | if ( Node ) ClassDeclaration.__proto__ = Node;
|
2234 | ClassDeclaration.prototype = Object.create( Node && Node.prototype );
|
2235 | ClassDeclaration.prototype.constructor = ClassDeclaration;
|
2236 |
|
2237 | ClassDeclaration.prototype.initialise = function initialise (transforms) {
|
2238 | if (this.id) {
|
2239 | this.name = this.id.name;
|
2240 | this.findScope(true).addDeclaration(this.id, 'class');
|
2241 | } else {
|
2242 | this.name = this.findScope(true).createIdentifier("defaultExport");
|
2243 | }
|
2244 |
|
2245 | Node.prototype.initialise.call(this, transforms);
|
2246 | };
|
2247 |
|
2248 | ClassDeclaration.prototype.transpile = function transpile (code, transforms) {
|
2249 | if (transforms.classes) {
|
2250 | if (!this.superClass) { deindent(this.body, code); }
|
2251 |
|
2252 | var superName =
|
2253 | this.superClass && (this.superClass.name || 'superclass');
|
2254 |
|
2255 | var i0 = this.getIndentation();
|
2256 | var i1 = i0 + code.getIndentString();
|
2257 |
|
2258 |
|
2259 |
|
2260 | var isExportDefaultDeclaration = this.parent.type === 'ExportDefaultDeclaration';
|
2261 |
|
2262 | if (isExportDefaultDeclaration) {
|
2263 | code.remove(this.parent.start, this.start);
|
2264 | }
|
2265 |
|
2266 | var c = this.start;
|
2267 | if (this.id) {
|
2268 | code.overwrite(c, this.id.start, 'var ');
|
2269 | c = this.id.end;
|
2270 | } else {
|
2271 | code.prependLeft(c, ("var " + (this.name)));
|
2272 | }
|
2273 |
|
2274 | if (this.superClass) {
|
2275 | if (this.superClass.end === this.body.start) {
|
2276 | code.remove(c, this.superClass.start);
|
2277 | code.appendLeft(c, (" = /*@__PURE__*/(function (" + superName + ") {\n" + i1));
|
2278 | } else {
|
2279 | code.overwrite(c, this.superClass.start, ' = ');
|
2280 | code.overwrite(
|
2281 | this.superClass.end,
|
2282 | this.body.start,
|
2283 | ("/*@__PURE__*/(function (" + superName + ") {\n" + i1)
|
2284 | );
|
2285 | }
|
2286 | } else {
|
2287 | if (c === this.body.start) {
|
2288 | code.appendLeft(c, ' = ');
|
2289 | } else {
|
2290 | code.overwrite(c, this.body.start, ' = ');
|
2291 | }
|
2292 | }
|
2293 |
|
2294 | this.body.transpile(code, transforms, !!this.superClass, superName);
|
2295 |
|
2296 | var syntheticDefaultExport =
|
2297 | isExportDefaultDeclaration
|
2298 | ? ("\n\n" + i0 + "export default " + (this.name) + ";")
|
2299 | : '';
|
2300 | if (this.superClass) {
|
2301 | code.appendLeft(this.end, ("\n\n" + i1 + "return " + (this.name) + ";\n" + i0 + "}("));
|
2302 | code.move(this.superClass.start, this.superClass.end, this.end);
|
2303 | code.prependRight(this.end, ("));" + syntheticDefaultExport));
|
2304 | } else if (syntheticDefaultExport) {
|
2305 | code.prependRight(this.end, syntheticDefaultExport);
|
2306 | }
|
2307 | } else {
|
2308 | this.body.transpile(code, transforms, false, null);
|
2309 | }
|
2310 | };
|
2311 |
|
2312 | return ClassDeclaration;
|
2313 | }(Node));
|
2314 |
|
2315 | var ClassExpression = (function (Node) {
|
2316 | function ClassExpression () {
|
2317 | Node.apply(this, arguments);
|
2318 | }
|
2319 |
|
2320 | if ( Node ) ClassExpression.__proto__ = Node;
|
2321 | ClassExpression.prototype = Object.create( Node && Node.prototype );
|
2322 | ClassExpression.prototype.constructor = ClassExpression;
|
2323 |
|
2324 | ClassExpression.prototype.initialise = function initialise (transforms) {
|
2325 | this.name = (this.id
|
2326 | ? this.id.name
|
2327 | : this.parent.type === 'VariableDeclarator'
|
2328 | ? this.parent.id.name
|
2329 | : this.parent.type !== 'AssignmentExpression'
|
2330 | ? null
|
2331 | : this.parent.left.type === 'Identifier'
|
2332 | ? this.parent.left.name
|
2333 | : this.parent.left.type === 'MemberExpression'
|
2334 | ? this.parent.left.property.name
|
2335 | : null) || this.findScope(true).createIdentifier('anonymous');
|
2336 |
|
2337 | Node.prototype.initialise.call(this, transforms);
|
2338 | };
|
2339 |
|
2340 | ClassExpression.prototype.transpile = function transpile (code, transforms) {
|
2341 | if (transforms.classes) {
|
2342 | var superName = this.superClass && (this.superClass.name || 'superclass');
|
2343 | if (superName === this.name) {
|
2344 | superName = this.findScope(true).createIdentifier(this.name);
|
2345 | }
|
2346 |
|
2347 | var i0 = this.getIndentation();
|
2348 | var i1 = i0 + code.getIndentString();
|
2349 |
|
2350 | if (this.superClass) {
|
2351 | code.remove(this.start, this.superClass.start);
|
2352 | code.remove(this.superClass.end, this.body.start);
|
2353 | code.appendRight(this.start, ("/*@__PURE__*/(function (" + superName + ") {\n" + i1));
|
2354 | } else {
|
2355 | code.overwrite(this.start, this.body.start, ("/*@__PURE__*/(function () {\n" + i1));
|
2356 | }
|
2357 |
|
2358 | this.body.transpile(code, transforms, true, superName);
|
2359 |
|
2360 | var superClass = '';
|
2361 | if (this.superClass) {
|
2362 | superClass = code.slice(this.superClass.start, this.superClass.end);
|
2363 | code.remove(this.superClass.start, this.superClass.end);
|
2364 | }
|
2365 | code.appendLeft(this.end, ("\n\n" + i1 + "return " + (this.name) + ";\n" + i0 + "}(" + superClass + "))"));
|
2366 | } else {
|
2367 | this.body.transpile(code, transforms, false);
|
2368 | }
|
2369 | };
|
2370 |
|
2371 | return ClassExpression;
|
2372 | }(Node));
|
2373 |
|
2374 | var ContinueStatement = (function (Node) {
|
2375 | function ContinueStatement () {
|
2376 | Node.apply(this, arguments);
|
2377 | }
|
2378 |
|
2379 | if ( Node ) ContinueStatement.__proto__ = Node;
|
2380 | ContinueStatement.prototype = Object.create( Node && Node.prototype );
|
2381 | ContinueStatement.prototype.constructor = ContinueStatement;
|
2382 |
|
2383 | ContinueStatement.prototype.transpile = function transpile (code) {
|
2384 | var loop = this.findNearest(loopStatement);
|
2385 | if (loop.shouldRewriteAsFunction) {
|
2386 | if (this.label)
|
2387 | { throw new CompileError(
|
2388 | 'Labels are not currently supported in a loop with locally-scoped variables',
|
2389 | this
|
2390 | ); }
|
2391 | code.overwrite(this.start, this.start + 8, 'return');
|
2392 | }
|
2393 | };
|
2394 |
|
2395 | return ContinueStatement;
|
2396 | }(Node));
|
2397 |
|
2398 | var ExportDefaultDeclaration = (function (Node) {
|
2399 | function ExportDefaultDeclaration () {
|
2400 | Node.apply(this, arguments);
|
2401 | }
|
2402 |
|
2403 | if ( Node ) ExportDefaultDeclaration.__proto__ = Node;
|
2404 | ExportDefaultDeclaration.prototype = Object.create( Node && Node.prototype );
|
2405 | ExportDefaultDeclaration.prototype.constructor = ExportDefaultDeclaration;
|
2406 |
|
2407 | ExportDefaultDeclaration.prototype.initialise = function initialise (transforms) {
|
2408 | if (transforms.moduleExport)
|
2409 | { CompileError.missingTransform("export", "moduleExport", this); }
|
2410 | Node.prototype.initialise.call(this, transforms);
|
2411 | };
|
2412 |
|
2413 | return ExportDefaultDeclaration;
|
2414 | }(Node));
|
2415 |
|
2416 | var ExportNamedDeclaration = (function (Node) {
|
2417 | function ExportNamedDeclaration () {
|
2418 | Node.apply(this, arguments);
|
2419 | }
|
2420 |
|
2421 | if ( Node ) ExportNamedDeclaration.__proto__ = Node;
|
2422 | ExportNamedDeclaration.prototype = Object.create( Node && Node.prototype );
|
2423 | ExportNamedDeclaration.prototype.constructor = ExportNamedDeclaration;
|
2424 |
|
2425 | ExportNamedDeclaration.prototype.initialise = function initialise (transforms) {
|
2426 | if (transforms.moduleExport)
|
2427 | { CompileError.missingTransform("export", "moduleExport", this); }
|
2428 | Node.prototype.initialise.call(this, transforms);
|
2429 | };
|
2430 |
|
2431 | return ExportNamedDeclaration;
|
2432 | }(Node));
|
2433 |
|
2434 | var LoopStatement = (function (Node) {
|
2435 | function LoopStatement () {
|
2436 | Node.apply(this, arguments);
|
2437 | }
|
2438 |
|
2439 | if ( Node ) LoopStatement.__proto__ = Node;
|
2440 | LoopStatement.prototype = Object.create( Node && Node.prototype );
|
2441 | LoopStatement.prototype.constructor = LoopStatement;
|
2442 |
|
2443 | LoopStatement.prototype.findScope = function findScope (functionScope) {
|
2444 | return functionScope || !this.createdScope
|
2445 | ? this.parent.findScope(functionScope)
|
2446 | : this.body.scope;
|
2447 | };
|
2448 |
|
2449 | LoopStatement.prototype.initialise = function initialise (transforms) {
|
2450 | this.body.createScope();
|
2451 | this.createdScope = true;
|
2452 |
|
2453 |
|
2454 | this.reassigned = Object.create(null);
|
2455 | this.aliases = Object.create(null);
|
2456 |
|
2457 | this.thisRefs = [];
|
2458 |
|
2459 | Node.prototype.initialise.call(this, transforms);
|
2460 | if (this.scope) {
|
2461 | this.scope.consolidate();
|
2462 | }
|
2463 |
|
2464 | var declarations = Object.assign({}, this.body.scope.declarations);
|
2465 | if (this.scope) {
|
2466 | Object.assign(declarations, this.scope.declarations);
|
2467 | }
|
2468 |
|
2469 | if (transforms.letConst) {
|
2470 |
|
2471 |
|
2472 | var names = Object.keys(declarations);
|
2473 |
|
2474 | var i = names.length;
|
2475 | while (i--) {
|
2476 | var name = names[i];
|
2477 | var declaration = declarations[name];
|
2478 |
|
2479 | var j = declaration.instances.length;
|
2480 | while (j--) {
|
2481 | var instance = declaration.instances[j];
|
2482 | var nearestFunctionExpression = instance.findNearest(/Function/);
|
2483 |
|
2484 | if (
|
2485 | nearestFunctionExpression &&
|
2486 | nearestFunctionExpression.depth > this.depth
|
2487 | ) {
|
2488 | this.shouldRewriteAsFunction = true;
|
2489 | for (var i$1 = 0, list = this.thisRefs; i$1 < list.length; i$1 += 1) {
|
2490 | var node = list[i$1];
|
2491 |
|
2492 | node.alias = node.alias || node.findLexicalBoundary().getThisAlias();
|
2493 | }
|
2494 | break;
|
2495 | }
|
2496 | }
|
2497 |
|
2498 | if (this.shouldRewriteAsFunction) { break; }
|
2499 | }
|
2500 | }
|
2501 | };
|
2502 |
|
2503 | LoopStatement.prototype.transpile = function transpile (code, transforms) {
|
2504 | var needsBlock =
|
2505 | this.type != 'ForOfStatement' &&
|
2506 | (this.body.type !== 'BlockStatement' ||
|
2507 | (this.body.type === 'BlockStatement' && this.body.synthetic));
|
2508 |
|
2509 | if (this.shouldRewriteAsFunction) {
|
2510 | var i0 = this.getIndentation();
|
2511 | var i1 = i0 + code.getIndentString();
|
2512 |
|
2513 | var argString = this.args ? (" " + (this.args.join(', ')) + " ") : '';
|
2514 | var paramString = this.params ? (" " + (this.params.join(', ')) + " ") : '';
|
2515 |
|
2516 | var functionScope = this.findScope(true);
|
2517 | var loop = functionScope.createIdentifier('loop');
|
2518 |
|
2519 | var before =
|
2520 | "var " + loop + " = function (" + paramString + ") " +
|
2521 | (this.body.synthetic ? ("{\n" + i0 + (code.getIndentString())) : '');
|
2522 | var after = (this.body.synthetic ? ("\n" + i0 + "}") : '') + ";\n\n" + i0;
|
2523 |
|
2524 | code.prependRight(this.body.start, before);
|
2525 | code.appendLeft(this.body.end, after);
|
2526 | code.move(this.start, this.body.start, this.body.end);
|
2527 |
|
2528 | if (this.canBreak || this.canReturn) {
|
2529 | var returned = functionScope.createIdentifier('returned');
|
2530 |
|
2531 | var insert = "{\n" + i1 + "var " + returned + " = " + loop + "(" + argString + ");\n";
|
2532 | if (this.canBreak)
|
2533 | { insert += "\n" + i1 + "if ( " + returned + " === 'break' ) break;"; }
|
2534 | if (this.canReturn)
|
2535 | { insert += "\n" + i1 + "if ( " + returned + " ) return " + returned + ".v;"; }
|
2536 | insert += "\n" + i0 + "}";
|
2537 |
|
2538 | code.prependRight(this.body.end, insert);
|
2539 | } else {
|
2540 | var callExpression = loop + "(" + argString + ");";
|
2541 |
|
2542 | if (this.type === 'DoWhileStatement') {
|
2543 | code.overwrite(
|
2544 | this.start,
|
2545 | this.body.start,
|
2546 | ("do {\n" + i1 + callExpression + "\n" + i0 + "}")
|
2547 | );
|
2548 | } else {
|
2549 | code.prependRight(this.body.end, callExpression);
|
2550 | }
|
2551 | }
|
2552 | } else if (needsBlock) {
|
2553 | code.appendLeft(this.body.start, '{ ');
|
2554 | code.prependRight(this.body.end, ' }');
|
2555 | }
|
2556 |
|
2557 | Node.prototype.transpile.call(this, code, transforms);
|
2558 | };
|
2559 |
|
2560 | return LoopStatement;
|
2561 | }(Node));
|
2562 |
|
2563 | var ForStatement = (function (LoopStatement) {
|
2564 | function ForStatement () {
|
2565 | LoopStatement.apply(this, arguments);
|
2566 | }
|
2567 |
|
2568 | if ( LoopStatement ) ForStatement.__proto__ = LoopStatement;
|
2569 | ForStatement.prototype = Object.create( LoopStatement && LoopStatement.prototype );
|
2570 | ForStatement.prototype.constructor = ForStatement;
|
2571 |
|
2572 | ForStatement.prototype.initialise = function initialise (transforms) {
|
2573 | var this$1 = this;
|
2574 |
|
2575 | this.createdDeclarations = [];
|
2576 |
|
2577 | this.scope = new Scope({
|
2578 | block: true,
|
2579 | parent: this.parent.findScope(false),
|
2580 | declare: function (id) { return this$1.createdDeclarations.push(id); }
|
2581 | });
|
2582 |
|
2583 | LoopStatement.prototype.initialise.call(this, transforms);
|
2584 | };
|
2585 |
|
2586 | ForStatement.prototype.findScope = function findScope (functionScope) {
|
2587 | return functionScope
|
2588 | ? this.parent.findScope(functionScope)
|
2589 | : this.scope;
|
2590 | };
|
2591 |
|
2592 | ForStatement.prototype.transpile = function transpile (code, transforms) {
|
2593 | var this$1 = this;
|
2594 |
|
2595 | var i1 = this.getIndentation() + code.getIndentString();
|
2596 |
|
2597 | if (this.shouldRewriteAsFunction) {
|
2598 |
|
2599 | var names = this.init && this.init.type === 'VariableDeclaration'
|
2600 | ? this.init.declarations.map(function (declarator) { return extractNames(declarator.id); })
|
2601 | : [];
|
2602 |
|
2603 | var aliases = this.aliases;
|
2604 |
|
2605 | this.args = names.map(
|
2606 | function (name) { return (name in this$1.aliases ? this$1.aliases[name].outer : name); }
|
2607 | );
|
2608 | this.params = names.map(
|
2609 | function (name) { return (name in this$1.aliases ? this$1.aliases[name].inner : name); }
|
2610 | );
|
2611 |
|
2612 | var updates = Object.keys(this.reassigned).map(
|
2613 | function (name) { return ((aliases[name].outer) + " = " + (aliases[name].inner) + ";"); }
|
2614 | );
|
2615 |
|
2616 | if (updates.length) {
|
2617 | if (this.body.synthetic) {
|
2618 | code.appendLeft(this.body.body[0].end, ("; " + (updates.join(" "))));
|
2619 | } else {
|
2620 | var lastStatement = this.body.body[this.body.body.length - 1];
|
2621 | code.appendLeft(
|
2622 | lastStatement.end,
|
2623 | ("\n\n" + i1 + (updates.join(("\n" + i1))))
|
2624 | );
|
2625 | }
|
2626 | }
|
2627 | }
|
2628 |
|
2629 | LoopStatement.prototype.transpile.call(this, code, transforms);
|
2630 | };
|
2631 |
|
2632 | return ForStatement;
|
2633 | }(LoopStatement));
|
2634 |
|
2635 | var ForInStatement = (function (LoopStatement) {
|
2636 | function ForInStatement () {
|
2637 | LoopStatement.apply(this, arguments);
|
2638 | }
|
2639 |
|
2640 | if ( LoopStatement ) ForInStatement.__proto__ = LoopStatement;
|
2641 | ForInStatement.prototype = Object.create( LoopStatement && LoopStatement.prototype );
|
2642 | ForInStatement.prototype.constructor = ForInStatement;
|
2643 |
|
2644 | ForInStatement.prototype.initialise = function initialise (transforms) {
|
2645 | var this$1 = this;
|
2646 |
|
2647 | this.createdDeclarations = [];
|
2648 |
|
2649 | this.scope = new Scope({
|
2650 | block: true,
|
2651 | parent: this.parent.findScope(false),
|
2652 | declare: function (id) { return this$1.createdDeclarations.push(id); }
|
2653 | });
|
2654 |
|
2655 | LoopStatement.prototype.initialise.call(this, transforms);
|
2656 | };
|
2657 |
|
2658 | ForInStatement.prototype.findScope = function findScope (functionScope) {
|
2659 | return functionScope
|
2660 | ? this.parent.findScope(functionScope)
|
2661 | : this.scope;
|
2662 | };
|
2663 |
|
2664 | ForInStatement.prototype.transpile = function transpile (code, transforms) {
|
2665 | var this$1 = this;
|
2666 |
|
2667 | var hasDeclaration = this.left.type === 'VariableDeclaration';
|
2668 |
|
2669 | if (this.shouldRewriteAsFunction) {
|
2670 |
|
2671 | var names = hasDeclaration
|
2672 | ? this.left.declarations.map(function (declarator) { return extractNames(declarator.id); })
|
2673 | : [];
|
2674 |
|
2675 | this.args = names.map(
|
2676 | function (name) { return (name in this$1.aliases ? this$1.aliases[name].outer : name); }
|
2677 | );
|
2678 | this.params = names.map(
|
2679 | function (name) { return (name in this$1.aliases ? this$1.aliases[name].inner : name); }
|
2680 | );
|
2681 | }
|
2682 |
|
2683 | LoopStatement.prototype.transpile.call(this, code, transforms);
|
2684 |
|
2685 | var maybePattern = hasDeclaration ? this.left.declarations[0].id : this.left;
|
2686 | if (maybePattern.type !== 'Identifier' && maybePattern.type !== 'MemberExpression') {
|
2687 | this.destructurePattern(code, maybePattern, hasDeclaration);
|
2688 | }
|
2689 | };
|
2690 |
|
2691 | ForInStatement.prototype.destructurePattern = function destructurePattern (code, pattern, isDeclaration) {
|
2692 | var scope = this.findScope(true);
|
2693 | var i0 = this.getIndentation();
|
2694 | var i1 = i0 + code.getIndentString();
|
2695 |
|
2696 | var ref = scope.createIdentifier('ref');
|
2697 |
|
2698 | var bodyStart = this.body.body.length ? this.body.body[0].start : this.body.start + 1;
|
2699 |
|
2700 | code.move(pattern.start, pattern.end, bodyStart);
|
2701 |
|
2702 | code.prependRight(pattern.end, isDeclaration ? ref : ("var " + ref));
|
2703 |
|
2704 | var statementGenerators = [];
|
2705 | destructure(
|
2706 | code,
|
2707 | function (id) { return scope.createIdentifier(id); },
|
2708 | function (ref) {
|
2709 | var name = ref.name;
|
2710 |
|
2711 | return scope.resolveName(name);
|
2712 | },
|
2713 | pattern,
|
2714 | ref,
|
2715 | false,
|
2716 | statementGenerators
|
2717 | );
|
2718 |
|
2719 | var suffix = ";\n" + i1;
|
2720 | statementGenerators.forEach(function (fn, i) {
|
2721 | if (i === statementGenerators.length - 1) {
|
2722 | suffix = ";\n\n" + i1;
|
2723 | }
|
2724 |
|
2725 | fn(bodyStart, '', suffix);
|
2726 | });
|
2727 | };
|
2728 |
|
2729 | return ForInStatement;
|
2730 | }(LoopStatement));
|
2731 |
|
2732 | var ForOfStatement = (function (LoopStatement) {
|
2733 | function ForOfStatement () {
|
2734 | LoopStatement.apply(this, arguments);
|
2735 | }
|
2736 |
|
2737 | if ( LoopStatement ) ForOfStatement.__proto__ = LoopStatement;
|
2738 | ForOfStatement.prototype = Object.create( LoopStatement && LoopStatement.prototype );
|
2739 | ForOfStatement.prototype.constructor = ForOfStatement;
|
2740 |
|
2741 | ForOfStatement.prototype.initialise = function initialise (transforms) {
|
2742 | var this$1 = this;
|
2743 |
|
2744 | if (transforms.forOf && !transforms.dangerousForOf)
|
2745 | { CompileError.missingTransform("for-of statements", "forOf", this, "dangerousForOf"); }
|
2746 | if (this.await && transforms.asyncAwait)
|
2747 | { CompileError.missingTransform("for-await-of statements", "asyncAwait", this); }
|
2748 |
|
2749 | this.createdDeclarations = [];
|
2750 |
|
2751 | this.scope = new Scope({
|
2752 | block: true,
|
2753 | parent: this.parent.findScope(false),
|
2754 | declare: function (id) { return this$1.createdDeclarations.push(id); }
|
2755 | });
|
2756 |
|
2757 | LoopStatement.prototype.initialise.call(this, transforms);
|
2758 | };
|
2759 |
|
2760 | ForOfStatement.prototype.findScope = function findScope (functionScope) {
|
2761 | return functionScope
|
2762 | ? this.parent.findScope(functionScope)
|
2763 | : this.scope;
|
2764 | };
|
2765 |
|
2766 | ForOfStatement.prototype.transpile = function transpile (code, transforms) {
|
2767 | LoopStatement.prototype.transpile.call(this, code, transforms);
|
2768 | if (!transforms.dangerousForOf) { return; }
|
2769 |
|
2770 |
|
2771 | if (!this.body.body[0]) {
|
2772 | if (
|
2773 | this.left.type === 'VariableDeclaration' &&
|
2774 | this.left.kind === 'var'
|
2775 | ) {
|
2776 | code.remove(this.start, this.left.start);
|
2777 | code.appendLeft(this.left.end, ';');
|
2778 | code.remove(this.left.end, this.end);
|
2779 | } else {
|
2780 | code.remove(this.start, this.end);
|
2781 | }
|
2782 |
|
2783 | return;
|
2784 | }
|
2785 |
|
2786 | var scope = this.findScope(true);
|
2787 | var i0 = this.getIndentation();
|
2788 | var i1 = i0 + code.getIndentString();
|
2789 |
|
2790 | var key = scope.createIdentifier('i');
|
2791 | var list = scope.createIdentifier('list');
|
2792 |
|
2793 | if (this.body.synthetic) {
|
2794 | code.prependRight(this.left.start, ("{\n" + i1));
|
2795 | code.appendLeft(this.body.body[0].end, ("\n" + i0 + "}"));
|
2796 | }
|
2797 |
|
2798 | var bodyStart = this.body.body[0].start;
|
2799 |
|
2800 | code.remove(this.left.end, this.right.start);
|
2801 | code.move(this.left.start, this.left.end, bodyStart);
|
2802 |
|
2803 | code.prependRight(this.right.start, ("var " + key + " = 0, " + list + " = "));
|
2804 | code.appendLeft(this.right.end, ("; " + key + " < " + list + ".length; " + key + " += 1"));
|
2805 |
|
2806 | var isDeclaration = this.left.type === 'VariableDeclaration';
|
2807 | var maybeDestructuring = isDeclaration ? this.left.declarations[0].id : this.left;
|
2808 | if (maybeDestructuring.type !== 'Identifier') {
|
2809 | var statementGenerators = [];
|
2810 | var ref = scope.createIdentifier('ref');
|
2811 | destructure(
|
2812 | code,
|
2813 | function (id) { return scope.createIdentifier(id); },
|
2814 | function (ref) {
|
2815 | var name = ref.name;
|
2816 |
|
2817 | return scope.resolveName(name);
|
2818 | },
|
2819 | maybeDestructuring,
|
2820 | ref,
|
2821 | !isDeclaration,
|
2822 | statementGenerators
|
2823 | );
|
2824 |
|
2825 | var suffix = ";\n" + i1;
|
2826 | statementGenerators.forEach(function (fn, i) {
|
2827 | if (i === statementGenerators.length - 1) {
|
2828 | suffix = ";\n\n" + i1;
|
2829 | }
|
2830 |
|
2831 | fn(bodyStart, '', suffix);
|
2832 | });
|
2833 |
|
2834 | if (isDeclaration) {
|
2835 | code.appendLeft(this.left.start + this.left.kind.length + 1, ref);
|
2836 | code.appendLeft(this.left.end, (" = " + list + "[" + key + "];\n" + i1));
|
2837 | } else {
|
2838 | code.appendLeft(this.left.end, ("var " + ref + " = " + list + "[" + key + "];\n" + i1));
|
2839 | }
|
2840 | } else {
|
2841 | code.appendLeft(this.left.end, (" = " + list + "[" + key + "];\n\n" + i1));
|
2842 | }
|
2843 | };
|
2844 |
|
2845 | return ForOfStatement;
|
2846 | }(LoopStatement));
|
2847 |
|
2848 | var FunctionDeclaration = (function (Node) {
|
2849 | function FunctionDeclaration () {
|
2850 | Node.apply(this, arguments);
|
2851 | }
|
2852 |
|
2853 | if ( Node ) FunctionDeclaration.__proto__ = Node;
|
2854 | FunctionDeclaration.prototype = Object.create( Node && Node.prototype );
|
2855 | FunctionDeclaration.prototype.constructor = FunctionDeclaration;
|
2856 |
|
2857 | FunctionDeclaration.prototype.initialise = function initialise (transforms) {
|
2858 | if (this.generator && transforms.generator) {
|
2859 | CompileError.missingTransform("generators", "generator", this);
|
2860 | }
|
2861 | if (this.async && transforms.asyncAwait) {
|
2862 | CompileError.missingTransform("async functions", "asyncAwait", this);
|
2863 | }
|
2864 |
|
2865 | this.body.createScope();
|
2866 |
|
2867 | if (this.id) {
|
2868 | this.findScope(true).addDeclaration(this.id, 'function');
|
2869 | }
|
2870 | Node.prototype.initialise.call(this, transforms);
|
2871 | };
|
2872 |
|
2873 | FunctionDeclaration.prototype.transpile = function transpile (code, transforms) {
|
2874 | Node.prototype.transpile.call(this, code, transforms);
|
2875 | if (transforms.trailingFunctionCommas && this.params.length) {
|
2876 | removeTrailingComma(code, this.params[this.params.length - 1].end);
|
2877 | }
|
2878 | };
|
2879 |
|
2880 | return FunctionDeclaration;
|
2881 | }(Node));
|
2882 |
|
2883 | var FunctionExpression = (function (Node) {
|
2884 | function FunctionExpression () {
|
2885 | Node.apply(this, arguments);
|
2886 | }
|
2887 |
|
2888 | if ( Node ) FunctionExpression.__proto__ = Node;
|
2889 | FunctionExpression.prototype = Object.create( Node && Node.prototype );
|
2890 | FunctionExpression.prototype.constructor = FunctionExpression;
|
2891 |
|
2892 | FunctionExpression.prototype.initialise = function initialise (transforms) {
|
2893 | if (this.generator && transforms.generator) {
|
2894 | CompileError.missingTransform("generators", "generator", this);
|
2895 | }
|
2896 | if (this.async && transforms.asyncAwait) {
|
2897 | CompileError.missingTransform("async functions", "asyncAwait", this);
|
2898 | }
|
2899 |
|
2900 | this.body.createScope();
|
2901 |
|
2902 | if (this.id) {
|
2903 |
|
2904 | this.body.scope.addDeclaration(this.id, 'function');
|
2905 | }
|
2906 |
|
2907 | Node.prototype.initialise.call(this, transforms);
|
2908 |
|
2909 | var parent = this.parent;
|
2910 | var methodName;
|
2911 |
|
2912 | if (
|
2913 | transforms.conciseMethodProperty &&
|
2914 | parent.type === 'Property' &&
|
2915 | parent.kind === 'init' &&
|
2916 | parent.method &&
|
2917 | parent.key.type === 'Identifier'
|
2918 | ) {
|
2919 |
|
2920 | methodName = parent.key.name;
|
2921 | } else if (
|
2922 | transforms.classes &&
|
2923 | parent.type === 'MethodDefinition' &&
|
2924 | parent.kind === 'method' &&
|
2925 | parent.key.type === 'Identifier'
|
2926 | ) {
|
2927 |
|
2928 | methodName = parent.key.name;
|
2929 | } else if (this.id && this.id.type === 'Identifier') {
|
2930 |
|
2931 | methodName = this.id.alias || this.id.name;
|
2932 | }
|
2933 |
|
2934 | if (methodName) {
|
2935 | for (var i$1 = 0, list$1 = this.params; i$1 < list$1.length; i$1 += 1) {
|
2936 | var param = list$1[i$1];
|
2937 |
|
2938 | if (param.type === 'Identifier' && methodName === param.name) {
|
2939 |
|
2940 |
|
2941 |
|
2942 |
|
2943 | var scope = this.body.scope;
|
2944 | var declaration = scope.declarations[methodName];
|
2945 |
|
2946 | var alias = scope.createIdentifier(methodName);
|
2947 | param.alias = alias;
|
2948 |
|
2949 | for (var i = 0, list = declaration.instances; i < list.length; i += 1) {
|
2950 | var identifier = list[i];
|
2951 |
|
2952 | identifier.alias = alias;
|
2953 | }
|
2954 |
|
2955 | break;
|
2956 | }
|
2957 | }
|
2958 | }
|
2959 | };
|
2960 |
|
2961 | FunctionExpression.prototype.transpile = function transpile (code, transforms) {
|
2962 | Node.prototype.transpile.call(this, code, transforms);
|
2963 | if (transforms.trailingFunctionCommas && this.params.length) {
|
2964 | removeTrailingComma(code, this.params[this.params.length - 1].end);
|
2965 | }
|
2966 | };
|
2967 |
|
2968 | return FunctionExpression;
|
2969 | }(Node));
|
2970 |
|
2971 | function isReference(node, parent) {
|
2972 | if (node.type === 'MemberExpression') {
|
2973 | return !node.computed && isReference(node.object, node);
|
2974 | }
|
2975 |
|
2976 | if (node.type === 'Identifier') {
|
2977 |
|
2978 |
|
2979 |
|
2980 | if (!parent) { return true; }
|
2981 |
|
2982 | if (/(Function|Class)Expression/.test(parent.type)) { return false; }
|
2983 |
|
2984 | if (parent.type === 'VariableDeclarator') { return node === parent.init; }
|
2985 |
|
2986 |
|
2987 | if (
|
2988 | parent.type === 'MemberExpression' ||
|
2989 | parent.type === 'MethodDefinition'
|
2990 | ) {
|
2991 | return parent.computed || node === parent.object;
|
2992 | }
|
2993 |
|
2994 | if (parent.type === 'ArrayPattern') { return false; }
|
2995 |
|
2996 |
|
2997 | if (parent.type === 'Property') {
|
2998 | if (parent.parent.type === 'ObjectPattern') { return false; }
|
2999 | return parent.computed || node === parent.value;
|
3000 | }
|
3001 |
|
3002 |
|
3003 | if (parent.type === 'MethodDefinition') { return false; }
|
3004 |
|
3005 |
|
3006 | if (parent.type === 'ExportSpecifier' && node !== parent.local)
|
3007 | { return false; }
|
3008 |
|
3009 | return true;
|
3010 | }
|
3011 | }
|
3012 |
|
3013 | var Identifier = (function (Node) {
|
3014 | function Identifier () {
|
3015 | Node.apply(this, arguments);
|
3016 | }
|
3017 |
|
3018 | if ( Node ) Identifier.__proto__ = Node;
|
3019 | Identifier.prototype = Object.create( Node && Node.prototype );
|
3020 | Identifier.prototype.constructor = Identifier;
|
3021 |
|
3022 | Identifier.prototype.findScope = function findScope (functionScope) {
|
3023 | if (this.parent.params && ~this.parent.params.indexOf(this)) {
|
3024 | return this.parent.body.scope;
|
3025 | }
|
3026 |
|
3027 | if (this.parent.type === 'FunctionExpression' && this === this.parent.id) {
|
3028 | return this.parent.body.scope;
|
3029 | }
|
3030 |
|
3031 | return this.parent.findScope(functionScope);
|
3032 | };
|
3033 |
|
3034 | Identifier.prototype.initialise = function initialise (transforms) {
|
3035 | if (this.isLabel()) {
|
3036 | return;
|
3037 | }
|
3038 |
|
3039 | if (isReference(this, this.parent)) {
|
3040 | if (
|
3041 | transforms.arrow &&
|
3042 | this.name === 'arguments' &&
|
3043 | !this.findScope(false).contains(this.name)
|
3044 | ) {
|
3045 | var lexicalBoundary = this.findLexicalBoundary();
|
3046 | var arrowFunction = this.findNearest('ArrowFunctionExpression');
|
3047 | var loop = this.findNearest(loopStatement);
|
3048 |
|
3049 | if (arrowFunction && arrowFunction.depth > lexicalBoundary.depth) {
|
3050 | this.alias = lexicalBoundary.getArgumentsAlias();
|
3051 | }
|
3052 |
|
3053 | if (
|
3054 | loop &&
|
3055 | loop.body.contains(this) &&
|
3056 | loop.depth > lexicalBoundary.depth
|
3057 | ) {
|
3058 | this.alias = lexicalBoundary.getArgumentsAlias();
|
3059 | }
|
3060 | }
|
3061 |
|
3062 | this.findScope(false).addReference(this);
|
3063 | }
|
3064 | };
|
3065 |
|
3066 | Identifier.prototype.isLabel = function isLabel () {
|
3067 | switch (this.parent.type) {
|
3068 | case 'BreakStatement': return true;
|
3069 | case 'ContinueStatement': return true;
|
3070 | case 'LabeledStatement': return true;
|
3071 | default: return false;
|
3072 | }
|
3073 | };
|
3074 |
|
3075 | Identifier.prototype.transpile = function transpile (code) {
|
3076 | if (this.alias) {
|
3077 | code.overwrite(this.start, this.end, this.alias, {
|
3078 | storeName: true,
|
3079 | contentOnly: true
|
3080 | });
|
3081 | }
|
3082 | };
|
3083 |
|
3084 | return Identifier;
|
3085 | }(Node));
|
3086 |
|
3087 | var IfStatement = (function (Node) {
|
3088 | function IfStatement () {
|
3089 | Node.apply(this, arguments);
|
3090 | }
|
3091 |
|
3092 | if ( Node ) IfStatement.__proto__ = Node;
|
3093 | IfStatement.prototype = Object.create( Node && Node.prototype );
|
3094 | IfStatement.prototype.constructor = IfStatement;
|
3095 |
|
3096 | IfStatement.prototype.initialise = function initialise (transforms) {
|
3097 | Node.prototype.initialise.call(this, transforms);
|
3098 | };
|
3099 |
|
3100 | IfStatement.prototype.transpile = function transpile (code, transforms) {
|
3101 | if (
|
3102 | this.consequent.type !== 'BlockStatement' ||
|
3103 | (this.consequent.type === 'BlockStatement' && this.consequent.synthetic)
|
3104 | ) {
|
3105 | code.appendLeft(this.consequent.start, '{ ');
|
3106 | code.prependRight(this.consequent.end, ' }');
|
3107 | }
|
3108 |
|
3109 | if (
|
3110 | this.alternate &&
|
3111 | this.alternate.type !== 'IfStatement' &&
|
3112 | (this.alternate.type !== 'BlockStatement' ||
|
3113 | (this.alternate.type === 'BlockStatement' && this.alternate.synthetic))
|
3114 | ) {
|
3115 | code.appendLeft(this.alternate.start, '{ ');
|
3116 | code.prependRight(this.alternate.end, ' }');
|
3117 | }
|
3118 |
|
3119 | Node.prototype.transpile.call(this, code, transforms);
|
3120 | };
|
3121 |
|
3122 | return IfStatement;
|
3123 | }(Node));
|
3124 |
|
3125 | var Import = (function (Node) {
|
3126 | function Import () {
|
3127 | Node.apply(this, arguments);
|
3128 | }
|
3129 |
|
3130 | if ( Node ) Import.__proto__ = Node;
|
3131 | Import.prototype = Object.create( Node && Node.prototype );
|
3132 | Import.prototype.constructor = Import;
|
3133 |
|
3134 | Import.prototype.initialise = function initialise (transforms) {
|
3135 | if (transforms.moduleImport) {
|
3136 | CompileError.missingTransform("dynamic import expressions", "moduleImport", this);
|
3137 | }
|
3138 | Node.prototype.initialise.call(this, transforms);
|
3139 | };
|
3140 |
|
3141 | return Import;
|
3142 | }(Node));
|
3143 |
|
3144 | var ImportDeclaration = (function (Node) {
|
3145 | function ImportDeclaration () {
|
3146 | Node.apply(this, arguments);
|
3147 | }
|
3148 |
|
3149 | if ( Node ) ImportDeclaration.__proto__ = Node;
|
3150 | ImportDeclaration.prototype = Object.create( Node && Node.prototype );
|
3151 | ImportDeclaration.prototype.constructor = ImportDeclaration;
|
3152 |
|
3153 | ImportDeclaration.prototype.initialise = function initialise (transforms) {
|
3154 | if (transforms.moduleImport)
|
3155 | { CompileError.missingTransform("import", "moduleImport", this); }
|
3156 | Node.prototype.initialise.call(this, transforms);
|
3157 | };
|
3158 |
|
3159 | return ImportDeclaration;
|
3160 | }(Node));
|
3161 |
|
3162 | var ImportDefaultSpecifier = (function (Node) {
|
3163 | function ImportDefaultSpecifier () {
|
3164 | Node.apply(this, arguments);
|
3165 | }
|
3166 |
|
3167 | if ( Node ) ImportDefaultSpecifier.__proto__ = Node;
|
3168 | ImportDefaultSpecifier.prototype = Object.create( Node && Node.prototype );
|
3169 | ImportDefaultSpecifier.prototype.constructor = ImportDefaultSpecifier;
|
3170 |
|
3171 | ImportDefaultSpecifier.prototype.initialise = function initialise (transforms) {
|
3172 | this.findScope(true).addDeclaration(this.local, 'import');
|
3173 | Node.prototype.initialise.call(this, transforms);
|
3174 | };
|
3175 |
|
3176 | return ImportDefaultSpecifier;
|
3177 | }(Node));
|
3178 |
|
3179 | var ImportSpecifier = (function (Node) {
|
3180 | function ImportSpecifier () {
|
3181 | Node.apply(this, arguments);
|
3182 | }
|
3183 |
|
3184 | if ( Node ) ImportSpecifier.__proto__ = Node;
|
3185 | ImportSpecifier.prototype = Object.create( Node && Node.prototype );
|
3186 | ImportSpecifier.prototype.constructor = ImportSpecifier;
|
3187 |
|
3188 | ImportSpecifier.prototype.initialise = function initialise (transforms) {
|
3189 | this.findScope(true).addDeclaration(this.local, 'import');
|
3190 | Node.prototype.initialise.call(this, transforms);
|
3191 | };
|
3192 |
|
3193 | return ImportSpecifier;
|
3194 | }(Node));
|
3195 |
|
3196 | var hasDashes = function (val) { return /-/.test(val); };
|
3197 |
|
3198 | var formatKey = function (key) { return (hasDashes(key) ? ("'" + key + "'") : key); };
|
3199 |
|
3200 | var formatVal = function (val) { return (val ? '' : 'true'); };
|
3201 |
|
3202 | var JSXAttribute = (function (Node) {
|
3203 | function JSXAttribute () {
|
3204 | Node.apply(this, arguments);
|
3205 | }
|
3206 |
|
3207 | if ( Node ) JSXAttribute.__proto__ = Node;
|
3208 | JSXAttribute.prototype = Object.create( Node && Node.prototype );
|
3209 | JSXAttribute.prototype.constructor = JSXAttribute;
|
3210 |
|
3211 | JSXAttribute.prototype.transpile = function transpile (code, transforms) {
|
3212 | var ref = this.name;
|
3213 | var start = ref.start;
|
3214 | var name = ref.name;
|
3215 |
|
3216 |
|
3217 | var end = this.value ? this.value.start : this.name.end;
|
3218 |
|
3219 | code.overwrite(start, end, ((formatKey(name)) + ": " + (formatVal(this.value))));
|
3220 |
|
3221 | Node.prototype.transpile.call(this, code, transforms);
|
3222 | };
|
3223 |
|
3224 | return JSXAttribute;
|
3225 | }(Node));
|
3226 |
|
3227 | function containsNewLine(node) {
|
3228 | return (
|
3229 | node.type === 'JSXText' && !/\S/.test(node.value) && /\n/.test(node.value)
|
3230 | );
|
3231 | }
|
3232 |
|
3233 | var JSXClosingElement = (function (Node) {
|
3234 | function JSXClosingElement () {
|
3235 | Node.apply(this, arguments);
|
3236 | }
|
3237 |
|
3238 | if ( Node ) JSXClosingElement.__proto__ = Node;
|
3239 | JSXClosingElement.prototype = Object.create( Node && Node.prototype );
|
3240 | JSXClosingElement.prototype.constructor = JSXClosingElement;
|
3241 |
|
3242 | JSXClosingElement.prototype.transpile = function transpile (code) {
|
3243 | var spaceBeforeParen = true;
|
3244 |
|
3245 | var lastChild = this.parent.children[this.parent.children.length - 1];
|
3246 |
|
3247 |
|
3248 |
|
3249 |
|
3250 | if (
|
3251 | (lastChild && containsNewLine(lastChild)) ||
|
3252 | this.parent.openingElement.attributes.length
|
3253 | ) {
|
3254 | spaceBeforeParen = false;
|
3255 | }
|
3256 |
|
3257 | code.overwrite(this.start, this.end, spaceBeforeParen ? ' )' : ')');
|
3258 | };
|
3259 |
|
3260 | return JSXClosingElement;
|
3261 | }(Node));
|
3262 |
|
3263 | function containsNewLine$1(node) {
|
3264 | return (
|
3265 | node.type === 'JSXText' && !/\S/.test(node.value) && /\n/.test(node.value)
|
3266 | );
|
3267 | }
|
3268 |
|
3269 | var JSXClosingFragment = (function (Node) {
|
3270 | function JSXClosingFragment () {
|
3271 | Node.apply(this, arguments);
|
3272 | }
|
3273 |
|
3274 | if ( Node ) JSXClosingFragment.__proto__ = Node;
|
3275 | JSXClosingFragment.prototype = Object.create( Node && Node.prototype );
|
3276 | JSXClosingFragment.prototype.constructor = JSXClosingFragment;
|
3277 |
|
3278 | JSXClosingFragment.prototype.transpile = function transpile (code) {
|
3279 | var spaceBeforeParen = true;
|
3280 |
|
3281 | var lastChild = this.parent.children[this.parent.children.length - 1];
|
3282 |
|
3283 |
|
3284 | if (lastChild && containsNewLine$1(lastChild)) {
|
3285 | spaceBeforeParen = false;
|
3286 | }
|
3287 |
|
3288 | code.overwrite(this.start, this.end, spaceBeforeParen ? ' )' : ')');
|
3289 | };
|
3290 |
|
3291 | return JSXClosingFragment;
|
3292 | }(Node));
|
3293 |
|
3294 | function normalise(str, removeTrailingWhitespace) {
|
3295 |
|
3296 | if (removeTrailingWhitespace && /\n/.test(str)) {
|
3297 | str = str.replace(/[ \f\n\r\t\v]+$/, '');
|
3298 | }
|
3299 |
|
3300 | str = str
|
3301 | .replace(/^\n\r?[ \f\n\r\t\v]+/, '')
|
3302 | .replace(/[ \f\n\r\t\v]*\n\r?[ \f\n\r\t\v]*/gm, ' ');
|
3303 |
|
3304 |
|
3305 | return JSON.stringify(str);
|
3306 | }
|
3307 |
|
3308 | var JSXElement = (function (Node) {
|
3309 | function JSXElement () {
|
3310 | Node.apply(this, arguments);
|
3311 | }
|
3312 |
|
3313 | if ( Node ) JSXElement.__proto__ = Node;
|
3314 | JSXElement.prototype = Object.create( Node && Node.prototype );
|
3315 | JSXElement.prototype.constructor = JSXElement;
|
3316 |
|
3317 | JSXElement.prototype.transpile = function transpile (code, transforms) {
|
3318 | Node.prototype.transpile.call(this, code, transforms);
|
3319 |
|
3320 | var children = this.children.filter(function (child) {
|
3321 | if (child.type !== 'JSXText') { return true; }
|
3322 |
|
3323 |
|
3324 | return /[^ \f\n\r\t\v]/.test(child.raw) || !/\n/.test(child.raw);
|
3325 | });
|
3326 |
|
3327 | if (children.length) {
|
3328 | var c = (this.openingElement || this.openingFragment).end;
|
3329 |
|
3330 | var i;
|
3331 | for (i = 0; i < children.length; i += 1) {
|
3332 | var child = children[i];
|
3333 |
|
3334 | if (
|
3335 | child.type === 'JSXExpressionContainer' &&
|
3336 | child.expression.type === 'JSXEmptyExpression'
|
3337 | ) ; else {
|
3338 | var tail =
|
3339 | code.original[c] === '\n' && child.type !== 'JSXText' ? '' : ' ';
|
3340 | code.appendLeft(c, ("," + tail));
|
3341 | }
|
3342 |
|
3343 | if (child.type === 'JSXText') {
|
3344 | var str = normalise(child.value, i === children.length - 1);
|
3345 | code.overwrite(child.start, child.end, str);
|
3346 | }
|
3347 |
|
3348 | c = child.end;
|
3349 | }
|
3350 | }
|
3351 | };
|
3352 |
|
3353 | return JSXElement;
|
3354 | }(Node));
|
3355 |
|
3356 | var JSXExpressionContainer = (function (Node) {
|
3357 | function JSXExpressionContainer () {
|
3358 | Node.apply(this, arguments);
|
3359 | }
|
3360 |
|
3361 | if ( Node ) JSXExpressionContainer.__proto__ = Node;
|
3362 | JSXExpressionContainer.prototype = Object.create( Node && Node.prototype );
|
3363 | JSXExpressionContainer.prototype.constructor = JSXExpressionContainer;
|
3364 |
|
3365 | JSXExpressionContainer.prototype.transpile = function transpile (code, transforms) {
|
3366 | code.remove(this.start, this.expression.start);
|
3367 | code.remove(this.expression.end, this.end);
|
3368 |
|
3369 | Node.prototype.transpile.call(this, code, transforms);
|
3370 | };
|
3371 |
|
3372 | return JSXExpressionContainer;
|
3373 | }(Node));
|
3374 |
|
3375 | var JSXFragment = (function (JSXElement) {
|
3376 | function JSXFragment () {
|
3377 | JSXElement.apply(this, arguments);
|
3378 | }if ( JSXElement ) JSXFragment.__proto__ = JSXElement;
|
3379 | JSXFragment.prototype = Object.create( JSXElement && JSXElement.prototype );
|
3380 | JSXFragment.prototype.constructor = JSXFragment;
|
3381 |
|
3382 |
|
3383 |
|
3384 | return JSXFragment;
|
3385 | }(JSXElement));
|
3386 |
|
3387 | var JSXOpeningElement = (function (Node) {
|
3388 | function JSXOpeningElement () {
|
3389 | Node.apply(this, arguments);
|
3390 | }
|
3391 |
|
3392 | if ( Node ) JSXOpeningElement.__proto__ = Node;
|
3393 | JSXOpeningElement.prototype = Object.create( Node && Node.prototype );
|
3394 | JSXOpeningElement.prototype.constructor = JSXOpeningElement;
|
3395 |
|
3396 | JSXOpeningElement.prototype.transpile = function transpile (code, transforms) {
|
3397 | Node.prototype.transpile.call(this, code, transforms);
|
3398 |
|
3399 | code.overwrite(this.start, this.name.start, ((this.program.jsx) + "( "));
|
3400 |
|
3401 | var html =
|
3402 | this.name.type === 'JSXIdentifier' &&
|
3403 | this.name.name[0] === this.name.name[0].toLowerCase();
|
3404 | if (html) { code.prependRight(this.name.start, "'"); }
|
3405 |
|
3406 | var len = this.attributes.length;
|
3407 | var c = this.name.end;
|
3408 |
|
3409 | if (len) {
|
3410 | var i;
|
3411 |
|
3412 | var hasSpread = false;
|
3413 | for (i = 0; i < len; i += 1) {
|
3414 | if (this.attributes[i].type === 'JSXSpreadAttribute') {
|
3415 | hasSpread = true;
|
3416 | break;
|
3417 | }
|
3418 | }
|
3419 |
|
3420 | c = this.attributes[0].end;
|
3421 |
|
3422 | for (i = 0; i < len; i += 1) {
|
3423 | var attr = this.attributes[i];
|
3424 |
|
3425 | if (i > 0) {
|
3426 | if (attr.start === c) { code.prependRight(c, ', '); }
|
3427 | else { code.overwrite(c, attr.start, ', '); }
|
3428 | }
|
3429 |
|
3430 | if (hasSpread && attr.type !== 'JSXSpreadAttribute') {
|
3431 | var lastAttr = this.attributes[i - 1];
|
3432 | var nextAttr = this.attributes[i + 1];
|
3433 |
|
3434 | if (!lastAttr || lastAttr.type === 'JSXSpreadAttribute') {
|
3435 | code.prependRight(attr.start, '{ ');
|
3436 | }
|
3437 |
|
3438 | if (!nextAttr || nextAttr.type === 'JSXSpreadAttribute') {
|
3439 | code.appendLeft(attr.end, ' }');
|
3440 | }
|
3441 | }
|
3442 |
|
3443 | c = attr.end;
|
3444 | }
|
3445 |
|
3446 | var after;
|
3447 | var before;
|
3448 | if (hasSpread) {
|
3449 | if (len === 1) {
|
3450 | before = html ? "'," : ',';
|
3451 | } else {
|
3452 | if (!this.program.options.objectAssign) {
|
3453 | throw new CompileError(
|
3454 | "Mixed JSX attributes ending in spread requires specified objectAssign option with 'Object.assign' or polyfill helper.",
|
3455 | this
|
3456 | );
|
3457 | }
|
3458 | before = html
|
3459 | ? ("', " + (this.program.options.objectAssign) + "({},")
|
3460 | : (", " + (this.program.options.objectAssign) + "({},");
|
3461 | after = ')';
|
3462 | }
|
3463 | } else {
|
3464 | before = html ? "', {" : ', {';
|
3465 | after = ' }';
|
3466 | }
|
3467 |
|
3468 | code.prependRight(this.name.end, before);
|
3469 |
|
3470 | if (after) {
|
3471 | code.appendLeft(this.attributes[len - 1].end, after);
|
3472 | }
|
3473 | } else {
|
3474 | code.appendLeft(this.name.end, html ? "', null" : ", null");
|
3475 | c = this.name.end;
|
3476 | }
|
3477 |
|
3478 | if (this.selfClosing) {
|
3479 | code.overwrite(c, this.end, this.attributes.length ? ")" : " )");
|
3480 | } else {
|
3481 | code.remove(c, this.end);
|
3482 | }
|
3483 | };
|
3484 |
|
3485 | return JSXOpeningElement;
|
3486 | }(Node));
|
3487 |
|
3488 | var JSXOpeningFragment = (function (Node) {
|
3489 | function JSXOpeningFragment () {
|
3490 | Node.apply(this, arguments);
|
3491 | }
|
3492 |
|
3493 | if ( Node ) JSXOpeningFragment.__proto__ = Node;
|
3494 | JSXOpeningFragment.prototype = Object.create( Node && Node.prototype );
|
3495 | JSXOpeningFragment.prototype.constructor = JSXOpeningFragment;
|
3496 |
|
3497 | JSXOpeningFragment.prototype.transpile = function transpile (code) {
|
3498 | code.overwrite(this.start, this.end, ((this.program.jsx) + "( " + (this.program.jsxFragment) + ", null"));
|
3499 | };
|
3500 |
|
3501 | return JSXOpeningFragment;
|
3502 | }(Node));
|
3503 |
|
3504 | var JSXSpreadAttribute = (function (Node) {
|
3505 | function JSXSpreadAttribute () {
|
3506 | Node.apply(this, arguments);
|
3507 | }
|
3508 |
|
3509 | if ( Node ) JSXSpreadAttribute.__proto__ = Node;
|
3510 | JSXSpreadAttribute.prototype = Object.create( Node && Node.prototype );
|
3511 | JSXSpreadAttribute.prototype.constructor = JSXSpreadAttribute;
|
3512 |
|
3513 | JSXSpreadAttribute.prototype.transpile = function transpile (code, transforms) {
|
3514 | code.remove(this.start, this.argument.start);
|
3515 | code.remove(this.argument.end, this.end);
|
3516 |
|
3517 | Node.prototype.transpile.call(this, code, transforms);
|
3518 | };
|
3519 |
|
3520 | return JSXSpreadAttribute;
|
3521 | }(Node));
|
3522 |
|
3523 | var nonAsciiLsOrPs = /[\u2028-\u2029]/g;
|
3524 |
|
3525 | var Literal = (function (Node) {
|
3526 | function Literal () {
|
3527 | Node.apply(this, arguments);
|
3528 | }
|
3529 |
|
3530 | if ( Node ) Literal.__proto__ = Node;
|
3531 | Literal.prototype = Object.create( Node && Node.prototype );
|
3532 | Literal.prototype.constructor = Literal;
|
3533 |
|
3534 | Literal.prototype.initialise = function initialise () {
|
3535 | if (typeof this.value === 'string') {
|
3536 | this.program.indentExclusionElements.push(this);
|
3537 | }
|
3538 | };
|
3539 |
|
3540 | Literal.prototype.transpile = function transpile (code, transforms) {
|
3541 | if (transforms.numericLiteral) {
|
3542 | if (this.raw.match(/^0[bo]/i)) {
|
3543 | code.overwrite(this.start, this.end, String(this.value), {
|
3544 | storeName: true,
|
3545 | contentOnly: true
|
3546 | });
|
3547 | }
|
3548 | }
|
3549 |
|
3550 | if (this.regex) {
|
3551 | var ref = this.regex;
|
3552 | var pattern = ref.pattern;
|
3553 | var flags = ref.flags;
|
3554 |
|
3555 | if (transforms.stickyRegExp && /y/.test(flags))
|
3556 | { CompileError.missingTransform('the regular expression sticky flag', 'stickyRegExp', this); }
|
3557 | if (transforms.unicodeRegExp && /u/.test(flags)) {
|
3558 | code.overwrite(
|
3559 | this.start,
|
3560 | this.end,
|
3561 | ("/" + (rewritePattern(pattern, flags)) + "/" + (flags.replace('u', ''))),
|
3562 | {
|
3563 | contentOnly: true
|
3564 | }
|
3565 | );
|
3566 | }
|
3567 | } else if (typeof this.value === "string" && this.value.match(nonAsciiLsOrPs)) {
|
3568 | code.overwrite(
|
3569 | this.start,
|
3570 | this.end,
|
3571 | this.raw.replace(nonAsciiLsOrPs, function (m) { return m == '\u2028' ? '\\u2028' : '\\u2029'; }),
|
3572 | {
|
3573 | contentOnly: true
|
3574 | }
|
3575 | );
|
3576 | }
|
3577 | };
|
3578 |
|
3579 | return Literal;
|
3580 | }(Node));
|
3581 |
|
3582 | var MemberExpression = (function (Node) {
|
3583 | function MemberExpression () {
|
3584 | Node.apply(this, arguments);
|
3585 | }
|
3586 |
|
3587 | if ( Node ) MemberExpression.__proto__ = Node;
|
3588 | MemberExpression.prototype = Object.create( Node && Node.prototype );
|
3589 | MemberExpression.prototype.constructor = MemberExpression;
|
3590 |
|
3591 | MemberExpression.prototype.transpile = function transpile (code, transforms) {
|
3592 | if (transforms.reservedProperties && reserved[this.property.name]) {
|
3593 | code.overwrite(this.object.end, this.property.start, "['");
|
3594 | code.appendLeft(this.property.end, "']");
|
3595 | }
|
3596 |
|
3597 | Node.prototype.transpile.call(this, code, transforms);
|
3598 | };
|
3599 |
|
3600 | return MemberExpression;
|
3601 | }(Node));
|
3602 |
|
3603 | var NewExpression = (function (Node) {
|
3604 | function NewExpression () {
|
3605 | Node.apply(this, arguments);
|
3606 | }
|
3607 |
|
3608 | if ( Node ) NewExpression.__proto__ = Node;
|
3609 | NewExpression.prototype = Object.create( Node && Node.prototype );
|
3610 | NewExpression.prototype.constructor = NewExpression;
|
3611 |
|
3612 | NewExpression.prototype.initialise = function initialise (transforms) {
|
3613 | if (transforms.spreadRest && this.arguments.length) {
|
3614 | var lexicalBoundary = this.findLexicalBoundary();
|
3615 |
|
3616 | var i = this.arguments.length;
|
3617 | while (i--) {
|
3618 | var arg = this.arguments[i];
|
3619 | if (arg.type === 'SpreadElement' && isArguments(arg.argument)) {
|
3620 | this.argumentsArrayAlias = lexicalBoundary.getArgumentsArrayAlias();
|
3621 | break;
|
3622 | }
|
3623 | }
|
3624 | }
|
3625 |
|
3626 | Node.prototype.initialise.call(this, transforms);
|
3627 | };
|
3628 |
|
3629 | NewExpression.prototype.transpile = function transpile (code, transforms) {
|
3630 | Node.prototype.transpile.call(this, code, transforms);
|
3631 |
|
3632 | if (transforms.spreadRest && this.arguments.length) {
|
3633 | inlineSpreads(code, this, this.arguments);
|
3634 |
|
3635 | }
|
3636 |
|
3637 | if (transforms.spreadRest && this.arguments.length) {
|
3638 | var firstArgument = this.arguments[0];
|
3639 | var isNew = true;
|
3640 | var hasSpreadElements = spread(
|
3641 | code,
|
3642 | this.arguments,
|
3643 | firstArgument.start,
|
3644 | this.argumentsArrayAlias,
|
3645 | isNew
|
3646 | );
|
3647 |
|
3648 | if (hasSpreadElements) {
|
3649 | code.prependRight(
|
3650 | this.start + 'new'.length,
|
3651 | ' (Function.prototype.bind.apply('
|
3652 | );
|
3653 | code.overwrite(
|
3654 | this.callee.end,
|
3655 | firstArgument.start,
|
3656 | ', [ null ].concat( '
|
3657 | );
|
3658 | code.appendLeft(this.end, ' ))');
|
3659 | }
|
3660 | }
|
3661 |
|
3662 | if (this.arguments.length) {
|
3663 | removeTrailingComma(code, this.arguments[this.arguments.length - 1].end);
|
3664 | }
|
3665 | };
|
3666 |
|
3667 | return NewExpression;
|
3668 | }(Node));
|
3669 |
|
3670 | var ObjectExpression = (function (Node) {
|
3671 | function ObjectExpression () {
|
3672 | Node.apply(this, arguments);
|
3673 | }
|
3674 |
|
3675 | if ( Node ) ObjectExpression.__proto__ = Node;
|
3676 | ObjectExpression.prototype = Object.create( Node && Node.prototype );
|
3677 | ObjectExpression.prototype.constructor = ObjectExpression;
|
3678 |
|
3679 | ObjectExpression.prototype.transpile = function transpile (code, transforms) {
|
3680 | var ref;
|
3681 |
|
3682 | Node.prototype.transpile.call(this, code, transforms);
|
3683 |
|
3684 | var firstPropertyStart = this.start + 1;
|
3685 | var spreadPropertyCount = 0;
|
3686 | var computedPropertyCount = 0;
|
3687 | var firstSpreadProperty = null;
|
3688 | var firstComputedProperty = null;
|
3689 |
|
3690 | for (var i = 0; i < this.properties.length; ++i) {
|
3691 | var prop = this.properties[i];
|
3692 | if (prop.type === 'SpreadElement') {
|
3693 |
|
3694 | var argument = prop.argument;
|
3695 | if (
|
3696 | argument.type === 'ObjectExpression' || (
|
3697 | argument.type === 'Literal' &&
|
3698 | typeof argument.value !== 'string'
|
3699 | )
|
3700 | ) {
|
3701 | if (argument.type === 'ObjectExpression' && argument.properties.length > 0) {
|
3702 |
|
3703 |
|
3704 | code.remove(prop.start, argument.properties[0].start);
|
3705 | code.remove(argument.properties[argument.properties.length - 1].end, prop.end);
|
3706 | (ref = this.properties).splice.apply(ref, [ i, 1 ].concat( argument.properties ));
|
3707 | i--;
|
3708 | } else {
|
3709 |
|
3710 |
|
3711 |
|
3712 | code.remove(prop.start, i === this.properties.length - 1
|
3713 | ? prop.end
|
3714 | : this.properties[i + 1].start);
|
3715 | this.properties.splice(i, 1);
|
3716 | i--;
|
3717 | }
|
3718 | } else {
|
3719 | spreadPropertyCount += 1;
|
3720 | if (firstSpreadProperty === null) { firstSpreadProperty = i; }
|
3721 | }
|
3722 | } else if (prop.computed && transforms.computedProperty) {
|
3723 | computedPropertyCount += 1;
|
3724 | if (firstComputedProperty === null) { firstComputedProperty = i; }
|
3725 | }
|
3726 | }
|
3727 |
|
3728 | if (spreadPropertyCount && !transforms.objectRestSpread && !(computedPropertyCount && transforms.computedProperty)) {
|
3729 | spreadPropertyCount = 0;
|
3730 | firstSpreadProperty = null;
|
3731 | } else if (spreadPropertyCount) {
|
3732 | if (!this.program.options.objectAssign) {
|
3733 | throw new CompileError(
|
3734 | "Object spread operator requires specified objectAssign option with 'Object.assign' or polyfill helper.",
|
3735 | this
|
3736 | );
|
3737 | }
|
3738 | var i$1 = this.properties.length;
|
3739 | while (i$1--) {
|
3740 | var prop$1 = this.properties[i$1];
|
3741 |
|
3742 |
|
3743 | if (prop$1.type === 'Property' && !computedPropertyCount) {
|
3744 | var lastProp = this.properties[i$1 - 1];
|
3745 | var nextProp = this.properties[i$1 + 1];
|
3746 |
|
3747 | if (!lastProp || lastProp.type !== 'Property') {
|
3748 | code.prependRight(prop$1.start, '{');
|
3749 | }
|
3750 |
|
3751 | if (!nextProp || nextProp.type !== 'Property') {
|
3752 | code.appendLeft(prop$1.end, '}');
|
3753 | }
|
3754 | }
|
3755 |
|
3756 |
|
3757 | if (prop$1.type === 'SpreadElement') {
|
3758 | code.remove(prop$1.start, prop$1.argument.start);
|
3759 | code.remove(prop$1.argument.end, prop$1.end);
|
3760 | }
|
3761 | }
|
3762 |
|
3763 |
|
3764 | firstPropertyStart = this.properties[0].start;
|
3765 | if (!computedPropertyCount) {
|
3766 | code.overwrite(
|
3767 | this.start,
|
3768 | firstPropertyStart,
|
3769 | ((this.program.options.objectAssign) + "({}, ")
|
3770 | );
|
3771 | code.overwrite(
|
3772 | this.properties[this.properties.length - 1].end,
|
3773 | this.end,
|
3774 | ')'
|
3775 | );
|
3776 | } else if (this.properties[0].type === 'SpreadElement') {
|
3777 | code.overwrite(
|
3778 | this.start,
|
3779 | firstPropertyStart,
|
3780 | ((this.program.options.objectAssign) + "({}, ")
|
3781 | );
|
3782 | code.remove(this.end - 1, this.end);
|
3783 | code.appendRight(this.end, ')');
|
3784 | } else {
|
3785 | code.prependLeft(this.start, ((this.program.options.objectAssign) + "("));
|
3786 | code.appendRight(this.end, ')');
|
3787 | }
|
3788 | }
|
3789 |
|
3790 | if (computedPropertyCount && transforms.computedProperty) {
|
3791 | var i0 = this.getIndentation();
|
3792 |
|
3793 | var isSimpleAssignment;
|
3794 | var name;
|
3795 |
|
3796 | if (
|
3797 | this.parent.type === 'VariableDeclarator' &&
|
3798 | this.parent.parent.declarations.length === 1 &&
|
3799 | this.parent.id.type === 'Identifier'
|
3800 | ) {
|
3801 | isSimpleAssignment = true;
|
3802 | name = this.parent.id.alias || this.parent.id.name;
|
3803 | } else if (
|
3804 | this.parent.type === 'AssignmentExpression' &&
|
3805 | this.parent.parent.type === 'ExpressionStatement' &&
|
3806 | this.parent.left.type === 'Identifier'
|
3807 | ) {
|
3808 | isSimpleAssignment = true;
|
3809 | name = this.parent.left.alias || this.parent.left.name;
|
3810 | } else if (
|
3811 | this.parent.type === 'AssignmentPattern' &&
|
3812 | this.parent.left.type === 'Identifier'
|
3813 | ) {
|
3814 | isSimpleAssignment = true;
|
3815 | name = this.parent.left.alias || this.parent.left.name;
|
3816 | }
|
3817 |
|
3818 | if (spreadPropertyCount) { isSimpleAssignment = false; }
|
3819 |
|
3820 |
|
3821 | name = this.findScope(false).resolveName(name);
|
3822 |
|
3823 | var start = firstPropertyStart;
|
3824 | var end = this.end;
|
3825 |
|
3826 | if (isSimpleAssignment) ; else {
|
3827 | if (
|
3828 | firstSpreadProperty === null ||
|
3829 | firstComputedProperty < firstSpreadProperty
|
3830 | ) {
|
3831 | name = this.findScope(true).createDeclaration('obj');
|
3832 |
|
3833 | code.prependRight(this.start, ("( " + name + " = "));
|
3834 | } else { name = null; }
|
3835 | }
|
3836 |
|
3837 | var len = this.properties.length;
|
3838 | var lastComputedProp;
|
3839 | var sawNonComputedProperty = false;
|
3840 | var isFirst = true;
|
3841 |
|
3842 | for (var i$2 = 0; i$2 < len; i$2 += 1) {
|
3843 | var prop$2 = this.properties[i$2];
|
3844 | var moveStart = i$2 > 0 ? this.properties[i$2 - 1].end : start;
|
3845 |
|
3846 | if (
|
3847 | prop$2.type === 'Property' &&
|
3848 | (prop$2.computed || (lastComputedProp && !spreadPropertyCount))
|
3849 | ) {
|
3850 | if (i$2 === 0) { moveStart = this.start + 1; }
|
3851 | lastComputedProp = prop$2;
|
3852 |
|
3853 | if (!name) {
|
3854 | name = this.findScope(true).createDeclaration('obj');
|
3855 |
|
3856 | var propId = name + (prop$2.computed ? '' : '.');
|
3857 | code.appendRight(prop$2.start, ("( " + name + " = {}, " + propId));
|
3858 | } else {
|
3859 | var propId$1 =
|
3860 | (isSimpleAssignment ? (";\n" + i0 + name) : (", " + name)) +
|
3861 | (prop$2.key.type === 'Literal' || prop$2.computed ? '' : '.');
|
3862 |
|
3863 | if (moveStart < prop$2.start) {
|
3864 | code.overwrite(moveStart, prop$2.start, propId$1);
|
3865 | } else {
|
3866 | code.prependRight(prop$2.start, propId$1);
|
3867 | }
|
3868 | }
|
3869 |
|
3870 | var c = prop$2.key.end;
|
3871 | if (prop$2.computed) {
|
3872 | while (code.original[c] !== ']') { c += 1; }
|
3873 | c += 1;
|
3874 | }
|
3875 | if (prop$2.key.type === 'Literal' && !prop$2.computed) {
|
3876 | code.overwrite(
|
3877 | prop$2.start,
|
3878 | prop$2.value.start,
|
3879 | '[' + code.slice(prop$2.start, prop$2.key.end) + '] = '
|
3880 | );
|
3881 | } else if (prop$2.shorthand || (prop$2.method && !prop$2.computed && transforms.conciseMethodProperty)) {
|
3882 |
|
3883 | code.overwrite(
|
3884 | prop$2.key.start,
|
3885 | prop$2.key.end,
|
3886 | code.slice(prop$2.key.start, prop$2.key.end).replace(/:/, ' =')
|
3887 | );
|
3888 | } else {
|
3889 | if (prop$2.value.start > c) { code.remove(c, prop$2.value.start); }
|
3890 | code.prependLeft(c, ' = ');
|
3891 | }
|
3892 |
|
3893 |
|
3894 |
|
3895 | if (prop$2.method && (prop$2.computed || !transforms.conciseMethodProperty)) {
|
3896 | if (prop$2.value.generator) { code.remove(prop$2.start, prop$2.key.start); }
|
3897 | code.prependRight(prop$2.value.start, ("function" + (prop$2.value.generator ? '*' : '') + " "));
|
3898 | }
|
3899 | } else if (prop$2.type === 'SpreadElement') {
|
3900 | if (name && i$2 > 0) {
|
3901 | if (!lastComputedProp) {
|
3902 | lastComputedProp = this.properties[i$2 - 1];
|
3903 | }
|
3904 | code.appendLeft(lastComputedProp.end, (", " + name + " )"));
|
3905 |
|
3906 | lastComputedProp = null;
|
3907 | name = null;
|
3908 | }
|
3909 | } else {
|
3910 | if (!isFirst && spreadPropertyCount) {
|
3911 |
|
3912 | code.prependRight(prop$2.start, '{');
|
3913 | code.appendLeft(prop$2.end, '}');
|
3914 | }
|
3915 | sawNonComputedProperty = true;
|
3916 | }
|
3917 | if (isFirst && (prop$2.type === 'SpreadElement' || prop$2.computed)) {
|
3918 | var beginEnd = sawNonComputedProperty
|
3919 | ? this.properties[this.properties.length - 1].end
|
3920 | : this.end - 1;
|
3921 |
|
3922 | if (code.original[beginEnd] == ',') { ++beginEnd; }
|
3923 | var closing = code.slice(beginEnd, end);
|
3924 | code.prependLeft(moveStart, closing);
|
3925 | code.remove(beginEnd, end);
|
3926 | isFirst = false;
|
3927 | }
|
3928 |
|
3929 |
|
3930 | var c$1 = prop$2.end;
|
3931 | if (i$2 < len - 1 && !sawNonComputedProperty) {
|
3932 | while (code.original[c$1] !== ',') { c$1 += 1; }
|
3933 | } else if (i$2 == len - 1) { c$1 = this.end; }
|
3934 | if (prop$2.end != c$1) { code.overwrite(prop$2.end, c$1, '', {contentOnly: true}); }
|
3935 | }
|
3936 |
|
3937 | if (!isSimpleAssignment && name) {
|
3938 | code.appendLeft(lastComputedProp.end, (", " + name + " )"));
|
3939 | }
|
3940 | }
|
3941 | };
|
3942 |
|
3943 | return ObjectExpression;
|
3944 | }(Node));
|
3945 |
|
3946 | var Property = (function (Node) {
|
3947 | function Property () {
|
3948 | Node.apply(this, arguments);
|
3949 | }
|
3950 |
|
3951 | if ( Node ) Property.__proto__ = Node;
|
3952 | Property.prototype = Object.create( Node && Node.prototype );
|
3953 | Property.prototype.constructor = Property;
|
3954 |
|
3955 | Property.prototype.initialise = function initialise (transforms) {
|
3956 | if ((this.kind === 'get' || this.kind === 'set') && transforms.getterSetter) {
|
3957 | CompileError.missingTransform("getters and setters", "getterSetter", this);
|
3958 | }
|
3959 | Node.prototype.initialise.call(this, transforms);
|
3960 | };
|
3961 |
|
3962 | Property.prototype.transpile = function transpile (code, transforms) {
|
3963 | Node.prototype.transpile.call(this, code, transforms);
|
3964 |
|
3965 | if (
|
3966 | transforms.conciseMethodProperty &&
|
3967 | !this.computed &&
|
3968 | this.parent.type !== 'ObjectPattern'
|
3969 | ) {
|
3970 | if (this.shorthand) {
|
3971 | code.prependRight(this.start, ((this.key.name) + ": "));
|
3972 | } else if (this.method) {
|
3973 | var name = '';
|
3974 | if (this.program.options.namedFunctionExpressions !== false) {
|
3975 | if (
|
3976 | this.key.type === 'Literal' &&
|
3977 | typeof this.key.value === 'number'
|
3978 | ) {
|
3979 | name = '';
|
3980 | } else if (this.key.type === 'Identifier') {
|
3981 | if (
|
3982 | reserved[this.key.name] ||
|
3983 | !/^[a-z_$][a-z0-9_$]*$/i.test(this.key.name) ||
|
3984 | this.value.body.scope.references[this.key.name]
|
3985 | ) {
|
3986 | name = this.findScope(true).createIdentifier(this.key.name);
|
3987 | } else {
|
3988 | name = this.key.name;
|
3989 | }
|
3990 | } else {
|
3991 | name = this.findScope(true).createIdentifier(this.key.value);
|
3992 | }
|
3993 | name = ' ' + name;
|
3994 | }
|
3995 |
|
3996 | if (this.start < this.key.start) { code.remove(this.start, this.key.start); }
|
3997 | code.appendLeft(
|
3998 | this.key.end,
|
3999 | (": " + (this.value.async ? 'async ' : '') + "function" + (this.value.generator ? '*' : '') + name)
|
4000 | );
|
4001 | }
|
4002 | }
|
4003 |
|
4004 | if (transforms.reservedProperties && reserved[this.key.name]) {
|
4005 | code.prependRight(this.key.start, "'");
|
4006 | code.appendLeft(this.key.end, "'");
|
4007 | }
|
4008 | };
|
4009 |
|
4010 | return Property;
|
4011 | }(Node));
|
4012 |
|
4013 | var ReturnStatement = (function (Node) {
|
4014 | function ReturnStatement () {
|
4015 | Node.apply(this, arguments);
|
4016 | }
|
4017 |
|
4018 | if ( Node ) ReturnStatement.__proto__ = Node;
|
4019 | ReturnStatement.prototype = Object.create( Node && Node.prototype );
|
4020 | ReturnStatement.prototype.constructor = ReturnStatement;
|
4021 |
|
4022 | ReturnStatement.prototype.initialise = function initialise (transforms) {
|
4023 | this.loop = this.findNearest(loopStatement);
|
4024 | this.nearestFunction = this.findNearest(/Function/);
|
4025 |
|
4026 | if (
|
4027 | this.loop &&
|
4028 | (!this.nearestFunction || this.loop.depth > this.nearestFunction.depth)
|
4029 | ) {
|
4030 | this.loop.canReturn = true;
|
4031 | this.shouldWrap = true;
|
4032 | }
|
4033 |
|
4034 | if (this.argument) { this.argument.initialise(transforms); }
|
4035 | };
|
4036 |
|
4037 | ReturnStatement.prototype.transpile = function transpile (code, transforms) {
|
4038 | var shouldWrap =
|
4039 | this.shouldWrap && this.loop && this.loop.shouldRewriteAsFunction;
|
4040 |
|
4041 | if (this.argument) {
|
4042 | if (shouldWrap) { code.prependRight(this.argument.start, "{ v: "); }
|
4043 | this.argument.transpile(code, transforms);
|
4044 | if (shouldWrap) { code.appendLeft(this.argument.end, " }"); }
|
4045 | } else if (shouldWrap) {
|
4046 | code.appendLeft(this.start + 6, ' {}');
|
4047 | }
|
4048 | };
|
4049 |
|
4050 | return ReturnStatement;
|
4051 | }(Node));
|
4052 |
|
4053 | var Super = (function (Node) {
|
4054 | function Super () {
|
4055 | Node.apply(this, arguments);
|
4056 | }
|
4057 |
|
4058 | if ( Node ) Super.__proto__ = Node;
|
4059 | Super.prototype = Object.create( Node && Node.prototype );
|
4060 | Super.prototype.constructor = Super;
|
4061 |
|
4062 | Super.prototype.initialise = function initialise (transforms) {
|
4063 | if (transforms.classes) {
|
4064 | this.method = this.findNearest('MethodDefinition');
|
4065 | if (!this.method)
|
4066 | { throw new CompileError('use of super outside class method', this); }
|
4067 |
|
4068 | var parentClass = this.findNearest('ClassBody').parent;
|
4069 | this.superClassName =
|
4070 | parentClass.superClass && (parentClass.superClass.name || 'superclass');
|
4071 |
|
4072 | if (!this.superClassName)
|
4073 | { throw new CompileError('super used in base class', this); }
|
4074 |
|
4075 | this.isCalled =
|
4076 | this.parent.type === 'CallExpression' && this === this.parent.callee;
|
4077 |
|
4078 | if (this.method.kind !== 'constructor' && this.isCalled) {
|
4079 | throw new CompileError(
|
4080 | 'super() not allowed outside class constructor',
|
4081 | this
|
4082 | );
|
4083 | }
|
4084 |
|
4085 | this.isMember = this.parent.type === 'MemberExpression';
|
4086 |
|
4087 | if (!this.isCalled && !this.isMember) {
|
4088 | throw new CompileError(
|
4089 | 'Unexpected use of `super` (expected `super(...)` or `super.*`)',
|
4090 | this
|
4091 | );
|
4092 | }
|
4093 | }
|
4094 |
|
4095 | if (transforms.arrow) {
|
4096 | var lexicalBoundary = this.findLexicalBoundary();
|
4097 | var arrowFunction = this.findNearest('ArrowFunctionExpression');
|
4098 | var loop = this.findNearest(loopStatement);
|
4099 |
|
4100 | if (arrowFunction && arrowFunction.depth > lexicalBoundary.depth) {
|
4101 | this.thisAlias = lexicalBoundary.getThisAlias();
|
4102 | }
|
4103 |
|
4104 | if (
|
4105 | loop &&
|
4106 | loop.body.contains(this) &&
|
4107 | loop.depth > lexicalBoundary.depth
|
4108 | ) {
|
4109 | this.thisAlias = lexicalBoundary.getThisAlias();
|
4110 | }
|
4111 | }
|
4112 | };
|
4113 |
|
4114 | Super.prototype.transpile = function transpile (code, transforms) {
|
4115 | if (transforms.classes) {
|
4116 | var expression =
|
4117 | this.isCalled || this.method.static
|
4118 | ? this.superClassName
|
4119 | : ((this.superClassName) + ".prototype");
|
4120 |
|
4121 | code.overwrite(this.start, this.end, expression, {
|
4122 | storeName: true,
|
4123 | contentOnly: true
|
4124 | });
|
4125 |
|
4126 | var callExpression = this.isCalled ? this.parent : this.parent.parent;
|
4127 |
|
4128 | if (callExpression && callExpression.type === 'CallExpression') {
|
4129 | if (!this.noCall) {
|
4130 |
|
4131 | code.appendLeft(callExpression.callee.end, '.call');
|
4132 | }
|
4133 |
|
4134 | var thisAlias = this.thisAlias || 'this';
|
4135 |
|
4136 | if (callExpression.arguments.length) {
|
4137 | code.appendLeft(callExpression.arguments[0].start, (thisAlias + ", "));
|
4138 | } else {
|
4139 | code.appendLeft(callExpression.end - 1, ("" + thisAlias));
|
4140 | }
|
4141 | }
|
4142 | }
|
4143 | };
|
4144 |
|
4145 | return Super;
|
4146 | }(Node));
|
4147 |
|
4148 | var TaggedTemplateExpression = (function (Node) {
|
4149 | function TaggedTemplateExpression () {
|
4150 | Node.apply(this, arguments);
|
4151 | }
|
4152 |
|
4153 | if ( Node ) TaggedTemplateExpression.__proto__ = Node;
|
4154 | TaggedTemplateExpression.prototype = Object.create( Node && Node.prototype );
|
4155 | TaggedTemplateExpression.prototype.constructor = TaggedTemplateExpression;
|
4156 |
|
4157 | TaggedTemplateExpression.prototype.initialise = function initialise (transforms) {
|
4158 | if (
|
4159 | transforms.templateString &&
|
4160 | !transforms.dangerousTaggedTemplateString
|
4161 | ) {
|
4162 | CompileError.missingTransform(
|
4163 | "tagged template strings", "templateString", this, "dangerousTaggedTemplateString"
|
4164 | );
|
4165 | }
|
4166 |
|
4167 | Node.prototype.initialise.call(this, transforms);
|
4168 | };
|
4169 |
|
4170 | TaggedTemplateExpression.prototype.transpile = function transpile (code, transforms) {
|
4171 | if (transforms.templateString && transforms.dangerousTaggedTemplateString) {
|
4172 | var ordered = this.quasi.expressions
|
4173 | .concat(this.quasi.quasis)
|
4174 | .sort(function (a, b) { return a.start - b.start; });
|
4175 |
|
4176 | var program = this.program;
|
4177 | var rootScope = program.body.scope;
|
4178 |
|
4179 |
|
4180 | var templateStrings = this.quasi.quasis.map(function (quasi) { return JSON.stringify(quasi.value.cooked); }
|
4181 | ).join(', ');
|
4182 |
|
4183 | var templateObject = this.program.templateLiteralQuasis[templateStrings];
|
4184 | if (!templateObject) {
|
4185 | templateObject = rootScope.createIdentifier('templateObject');
|
4186 | code.prependLeft(this.program.prependAt, ("var " + templateObject + " = Object.freeze([" + templateStrings + "]);\n"));
|
4187 |
|
4188 | this.program.templateLiteralQuasis[templateStrings] = templateObject;
|
4189 | }
|
4190 |
|
4191 | code.overwrite(
|
4192 | this.tag.end,
|
4193 | ordered[0].start,
|
4194 | ("(" + templateObject)
|
4195 | );
|
4196 |
|
4197 | var lastIndex = ordered[0].start;
|
4198 | ordered.forEach(function (node) {
|
4199 | if (node.type === 'TemplateElement') {
|
4200 | code.remove(lastIndex, node.end);
|
4201 | } else {
|
4202 | code.overwrite(lastIndex, node.start, ', ');
|
4203 | }
|
4204 |
|
4205 | lastIndex = node.end;
|
4206 | });
|
4207 |
|
4208 | code.overwrite(lastIndex, this.end, ')');
|
4209 | }
|
4210 |
|
4211 | Node.prototype.transpile.call(this, code, transforms);
|
4212 | };
|
4213 |
|
4214 | return TaggedTemplateExpression;
|
4215 | }(Node));
|
4216 |
|
4217 | var TemplateElement = (function (Node) {
|
4218 | function TemplateElement () {
|
4219 | Node.apply(this, arguments);
|
4220 | }
|
4221 |
|
4222 | if ( Node ) TemplateElement.__proto__ = Node;
|
4223 | TemplateElement.prototype = Object.create( Node && Node.prototype );
|
4224 | TemplateElement.prototype.constructor = TemplateElement;
|
4225 |
|
4226 | TemplateElement.prototype.initialise = function initialise () {
|
4227 | this.program.indentExclusionElements.push(this);
|
4228 | };
|
4229 |
|
4230 | return TemplateElement;
|
4231 | }(Node));
|
4232 |
|
4233 | var TemplateLiteral = (function (Node) {
|
4234 | function TemplateLiteral () {
|
4235 | Node.apply(this, arguments);
|
4236 | }
|
4237 |
|
4238 | if ( Node ) TemplateLiteral.__proto__ = Node;
|
4239 | TemplateLiteral.prototype = Object.create( Node && Node.prototype );
|
4240 | TemplateLiteral.prototype.constructor = TemplateLiteral;
|
4241 |
|
4242 | TemplateLiteral.prototype.transpile = function transpile (code, transforms) {
|
4243 | Node.prototype.transpile.call(this, code, transforms);
|
4244 |
|
4245 | if (
|
4246 | transforms.templateString &&
|
4247 | this.parent.type !== 'TaggedTemplateExpression'
|
4248 | ) {
|
4249 | var ordered = this.expressions
|
4250 | .concat(this.quasis)
|
4251 | .sort(function (a, b) { return a.start - b.start || a.end - b.end; })
|
4252 | .filter(function (node, i) {
|
4253 |
|
4254 | if (node.type !== 'TemplateElement') { return true; }
|
4255 |
|
4256 |
|
4257 | if (node.value.raw) { return true; }
|
4258 |
|
4259 |
|
4260 | return !i;
|
4261 | });
|
4262 |
|
4263 |
|
4264 |
|
4265 |
|
4266 |
|
4267 | if (ordered.length >= 3) {
|
4268 | var first = ordered[0];
|
4269 | var third = ordered[2];
|
4270 | if (
|
4271 | first.type === 'TemplateElement' &&
|
4272 | first.value.raw === '' &&
|
4273 | third.type === 'TemplateElement'
|
4274 | ) {
|
4275 | ordered.shift();
|
4276 | }
|
4277 | }
|
4278 |
|
4279 | var parenthesise =
|
4280 | (this.quasis.length !== 1 || this.expressions.length !== 0) &&
|
4281 | this.parent.type !== 'TemplateLiteral' &&
|
4282 | this.parent.type !== 'AssignmentExpression' &&
|
4283 | this.parent.type !== 'AssignmentPattern' &&
|
4284 | this.parent.type !== 'VariableDeclarator' &&
|
4285 | (this.parent.type !== 'BinaryExpression' ||
|
4286 | this.parent.operator !== '+');
|
4287 |
|
4288 | if (parenthesise) { code.appendRight(this.start, '('); }
|
4289 |
|
4290 | var lastIndex = this.start;
|
4291 |
|
4292 | ordered.forEach(function (node, i) {
|
4293 | var prefix = i === 0 ? (parenthesise ? '(' : '') : ' + ';
|
4294 |
|
4295 | if (node.type === 'TemplateElement') {
|
4296 | code.overwrite(
|
4297 | lastIndex,
|
4298 | node.end,
|
4299 | prefix + JSON.stringify(node.value.cooked)
|
4300 | );
|
4301 | } else {
|
4302 | var parenthesise$1 = node.type !== 'Identifier';
|
4303 |
|
4304 | if (parenthesise$1) { prefix += '('; }
|
4305 |
|
4306 | code.remove(lastIndex, node.start);
|
4307 |
|
4308 | if (prefix) { code.prependRight(node.start, prefix); }
|
4309 | if (parenthesise$1) { code.appendLeft(node.end, ')'); }
|
4310 | }
|
4311 |
|
4312 | lastIndex = node.end;
|
4313 | });
|
4314 |
|
4315 | if (parenthesise) { code.appendLeft(lastIndex, ')'); }
|
4316 | code.overwrite(lastIndex, this.end, "", { contentOnly: true });
|
4317 | }
|
4318 | };
|
4319 |
|
4320 | return TemplateLiteral;
|
4321 | }(Node));
|
4322 |
|
4323 | var ThisExpression = (function (Node) {
|
4324 | function ThisExpression () {
|
4325 | Node.apply(this, arguments);
|
4326 | }
|
4327 |
|
4328 | if ( Node ) ThisExpression.__proto__ = Node;
|
4329 | ThisExpression.prototype = Object.create( Node && Node.prototype );
|
4330 | ThisExpression.prototype.constructor = ThisExpression;
|
4331 |
|
4332 | ThisExpression.prototype.initialise = function initialise (transforms) {
|
4333 | var lexicalBoundary = this.findLexicalBoundary();
|
4334 |
|
4335 | if (transforms.letConst) {
|
4336 |
|
4337 |
|
4338 | var node = this.findNearest(loopStatement);
|
4339 | while (node && node.depth > lexicalBoundary.depth) {
|
4340 | node.thisRefs.push(this);
|
4341 | node = node.parent.findNearest(loopStatement);
|
4342 | }
|
4343 | }
|
4344 |
|
4345 | if (transforms.arrow) {
|
4346 | var arrowFunction = this.findNearest('ArrowFunctionExpression');
|
4347 |
|
4348 | if (arrowFunction && arrowFunction.depth > lexicalBoundary.depth) {
|
4349 | this.alias = lexicalBoundary.getThisAlias();
|
4350 | }
|
4351 | }
|
4352 | };
|
4353 |
|
4354 | ThisExpression.prototype.transpile = function transpile (code) {
|
4355 | if (this.alias) {
|
4356 | code.overwrite(this.start, this.end, this.alias, {
|
4357 | storeName: true,
|
4358 | contentOnly: true
|
4359 | });
|
4360 | }
|
4361 | };
|
4362 |
|
4363 | return ThisExpression;
|
4364 | }(Node));
|
4365 |
|
4366 | var UpdateExpression = (function (Node) {
|
4367 | function UpdateExpression () {
|
4368 | Node.apply(this, arguments);
|
4369 | }
|
4370 |
|
4371 | if ( Node ) UpdateExpression.__proto__ = Node;
|
4372 | UpdateExpression.prototype = Object.create( Node && Node.prototype );
|
4373 | UpdateExpression.prototype.constructor = UpdateExpression;
|
4374 |
|
4375 | UpdateExpression.prototype.initialise = function initialise (transforms) {
|
4376 | if (this.argument.type === 'Identifier') {
|
4377 | var declaration = this.findScope(false).findDeclaration(
|
4378 | this.argument.name
|
4379 | );
|
4380 |
|
4381 | var statement = declaration && declaration.node.ancestor(3);
|
4382 | if (
|
4383 | statement &&
|
4384 | statement.type === 'ForStatement' &&
|
4385 | statement.body.contains(this)
|
4386 | ) {
|
4387 | statement.reassigned[this.argument.name] = true;
|
4388 | }
|
4389 | }
|
4390 |
|
4391 | Node.prototype.initialise.call(this, transforms);
|
4392 | };
|
4393 |
|
4394 | UpdateExpression.prototype.transpile = function transpile (code, transforms) {
|
4395 | if (this.argument.type === 'Identifier') {
|
4396 |
|
4397 |
|
4398 | checkConst(this.argument, this.findScope(false));
|
4399 | }
|
4400 | Node.prototype.transpile.call(this, code, transforms);
|
4401 | };
|
4402 |
|
4403 | return UpdateExpression;
|
4404 | }(Node));
|
4405 |
|
4406 | var VariableDeclaration = (function (Node) {
|
4407 | function VariableDeclaration () {
|
4408 | Node.apply(this, arguments);
|
4409 | }
|
4410 |
|
4411 | if ( Node ) VariableDeclaration.__proto__ = Node;
|
4412 | VariableDeclaration.prototype = Object.create( Node && Node.prototype );
|
4413 | VariableDeclaration.prototype.constructor = VariableDeclaration;
|
4414 |
|
4415 | VariableDeclaration.prototype.initialise = function initialise (transforms) {
|
4416 | this.scope = this.findScope(this.kind === 'var');
|
4417 | this.declarations.forEach(function (declarator) { return declarator.initialise(transforms); });
|
4418 | };
|
4419 |
|
4420 | VariableDeclaration.prototype.transpile = function transpile (code, transforms) {
|
4421 | var this$1 = this;
|
4422 |
|
4423 | var i0 = this.getIndentation();
|
4424 | var kind = this.kind;
|
4425 |
|
4426 | if (transforms.letConst && kind !== 'var') {
|
4427 | kind = 'var';
|
4428 | code.overwrite(this.start, this.start + this.kind.length, kind, {
|
4429 | contentOnly: true,
|
4430 | storeName: true
|
4431 | });
|
4432 | }
|
4433 |
|
4434 | if (transforms.destructuring && this.parent.type !== 'ForOfStatement' && this.parent.type !== 'ForInStatement') {
|
4435 | var c = this.start;
|
4436 | var lastDeclaratorIsPattern;
|
4437 |
|
4438 | this.declarations.forEach(function (declarator, i) {
|
4439 | declarator.transpile(code, transforms);
|
4440 |
|
4441 | if (declarator.id.type === 'Identifier') {
|
4442 | if (i > 0 && this$1.declarations[i - 1].id.type !== 'Identifier') {
|
4443 | code.overwrite(c, declarator.id.start, "var ");
|
4444 | }
|
4445 | } else {
|
4446 | var inline = loopStatement.test(this$1.parent.type);
|
4447 |
|
4448 | if (i === 0) {
|
4449 | code.remove(c, declarator.id.start);
|
4450 | } else {
|
4451 | code.overwrite(c, declarator.id.start, (";\n" + i0));
|
4452 | }
|
4453 |
|
4454 | var simple =
|
4455 | declarator.init.type === 'Identifier' && !declarator.init.rewritten;
|
4456 |
|
4457 | var name = simple
|
4458 | ? (declarator.init.alias || declarator.init.name)
|
4459 | : declarator.findScope(true).createIdentifier('ref');
|
4460 |
|
4461 | c = declarator.start;
|
4462 |
|
4463 | var statementGenerators = [];
|
4464 |
|
4465 | if (simple) {
|
4466 | code.remove(declarator.id.end, declarator.end);
|
4467 | } else {
|
4468 | statementGenerators.push(function (start, prefix, suffix) {
|
4469 | code.prependRight(declarator.id.end, ("var " + name));
|
4470 | code.appendLeft(declarator.init.end, ("" + suffix));
|
4471 | code.move(declarator.id.end, declarator.end, start);
|
4472 | });
|
4473 | }
|
4474 |
|
4475 | var scope = declarator.findScope(false);
|
4476 | destructure(
|
4477 | code,
|
4478 | function (id) { return scope.createIdentifier(id); },
|
4479 | function (ref) {
|
4480 | var name = ref.name;
|
4481 |
|
4482 | return scope.resolveName(name);
|
4483 | },
|
4484 | declarator.id,
|
4485 | name,
|
4486 | inline,
|
4487 | statementGenerators
|
4488 | );
|
4489 |
|
4490 | var prefix = inline ? 'var ' : '';
|
4491 | var suffix = inline ? ", " : (";\n" + i0);
|
4492 | statementGenerators.forEach(function (fn, j) {
|
4493 | if (
|
4494 | i === this$1.declarations.length - 1 &&
|
4495 | j === statementGenerators.length - 1
|
4496 | ) {
|
4497 | suffix = inline ? '' : ';';
|
4498 | }
|
4499 |
|
4500 | fn(declarator.start, j === 0 ? prefix : '', suffix);
|
4501 | });
|
4502 | }
|
4503 |
|
4504 | c = declarator.end;
|
4505 | lastDeclaratorIsPattern = declarator.id.type !== 'Identifier';
|
4506 | });
|
4507 |
|
4508 | if (lastDeclaratorIsPattern && this.end > c) {
|
4509 | code.overwrite(c, this.end, '', { contentOnly: true });
|
4510 | }
|
4511 | } else {
|
4512 | this.declarations.forEach(function (declarator) {
|
4513 | declarator.transpile(code, transforms);
|
4514 | });
|
4515 | }
|
4516 | };
|
4517 |
|
4518 | return VariableDeclaration;
|
4519 | }(Node));
|
4520 |
|
4521 | var VariableDeclarator = (function (Node) {
|
4522 | function VariableDeclarator () {
|
4523 | Node.apply(this, arguments);
|
4524 | }
|
4525 |
|
4526 | if ( Node ) VariableDeclarator.__proto__ = Node;
|
4527 | VariableDeclarator.prototype = Object.create( Node && Node.prototype );
|
4528 | VariableDeclarator.prototype.constructor = VariableDeclarator;
|
4529 |
|
4530 | VariableDeclarator.prototype.initialise = function initialise (transforms) {
|
4531 | var kind = this.parent.kind;
|
4532 | if (kind === 'let' && this.parent.parent.type === 'ForStatement') {
|
4533 | kind = 'for.let';
|
4534 | }
|
4535 |
|
4536 | this.parent.scope.addDeclaration(this.id, kind);
|
4537 | Node.prototype.initialise.call(this, transforms);
|
4538 | };
|
4539 |
|
4540 | VariableDeclarator.prototype.transpile = function transpile (code, transforms) {
|
4541 | if (!this.init && transforms.letConst && this.parent.kind !== 'var') {
|
4542 | var inLoop = this.findNearest(
|
4543 | /Function|^For(In|Of)?Statement|^(?:Do)?WhileStatement/
|
4544 | );
|
4545 | if (
|
4546 | inLoop &&
|
4547 | !/Function/.test(inLoop.type) &&
|
4548 | !this.isLeftDeclaratorOfLoop()
|
4549 | ) {
|
4550 | code.appendLeft(this.id.end, ' = (void 0)');
|
4551 | }
|
4552 | }
|
4553 |
|
4554 | if (this.id) { this.id.transpile(code, transforms); }
|
4555 | if (this.init) { this.init.transpile(code, transforms); }
|
4556 | };
|
4557 |
|
4558 | VariableDeclarator.prototype.isLeftDeclaratorOfLoop = function isLeftDeclaratorOfLoop () {
|
4559 | return (
|
4560 | this.parent &&
|
4561 | this.parent.type === 'VariableDeclaration' &&
|
4562 | this.parent.parent &&
|
4563 | (this.parent.parent.type === 'ForInStatement' ||
|
4564 | this.parent.parent.type === 'ForOfStatement') &&
|
4565 | this.parent.parent.left &&
|
4566 | this.parent.parent.left.declarations[0] === this
|
4567 | );
|
4568 | };
|
4569 |
|
4570 | return VariableDeclarator;
|
4571 | }(Node));
|
4572 |
|
4573 | var types = {
|
4574 | ArrayExpression: ArrayExpression,
|
4575 | ArrowFunctionExpression: ArrowFunctionExpression,
|
4576 | AssignmentExpression: AssignmentExpression,
|
4577 | AwaitExpression: AwaitExpression,
|
4578 | BinaryExpression: BinaryExpression,
|
4579 | BreakStatement: BreakStatement,
|
4580 | CallExpression: CallExpression,
|
4581 | CatchClause: CatchClause,
|
4582 | ClassBody: ClassBody,
|
4583 | ClassDeclaration: ClassDeclaration,
|
4584 | ClassExpression: ClassExpression,
|
4585 | ContinueStatement: ContinueStatement,
|
4586 | DoWhileStatement: LoopStatement,
|
4587 | ExportNamedDeclaration: ExportNamedDeclaration,
|
4588 | ExportDefaultDeclaration: ExportDefaultDeclaration,
|
4589 | ForStatement: ForStatement,
|
4590 | ForInStatement: ForInStatement,
|
4591 | ForOfStatement: ForOfStatement,
|
4592 | FunctionDeclaration: FunctionDeclaration,
|
4593 | FunctionExpression: FunctionExpression,
|
4594 | Identifier: Identifier,
|
4595 | IfStatement: IfStatement,
|
4596 | Import: Import,
|
4597 | ImportDeclaration: ImportDeclaration,
|
4598 | ImportDefaultSpecifier: ImportDefaultSpecifier,
|
4599 | ImportSpecifier: ImportSpecifier,
|
4600 | JSXAttribute: JSXAttribute,
|
4601 | JSXClosingElement: JSXClosingElement,
|
4602 | JSXClosingFragment: JSXClosingFragment,
|
4603 | JSXElement: JSXElement,
|
4604 | JSXExpressionContainer: JSXExpressionContainer,
|
4605 | JSXFragment: JSXFragment,
|
4606 | JSXOpeningElement: JSXOpeningElement,
|
4607 | JSXOpeningFragment: JSXOpeningFragment,
|
4608 | JSXSpreadAttribute: JSXSpreadAttribute,
|
4609 | Literal: Literal,
|
4610 | MemberExpression: MemberExpression,
|
4611 | NewExpression: NewExpression,
|
4612 | ObjectExpression: ObjectExpression,
|
4613 | Property: Property,
|
4614 | ReturnStatement: ReturnStatement,
|
4615 | Super: Super,
|
4616 | TaggedTemplateExpression: TaggedTemplateExpression,
|
4617 | TemplateElement: TemplateElement,
|
4618 | TemplateLiteral: TemplateLiteral,
|
4619 | ThisExpression: ThisExpression,
|
4620 | UpdateExpression: UpdateExpression,
|
4621 | VariableDeclaration: VariableDeclaration,
|
4622 | VariableDeclarator: VariableDeclarator,
|
4623 | WhileStatement: LoopStatement
|
4624 | };
|
4625 |
|
4626 | var keys = {
|
4627 | Program: ['body'],
|
4628 | Literal: []
|
4629 | };
|
4630 |
|
4631 | var statementsWithBlocks = {
|
4632 | IfStatement: 'consequent',
|
4633 | ForStatement: 'body',
|
4634 | ForInStatement: 'body',
|
4635 | ForOfStatement: 'body',
|
4636 | WhileStatement: 'body',
|
4637 | DoWhileStatement: 'body',
|
4638 | ArrowFunctionExpression: 'body'
|
4639 | };
|
4640 |
|
4641 | function wrap(raw, parent) {
|
4642 | if (!raw) { return; }
|
4643 |
|
4644 | if ('length' in raw) {
|
4645 | var i = raw.length;
|
4646 | while (i--) { wrap(raw[i], parent); }
|
4647 | return;
|
4648 | }
|
4649 |
|
4650 |
|
4651 |
|
4652 | if (raw.__wrapped) { return; }
|
4653 | raw.__wrapped = true;
|
4654 |
|
4655 | if (!keys[raw.type]) {
|
4656 | keys[raw.type] = Object.keys(raw).filter(
|
4657 | function (key) { return typeof raw[key] === 'object'; }
|
4658 | );
|
4659 | }
|
4660 |
|
4661 |
|
4662 | var bodyType = statementsWithBlocks[raw.type];
|
4663 | if (bodyType && raw[bodyType].type !== 'BlockStatement') {
|
4664 | var expression = raw[bodyType];
|
4665 |
|
4666 |
|
4667 |
|
4668 | raw[bodyType] = {
|
4669 | start: expression.start,
|
4670 | end: expression.end,
|
4671 | type: 'BlockStatement',
|
4672 | body: [expression],
|
4673 | synthetic: true
|
4674 | };
|
4675 | }
|
4676 |
|
4677 | raw.parent = parent;
|
4678 | raw.program = parent.program || parent;
|
4679 | raw.depth = parent.depth + 1;
|
4680 | raw.keys = keys[raw.type];
|
4681 | raw.indentation = undefined;
|
4682 |
|
4683 | for (var i$1 = 0, list = keys[raw.type]; i$1 < list.length; i$1 += 1) {
|
4684 | var key = list[i$1];
|
4685 |
|
4686 | wrap(raw[key], raw);
|
4687 | }
|
4688 |
|
4689 | raw.program.magicString.addSourcemapLocation(raw.start);
|
4690 | raw.program.magicString.addSourcemapLocation(raw.end);
|
4691 |
|
4692 | var type =
|
4693 | (raw.type === 'BlockStatement' ? BlockStatement : types[raw.type]) || Node;
|
4694 | raw.__proto__ = type.prototype;
|
4695 | }
|
4696 |
|
4697 | function Program(source, ast, transforms, options) {
|
4698 | this.type = 'Root';
|
4699 |
|
4700 |
|
4701 | this.jsx = options.jsx || 'React.createElement';
|
4702 | this.jsxFragment = options.jsxFragment || 'React.Fragment';
|
4703 | this.options = options;
|
4704 |
|
4705 | this.source = source;
|
4706 | this.magicString = new MagicString(source);
|
4707 |
|
4708 | this.ast = ast;
|
4709 | this.depth = 0;
|
4710 |
|
4711 | wrap((this.body = ast), this);
|
4712 | this.body.__proto__ = BlockStatement.prototype;
|
4713 |
|
4714 | this.templateLiteralQuasis = Object.create(null);
|
4715 | for (var i = 0; i < this.body.body.length; ++i) {
|
4716 | if (!this.body.body[i].directive) {
|
4717 | this.prependAt = this.body.body[i].start;
|
4718 | break;
|
4719 | }
|
4720 | }
|
4721 | this.objectWithoutPropertiesHelper = null;
|
4722 |
|
4723 | this.indentExclusionElements = [];
|
4724 | this.body.initialise(transforms);
|
4725 |
|
4726 | this.indentExclusions = Object.create(null);
|
4727 | for (var i$2 = 0, list = this.indentExclusionElements; i$2 < list.length; i$2 += 1) {
|
4728 | var node = list[i$2];
|
4729 |
|
4730 | for (var i$1 = node.start; i$1 < node.end; i$1 += 1) {
|
4731 | this.indentExclusions[i$1] = true;
|
4732 | }
|
4733 | }
|
4734 |
|
4735 | this.body.transpile(this.magicString, transforms);
|
4736 | }
|
4737 |
|
4738 | Program.prototype = {
|
4739 | export: function export$1(options) {
|
4740 | if ( options === void 0 ) options = {};
|
4741 |
|
4742 | return {
|
4743 | code: this.magicString.toString(),
|
4744 | map: this.magicString.generateMap({
|
4745 | file: options.file,
|
4746 | source: options.source,
|
4747 | includeContent: options.includeContent !== false
|
4748 | })
|
4749 | };
|
4750 | },
|
4751 |
|
4752 | findNearest: function findNearest() {
|
4753 | return null;
|
4754 | },
|
4755 |
|
4756 | findScope: function findScope() {
|
4757 | return null;
|
4758 | },
|
4759 |
|
4760 | getObjectWithoutPropertiesHelper: function getObjectWithoutPropertiesHelper(code) {
|
4761 | if (!this.objectWithoutPropertiesHelper) {
|
4762 | this.objectWithoutPropertiesHelper = this.body.scope.createIdentifier('objectWithoutProperties');
|
4763 | code.prependLeft(this.prependAt, "function " + (this.objectWithoutPropertiesHelper) + " (obj, exclude) { " +
|
4764 | "var target = {}; for (var k in obj) " +
|
4765 | "if (Object.prototype.hasOwnProperty.call(obj, k) && exclude.indexOf(k) === -1) " +
|
4766 | "target[k] = obj[k]; return target; }\n"
|
4767 | );
|
4768 | }
|
4769 | return this.objectWithoutPropertiesHelper;
|
4770 | }
|
4771 | };
|
4772 |
|
4773 | var matrix = {
|
4774 | chrome: {
|
4775 | 48: 610719,
|
4776 | 49: 652287,
|
4777 | 50: 783359,
|
4778 | 51: 783359,
|
4779 | 52: 1045503,
|
4780 | 53: 1045503,
|
4781 | 54: 1045503,
|
4782 | 55: 3142655,
|
4783 | 56: 3142655,
|
4784 | 57: 3142655,
|
4785 | 58: 4191231,
|
4786 | 59: 4191231,
|
4787 | 60: 8385535,
|
4788 | 61: 8385535,
|
4789 | 62: 8385535,
|
4790 | 63: 8385535,
|
4791 | 64: 8385535,
|
4792 | 65: 8385535,
|
4793 | 66: 8385535,
|
4794 | 67: 8385535,
|
4795 | 68: 8385535,
|
4796 | 69: 8385535,
|
4797 | 70: 8385535,
|
4798 | 71: 8385535
|
4799 | },
|
4800 | firefox: {
|
4801 | 43: 643515,
|
4802 | 44: 643515,
|
4803 | 45: 643519,
|
4804 | 46: 774591,
|
4805 | 47: 774655,
|
4806 | 48: 774655,
|
4807 | 49: 774655,
|
4808 | 50: 774655,
|
4809 | 51: 775167,
|
4810 | 52: 4191231,
|
4811 | 53: 4191231,
|
4812 | 54: 4191231,
|
4813 | 55: 8385535,
|
4814 | 56: 8385535,
|
4815 | 57: 8385535,
|
4816 | 58: 8385535,
|
4817 | 59: 8385535,
|
4818 | 60: 8385535,
|
4819 | 61: 8385535,
|
4820 | 62: 8385535,
|
4821 | 63: 8385535,
|
4822 | 64: 8385535
|
4823 | },
|
4824 | safari: {
|
4825 | 8: 524297,
|
4826 | 9: 594141,
|
4827 | 10: 1831935,
|
4828 | '10.1': 4191231,
|
4829 | 11: 4191231,
|
4830 | '11.1': 8385535,
|
4831 | 12: 8385535
|
4832 | },
|
4833 | ie: {
|
4834 | 8: 0,
|
4835 | 9: 524289,
|
4836 | 10: 524289,
|
4837 | 11: 524289
|
4838 | },
|
4839 | edge: {
|
4840 | 12: 610459,
|
4841 | 13: 774559,
|
4842 | 14: 2085887,
|
4843 | 15: 4183039,
|
4844 | 16: 4183039,
|
4845 | 17: 4183039,
|
4846 | 18: 4183039,
|
4847 | 19: 4183039
|
4848 | },
|
4849 | node: {
|
4850 | '0.10': 524289,
|
4851 | '0.12': 524417,
|
4852 | 4: 594335,
|
4853 | 5: 594335,
|
4854 | 6: 783359,
|
4855 | 8: 4191231,
|
4856 | '8.3': 8385535,
|
4857 | '8.7': 8385535,
|
4858 | '8.10': 8385535
|
4859 | }
|
4860 | };
|
4861 |
|
4862 | var features = [
|
4863 | 'getterSetter',
|
4864 | 'arrow',
|
4865 | 'classes',
|
4866 | 'computedProperty',
|
4867 | 'conciseMethodProperty',
|
4868 | 'defaultParameter',
|
4869 | 'destructuring',
|
4870 | 'forOf',
|
4871 | 'generator',
|
4872 | 'letConst',
|
4873 | 'moduleExport',
|
4874 | 'moduleImport',
|
4875 | 'numericLiteral',
|
4876 | 'parameterDestructuring',
|
4877 | 'spreadRest',
|
4878 | 'stickyRegExp',
|
4879 | 'templateString',
|
4880 | 'unicodeRegExp',
|
4881 |
|
4882 |
|
4883 | 'exponentiation',
|
4884 |
|
4885 |
|
4886 |
|
4887 | 'reservedProperties',
|
4888 |
|
4889 | 'trailingFunctionCommas',
|
4890 | 'asyncAwait',
|
4891 | 'objectRestSpread'
|
4892 | ];
|
4893 |
|
4894 | var version = "0.20.0";
|
4895 |
|
4896 | var parser = acorn.Parser.extend(acornDynamicImport, acornJsx());
|
4897 |
|
4898 | var dangerousTransforms = ['dangerousTaggedTemplateString', 'dangerousForOf'];
|
4899 |
|
4900 | function target(target) {
|
4901 | var targets = Object.keys(target);
|
4902 | var bitmask = targets.length
|
4903 | ? 8388607
|
4904 | : 524289;
|
4905 |
|
4906 | Object.keys(target).forEach(function (environment) {
|
4907 | var versions = matrix[environment];
|
4908 | if (!versions)
|
4909 | { throw new Error(
|
4910 | ("Unknown environment '" + environment + "'. Please raise an issue at https://github.com/bublejs/buble/issues")
|
4911 | ); }
|
4912 |
|
4913 | var targetVersion = target[environment];
|
4914 | if (!(targetVersion in versions))
|
4915 | { throw new Error(
|
4916 | ("Support data exists for the following versions of " + environment + ": " + (Object.keys(
|
4917 | versions
|
4918 | ).join(
|
4919 | ', '
|
4920 | )) + ". Please raise an issue at https://github.com/bublejs/buble/issues")
|
4921 | ); }
|
4922 | var support = versions[targetVersion];
|
4923 |
|
4924 | bitmask &= support;
|
4925 | });
|
4926 |
|
4927 | var transforms = Object.create(null);
|
4928 | features.forEach(function (name, i) {
|
4929 | transforms[name] = !(bitmask & (1 << i));
|
4930 | });
|
4931 |
|
4932 | dangerousTransforms.forEach(function (name) {
|
4933 | transforms[name] = false;
|
4934 | });
|
4935 |
|
4936 | return transforms;
|
4937 | }
|
4938 |
|
4939 | function transform(source, options) {
|
4940 | if ( options === void 0 ) options = {};
|
4941 |
|
4942 | var ast;
|
4943 | var jsx = null;
|
4944 |
|
4945 | try {
|
4946 | ast = parser.parse(source, {
|
4947 | ecmaVersion: 10,
|
4948 | preserveParens: true,
|
4949 | sourceType: 'module',
|
4950 | allowAwaitOutsideFunction: true,
|
4951 | allowReturnOutsideFunction: true,
|
4952 | allowHashBang: true,
|
4953 | onComment: function (block, text) {
|
4954 | if (!jsx) {
|
4955 | var match = /@jsx\s+([^\s]+)/.exec(text);
|
4956 | if (match) { jsx = match[1]; }
|
4957 | }
|
4958 | }
|
4959 | });
|
4960 | options.jsx = jsx || options.jsx;
|
4961 | } catch (err) {
|
4962 | err.snippet = getSnippet(source, err.loc);
|
4963 | err.toString = function () { return ((err.name) + ": " + (err.message) + "\n" + (err.snippet)); };
|
4964 | throw err;
|
4965 | }
|
4966 |
|
4967 | var transforms = target(options.target || {});
|
4968 | Object.keys(options.transforms || {}).forEach(function (name) {
|
4969 | if (name === 'modules') {
|
4970 | if (!('moduleImport' in options.transforms))
|
4971 | { transforms.moduleImport = options.transforms.modules; }
|
4972 | if (!('moduleExport' in options.transforms))
|
4973 | { transforms.moduleExport = options.transforms.modules; }
|
4974 | return;
|
4975 | }
|
4976 |
|
4977 | if (!(name in transforms)) { throw new Error(("Unknown transform '" + name + "'")); }
|
4978 | transforms[name] = options.transforms[name];
|
4979 | });
|
4980 | if (options.objectAssign === true) { options.objectAssign = 'Object.assign'; }
|
4981 | return new Program(source, ast, transforms, options).export(options);
|
4982 | }
|
4983 |
|
4984 | exports.VERSION = version;
|
4985 | exports.target = target;
|
4986 | exports.transform = transform;
|
4987 |
|