mambascript 0.5.4 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (82) hide show
  1. package/CHANGELOG.md +326 -0
  2. package/README.md +509 -267
  3. package/bin/mamba +1 -258
  4. package/decision.js +1 -246
  5. package/docs/coverage.md +70 -0
  6. package/docs/features.md +3348 -0
  7. package/docs/perf.md +94 -0
  8. package/docs/reference.md +1295 -0
  9. package/docs/vs-coffeescript.md +1593 -0
  10. package/docs/vs-iced-coffeescript.md +626 -0
  11. package/docs/vs-typescript.md +107 -0
  12. package/esm.js +8 -0
  13. package/jest.js +33 -0
  14. package/lib/black.js +17 -13
  15. package/lib/browser.js +2 -4
  16. package/lib/cli.js +207 -136
  17. package/lib/compiler.js +1162 -93
  18. package/lib/decision.js +10 -10
  19. package/lib/diagnostics.js +219 -0
  20. package/lib/esm-hooks.js +60 -0
  21. package/lib/fast-parser.js +3469 -0
  22. package/lib/functional-helpers.js +26 -9
  23. package/lib/helpers.js +67 -50
  24. package/lib/js-nodes.js +151 -12
  25. package/lib/lexer.js +1499 -0
  26. package/lib/light-module.js +200 -63
  27. package/lib/mamba.js +372 -0
  28. package/lib/module.js +23 -172
  29. package/lib/nodes.js +84 -21
  30. package/lib/optimiser.js +29 -9
  31. package/lib/optparse.js +32 -15
  32. package/lib/parser.js +9433 -4840
  33. package/lib/preprocessor.js +3 -5
  34. package/lib/register.js +49 -56
  35. package/lib/repl.js +28 -9
  36. package/lib/reporter.js +106 -69
  37. package/lib/run.js +3 -5
  38. package/lib/string-scanner.js +94 -0
  39. package/lib/syntax-tables.js +480 -0
  40. package/lib/type-checker.js +258 -44
  41. package/lib/type-helpers.js +28 -10
  42. package/lib/type-resolver.js +256 -34
  43. package/lib/type-scope.js +10 -6
  44. package/lib/type-walker.js +664 -85
  45. package/lib/types.js +17 -7
  46. package/lib/underscore.js +170 -76
  47. package/lib/unicode-classes.js +122 -0
  48. package/mambaview.js +1 -0
  49. package/package.json +49 -46
  50. package/stdlib.js +1 -0
  51. package/vendor/README.md +40 -0
  52. package/vendor/escodegen/LICENSE.BSD +19 -0
  53. package/vendor/escodegen/escodegen.js +2289 -0
  54. package/vendor/estraverse/LICENSE.BSD +19 -0
  55. package/vendor/estraverse/estraverse.js +706 -0
  56. package/vendor/mambacodegen/LICENSE +24 -0
  57. package/vendor/mambacodegen/lib/mscodegen.js +620 -0
  58. package/vendor/patches/escodegen.diff +130 -0
  59. package/vendor/patches/estraverse.diff +51 -0
  60. package/vendor/patches/pegjs.diff +70 -0
  61. package/vendor/patches/source-map.diff +160 -0
  62. package/vendor/source-map/LICENSE +28 -0
  63. package/vendor/source-map/lib/source-map/array-set.js +92 -0
  64. package/vendor/source-map/lib/source-map/base64-vlq.js +140 -0
  65. package/vendor/source-map/lib/source-map/base64.js +38 -0
  66. package/vendor/source-map/lib/source-map/binary-search.js +77 -0
  67. package/vendor/source-map/lib/source-map/source-map-consumer.js +404 -0
  68. package/vendor/source-map/lib/source-map/source-map-generator.js +229 -0
  69. package/vendor/source-map/lib/source-map/source-node.js +201 -0
  70. package/vendor/source-map/lib/source-map/util.js +57 -0
  71. package/vendor/source-map/lib/source-map.js +8 -0
  72. package/bin/fix +0 -17
  73. package/bin/updateImports +0 -46
  74. package/build.js +0 -25
  75. package/lib/module-esm.js +0 -901
  76. package/lib/pegjs-kofu-plugin.js +0 -35
  77. package/lib/pegjs-mamba-plugin.js +0 -35
  78. package/lib/processor-esm.js +0 -507
  79. package/mamba +0 -259
  80. package/msast.js +0 -1
  81. package/utils/gulp-mamba.js +0 -29
  82. /package/{lib/esm.js → vendor/patches/mambacodegen.diff} +0 -0
