1 | import { __read } from "tslib";
|
2 | import { SourceType } from 'coffee-lex';
|
3 | import { FunctionApplication, NewOp, SoakedFunctionApplication } from 'decaffeinate-parser/dist/nodes';
|
4 | import { AVOID_IIFES, AVOID_INLINE_ASSIGNMENTS, CLEAN_UP_IMPLICIT_RETURNS } from '../suggestions';
|
5 | import adjustIndent from '../utils/adjustIndent';
|
6 | import { logger } from '../utils/debug';
|
7 | import notNull from '../utils/notNull';
|
8 | import PatcherError from '../utils/PatchError';
|
9 | import referencesArguments from '../utils/referencesArguments';
|
10 | import { isFunction, isSemanticToken } from '../utils/types';
|
11 | var NodePatcher = (function () {
|
12 | function NodePatcher(_a) {
|
13 | var _this = this;
|
14 | var node = _a.node, context = _a.context, editor = _a.editor, options = _a.options, addSuggestion = _a.addSuggestion;
|
15 | this.adjustedIndentLevel = 0;
|
16 | this._assignee = false;
|
17 | this._containsYield = false;
|
18 | this._containsAwait = false;
|
19 | this._deferredSuffix = '';
|
20 | this._expression = false;
|
21 | this._hadUnparenthesizedNegation = false;
|
22 | this._implicitlyReturns = false;
|
23 | this._repeatableOptions = null;
|
24 | this._repeatCode = null;
|
25 | this._returns = false;
|
26 |
|
27 | this.addThisAssignmentAtScopeHeader = null;
|
28 | this.addDefaultParamAssignmentAtScopeHeader = null;
|
29 | this.log = logger(this.constructor.name);
|
30 | this.node = node;
|
31 | this.context = context;
|
32 | this.editor = editor;
|
33 | this.options = options;
|
34 | this.addSuggestion = addSuggestion;
|
35 | this.withPrettyErrors(function () { return _this.setupLocationInformation(); });
|
36 | }
|
37 | |
38 |
|
39 |
|
40 |
|
41 | NodePatcher.patcherClassForChildNode = function (_node, _property) {
|
42 | return null;
|
43 | };
|
44 | |
45 |
|
46 |
|
47 |
|
48 | NodePatcher.patcherClassOverrideForNode = function (_node) {
|
49 | return null;
|
50 | };
|
51 | |
52 |
|
53 |
|
54 | NodePatcher.prototype.setupLocationInformation = function () {
|
55 | var _a = this, node = _a.node, context = _a.context;
|
56 | |
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 | this.contentStart = node.start;
|
65 | this.contentEnd = node.end;
|
66 | if (this.shouldTrimContentRange()) {
|
67 | this.trimContentRange();
|
68 | }
|
69 | var tokens = context.sourceTokens;
|
70 | var firstSourceTokenIndex = tokens.indexOfTokenStartingAtSourceIndex(this.contentStart);
|
71 | var lastSourceTokenIndex = tokens.indexOfTokenEndingAtSourceIndex(this.contentEnd);
|
72 | if (!firstSourceTokenIndex || !lastSourceTokenIndex) {
|
73 | if (node.type === 'Program') {
|
74 |
|
75 | return;
|
76 | }
|
77 | throw this.error("cannot find first or last token in " + node.type + " node");
|
78 | }
|
79 | this.contentStartTokenIndex = firstSourceTokenIndex;
|
80 | this.contentEndTokenIndex = lastSourceTokenIndex;
|
81 | var outerStartTokenIndex = firstSourceTokenIndex;
|
82 | var outerEndTokenIndex = lastSourceTokenIndex;
|
83 | var innerStartTokenIndex = firstSourceTokenIndex;
|
84 | var innerEndTokenIndex = lastSourceTokenIndex;
|
85 | for (;;) {
|
86 | var previousSurroundingTokenIndex = tokens.lastIndexOfTokenMatchingPredicate(isSemanticToken, outerStartTokenIndex.previous());
|
87 | var nextSurroundingTokenIndex = tokens.indexOfTokenMatchingPredicate(isSemanticToken, outerEndTokenIndex.next());
|
88 | if (!previousSurroundingTokenIndex || !nextSurroundingTokenIndex) {
|
89 | break;
|
90 | }
|
91 | var previousSurroundingToken = tokens.tokenAtIndex(previousSurroundingTokenIndex);
|
92 | var nextSurroundingToken = tokens.tokenAtIndex(nextSurroundingTokenIndex);
|
93 | if (!previousSurroundingToken ||
|
94 | (previousSurroundingToken.type !== SourceType.LPAREN && previousSurroundingToken.type !== SourceType.CALL_START)) {
|
95 | break;
|
96 | }
|
97 | if (!nextSurroundingToken ||
|
98 | (nextSurroundingToken.type !== SourceType.RPAREN && nextSurroundingToken.type !== SourceType.CALL_END)) {
|
99 | break;
|
100 | }
|
101 | if (innerStartTokenIndex === firstSourceTokenIndex) {
|
102 | innerStartTokenIndex = previousSurroundingTokenIndex;
|
103 | }
|
104 | if (innerEndTokenIndex === lastSourceTokenIndex) {
|
105 | innerEndTokenIndex = nextSurroundingTokenIndex;
|
106 | }
|
107 | outerStartTokenIndex = previousSurroundingTokenIndex;
|
108 | outerEndTokenIndex = nextSurroundingTokenIndex;
|
109 | }
|
110 | this.innerStartTokenIndex = innerStartTokenIndex;
|
111 | this.innerEndTokenIndex = innerEndTokenIndex;
|
112 | this.outerStartTokenIndex = outerStartTokenIndex;
|
113 | this.outerEndTokenIndex = outerEndTokenIndex;
|
114 | |
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 |
|
131 | if (innerStartTokenIndex === firstSourceTokenIndex) {
|
132 | this.innerStart = this.contentStart;
|
133 | }
|
134 | else {
|
135 | this.innerStart = notNull(tokens.tokenAtIndex(innerStartTokenIndex)).end;
|
136 | }
|
137 | if (innerEndTokenIndex === lastSourceTokenIndex) {
|
138 | this.innerEnd = this.contentEnd;
|
139 | }
|
140 | else {
|
141 | this.innerEnd = notNull(tokens.tokenAtIndex(innerEndTokenIndex)).start;
|
142 | }
|
143 | this.outerStart = notNull(tokens.tokenAtIndex(outerStartTokenIndex)).start;
|
144 | this.outerEnd = notNull(tokens.tokenAtIndex(outerEndTokenIndex)).end;
|
145 | };
|
146 | |
147 |
|
148 |
|
149 |
|
150 |
|
151 | NodePatcher.prototype.trimContentRange = function () {
|
152 | var context = this.context;
|
153 | for (;;) {
|
154 | var startChar = context.source[this.contentStart];
|
155 | if (startChar === ' ' || startChar === '\t') {
|
156 | this.contentStart++;
|
157 | }
|
158 | else {
|
159 | break;
|
160 | }
|
161 | }
|
162 | for (;;) {
|
163 | var lastChar = context.source[this.contentEnd - 1];
|
164 | if (lastChar === ' ' || lastChar === '\t') {
|
165 | this.contentEnd--;
|
166 | }
|
167 | else {
|
168 | break;
|
169 | }
|
170 | }
|
171 | };
|
172 | |
173 |
|
174 |
|
175 | NodePatcher.prototype.shouldTrimContentRange = function () {
|
176 | return false;
|
177 | };
|
178 | |
179 |
|
180 |
|
181 |
|
182 | NodePatcher.prototype.initialize = function () {
|
183 |
|
184 | };
|
185 | |
186 |
|
187 |
|
188 |
|
189 |
|
190 | NodePatcher.prototype.patch = function (options) {
|
191 | var _this = this;
|
192 | if (options === void 0) { options = {}; }
|
193 | this.withPrettyErrors(function () {
|
194 | if (_this._repeatableOptions !== null) {
|
195 | _this._repeatCode = _this.patchAsRepeatableExpression(_this._repeatableOptions, options);
|
196 | }
|
197 | else if (_this.forcedToPatchAsExpression()) {
|
198 | _this.patchAsForcedExpression(options);
|
199 | _this.commitDeferredSuffix();
|
200 | }
|
201 | else if (_this.willPatchAsExpression()) {
|
202 | _this.patchAsExpression(options);
|
203 | _this.commitDeferredSuffix();
|
204 | }
|
205 | else {
|
206 | _this.patchAsStatement(options);
|
207 | _this.commitDeferredSuffix();
|
208 | }
|
209 | });
|
210 | };
|
211 | |
212 |
|
213 |
|
214 |
|
215 |
|
216 |
|
217 |
|
218 |
|
219 |
|
220 |
|
221 |
|
222 |
|
223 | NodePatcher.prototype.patchRepeatable = function (repeatableOptions) {
|
224 | if (repeatableOptions === void 0) { repeatableOptions = {}; }
|
225 | this.setRequiresRepeatableExpression(repeatableOptions);
|
226 | this.patch();
|
227 | return this.getRepeatCode();
|
228 | };
|
229 | |
230 |
|
231 |
|
232 |
|
233 |
|
234 |
|
235 |
|
236 |
|
237 | NodePatcher.prototype.patchAndGetCode = function (options) {
|
238 | var _this = this;
|
239 | if (options === void 0) { options = {}; }
|
240 | return this.captureCodeForPatchOperation(function () { return _this.patch(options); });
|
241 | };
|
242 | NodePatcher.prototype.captureCodeForPatchOperation = function (patchFn) {
|
243 | var sliceStart = this.contentStart > 0 ? this.contentStart - 1 : 0;
|
244 |
|
245 |
|
246 |
|
247 | var beforeCode = null;
|
248 | while (beforeCode === null) {
|
249 | try {
|
250 | beforeCode = this.slice(sliceStart, this.contentStart);
|
251 | }
|
252 | catch (e) {
|
253 |
|
254 |
|
255 |
|
256 | sliceStart -= 1;
|
257 | if (sliceStart < 0) {
|
258 | throw this.error('Could not find a valid index to slice for patch operation.');
|
259 | }
|
260 | }
|
261 | }
|
262 | patchFn();
|
263 | var code = this.slice(sliceStart, this.contentEnd);
|
264 | var startIndex = 0;
|
265 | while (startIndex < beforeCode.length && startIndex < code.length && beforeCode[startIndex] === code[startIndex]) {
|
266 | startIndex++;
|
267 | }
|
268 | return code.substr(startIndex);
|
269 | };
|
270 | |
271 |
|
272 |
|
273 | NodePatcher.prototype.withPrettyErrors = function (body) {
|
274 | try {
|
275 | body();
|
276 | }
|
277 | catch (err) {
|
278 | if (!PatcherError.detect(err)) {
|
279 | throw this.error(err.message, this.contentStart, this.contentEnd, err);
|
280 | }
|
281 | else {
|
282 | throw err;
|
283 | }
|
284 | }
|
285 | };
|
286 | |
287 |
|
288 |
|
289 |
|
290 |
|
291 |
|
292 |
|
293 |
|
294 |
|
295 |
|
296 |
|
297 |
|
298 |
|
299 |
|
300 |
|
301 |
|
302 | NodePatcher.prototype.patchAsRepeatableExpression = function (repeatableOptions, patchOptions) {
|
303 | var _this = this;
|
304 | if (repeatableOptions === void 0) { repeatableOptions = {}; }
|
305 | if (patchOptions === void 0) { patchOptions = {}; }
|
306 | if (this.isRepeatable() && !repeatableOptions.forceRepeat) {
|
307 | return this.captureCodeForPatchOperation(function () {
|
308 | _this.patchAsForcedExpression(patchOptions);
|
309 | _this.commitDeferredSuffix();
|
310 | });
|
311 | }
|
312 | else {
|
313 | this.addSuggestion(AVOID_INLINE_ASSIGNMENTS);
|
314 |
|
315 |
|
316 | if (repeatableOptions.parens) {
|
317 | this.insert(this.innerStart, '(');
|
318 | }
|
319 | var ref = this.claimFreeBinding(repeatableOptions.ref);
|
320 | this.insert(this.innerStart, ref + " = ");
|
321 | this.patchAsForcedExpression(patchOptions);
|
322 | this.commitDeferredSuffix();
|
323 | if (repeatableOptions.parens) {
|
324 | this.insert(this.innerEnd, ')');
|
325 | }
|
326 | return ref;
|
327 | }
|
328 | };
|
329 | |
330 |
|
331 |
|
332 |
|
333 | NodePatcher.prototype.patchAsExpression = function (_options) {
|
334 | if (_options === void 0) { _options = {}; }
|
335 | throw this.error("'patchAsExpression' must be overridden in subclasses");
|
336 | };
|
337 | |
338 |
|
339 |
|
340 | NodePatcher.prototype.patchAsStatement = function (options) {
|
341 | if (options === void 0) { options = {}; }
|
342 | var addParens = this.statementShouldAddParens();
|
343 | if (addParens) {
|
344 | this.insert(this.outerStart, '(');
|
345 | }
|
346 | this.patchAsExpression(options);
|
347 | if (addParens) {
|
348 | this.insert(this.outerEnd, ')');
|
349 | }
|
350 | };
|
351 | |
352 |
|
353 |
|
354 |
|
355 |
|
356 | NodePatcher.prototype.patchAsForcedExpression = function (options) {
|
357 | if (options === void 0) { options = {}; }
|
358 | this.patchAsExpression(options);
|
359 | };
|
360 | |
361 |
|
362 |
|
363 | NodePatcher.prototype.insert = function (index, content) {
|
364 | if (typeof index !== 'number') {
|
365 | throw new Error("cannot insert " + JSON.stringify(content) + " at non-numeric index " + index);
|
366 | }
|
367 | this.log('INSERT', index, JSON.stringify(content), 'BEFORE', JSON.stringify(this.context.source.slice(index, index + 8)));
|
368 | this.adjustBoundsToInclude(index);
|
369 | this.editor.appendLeft(index, content);
|
370 | };
|
371 | |
372 |
|
373 |
|
374 |
|
375 |
|
376 |
|
377 |
|
378 |
|
379 |
|
380 | NodePatcher.prototype.prependLeft = function (index, content) {
|
381 | if (typeof index !== 'number') {
|
382 | throw new Error("cannot insert " + JSON.stringify(content) + " at non-numeric index " + index);
|
383 | }
|
384 | this.log('PREPEND LEFT', index, JSON.stringify(content), 'BEFORE', JSON.stringify(this.context.source.slice(index, index + 8)));
|
385 | this.adjustBoundsToInclude(index);
|
386 | this.editor.prependLeft(index, content);
|
387 | };
|
388 | NodePatcher.prototype.allowPatchingOuterBounds = function () {
|
389 | return false;
|
390 | };
|
391 | |
392 |
|
393 |
|
394 | NodePatcher.prototype.getEditingBounds = function () {
|
395 | var boundingPatcher = this.getBoundingPatcher();
|
396 |
|
397 |
|
398 |
|
399 |
|
400 | if (boundingPatcher.parent &&
|
401 | (this.isNodeFunctionApplication(boundingPatcher.parent.node) ||
|
402 | boundingPatcher.parent.node.type === 'ArrayInitialiser')) {
|
403 | boundingPatcher = boundingPatcher.parent;
|
404 | }
|
405 | if (this.allowPatchingOuterBounds()) {
|
406 | return [boundingPatcher.outerStart, boundingPatcher.outerEnd];
|
407 | }
|
408 | else {
|
409 | return [boundingPatcher.innerStart, boundingPatcher.innerEnd];
|
410 | }
|
411 | };
|
412 | |
413 |
|
414 |
|
415 | NodePatcher.prototype.isIndexEditable = function (index) {
|
416 | var _a = __read(this.getEditingBounds(), 2), start = _a[0], end = _a[1];
|
417 | return index >= start && index <= end;
|
418 | };
|
419 | |
420 |
|
421 |
|
422 | NodePatcher.prototype.assertEditableIndex = function (index) {
|
423 | if (!this.isIndexEditable(index)) {
|
424 | var _a = __read(this.getEditingBounds(), 2), start = _a[0], end = _a[1];
|
425 | throw this.error("cannot edit index " + index + " because it is not editable (i.e. outside [" + start + ", " + end + "))", start, end);
|
426 | }
|
427 | };
|
428 | |
429 |
|
430 |
|
431 |
|
432 |
|
433 | NodePatcher.prototype.adjustBoundsToInclude = function (index) {
|
434 | this.assertEditableIndex(index);
|
435 | if (index < this.innerStart) {
|
436 | this.log('Moving `innerStart` from', this.innerStart, 'to', index);
|
437 | this.innerStart = index;
|
438 | }
|
439 | if (index > this.innerEnd) {
|
440 | this.log('Moving `innerEnd` from', this.innerEnd, 'to', index);
|
441 | this.innerEnd = index;
|
442 | }
|
443 | if (index < this.outerStart) {
|
444 | this.log('Moving `outerStart` from', this.outerStart, 'to', index);
|
445 | this.outerStart = index;
|
446 | }
|
447 | if (index > this.outerEnd) {
|
448 | this.log('Moving `outerEnd` from', this.outerEnd, 'to', index);
|
449 | this.outerEnd = index;
|
450 | }
|
451 | if (this.parent) {
|
452 | this.parent.adjustBoundsToInclude(index);
|
453 | }
|
454 | };
|
455 | |
456 |
|
457 |
|
458 | NodePatcher.prototype.overwrite = function (start, end, content) {
|
459 | if (typeof start !== 'number' || typeof end !== 'number') {
|
460 | throw new Error("cannot overwrite non-numeric range [" + start + ", " + end + ") " + ("with " + JSON.stringify(content)));
|
461 | }
|
462 | this.log('OVERWRITE', "[" + start + ", " + end + ")", JSON.stringify(this.context.source.slice(start, end)), '→', JSON.stringify(content));
|
463 | this.editor.overwrite(start, end, content);
|
464 | };
|
465 | |
466 |
|
467 |
|
468 | NodePatcher.prototype.remove = function (start, end) {
|
469 | if (typeof start !== 'number' || typeof end !== 'number') {
|
470 | throw new Error("cannot remove non-numeric range [" + start + ", " + end + ")");
|
471 | }
|
472 | this.log('REMOVE', "[" + start + ", " + end + ")", JSON.stringify(this.context.source.slice(start, end)));
|
473 | this.editor.remove(start, end);
|
474 | };
|
475 | |
476 |
|
477 |
|
478 | NodePatcher.prototype.move = function (start, end, index) {
|
479 | if (typeof start !== 'number' || typeof end !== 'number') {
|
480 | throw this.error("cannot remove non-numeric range [" + start + ", " + end + ")");
|
481 | }
|
482 | if (typeof index !== 'number') {
|
483 | throw this.error("cannot move to non-numeric index: " + index);
|
484 | }
|
485 | this.log('MOVE', "[" + start + ", " + end + ") \u2192 " + index, JSON.stringify(this.context.source.slice(start, end)), 'BEFORE', JSON.stringify(this.context.source.slice(index, index + 8)));
|
486 | this.editor.move(start, end, index);
|
487 | };
|
488 | |
489 |
|
490 |
|
491 | NodePatcher.prototype.slice = function (start, end) {
|
492 |
|
493 | if (end === 0) {
|
494 | return '';
|
495 | }
|
496 | return this.editor.slice(start, end);
|
497 | };
|
498 | |
499 |
|
500 |
|
501 | NodePatcher.prototype.startsWith = function (string) {
|
502 | return this.context.source.slice(this.contentStart, this.contentStart + string.length) === string;
|
503 | };
|
504 | |
505 |
|
506 |
|
507 | NodePatcher.prototype.endsWith = function (string) {
|
508 | return this.context.source.slice(this.contentEnd - string.length, this.contentEnd) === string;
|
509 | };
|
510 | |
511 |
|
512 |
|
513 | NodePatcher.prototype.setRequiresExpression = function () {
|
514 | this.setExpression(true);
|
515 | };
|
516 | |
517 |
|
518 |
|
519 | NodePatcher.prototype.setExpression = function (force) {
|
520 | if (force === void 0) { force = false; }
|
521 | if (force) {
|
522 | if (!this.canPatchAsExpression()) {
|
523 | throw this.error("cannot represent " + this.node.type + " as an expression");
|
524 | }
|
525 | }
|
526 | else if (!this.prefersToPatchAsExpression()) {
|
527 | return false;
|
528 | }
|
529 | this._expression = true;
|
530 | return true;
|
531 | };
|
532 | |
533 |
|
534 |
|
535 |
|
536 | NodePatcher.prototype.prefersToPatchAsExpression = function () {
|
537 | return this.canPatchAsExpression();
|
538 | };
|
539 | |
540 |
|
541 |
|
542 | NodePatcher.prototype.canPatchAsExpression = function () {
|
543 | return true;
|
544 | };
|
545 | |
546 |
|
547 |
|
548 | NodePatcher.prototype.willPatchAsExpression = function () {
|
549 | return this._expression;
|
550 | };
|
551 | |
552 |
|
553 |
|
554 | NodePatcher.prototype.forcedToPatchAsExpression = function () {
|
555 | return this.willPatchAsExpression() && !this.prefersToPatchAsExpression();
|
556 | };
|
557 | |
558 |
|
559 |
|
560 |
|
561 | NodePatcher.prototype.setAssignee = function () {
|
562 | this._assignee = true;
|
563 | };
|
564 | |
565 |
|
566 |
|
567 |
|
568 | NodePatcher.prototype.isAssignee = function () {
|
569 | return this._assignee;
|
570 | };
|
571 | |
572 |
|
573 |
|
574 | NodePatcher.prototype.implicitlyReturns = function () {
|
575 | return this._implicitlyReturns || false;
|
576 | };
|
577 | |
578 |
|
579 |
|
580 | NodePatcher.prototype.setImplicitlyReturns = function () {
|
581 | this._implicitlyReturns = true;
|
582 | };
|
583 | |
584 |
|
585 |
|
586 |
|
587 |
|
588 |
|
589 |
|
590 |
|
591 | NodePatcher.prototype.implicitReturnPatcher = function () {
|
592 | if (this.canHandleImplicitReturn()) {
|
593 | return this;
|
594 | }
|
595 | else {
|
596 | return notNull(this.parent).implicitReturnPatcher();
|
597 | }
|
598 | };
|
599 | |
600 |
|
601 |
|
602 |
|
603 | NodePatcher.prototype.canHandleImplicitReturn = function () {
|
604 | return false;
|
605 | };
|
606 | |
607 |
|
608 |
|
609 |
|
610 |
|
611 |
|
612 |
|
613 | NodePatcher.prototype.implicitReturnWillBreak = function () {
|
614 | return true;
|
615 | };
|
616 | |
617 |
|
618 |
|
619 |
|
620 |
|
621 |
|
622 | NodePatcher.prototype.patchImplicitReturnStart = function (patcher) {
|
623 | if (patcher.node.type === 'Break' || patcher.node.type === 'Continue') {
|
624 | if (patcher.isSurroundedByParentheses()) {
|
625 | this.remove(patcher.outerStart, patcher.innerStart);
|
626 | this.remove(patcher.innerEnd, patcher.outerEnd);
|
627 | }
|
628 | return;
|
629 | }
|
630 | if (isFunction(this.node) && this.isMultiline()) {
|
631 | this.addSuggestion(CLEAN_UP_IMPLICIT_RETURNS);
|
632 | }
|
633 | patcher.setRequiresExpression();
|
634 | this.insert(patcher.outerStart, 'return ');
|
635 | };
|
636 | |
637 |
|
638 |
|
639 | NodePatcher.prototype.getEmptyImplicitReturnCode = function () {
|
640 | return null;
|
641 | };
|
642 | |
643 |
|
644 |
|
645 |
|
646 | NodePatcher.prototype.patchImplicitReturnEnd = function (_patcher) {
|
647 |
|
648 | };
|
649 | |
650 |
|
651 |
|
652 | NodePatcher.prototype.explicitlyReturns = function () {
|
653 | return this._returns || false;
|
654 | };
|
655 | |
656 |
|
657 |
|
658 | NodePatcher.prototype.setExplicitlyReturns = function () {
|
659 | this._returns = true;
|
660 | if (this.parent) {
|
661 | this.parent.setExplicitlyReturns();
|
662 | }
|
663 | };
|
664 | |
665 |
|
666 |
|
667 |
|
668 |
|
669 |
|
670 | NodePatcher.prototype.appendDeferredSuffix = function (suffix) {
|
671 | this._deferredSuffix += suffix;
|
672 | };
|
673 | |
674 |
|
675 |
|
676 |
|
677 |
|
678 |
|
679 | NodePatcher.prototype.commitDeferredSuffix = function () {
|
680 | if (this._deferredSuffix) {
|
681 | this.insert(this.innerEnd, this._deferredSuffix);
|
682 | }
|
683 | };
|
684 | |
685 |
|
686 |
|
687 |
|
688 | NodePatcher.prototype.statementNeedsSemicolon = function () {
|
689 | return true;
|
690 | };
|
691 | |
692 |
|
693 |
|
694 |
|
695 |
|
696 |
|
697 |
|
698 |
|
699 | NodePatcher.prototype.statementNeedsParens = function () {
|
700 | return false;
|
701 | };
|
702 | |
703 |
|
704 |
|
705 |
|
706 | NodePatcher.prototype.statementShouldAddParens = function () {
|
707 | return this.statementNeedsParens() && !this.isSurroundedByParentheses();
|
708 | };
|
709 | |
710 |
|
711 |
|
712 | NodePatcher.prototype.getProgramSourceTokens = function () {
|
713 | return this.context.sourceTokens;
|
714 | };
|
715 | |
716 |
|
717 |
|
718 | NodePatcher.prototype.indexOfSourceTokenStartingAtSourceIndex = function (index) {
|
719 | return this.getProgramSourceTokens().indexOfTokenStartingAtSourceIndex(index);
|
720 | };
|
721 | |
722 |
|
723 |
|
724 |
|
725 | NodePatcher.prototype.indexOfSourceTokenBetweenPatchersMatching = function (left, right, predicate) {
|
726 | return this.indexOfSourceTokenBetweenSourceIndicesMatching(left.outerEnd, right.outerStart, predicate);
|
727 | };
|
728 | |
729 |
|
730 |
|
731 |
|
732 | NodePatcher.prototype.indexOfSourceTokenBetweenSourceIndicesMatching = function (left, right, predicate) {
|
733 | var tokenList = this.getProgramSourceTokens();
|
734 | return tokenList.indexOfTokenMatchingPredicate(function (token) {
|
735 | return token.start >= left && token.start <= right && predicate(token);
|
736 | }, tokenList.indexOfTokenNearSourceIndex(left), tokenList.indexOfTokenNearSourceIndex(right).next());
|
737 | };
|
738 | |
739 |
|
740 |
|
741 | NodePatcher.prototype.sourceTokenAtIndex = function (index) {
|
742 | return this.getProgramSourceTokens().tokenAtIndex(index);
|
743 | };
|
744 | |
745 |
|
746 |
|
747 | NodePatcher.prototype.sourceOfToken = function (token) {
|
748 | return this.context.source.slice(token.start, token.end);
|
749 | };
|
750 | |
751 |
|
752 |
|
753 | NodePatcher.prototype.firstToken = function () {
|
754 | var token = this.sourceTokenAtIndex(this.contentStartTokenIndex);
|
755 | if (!token) {
|
756 | throw this.error('Expected to find a first token for node.');
|
757 | }
|
758 | return token;
|
759 | };
|
760 | |
761 |
|
762 |
|
763 | NodePatcher.prototype.lastToken = function () {
|
764 | var token = this.sourceTokenAtIndex(this.contentEndTokenIndex);
|
765 | if (!token) {
|
766 | throw this.error('Expected to find a last token for node.');
|
767 | }
|
768 | return token;
|
769 | };
|
770 | |
771 |
|
772 |
|
773 | NodePatcher.prototype.nextSemanticToken = function () {
|
774 | return this.getFirstSemanticToken(this.contentEnd, this.context.source.length);
|
775 | };
|
776 | |
777 |
|
778 |
|
779 | NodePatcher.prototype.getOriginalSource = function () {
|
780 | return this.context.source.slice(this.contentStart, this.contentEnd);
|
781 | };
|
782 | |
783 |
|
784 |
|
785 | NodePatcher.prototype.isMultiline = function () {
|
786 | return /\n/.test(this.getOriginalSource());
|
787 | };
|
788 | |
789 |
|
790 |
|
791 | NodePatcher.prototype.getPatchedSource = function () {
|
792 | return this.slice(this.contentStart, this.contentEnd);
|
793 | };
|
794 | |
795 |
|
796 |
|
797 |
|
798 | NodePatcher.prototype.indexOfSourceTokenAfterSourceTokenIndex = function (start, type, predicate) {
|
799 | if (predicate === void 0) { predicate = isSemanticToken; }
|
800 | var index = this.getProgramSourceTokens().indexOfTokenMatchingPredicate(predicate, start.next());
|
801 | if (!index) {
|
802 | return null;
|
803 | }
|
804 | var token = this.sourceTokenAtIndex(index);
|
805 | if (!token || token.type !== type) {
|
806 | return null;
|
807 | }
|
808 | return index;
|
809 | };
|
810 | |
811 |
|
812 |
|
813 | NodePatcher.prototype.hasSourceTokenAfter = function (type, predicate) {
|
814 | if (predicate === void 0) { predicate = isSemanticToken; }
|
815 | return this.indexOfSourceTokenAfterSourceTokenIndex(this.outerEndTokenIndex, type, predicate) !== null;
|
816 | };
|
817 | |
818 |
|
819 |
|
820 |
|
821 | NodePatcher.prototype.isSurroundedByParentheses = function () {
|
822 |
|
823 |
|
824 |
|
825 |
|
826 | if (this.contentStart === this.outerStart && this.contentEnd === this.outerEnd) {
|
827 | return false;
|
828 | }
|
829 | var beforeToken = this.sourceTokenAtIndex(this.outerStartTokenIndex);
|
830 | var afterToken = this.sourceTokenAtIndex(this.outerEndTokenIndex);
|
831 | if (!beforeToken || !afterToken) {
|
832 | return false;
|
833 | }
|
834 | var leftTokenType = SourceType.LPAREN;
|
835 | var rightTokenType = SourceType.RPAREN;
|
836 | if (beforeToken.type === SourceType.LPAREN && afterToken.type === SourceType.RPAREN) {
|
837 |
|
838 | }
|
839 | else if (beforeToken.type === SourceType.CALL_START && afterToken.type === SourceType.CALL_END) {
|
840 | leftTokenType = SourceType.CALL_START;
|
841 | rightTokenType = SourceType.CALL_END;
|
842 | }
|
843 | else {
|
844 | return false;
|
845 | }
|
846 | var parenRange = this.getProgramSourceTokens().rangeOfMatchingTokensContainingTokenIndex(leftTokenType, rightTokenType, this.outerStartTokenIndex);
|
847 | if (!parenRange) {
|
848 | return false;
|
849 | }
|
850 | var rparenIndex = parenRange[1].previous();
|
851 | var rparen = this.sourceTokenAtIndex(notNull(rparenIndex));
|
852 | return rparen === afterToken;
|
853 | };
|
854 | NodePatcher.prototype.surroundInParens = function () {
|
855 | if (!this.isSurroundedByParentheses()) {
|
856 | this.insert(this.outerStart, '(');
|
857 | this.insert(this.outerEnd, ')');
|
858 | }
|
859 | };
|
860 | NodePatcher.prototype.getBoundingPatcher = function () {
|
861 | var _this = this;
|
862 | if (this.isSurroundedByParentheses()) {
|
863 | return this;
|
864 | }
|
865 | else if (this.parent) {
|
866 | if (this.isNodeFunctionApplication(this.parent.node) &&
|
867 | this.parent.node.arguments.some(function (arg) { return arg === _this.node; })) {
|
868 | return this;
|
869 | }
|
870 | else if (this.parent.node.type === 'ArrayInitialiser') {
|
871 | return this;
|
872 | }
|
873 | else if (this.parent.node.type === 'ObjectInitialiser') {
|
874 | return this;
|
875 | }
|
876 | return this.parent.getBoundingPatcher();
|
877 | }
|
878 | else {
|
879 | return this;
|
880 | }
|
881 | };
|
882 | NodePatcher.prototype.isNodeFunctionApplication = function (node) {
|
883 | return node instanceof FunctionApplication || node instanceof SoakedFunctionApplication || node instanceof NewOp;
|
884 | };
|
885 | |
886 |
|
887 |
|
888 |
|
889 | NodePatcher.prototype.canHandleNegationInternally = function () {
|
890 | return false;
|
891 | };
|
892 | |
893 |
|
894 |
|
895 |
|
896 |
|
897 |
|
898 |
|
899 | NodePatcher.prototype.negate = function () {
|
900 | this.insert(this.contentStart, '!');
|
901 | this._hadUnparenthesizedNegation = true;
|
902 | };
|
903 | |
904 |
|
905 |
|
906 |
|
907 |
|
908 |
|
909 |
|
910 |
|
911 |
|
912 |
|
913 | NodePatcher.prototype.hadUnparenthesizedNegation = function () {
|
914 | return this._hadUnparenthesizedNegation;
|
915 | };
|
916 | NodePatcher.prototype.getScope = function () {
|
917 | return this.context.getScope(this.node);
|
918 | };
|
919 | |
920 |
|
921 |
|
922 | NodePatcher.prototype.getIndent = function (offset) {
|
923 | if (offset === void 0) { offset = 0; }
|
924 | return adjustIndent(this.context.source, this.contentStart, this.getAdjustedIndentLevel() + offset);
|
925 | };
|
926 | |
927 |
|
928 |
|
929 |
|
930 |
|
931 | NodePatcher.prototype.setIndent = function (indentStr) {
|
932 | var currentIndent = this.getIndent();
|
933 | var indentLength = this.getProgramIndentString().length;
|
934 | var currentIndentLevel = currentIndent.length / indentLength;
|
935 | var desiredIndentLevel = indentStr.length / indentLength;
|
936 | this.indent(desiredIndentLevel - currentIndentLevel);
|
937 | };
|
938 | |
939 |
|
940 |
|
941 | NodePatcher.prototype.getAdjustedIndentLevel = function () {
|
942 | return this.adjustedIndentLevel + (this.parent ? this.parent.getAdjustedIndentLevel() : 0);
|
943 | };
|
944 | |
945 |
|
946 |
|
947 | NodePatcher.prototype.getProgramIndentString = function () {
|
948 | return notNull(this.parent).getProgramIndentString();
|
949 | };
|
950 | |
951 |
|
952 |
|
953 |
|
954 |
|
955 |
|
956 |
|
957 |
|
958 |
|
959 | NodePatcher.prototype.indent = function (offset, _a) {
|
960 | if (offset === void 0) { offset = 1; }
|
961 | var _b = (_a === void 0 ? {} : _a).skipFirstLine, skipFirstLine = _b === void 0 ? false : _b;
|
962 | if (offset === 0) {
|
963 | return;
|
964 | }
|
965 | this.adjustedIndentLevel += offset;
|
966 | var indentString = this.getProgramIndentString();
|
967 | var indentToChange = indentString.repeat(Math.abs(offset));
|
968 | var start = this.outerStart;
|
969 | var end = this.outerEnd;
|
970 | var source = this.context.source;
|
971 |
|
972 |
|
973 |
|
974 | if (skipFirstLine || !this.isFirstNodeInLine()) {
|
975 | while (start < end && source[start] !== '\n') {
|
976 | start++;
|
977 | }
|
978 | }
|
979 | var hasIndentedThisLine = false;
|
980 | for (var i = start; i < end; i++) {
|
981 | switch (source[i]) {
|
982 | case '\n':
|
983 | hasIndentedThisLine = false;
|
984 | break;
|
985 | case ' ':
|
986 | case '\t':
|
987 | break;
|
988 | default:
|
989 | if (!hasIndentedThisLine) {
|
990 | if (offset > 0) {
|
991 | this.insert(i, indentToChange);
|
992 | }
|
993 | else if (source.slice(i - indentToChange.length, i) === indentToChange) {
|
994 | this.remove(i - indentToChange.length, i);
|
995 | }
|
996 | else {
|
997 |
|
998 |
|
999 |
|
1000 |
|
1001 |
|
1002 |
|
1003 |
|
1004 | this.log('Warning: Ignoring an unindent operation because the line ' +
|
1005 | 'did not start with the proper indentation.');
|
1006 | }
|
1007 | hasIndentedThisLine = true;
|
1008 | }
|
1009 | break;
|
1010 | }
|
1011 | }
|
1012 | };
|
1013 | NodePatcher.prototype.isFirstNodeInLine = function (startingPoint) {
|
1014 | if (startingPoint === void 0) { startingPoint = this.outerStart; }
|
1015 | var source = this.context.source;
|
1016 | for (var i = startingPoint - 1; i >= 0 && source[i] !== '\n'; i--) {
|
1017 | if (source[i] !== '\t' && source[i] !== ' ') {
|
1018 | return false;
|
1019 | }
|
1020 | }
|
1021 | return true;
|
1022 | };
|
1023 | |
1024 |
|
1025 |
|
1026 |
|
1027 |
|
1028 | NodePatcher.prototype.getEndOfLine = function () {
|
1029 | var source = this.context.source;
|
1030 | for (var i = this.outerEnd - '\n'.length; i < source.length; i++) {
|
1031 | if (source[i] === '\n') {
|
1032 | return i;
|
1033 | }
|
1034 | }
|
1035 | return source.length;
|
1036 | };
|
1037 | |
1038 |
|
1039 |
|
1040 | NodePatcher.prototype.appendLineAfter = function (content, indentOffset) {
|
1041 | if (indentOffset === void 0) { indentOffset = 0; }
|
1042 | var boundingPatcher = this.getBoundingPatcher();
|
1043 | var endOfLine = this.getEndOfLine();
|
1044 | var nextToken = this.nextSemanticToken();
|
1045 | var insertPoint = Math.min(Math.min(endOfLine, boundingPatcher.innerEnd));
|
1046 | if (nextToken) {
|
1047 | insertPoint = Math.min(insertPoint, nextToken.start);
|
1048 | }
|
1049 | this.insert(insertPoint, "\n" + this.getIndent(indentOffset) + content);
|
1050 | };
|
1051 | |
1052 |
|
1053 |
|
1054 | NodePatcher.prototype.error = function (message, start, end, error) {
|
1055 | if (start === void 0) { start = this.contentStart; }
|
1056 | if (end === void 0) { end = this.contentEnd; }
|
1057 | if (error === void 0) { error = null; }
|
1058 | var patcherError = new PatcherError(message, this.context.source, start, end);
|
1059 | if (error) {
|
1060 | patcherError.stack = error.stack;
|
1061 | }
|
1062 | return patcherError;
|
1063 | };
|
1064 | |
1065 |
|
1066 |
|
1067 | NodePatcher.prototype.registerHelper = function (name, code) {
|
1068 | return notNull(this.parent).registerHelper(name, code);
|
1069 | };
|
1070 | |
1071 |
|
1072 |
|
1073 |
|
1074 |
|
1075 |
|
1076 |
|
1077 | NodePatcher.prototype.isPure = function () {
|
1078 | return this.isRepeatable();
|
1079 | };
|
1080 | |
1081 |
|
1082 |
|
1083 |
|
1084 |
|
1085 | NodePatcher.prototype.isRepeatable = function () {
|
1086 | return false;
|
1087 | };
|
1088 | |
1089 |
|
1090 |
|
1091 |
|
1092 |
|
1093 | NodePatcher.prototype.setRequiresRepeatableExpression = function (repeatableOptions) {
|
1094 | if (repeatableOptions === void 0) { repeatableOptions = {}; }
|
1095 | this._repeatableOptions = repeatableOptions;
|
1096 | };
|
1097 | |
1098 |
|
1099 |
|
1100 |
|
1101 |
|
1102 | NodePatcher.prototype.getRepeatableOptions = function () {
|
1103 | return this._repeatableOptions;
|
1104 | };
|
1105 | |
1106 |
|
1107 |
|
1108 |
|
1109 |
|
1110 | NodePatcher.prototype.getRepeatCode = function () {
|
1111 | if (this._repeatCode === null) {
|
1112 | throw new Error('Must patch as a repeatable expression to access repeat code.');
|
1113 | }
|
1114 | return this._repeatCode;
|
1115 | };
|
1116 | |
1117 |
|
1118 |
|
1119 |
|
1120 |
|
1121 | NodePatcher.prototype.overrideRepeatCode = function (repeatCode) {
|
1122 | this._repeatCode = repeatCode;
|
1123 | };
|
1124 | |
1125 |
|
1126 |
|
1127 | NodePatcher.prototype.claimFreeBinding = function (ref) {
|
1128 | if (ref === void 0) { ref = null; }
|
1129 | return this.getScope().claimFreeBinding(this.node, ref);
|
1130 | };
|
1131 | |
1132 |
|
1133 |
|
1134 | NodePatcher.prototype.allCodePathsPresent = function () {
|
1135 | return true;
|
1136 | };
|
1137 | |
1138 |
|
1139 |
|
1140 | NodePatcher.prototype.getFirstSemanticToken = function (from, to) {
|
1141 | if (from === void 0) { from = this.contentStart; }
|
1142 | if (to === void 0) { to = notNull(this.parent).contentEnd; }
|
1143 | var nextSemanticIdx = this.indexOfSourceTokenBetweenSourceIndicesMatching(from, to, isSemanticToken);
|
1144 | return nextSemanticIdx && this.sourceTokenAtIndex(nextSemanticIdx);
|
1145 | };
|
1146 | |
1147 |
|
1148 |
|
1149 |
|
1150 |
|
1151 | NodePatcher.prototype.mayBeUnboundReference = function () {
|
1152 | return false;
|
1153 | };
|
1154 | NodePatcher.prototype.patchInIIFE = function (innerPatchFn) {
|
1155 | this.addSuggestion(AVOID_IIFES);
|
1156 | if (this.containsYield()) {
|
1157 | this.insert(this.innerStart, 'yield* (function*() {');
|
1158 | }
|
1159 | else if (this.containsAwait()) {
|
1160 | this.insert(this.innerStart, 'await (async () => {');
|
1161 | }
|
1162 | else {
|
1163 | this.insert(this.innerStart, '(() => {');
|
1164 | }
|
1165 | innerPatchFn();
|
1166 | if (this.containsYield()) {
|
1167 | if (referencesArguments(this.node)) {
|
1168 | this.insert(this.innerEnd, '}).apply(this, arguments)');
|
1169 | }
|
1170 | else {
|
1171 | this.insert(this.innerEnd, '}).call(this)');
|
1172 | }
|
1173 | }
|
1174 | else if (this.containsAwait()) {
|
1175 | this.insert(this.innerEnd, '})()');
|
1176 | }
|
1177 | else {
|
1178 | this.insert(this.innerEnd, '})()');
|
1179 | }
|
1180 | };
|
1181 | |
1182 |
|
1183 |
|
1184 | NodePatcher.prototype.yields = function () {
|
1185 | this._containsYield = true;
|
1186 | if (this.parent && !isFunction(this.parent.node)) {
|
1187 | this.parent.yields();
|
1188 | }
|
1189 | };
|
1190 | |
1191 |
|
1192 |
|
1193 |
|
1194 | NodePatcher.prototype.containsYield = function () {
|
1195 | return this._containsYield;
|
1196 | };
|
1197 | NodePatcher.prototype.awaits = function () {
|
1198 | this._containsAwait = true;
|
1199 | if (this.parent && !isFunction(this.parent.node)) {
|
1200 | this.parent.awaits();
|
1201 | }
|
1202 | };
|
1203 | NodePatcher.prototype.containsAwait = function () {
|
1204 | return this._containsAwait;
|
1205 | };
|
1206 | return NodePatcher;
|
1207 | }());
|
1208 | export default NodePatcher;
|