@@ -0,0 +1,706 @@
1
+ /*
2
+ Copyright (C) 2012-2013 Yusuke Suzuki <utatane.tea@gmail.com>
3
+ Copyright (C) 2012 Ariya Hidayat <ariya.hidayat@gmail.com>
4
+
5
+ Redistribution and use in source and binary forms, with or without
6
+ modification, are permitted provided that the following conditions are met:
7
+
8
+ * Redistributions of source code must retain the above copyright
9
+ notice, this list of conditions and the following disclaimer.
10
+ * Redistributions in binary form must reproduce the above copyright
11
+ notice, this list of conditions and the following disclaimer in the
12
+ documentation and/or other materials provided with the distribution.
13
+
14
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17
+ ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
18
+ DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19
+ (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20
+ LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21
+ ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23
+ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24
+ */
25
+ /*jslint vars:false, bitwise:true*/
26
+ /*jshint indent:4*/
27
+ /*global exports:true, define:true*/
28
+ /*
29
+ * Vendored into MambaScript from estraverse 1.3.2 (BSD-2-Clause, see LICENSE.BSD).
30
+ * In-house changes: CommonJS only (UMD/AMD wrapper removed), AwaitExpression added.
31
+ */
32
+ (function (exports) {
33
+ 'use strict';
34
+
35
+ var Syntax,
36
+ isArray,
37
+ VisitorOption,
38
+ VisitorKeys,
39
+ BREAK,
40
+ SKIP;
41
+
42
+ Syntax = {
43
+ AssignmentExpression: 'AssignmentExpression',
44
+ ArrayExpression: 'ArrayExpression',
45
+ ArrowFunctionExpression: 'ArrowFunctionExpression',
46
+ BlockStatement: 'BlockStatement',
47
+ BigIntLiteral: 'BigIntLiteral',
48
+ BinaryExpression: 'BinaryExpression',
49
+ BreakStatement: 'BreakStatement',
50
+ CallExpression: 'CallExpression',
51
+ CatchClause: 'CatchClause',
52
+ ClassBody: 'ClassBody',
53
+ ClassDeclaration: 'ClassDeclaration',
54
+ ClassExpression: 'ClassExpression',
55
+ ConditionalExpression: 'ConditionalExpression',
56
+ ContinueStatement: 'ContinueStatement',
57
+ DebuggerStatement: 'DebuggerStatement',
58
+ DirectiveStatement: 'DirectiveStatement',
59
+ DoWhileStatement: 'DoWhileStatement',
60
+ EmptyStatement: 'EmptyStatement',
61
+ ExpressionStatement: 'ExpressionStatement',
62
+ ForStatement: 'ForStatement',
63
+ ForInStatement: 'ForInStatement',
64
+ ForOfStatement: 'ForOfStatement',
65
+ ImportDeclaration: 'ImportDeclaration',
66
+ ImportSpecifier: 'ImportSpecifier',
67
+ ImportDefaultSpecifier: 'ImportDefaultSpecifier',
68
+ ImportNamespaceSpecifier: 'ImportNamespaceSpecifier',
69
+ ImportExpression: 'ImportExpression',
70
+ ExportNamedDeclaration: 'ExportNamedDeclaration',
71
+ ExportSpecifier: 'ExportSpecifier',
72
+ ExportDefaultDeclaration: 'ExportDefaultDeclaration',
73
+ ExportAllDeclaration: 'ExportAllDeclaration',
74
+ FunctionDeclaration: 'FunctionDeclaration',
75
+ FunctionExpression: 'FunctionExpression',
76
+ Identifier: 'Identifier',
77
+ IfStatement: 'IfStatement',
78
+ Literal: 'Literal',
79
+ LabeledStatement: 'LabeledStatement',
80
+ LogicalExpression: 'LogicalExpression',
81
+ MemberExpression: 'MemberExpression',
82
+ MethodDefinition: 'MethodDefinition',
83
+ NewExpression: 'NewExpression',
84
+ ObjectExpression: 'ObjectExpression',
85
+ Program: 'Program',
86
+ Property: 'Property',
87
+ ReturnStatement: 'ReturnStatement',
88
+ SequenceExpression: 'SequenceExpression',
89
+ SwitchStatement: 'SwitchStatement',
90
+ TaggedTemplateExpression: 'TaggedTemplateExpression',
91
+ TemplateElement: 'TemplateElement',
92
+ TemplateLiteral: 'TemplateLiteral',
93
+ SwitchCase: 'SwitchCase',
94
+ ThisExpression: 'ThisExpression',
95
+ ThrowStatement: 'ThrowStatement',
96
+ TryStatement: 'TryStatement',
97
+ UnaryExpression: 'UnaryExpression',
98
+ AwaitExpression: 'AwaitExpression',
99
+ UpdateExpression: 'UpdateExpression',
100
+ VariableDeclaration: 'VariableDeclaration',
101
+ VariableDeclarator: 'VariableDeclarator',
102
+ WhileStatement: 'WhileStatement',
103
+ WithStatement: 'WithStatement',
104
+ YieldExpression: 'YieldExpression'
105
+ };
106
+
107
+ function ignoreJSHintError() { }
108
+
109
+ isArray = Array.isArray;
110
+ if (!isArray) {
111
+ isArray = function isArray(array) {
112
+ return Object.prototype.toString.call(array) === '[object Array]';
113
+ };
114
+ }
115
+
116
+ function deepCopy(obj) {
117
+ var ret = {}, key, val;
118
+ for (key in obj) {
119
+ if (obj.hasOwnProperty(key)) {
120
+ val = obj[key];
121
+ if (typeof val === 'object' && val !== null) {
122
+ ret[key] = deepCopy(val);
123
+ } else {
124
+ ret[key] = val;
125
+ }
126
+ }
127
+ }
128
+ return ret;
129
+ }
130
+
131
+ function shallowCopy(obj) {
132
+ var ret = {}, key;
133
+ for (key in obj) {
134
+ if (obj.hasOwnProperty(key)) {
135
+ ret[key] = obj[key];
136
+ }
137
+ }
138
+ return ret;
139
+ }
140
+ ignoreJSHintError(shallowCopy);
141
+
142
+ // based on LLVM libc++ upper_bound / lower_bound
143
+ // MIT License
144
+
145
+ function upperBound(array, func) {
146
+ var diff, len, i, current;
147
+
148
+ len = array.length;
149
+ i = 0;
150
+
151
+ while (len) {
152
+ diff = len >>> 1;
153
+ current = i + diff;
154
+ if (func(array[current])) {
155
+ len = diff;
156
+ } else {
157
+ i = current + 1;
158
+ len -= diff + 1;
159
+ }
160
+ }
161
+ return i;
162
+ }
163
+
164
+ function lowerBound(array, func) {
165
+ var diff, len, i, current;
166
+
167
+ len = array.length;
168
+ i = 0;
169
+
170
+ while (len) {
171
+ diff = len >>> 1;
172
+ current = i + diff;
173
+ if (func(array[current])) {
174
+ i = current + 1;
175
+ len -= diff + 1;
176
+ } else {
177
+ len = diff;
178
+ }
179
+ }
180
+ return i;
181
+ }
182
+ ignoreJSHintError(lowerBound);
183
+
184
+ VisitorKeys = {
185
+ AssignmentExpression: ['left', 'right'],
186
+ ArrayExpression: ['elements'],
187
+ ArrowFunctionExpression: ['params', 'body'],
188
+ AwaitExpression: ['argument'],
189
+ BlockStatement: ['body'],
190
+ BinaryExpression: ['left', 'right'],
191
+ BreakStatement: ['label'],
192
+ CallExpression: ['callee', 'arguments'],
193
+ CatchClause: ['param', 'body'],
194
+ ClassBody: ['body'],
195
+ ClassDeclaration: ['id', 'body', 'superClass'],
196
+ ClassExpression: ['id', 'body', 'superClass'],
197
+ ConditionalExpression: ['test', 'consequent', 'alternate'],
198
+ ContinueStatement: ['label'],
199
+ DebuggerStatement: [],
200
+ DirectiveStatement: [],
201
+ DoWhileStatement: ['body', 'test'],
202
+ EmptyStatement: [],
203
+ ExpressionStatement: ['expression'],
204
+ ForStatement: ['init', 'test', 'update', 'body'],
205
+ ForInStatement: ['left', 'right', 'body'],
206
+ ForOfStatement: ['left', 'right', 'body'],
207
+ ImportDeclaration: ['specifiers', 'source'],
208
+ ImportSpecifier: ['imported', 'local'],
209
+ ImportDefaultSpecifier: ['local'],
210
+ ImportNamespaceSpecifier: ['local'],
211
+ ImportExpression: ['source'],
212
+ ExportNamedDeclaration: ['declaration', 'specifiers', 'source'],
213
+ ExportSpecifier: ['local', 'exported'],
214
+ ExportDefaultDeclaration: ['declaration'],
215
+ ExportAllDeclaration: ['source'],
216
+ FunctionDeclaration: ['id', 'params', 'body'],
217
+ FunctionExpression: ['id', 'params', 'body'],
218
+ Identifier: [],
219
+ IfStatement: ['test', 'consequent', 'alternate'],
220
+ Literal: [],
221
+ LabeledStatement: ['label', 'body'],
222
+ LogicalExpression: ['left', 'right'],
223
+ MemberExpression: ['object', 'property'],
224
+ MethodDefinition: ['key', 'value'],
225
+ NewExpression: ['callee', 'arguments'],
226
+ ObjectExpression: ['properties'],
227
+ Program: ['body'],
228
+ Property: ['key', 'value'],
229
+ ReturnStatement: ['argument'],
230
+ SequenceExpression: ['expressions'],
231
+ SwitchStatement: ['discriminant', 'cases'],
232
+ SwitchCase: ['test', 'consequent'],
233
+ ThisExpression: [],
234
+ BigIntLiteral: [],
235
+ TaggedTemplateExpression: ['tag', 'quasi'],
236
+ TemplateLiteral: ['quasis', 'expressions'],
237
+ TemplateElement: [],
238
+ ThrowStatement: ['argument'],
239
+ TryStatement: ['block', 'handlers', 'handler', 'guardedHandlers', 'finalizer'],
240
+ UnaryExpression: ['argument'],
241
+ UpdateExpression: ['argument'],
242
+ VariableDeclaration: ['declarations'],
243
+ VariableDeclarator: ['id', 'init'],
244
+ WhileStatement: ['test', 'body'],
245
+ WithStatement: ['object', 'body'],
246
+ YieldExpression: ['argument']
247
+ };
248
+
249
+ // unique id
250
+ BREAK = {};
251
+ SKIP = {};
252
+
253
+ VisitorOption = {
254
+ Break: BREAK,
255
+ Skip: SKIP
256
+ };
257
+
258
+ function Reference(parent, key) {
259
+ this.parent = parent;
260
+ this.key = key;
261
+ }
262
+
263
+ Reference.prototype.replace = function replace(node) {
264
+ this.parent[this.key] = node;
265
+ };
266
+
267
+ function Element(node, path, wrap, ref) {
268
+ this.node = node;
269
+ this.path = path;
270
+ this.wrap = wrap;
271
+ this.ref = ref;
272
+ }
273
+
274
+ function Controller() { }
275
+
276
+ // API:
277
+ // return property path array from root to current node
278
+ Controller.prototype.path = function path() {
279
+ var i, iz, j, jz, result, element;
280
+
281
+ function addToPath(result, path) {
282
+ if (isArray(path)) {
283
+ for (j = 0, jz = path.length; j < jz; ++j) {
284
+ result.push(path[j]);
285
+ }
286
+ } else {
287
+ result.push(path);
288
+ }
289
+ }
290
+
291
+ // root node
292
+ if (!this.__current.path) {
293
+ return null;
294
+ }
295
+
296
+ // first node is sentinel, second node is root element
297
+ result = [];
298
+ for (i = 2, iz = this.__leavelist.length; i < iz; ++i) {
299
+ element = this.__leavelist[i];
300
+ addToPath(result, element.path);
301
+ }
302
+ addToPath(result, this.__current.path);
303
+ return result;
304
+ };
305
+
306
+ // API:
307
+ // return array of parent elements
308
+ Controller.prototype.parents = function parents() {
309
+ var i, iz, result;
310
+
311
+ // first node is sentinel
312
+ result = [];
313
+ for (i = 1, iz = this.__leavelist.length; i < iz; ++i) {
314
+ result.push(this.__leavelist[i].node);
315
+ }
316
+
317
+ return result;
318
+ };
319
+
320
+ // API:
321
+ // return current node
322
+ Controller.prototype.current = function current() {
323
+ return this.__current.node;
324
+ };
325
+
326
+ Controller.prototype.__execute = function __execute(callback, element) {
327
+ var previous, result;
328
+
329
+ result = undefined;
330
+
331
+ previous = this.__current;
332
+ this.__current = element;
333
+ this.__state = null;
334
+ if (callback) {
335
+ result = callback.call(this, element.node, this.__leavelist[this.__leavelist.length - 1].node);
336
+ }
337
+ this.__current = previous;
338
+
339
+ return result;
340
+ };
341
+
342
+ // API:
343
+ // notify control skip / break
344
+ Controller.prototype.notify = function notify(flag) {
345
+ this.__state = flag;
346
+ };
347
+
348
+ // API:
349
+ // skip child nodes of current node
350
+ Controller.prototype.skip = function () {
351
+ this.notify(SKIP);
352
+ };
353
+
354
+ // API:
355
+ // break traversals
356
+ Controller.prototype['break'] = function () {
357
+ this.notify(BREAK);
358
+ };
359
+
360
+ Controller.prototype.__initialize = function(root, visitor) {
361
+ this.visitor = visitor;
362
+ this.root = root;
363
+ this.__worklist = [];
364
+ this.__leavelist = [];
365
+ this.__current = null;
366
+ this.__state = null;
367
+ };
368
+
369
+ Controller.prototype.traverse = function traverse(root, visitor) {
370
+ var worklist,
371
+ leavelist,
372
+ element,
373
+ node,
374
+ nodeType,
375
+ ret,
376
+ key,
377
+ current,
378
+ current2,
379
+ candidates,
380
+ candidate,
381
+ sentinel;
382
+
383
+ this.__initialize(root, visitor);
384
+
385
+ sentinel = {};
386
+
387
+ // reference
388
+ worklist = this.__worklist;
389
+ leavelist = this.__leavelist;
390
+
391
+ // initialize
392
+ worklist.push(new Element(root, null, null, null));
393
+ leavelist.push(new Element(null, null, null, null));
394
+
395
+ while (worklist.length) {
396
+ element = worklist.pop();
397
+
398
+ if (element === sentinel) {
399
+ element = leavelist.pop();
400
+
401
+ ret = this.__execute(visitor.leave, element);
402
+
403
+ if (this.__state === BREAK || ret === BREAK) {
404
+ return;
405
+ }
406
+ continue;
407
+ }
408
+
409
+ if (element.node) {
410
+
411
+ ret = this.__execute(visitor.enter, element);
412
+
413
+ if (this.__state === BREAK || ret === BREAK) {
414
+ return;
415
+ }
416
+
417
+ worklist.push(sentinel);
418
+ leavelist.push(element);
419
+
420
+ if (this.__state === SKIP || ret === SKIP) {
421
+ continue;
422
+ }
423
+
424
+ node = element.node;
425
+ nodeType = element.wrap || node.type;
426
+ candidates = VisitorKeys[nodeType];
427
+
428
+ current = candidates.length;
429
+ while ((current -= 1) >= 0) {
430
+ key = candidates[current];
431
+ candidate = node[key];
432
+ if (!candidate) {
433
+ continue;
434
+ }
435
+
436
+ if (!isArray(candidate)) {
437
+ worklist.push(new Element(candidate, key, null, null));
438
+ continue;
439
+ }
440
+
441
+ current2 = candidate.length;
442
+ while ((current2 -= 1) >= 0) {
443
+ if (!candidate[current2]) {
444
+ continue;
445
+ }
446
+ if (nodeType === Syntax.ObjectExpression && 'properties' === candidates[current]) {
447
+ element = new Element(candidate[current2], [key, current2], 'Property', null);
448
+ } else {
449
+ element = new Element(candidate[current2], [key, current2], null, null);
450
+ }
451
+ worklist.push(element);
452
+ }
453
+ }
454
+ }
455
+ }
456
+ };
457
+
458
+ Controller.prototype.replace = function replace(root, visitor) {
459
+ var worklist,
460
+ leavelist,
461
+ node,
462
+ nodeType,
463
+ target,
464
+ element,
465
+ current,
466
+ current2,
467
+ candidates,
468
+ candidate,
469
+ sentinel,
470
+ outer,
471
+ key;
472
+
473
+ this.__initialize(root, visitor);
474
+
475
+ sentinel = {};
476
+
477
+ // reference
478
+ worklist = this.__worklist;
479
+ leavelist = this.__leavelist;
480
+
481
+ // initialize
482
+ outer = {
483
+ root: root
484
+ };
485
+ element = new Element(root, null, null, new Reference(outer, 'root'));
486
+ worklist.push(element);
487
+ leavelist.push(element);
488
+
489
+ while (worklist.length) {
490
+ element = worklist.pop();
491
+
492
+ if (element === sentinel) {
493
+ element = leavelist.pop();
494
+
495
+ target = this.__execute(visitor.leave, element);
496
+
497
+ // node may be replaced with null,
498
+ // so distinguish between undefined and null in this place
499
+ if (target !== undefined && target !== BREAK && target !== SKIP) {
500
+ // replace
501
+ element.ref.replace(target);
502
+ }
503
+
504
+ if (this.__state === BREAK || target === BREAK) {
505
+ return outer.root;
506
+ }
507
+ continue;
508
+ }
509
+
510
+ target = this.__execute(visitor.enter, element);
511
+
512
+ // node may be replaced with null,
513
+ // so distinguish between undefined and null in this place
514
+ if (target !== undefined && target !== BREAK && target !== SKIP) {
515
+ // replace
516
+ element.ref.replace(target);
517
+ element.node = target;
518
+ }
519
+
520
+ if (this.__state === BREAK || target === BREAK) {
521
+ return outer.root;
522
+ }
523
+
524
+ // node may be null
525
+ node = element.node;
526
+ if (!node) {
527
+ continue;
528
+ }
529
+
530
+ worklist.push(sentinel);
531
+ leavelist.push(element);
532
+
533
+ if (this.__state === SKIP || target === SKIP) {
534
+ continue;
535
+ }
536
+
537
+ nodeType = element.wrap || node.type;
538
+ candidates = VisitorKeys[nodeType];
539
+
540
+ current = candidates.length;
541
+ while ((current -= 1) >= 0) {
542
+ key = candidates[current];
543
+ candidate = node[key];
544
+ if (!candidate) {
545
+ continue;
546
+ }
547
+
548
+ if (!isArray(candidate)) {
549
+ worklist.push(new Element(candidate, key, null, new Reference(node, key)));
550
+ continue;
551
+ }
552
+
553
+ current2 = candidate.length;
554
+ while ((current2 -= 1) >= 0) {
555
+ if (!candidate[current2]) {
556
+ continue;
557
+ }
558
+ if (nodeType === Syntax.ObjectExpression && 'properties' === candidates[current]) {
559
+ element = new Element(candidate[current2], [key, current2], 'Property', new Reference(candidate, current2));
560
+ } else {
561
+ element = new Element(candidate[current2], [key, current2], null, new Reference(candidate, current2));
562
+ }
563
+ worklist.push(element);
564
+ }
565
+ }
566
+ }
567
+
568
+ return outer.root;
569
+ };
570
+
571
+ function traverse(root, visitor) {
572
+ var controller = new Controller();
573
+ return controller.traverse(root, visitor);
574
+ }
575
+
576
+ function replace(root, visitor) {
577
+ var controller = new Controller();
578
+ return controller.replace(root, visitor);
579
+ }
580
+
581
+ function extendCommentRange(comment, tokens) {
582
+ var target;
583
+
584
+ target = upperBound(tokens, function search(token) {
585
+ return token.range[0] > comment.range[0];
586
+ });
587
+
588
+ comment.extendedRange = [comment.range[0], comment.range[1]];
589
+
590
+ if (target !== tokens.length) {
591
+ comment.extendedRange[1] = tokens[target].range[0];
592
+ }
593
+
594
+ target -= 1;
595
+ if (target >= 0) {
596
+ comment.extendedRange[0] = tokens[target].range[1];
597
+ }
598
+
599
+ return comment;
600
+ }
601
+
602
+ function attachComments(tree, providedComments, tokens) {
603
+ // At first, we should calculate extended comment ranges.
604
+ var comments = [], comment, len, i, cursor;
605
+
606
+ if (!tree.range) {
607
+ throw new Error('attachComments needs range information');
608
+ }
609
+
610
+ // tokens array is empty, we attach comments to tree as 'leadingComments'
611
+ if (!tokens.length) {
612
+ if (providedComments.length) {
613
+ for (i = 0, len = providedComments.length; i < len; i += 1) {
614
+ comment = deepCopy(providedComments[i]);
615
+ comment.extendedRange = [0, tree.range[0]];
616
+ comments.push(comment);
617
+ }
618
+ tree.leadingComments = comments;
619
+ }
620
+ return tree;
621
+ }
622
+
623
+ for (i = 0, len = providedComments.length; i < len; i += 1) {
624
+ comments.push(extendCommentRange(deepCopy(providedComments[i]), tokens));
625
+ }
626
+
627
+ // This is based on John Freeman's implementation.
628
+ cursor = 0;
629
+ traverse(tree, {
630
+ enter: function (node) {
631
+ var comment;
632
+
633
+ while (cursor < comments.length) {
634
+ comment = comments[cursor];
635
+ if (comment.extendedRange[1] > node.range[0]) {
636
+ break;
637
+ }
638
+
639
+ if (comment.extendedRange[1] === node.range[0]) {
640
+ if (!node.leadingComments) {
641
+ node.leadingComments = [];
642
+ }
643
+ node.leadingComments.push(comment);
644
+ comments.splice(cursor, 1);
645
+ } else {
646
+ cursor += 1;
647
+ }
648
+ }
649
+
650
+ // already out of owned node
651
+ if (cursor === comments.length) {
652
+ return VisitorOption.Break;
653
+ }
654
+
655
+ if (comments[cursor].extendedRange[0] > node.range[1]) {
656
+ return VisitorOption.Skip;
657
+ }
658
+ }
659
+ });
660
+
661
+ cursor = 0;
662
+ traverse(tree, {
663
+ leave: function (node) {
664
+ var comment;
665
+
666
+ while (cursor < comments.length) {
667
+ comment = comments[cursor];
668
+ if (node.range[1] < comment.extendedRange[0]) {
669
+ break;
670
+ }
671
+
672
+ if (node.range[1] === comment.extendedRange[0]) {
673
+ if (!node.trailingComments) {
674
+ node.trailingComments = [];
675
+ }
676
+ node.trailingComments.push(comment);
677
+ comments.splice(cursor, 1);
678
+ } else {
679
+ cursor += 1;
680
+ }
681
+ }
682
+
683
+ // already out of owned node
684
+ if (cursor === comments.length) {
685
+ return VisitorOption.Break;
686
+ }
687
+
688
+ if (comments[cursor].extendedRange[0] > node.range[1]) {
689
+ return VisitorOption.Skip;
690
+ }
691
+ }
692
+ });
693
+
694
+ return tree;
695
+ }
696
+
697
+ exports.version = '1.3.2';
698
+ exports.Syntax = Syntax;
699
+ exports.traverse = traverse;
700
+ exports.replace = replace;
701
+ exports.attachComments = attachComments;
702
+ exports.VisitorKeys = VisitorKeys;
703
+ exports.VisitorOption = VisitorOption;
704
+ exports.Controller = Controller;
705
+ }(exports));
706
+ /* vim: set sw=4 ts=4 et tw=80 : */