beautifier.js
54.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
/*jshint node:true */
/*
The MIT License (MIT)
Copyright (c) 2007-2018 Einar Lielmanis, Liam Newman, and contributors.
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation files
(the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of the Software,
and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
'use strict';
var Output = require('../core/output').Output;
var Token = require('../core/token').Token;
var acorn = require('./acorn');
var Options = require('./options').Options;
var Tokenizer = require('./tokenizer').Tokenizer;
var line_starters = require('./tokenizer').line_starters;
var positionable_operators = require('./tokenizer').positionable_operators;
var TOKEN = require('./tokenizer').TOKEN;
function in_array(what, arr) {
return arr.indexOf(what) !== -1;
}
function ltrim(s) {
return s.replace(/^\s+/g, '');
}
function generateMapFromStrings(list) {
var result = {};
for (var x = 0; x < list.length; x++) {
// make the mapped names underscored instead of dash
result[list[x].replace(/-/g, '_')] = list[x];
}
return result;
}
function reserved_word(token, word) {
return token && token.type === TOKEN.RESERVED && token.text === word;
}
function reserved_array(token, words) {
return token && token.type === TOKEN.RESERVED && in_array(token.text, words);
}
// Unsure of what they mean, but they work. Worth cleaning up in future.
var special_words = ['case', 'return', 'do', 'if', 'throw', 'else', 'await', 'break', 'continue', 'async'];
var validPositionValues = ['before-newline', 'after-newline', 'preserve-newline'];
// Generate map from array
var OPERATOR_POSITION = generateMapFromStrings(validPositionValues);
var OPERATOR_POSITION_BEFORE_OR_PRESERVE = [OPERATOR_POSITION.before_newline, OPERATOR_POSITION.preserve_newline];
var MODE = {
BlockStatement: 'BlockStatement', // 'BLOCK'
Statement: 'Statement', // 'STATEMENT'
ObjectLiteral: 'ObjectLiteral', // 'OBJECT',
ArrayLiteral: 'ArrayLiteral', //'[EXPRESSION]',
ForInitializer: 'ForInitializer', //'(FOR-EXPRESSION)',
Conditional: 'Conditional', //'(COND-EXPRESSION)',
Expression: 'Expression' //'(EXPRESSION)'
};
function remove_redundant_indentation(output, frame) {
// This implementation is effective but has some issues:
// - can cause line wrap to happen too soon due to indent removal
// after wrap points are calculated
// These issues are minor compared to ugly indentation.
if (frame.multiline_frame ||
frame.mode === MODE.ForInitializer ||
frame.mode === MODE.Conditional) {
return;
}
// remove one indent from each line inside this section
output.remove_indent(frame.start_line_index);
}
// we could use just string.split, but
// IE doesn't like returning empty strings
function split_linebreaks(s) {
//return s.split(/\x0d\x0a|\x0a/);
s = s.replace(acorn.allLineBreaks, '\n');
var out = [],
idx = s.indexOf("\n");
while (idx !== -1) {
out.push(s.substring(0, idx));
s = s.substring(idx + 1);
idx = s.indexOf("\n");
}
if (s.length) {
out.push(s);
}
return out;
}
function is_array(mode) {
return mode === MODE.ArrayLiteral;
}
function is_expression(mode) {
return in_array(mode, [MODE.Expression, MODE.ForInitializer, MODE.Conditional]);
}
function all_lines_start_with(lines, c) {
for (var i = 0; i < lines.length; i++) {
var line = lines[i].trim();
if (line.charAt(0) !== c) {
return false;
}
}
return true;
}
function each_line_matches_indent(lines, indent) {
var i = 0,
len = lines.length,
line;
for (; i < len; i++) {
line = lines[i];
// allow empty lines to pass through
if (line && line.indexOf(indent) !== 0) {
return false;
}
}
return true;
}
function Beautifier(source_text, options) {
options = options || {};
this._source_text = source_text || '';
this._output = null;
this._tokens = null;
this._last_last_text = null;
this._flags = null;
this._previous_flags = null;
this._flag_store = null;
this._options = new Options(options);
}
Beautifier.prototype.create_flags = function(flags_base, mode) {
var next_indent_level = 0;
if (flags_base) {
next_indent_level = flags_base.indentation_level;
if (!this._output.just_added_newline() &&
flags_base.line_indent_level > next_indent_level) {
next_indent_level = flags_base.line_indent_level;
}
}
var next_flags = {
mode: mode,
parent: flags_base,
last_token: flags_base ? flags_base.last_token : new Token(TOKEN.START_BLOCK, ''), // last token text
last_word: flags_base ? flags_base.last_word : '', // last TOKEN.WORD passed
declaration_statement: false,
declaration_assignment: false,
multiline_frame: false,
inline_frame: false,
if_block: false,
else_block: false,
do_block: false,
do_while: false,
import_block: false,
in_case_statement: false, // switch(..){ INSIDE HERE }
in_case: false, // we're on the exact line with "case 0:"
case_body: false, // the indented case-action block
indentation_level: next_indent_level,
alignment: 0,
line_indent_level: flags_base ? flags_base.line_indent_level : next_indent_level,
start_line_index: this._output.get_line_number(),
ternary_depth: 0
};
return next_flags;
};
Beautifier.prototype._reset = function(source_text) {
var baseIndentString = source_text.match(/^[\t ]*/)[0];
this._last_last_text = ''; // pre-last token text
this._output = new Output(this._options, baseIndentString);
// If testing the ignore directive, start with output disable set to true
this._output.raw = this._options.test_output_raw;
// Stack of parsing/formatting states, including MODE.
// We tokenize, parse, and output in an almost purely a forward-only stream of token input
// and formatted output. This makes the beautifier less accurate than full parsers
// but also far more tolerant of syntax errors.
//
// For example, the default mode is MODE.BlockStatement. If we see a '{' we push a new frame of type
// MODE.BlockStatement on the the stack, even though it could be object literal. If we later
// encounter a ":", we'll switch to to MODE.ObjectLiteral. If we then see a ";",
// most full parsers would die, but the beautifier gracefully falls back to
// MODE.BlockStatement and continues on.
this._flag_store = [];
this.set_mode(MODE.BlockStatement);
var tokenizer = new Tokenizer(source_text, this._options);
this._tokens = tokenizer.tokenize();
return source_text;
};
Beautifier.prototype.beautify = function() {
// if disabled, return the input unchanged.
if (this._options.disabled) {
return this._source_text;
}
var sweet_code;
var source_text = this._reset(this._source_text);
var eol = this._options.eol;
if (this._options.eol === 'auto') {
eol = '\n';
if (source_text && acorn.lineBreak.test(source_text || '')) {
eol = source_text.match(acorn.lineBreak)[0];
}
}
var current_token = this._tokens.next();
while (current_token) {
this.handle_token(current_token);
this._last_last_text = this._flags.last_token.text;
this._flags.last_token = current_token;
current_token = this._tokens.next();
}
sweet_code = this._output.get_code(eol);
return sweet_code;
};
Beautifier.prototype.handle_token = function(current_token, preserve_statement_flags) {
if (current_token.type === TOKEN.START_EXPR) {
this.handle_start_expr(current_token);
} else if (current_token.type === TOKEN.END_EXPR) {
this.handle_end_expr(current_token);
} else if (current_token.type === TOKEN.START_BLOCK) {
this.handle_start_block(current_token);
} else if (current_token.type === TOKEN.END_BLOCK) {
this.handle_end_block(current_token);
} else if (current_token.type === TOKEN.WORD) {
this.handle_word(current_token);
} else if (current_token.type === TOKEN.RESERVED) {
this.handle_word(current_token);
} else if (current_token.type === TOKEN.SEMICOLON) {
this.handle_semicolon(current_token);
} else if (current_token.type === TOKEN.STRING) {
this.handle_string(current_token);
} else if (current_token.type === TOKEN.EQUALS) {
this.handle_equals(current_token);
} else if (current_token.type === TOKEN.OPERATOR) {
this.handle_operator(current_token);
} else if (current_token.type === TOKEN.COMMA) {
this.handle_comma(current_token);
} else if (current_token.type === TOKEN.BLOCK_COMMENT) {
this.handle_block_comment(current_token, preserve_statement_flags);
} else if (current_token.type === TOKEN.COMMENT) {
this.handle_comment(current_token, preserve_statement_flags);
} else if (current_token.type === TOKEN.DOT) {
this.handle_dot(current_token);
} else if (current_token.type === TOKEN.EOF) {
this.handle_eof(current_token);
} else if (current_token.type === TOKEN.UNKNOWN) {
this.handle_unknown(current_token, preserve_statement_flags);
} else {
this.handle_unknown(current_token, preserve_statement_flags);
}
};
Beautifier.prototype.handle_whitespace_and_comments = function(current_token, preserve_statement_flags) {
var newlines = current_token.newlines;
var keep_whitespace = this._options.keep_array_indentation && is_array(this._flags.mode);
if (current_token.comments_before) {
var comment_token = current_token.comments_before.next();
while (comment_token) {
// The cleanest handling of inline comments is to treat them as though they aren't there.
// Just continue formatting and the behavior should be logical.
// Also ignore unknown tokens. Again, this should result in better behavior.
this.handle_whitespace_and_comments(comment_token, preserve_statement_flags);
this.handle_token(comment_token, preserve_statement_flags);
comment_token = current_token.comments_before.next();
}
}
if (keep_whitespace) {
for (var i = 0; i < newlines; i += 1) {
this.print_newline(i > 0, preserve_statement_flags);
}
} else {
if (this._options.max_preserve_newlines && newlines > this._options.max_preserve_newlines) {
newlines = this._options.max_preserve_newlines;
}
if (this._options.preserve_newlines) {
if (newlines > 1) {
this.print_newline(false, preserve_statement_flags);
for (var j = 1; j < newlines; j += 1) {
this.print_newline(true, preserve_statement_flags);
}
}
}
}
};
var newline_restricted_tokens = ['async', 'break', 'continue', 'return', 'throw', 'yield'];
Beautifier.prototype.allow_wrap_or_preserved_newline = function(current_token, force_linewrap) {
force_linewrap = (force_linewrap === undefined) ? false : force_linewrap;
// Never wrap the first token on a line
if (this._output.just_added_newline()) {
return;
}
var shouldPreserveOrForce = (this._options.preserve_newlines && current_token.newlines) || force_linewrap;
var operatorLogicApplies = in_array(this._flags.last_token.text, positionable_operators) ||
in_array(current_token.text, positionable_operators);
if (operatorLogicApplies) {
var shouldPrintOperatorNewline = (
in_array(this._flags.last_token.text, positionable_operators) &&
in_array(this._options.operator_position, OPERATOR_POSITION_BEFORE_OR_PRESERVE)
) ||
in_array(current_token.text, positionable_operators);
shouldPreserveOrForce = shouldPreserveOrForce && shouldPrintOperatorNewline;
}
if (shouldPreserveOrForce) {
this.print_newline(false, true);
} else if (this._options.wrap_line_length) {
if (reserved_array(this._flags.last_token, newline_restricted_tokens)) {
// These tokens should never have a newline inserted
// between them and the following expression.
return;
}
this._output.set_wrap_point();
}
};
Beautifier.prototype.print_newline = function(force_newline, preserve_statement_flags) {
if (!preserve_statement_flags) {
if (this._flags.last_token.text !== ';' && this._flags.last_token.text !== ',' && this._flags.last_token.text !== '=' && (this._flags.last_token.type !== TOKEN.OPERATOR || this._flags.last_token.text === '--' || this._flags.last_token.text === '++')) {
var next_token = this._tokens.peek();
while (this._flags.mode === MODE.Statement &&
!(this._flags.if_block && reserved_word(next_token, 'else')) &&
!this._flags.do_block) {
this.restore_mode();
}
}
}
if (this._output.add_new_line(force_newline)) {
this._flags.multiline_frame = true;
}
};
Beautifier.prototype.print_token_line_indentation = function(current_token) {
if (this._output.just_added_newline()) {
if (this._options.keep_array_indentation &&
current_token.newlines &&
(current_token.text === '[' || is_array(this._flags.mode))) {
this._output.current_line.set_indent(-1);
this._output.current_line.push(current_token.whitespace_before);
this._output.space_before_token = false;
} else if (this._output.set_indent(this._flags.indentation_level, this._flags.alignment)) {
this._flags.line_indent_level = this._flags.indentation_level;
}
}
};
Beautifier.prototype.print_token = function(current_token, printable_token) {
if (this._output.raw) {
this._output.add_raw_token(current_token);
return;
}
if (this._options.comma_first && current_token.previous && current_token.previous.type === TOKEN.COMMA &&
this._output.just_added_newline()) {
if (this._output.previous_line.last() === ',') {
var popped = this._output.previous_line.pop();
// if the comma was already at the start of the line,
// pull back onto that line and reprint the indentation
if (this._output.previous_line.is_empty()) {
this._output.previous_line.push(popped);
this._output.trim(true);
this._output.current_line.pop();
this._output.trim();
}
// add the comma in front of the next token
this.print_token_line_indentation(current_token);
this._output.add_token(',');
this._output.space_before_token = true;
}
}
printable_token = printable_token || current_token.text;
this.print_token_line_indentation(current_token);
this._output.non_breaking_space = true;
this._output.add_token(printable_token);
if (this._output.previous_token_wrapped) {
this._flags.multiline_frame = true;
}
};
Beautifier.prototype.indent = function() {
this._flags.indentation_level += 1;
this._output.set_indent(this._flags.indentation_level, this._flags.alignment);
};
Beautifier.prototype.deindent = function() {
if (this._flags.indentation_level > 0 &&
((!this._flags.parent) || this._flags.indentation_level > this._flags.parent.indentation_level)) {
this._flags.indentation_level -= 1;
this._output.set_indent(this._flags.indentation_level, this._flags.alignment);
}
};
Beautifier.prototype.set_mode = function(mode) {
if (this._flags) {
this._flag_store.push(this._flags);
this._previous_flags = this._flags;
} else {
this._previous_flags = this.create_flags(null, mode);
}
this._flags = this.create_flags(this._previous_flags, mode);
this._output.set_indent(this._flags.indentation_level, this._flags.alignment);
};
Beautifier.prototype.restore_mode = function() {
if (this._flag_store.length > 0) {
this._previous_flags = this._flags;
this._flags = this._flag_store.pop();
if (this._previous_flags.mode === MODE.Statement) {
remove_redundant_indentation(this._output, this._previous_flags);
}
this._output.set_indent(this._flags.indentation_level, this._flags.alignment);
}
};
Beautifier.prototype.start_of_object_property = function() {
return this._flags.parent.mode === MODE.ObjectLiteral && this._flags.mode === MODE.Statement && (
(this._flags.last_token.text === ':' && this._flags.ternary_depth === 0) || (reserved_array(this._flags.last_token, ['get', 'set'])));
};
Beautifier.prototype.start_of_statement = function(current_token) {
var start = false;
start = start || reserved_array(this._flags.last_token, ['var', 'let', 'const']) && current_token.type === TOKEN.WORD;
start = start || reserved_word(this._flags.last_token, 'do');
start = start || (!(this._flags.parent.mode === MODE.ObjectLiteral && this._flags.mode === MODE.Statement)) && reserved_array(this._flags.last_token, newline_restricted_tokens) && !current_token.newlines;
start = start || reserved_word(this._flags.last_token, 'else') &&
!(reserved_word(current_token, 'if') && !current_token.comments_before);
start = start || (this._flags.last_token.type === TOKEN.END_EXPR && (this._previous_flags.mode === MODE.ForInitializer || this._previous_flags.mode === MODE.Conditional));
start = start || (this._flags.last_token.type === TOKEN.WORD && this._flags.mode === MODE.BlockStatement &&
!this._flags.in_case &&
!(current_token.text === '--' || current_token.text === '++') &&
this._last_last_text !== 'function' &&
current_token.type !== TOKEN.WORD && current_token.type !== TOKEN.RESERVED);
start = start || (this._flags.mode === MODE.ObjectLiteral && (
(this._flags.last_token.text === ':' && this._flags.ternary_depth === 0) || reserved_array(this._flags.last_token, ['get', 'set'])));
if (start) {
this.set_mode(MODE.Statement);
this.indent();
this.handle_whitespace_and_comments(current_token, true);
// Issue #276:
// If starting a new statement with [if, for, while, do], push to a new line.
// if (a) if (b) if(c) d(); else e(); else f();
if (!this.start_of_object_property()) {
this.allow_wrap_or_preserved_newline(current_token,
reserved_array(current_token, ['do', 'for', 'if', 'while']));
}
return true;
}
return false;
};
Beautifier.prototype.handle_start_expr = function(current_token) {
// The conditional starts the statement if appropriate.
if (!this.start_of_statement(current_token)) {
this.handle_whitespace_and_comments(current_token);
}
var next_mode = MODE.Expression;
if (current_token.text === '[') {
if (this._flags.last_token.type === TOKEN.WORD || this._flags.last_token.text === ')') {
// this is array index specifier, break immediately
// a[x], fn()[x]
if (reserved_array(this._flags.last_token, line_starters)) {
this._output.space_before_token = true;
}
this.print_token(current_token);
this.set_mode(next_mode);
this.indent();
if (this._options.space_in_paren) {
this._output.space_before_token = true;
}
return;
}
next_mode = MODE.ArrayLiteral;
if (is_array(this._flags.mode)) {
if (this._flags.last_token.text === '[' ||
(this._flags.last_token.text === ',' && (this._last_last_text === ']' || this._last_last_text === '}'))) {
// ], [ goes to new line
// }, [ goes to new line
if (!this._options.keep_array_indentation) {
this.print_newline();
}
}
}
if (!in_array(this._flags.last_token.type, [TOKEN.START_EXPR, TOKEN.END_EXPR, TOKEN.WORD, TOKEN.OPERATOR])) {
this._output.space_before_token = true;
}
} else {
if (this._flags.last_token.type === TOKEN.RESERVED) {
if (this._flags.last_token.text === 'for') {
this._output.space_before_token = this._options.space_before_conditional;
next_mode = MODE.ForInitializer;
} else if (in_array(this._flags.last_token.text, ['if', 'while'])) {
this._output.space_before_token = this._options.space_before_conditional;
next_mode = MODE.Conditional;
} else if (in_array(this._flags.last_word, ['await', 'async'])) {
// Should be a space between await and an IIFE, or async and an arrow function
this._output.space_before_token = true;
} else if (this._flags.last_token.text === 'import' && current_token.whitespace_before === '') {
this._output.space_before_token = false;
} else if (in_array(this._flags.last_token.text, line_starters) || this._flags.last_token.text === 'catch') {
this._output.space_before_token = true;
}
} else if (this._flags.last_token.type === TOKEN.EQUALS || this._flags.last_token.type === TOKEN.OPERATOR) {
// Support of this kind of newline preservation.
// a = (b &&
// (c || d));
if (!this.start_of_object_property()) {
this.allow_wrap_or_preserved_newline(current_token);
}
} else if (this._flags.last_token.type === TOKEN.WORD) {
this._output.space_before_token = false;
// function name() vs function name ()
// function* name() vs function* name ()
// async name() vs async name ()
// In ES6, you can also define the method properties of an object
// var obj = {a: function() {}}
// It can be abbreviated
// var obj = {a() {}}
// var obj = { a() {}} vs var obj = { a () {}}
// var obj = { * a() {}} vs var obj = { * a () {}}
var peek_back_two = this._tokens.peek(-3);
if (this._options.space_after_named_function && peek_back_two) {
// peek starts at next character so -1 is current token
var peek_back_three = this._tokens.peek(-4);
if (reserved_array(peek_back_two, ['async', 'function']) ||
(peek_back_two.text === '*' && reserved_array(peek_back_three, ['async', 'function']))) {
this._output.space_before_token = true;
} else if (this._flags.mode === MODE.ObjectLiteral) {
if ((peek_back_two.text === '{' || peek_back_two.text === ',') ||
(peek_back_two.text === '*' && (peek_back_three.text === '{' || peek_back_three.text === ','))) {
this._output.space_before_token = true;
}
}
}
} else {
// Support preserving wrapped arrow function expressions
// a.b('c',
// () => d.e
// )
this.allow_wrap_or_preserved_newline(current_token);
}
// function() vs function ()
// yield*() vs yield* ()
// function*() vs function* ()
if ((this._flags.last_token.type === TOKEN.RESERVED && (this._flags.last_word === 'function' || this._flags.last_word === 'typeof')) ||
(this._flags.last_token.text === '*' &&
(in_array(this._last_last_text, ['function', 'yield']) ||
(this._flags.mode === MODE.ObjectLiteral && in_array(this._last_last_text, ['{', ',']))))) {
this._output.space_before_token = this._options.space_after_anon_function;
}
}
if (this._flags.last_token.text === ';' || this._flags.last_token.type === TOKEN.START_BLOCK) {
this.print_newline();
} else if (this._flags.last_token.type === TOKEN.END_EXPR || this._flags.last_token.type === TOKEN.START_EXPR || this._flags.last_token.type === TOKEN.END_BLOCK || this._flags.last_token.text === '.' || this._flags.last_token.type === TOKEN.COMMA) {
// do nothing on (( and )( and ][ and ]( and .(
// TODO: Consider whether forcing this is required. Review failing tests when removed.
this.allow_wrap_or_preserved_newline(current_token, current_token.newlines);
}
this.print_token(current_token);
this.set_mode(next_mode);
if (this._options.space_in_paren) {
this._output.space_before_token = true;
}
// In all cases, if we newline while inside an expression it should be indented.
this.indent();
};
Beautifier.prototype.handle_end_expr = function(current_token) {
// statements inside expressions are not valid syntax, but...
// statements must all be closed when their container closes
while (this._flags.mode === MODE.Statement) {
this.restore_mode();
}
this.handle_whitespace_and_comments(current_token);
if (this._flags.multiline_frame) {
this.allow_wrap_or_preserved_newline(current_token,
current_token.text === ']' && is_array(this._flags.mode) && !this._options.keep_array_indentation);
}
if (this._options.space_in_paren) {
if (this._flags.last_token.type === TOKEN.START_EXPR && !this._options.space_in_empty_paren) {
// () [] no inner space in empty parens like these, ever, ref #320
this._output.trim();
this._output.space_before_token = false;
} else {
this._output.space_before_token = true;
}
}
this.deindent();
this.print_token(current_token);
this.restore_mode();
remove_redundant_indentation(this._output, this._previous_flags);
// do {} while () // no statement required after
if (this._flags.do_while && this._previous_flags.mode === MODE.Conditional) {
this._previous_flags.mode = MODE.Expression;
this._flags.do_block = false;
this._flags.do_while = false;
}
};
Beautifier.prototype.handle_start_block = function(current_token) {
this.handle_whitespace_and_comments(current_token);
// Check if this is should be treated as a ObjectLiteral
var next_token = this._tokens.peek();
var second_token = this._tokens.peek(1);
if (this._flags.last_word === 'switch' && this._flags.last_token.type === TOKEN.END_EXPR) {
this.set_mode(MODE.BlockStatement);
this._flags.in_case_statement = true;
} else if (second_token && (
(in_array(second_token.text, [':', ',']) && in_array(next_token.type, [TOKEN.STRING, TOKEN.WORD, TOKEN.RESERVED])) ||
(in_array(next_token.text, ['get', 'set', '...']) && in_array(second_token.type, [TOKEN.WORD, TOKEN.RESERVED]))
)) {
// We don't support TypeScript,but we didn't break it for a very long time.
// We'll try to keep not breaking it.
if (!in_array(this._last_last_text, ['class', 'interface'])) {
this.set_mode(MODE.ObjectLiteral);
} else {
this.set_mode(MODE.BlockStatement);
}
} else if (this._flags.last_token.type === TOKEN.OPERATOR && this._flags.last_token.text === '=>') {
// arrow function: (param1, paramN) => { statements }
this.set_mode(MODE.BlockStatement);
} else if (in_array(this._flags.last_token.type, [TOKEN.EQUALS, TOKEN.START_EXPR, TOKEN.COMMA, TOKEN.OPERATOR]) ||
reserved_array(this._flags.last_token, ['return', 'throw', 'import', 'default'])
) {
// Detecting shorthand function syntax is difficult by scanning forward,
// so check the surrounding context.
// If the block is being returned, imported, export default, passed as arg,
// assigned with = or assigned in a nested object, treat as an ObjectLiteral.
this.set_mode(MODE.ObjectLiteral);
} else {
this.set_mode(MODE.BlockStatement);
}
var empty_braces = !next_token.comments_before && next_token.text === '}';
var empty_anonymous_function = empty_braces && this._flags.last_word === 'function' &&
this._flags.last_token.type === TOKEN.END_EXPR;
if (this._options.brace_preserve_inline) // check for inline, set inline_frame if so
{
// search forward for a newline wanted inside this block
var index = 0;
var check_token = null;
this._flags.inline_frame = true;
do {
index += 1;
check_token = this._tokens.peek(index - 1);
if (check_token.newlines) {
this._flags.inline_frame = false;
break;
}
} while (check_token.type !== TOKEN.EOF &&
!(check_token.type === TOKEN.END_BLOCK && check_token.opened === current_token));
}
if ((this._options.brace_style === "expand" ||
(this._options.brace_style === "none" && current_token.newlines)) &&
!this._flags.inline_frame) {
if (this._flags.last_token.type !== TOKEN.OPERATOR &&
(empty_anonymous_function ||
this._flags.last_token.type === TOKEN.EQUALS ||
(reserved_array(this._flags.last_token, special_words) && this._flags.last_token.text !== 'else'))) {
this._output.space_before_token = true;
} else {
this.print_newline(false, true);
}
} else { // collapse || inline_frame
if (is_array(this._previous_flags.mode) && (this._flags.last_token.type === TOKEN.START_EXPR || this._flags.last_token.type === TOKEN.COMMA)) {
if (this._flags.last_token.type === TOKEN.COMMA || this._options.space_in_paren) {
this._output.space_before_token = true;
}
if (this._flags.last_token.type === TOKEN.COMMA || (this._flags.last_token.type === TOKEN.START_EXPR && this._flags.inline_frame)) {
this.allow_wrap_or_preserved_newline(current_token);
this._previous_flags.multiline_frame = this._previous_flags.multiline_frame || this._flags.multiline_frame;
this._flags.multiline_frame = false;
}
}
if (this._flags.last_token.type !== TOKEN.OPERATOR && this._flags.last_token.type !== TOKEN.START_EXPR) {
if (this._flags.last_token.type === TOKEN.START_BLOCK && !this._flags.inline_frame) {
this.print_newline();
} else {
this._output.space_before_token = true;
}
}
}
this.print_token(current_token);
this.indent();
// Except for specific cases, open braces are followed by a new line.
if (!empty_braces && !(this._options.brace_preserve_inline && this._flags.inline_frame)) {
this.print_newline();
}
};
Beautifier.prototype.handle_end_block = function(current_token) {
// statements must all be closed when their container closes
this.handle_whitespace_and_comments(current_token);
while (this._flags.mode === MODE.Statement) {
this.restore_mode();
}
var empty_braces = this._flags.last_token.type === TOKEN.START_BLOCK;
if (this._flags.inline_frame && !empty_braces) { // try inline_frame (only set if this._options.braces-preserve-inline) first
this._output.space_before_token = true;
} else if (this._options.brace_style === "expand") {
if (!empty_braces) {
this.print_newline();
}
} else {
// skip {}
if (!empty_braces) {
if (is_array(this._flags.mode) && this._options.keep_array_indentation) {
// we REALLY need a newline here, but newliner would skip that
this._options.keep_array_indentation = false;
this.print_newline();
this._options.keep_array_indentation = true;
} else {
this.print_newline();
}
}
}
this.restore_mode();
this.print_token(current_token);
};
Beautifier.prototype.handle_word = function(current_token) {
if (current_token.type === TOKEN.RESERVED) {
if (in_array(current_token.text, ['set', 'get']) && this._flags.mode !== MODE.ObjectLiteral) {
current_token.type = TOKEN.WORD;
} else if (current_token.text === 'import' && this._tokens.peek().text === '(') {
current_token.type = TOKEN.WORD;
} else if (in_array(current_token.text, ['as', 'from']) && !this._flags.import_block) {
current_token.type = TOKEN.WORD;
} else if (this._flags.mode === MODE.ObjectLiteral) {
var next_token = this._tokens.peek();
if (next_token.text === ':') {
current_token.type = TOKEN.WORD;
}
}
}
if (this.start_of_statement(current_token)) {
// The conditional starts the statement if appropriate.
if (reserved_array(this._flags.last_token, ['var', 'let', 'const']) && current_token.type === TOKEN.WORD) {
this._flags.declaration_statement = true;
}
} else if (current_token.newlines && !is_expression(this._flags.mode) &&
(this._flags.last_token.type !== TOKEN.OPERATOR || (this._flags.last_token.text === '--' || this._flags.last_token.text === '++')) &&
this._flags.last_token.type !== TOKEN.EQUALS &&
(this._options.preserve_newlines || !reserved_array(this._flags.last_token, ['var', 'let', 'const', 'set', 'get']))) {
this.handle_whitespace_and_comments(current_token);
this.print_newline();
} else {
this.handle_whitespace_and_comments(current_token);
}
if (this._flags.do_block && !this._flags.do_while) {
if (reserved_word(current_token, 'while')) {
// do {} ## while ()
this._output.space_before_token = true;
this.print_token(current_token);
this._output.space_before_token = true;
this._flags.do_while = true;
return;
} else {
// do {} should always have while as the next word.
// if we don't see the expected while, recover
this.print_newline();
this._flags.do_block = false;
}
}
// if may be followed by else, or not
// Bare/inline ifs are tricky
// Need to unwind the modes correctly: if (a) if (b) c(); else d(); else e();
if (this._flags.if_block) {
if (!this._flags.else_block && reserved_word(current_token, 'else')) {
this._flags.else_block = true;
} else {
while (this._flags.mode === MODE.Statement) {
this.restore_mode();
}
this._flags.if_block = false;
this._flags.else_block = false;
}
}
if (this._flags.in_case_statement && reserved_array(current_token, ['case', 'default'])) {
this.print_newline();
if (this._flags.case_body || this._options.jslint_happy) {
// switch cases following one another
this.deindent();
this._flags.case_body = false;
}
this.print_token(current_token);
this._flags.in_case = true;
return;
}
if (this._flags.last_token.type === TOKEN.COMMA || this._flags.last_token.type === TOKEN.START_EXPR || this._flags.last_token.type === TOKEN.EQUALS || this._flags.last_token.type === TOKEN.OPERATOR) {
if (!this.start_of_object_property()) {
this.allow_wrap_or_preserved_newline(current_token);
}
}
if (reserved_word(current_token, 'function')) {
if (in_array(this._flags.last_token.text, ['}', ';']) ||
(this._output.just_added_newline() && !(in_array(this._flags.last_token.text, ['(', '[', '{', ':', '=', ',']) || this._flags.last_token.type === TOKEN.OPERATOR))) {
// make sure there is a nice clean space of at least one blank line
// before a new function definition
if (!this._output.just_added_blankline() && !current_token.comments_before) {
this.print_newline();
this.print_newline(true);
}
}
if (this._flags.last_token.type === TOKEN.RESERVED || this._flags.last_token.type === TOKEN.WORD) {
if (reserved_array(this._flags.last_token, ['get', 'set', 'new', 'export']) ||
reserved_array(this._flags.last_token, newline_restricted_tokens)) {
this._output.space_before_token = true;
} else if (reserved_word(this._flags.last_token, 'default') && this._last_last_text === 'export') {
this._output.space_before_token = true;
} else if (this._flags.last_token.text === 'declare') {
// accomodates Typescript declare function formatting
this._output.space_before_token = true;
} else {
this.print_newline();
}
} else if (this._flags.last_token.type === TOKEN.OPERATOR || this._flags.last_token.text === '=') {
// foo = function
this._output.space_before_token = true;
} else if (!this._flags.multiline_frame && (is_expression(this._flags.mode) || is_array(this._flags.mode))) {
// (function
} else {
this.print_newline();
}
this.print_token(current_token);
this._flags.last_word = current_token.text;
return;
}
var prefix = 'NONE';
if (this._flags.last_token.type === TOKEN.END_BLOCK) {
if (this._previous_flags.inline_frame) {
prefix = 'SPACE';
} else if (!reserved_array(current_token, ['else', 'catch', 'finally', 'from'])) {
prefix = 'NEWLINE';
} else {
if (this._options.brace_style === "expand" ||
this._options.brace_style === "end-expand" ||
(this._options.brace_style === "none" && current_token.newlines)) {
prefix = 'NEWLINE';
} else {
prefix = 'SPACE';
this._output.space_before_token = true;
}
}
} else if (this._flags.last_token.type === TOKEN.SEMICOLON && this._flags.mode === MODE.BlockStatement) {
// TODO: Should this be for STATEMENT as well?
prefix = 'NEWLINE';
} else if (this._flags.last_token.type === TOKEN.SEMICOLON && is_expression(this._flags.mode)) {
prefix = 'SPACE';
} else if (this._flags.last_token.type === TOKEN.STRING) {
prefix = 'NEWLINE';
} else if (this._flags.last_token.type === TOKEN.RESERVED || this._flags.last_token.type === TOKEN.WORD ||
(this._flags.last_token.text === '*' &&
(in_array(this._last_last_text, ['function', 'yield']) ||
(this._flags.mode === MODE.ObjectLiteral && in_array(this._last_last_text, ['{', ',']))))) {
prefix = 'SPACE';
} else if (this._flags.last_token.type === TOKEN.START_BLOCK) {
if (this._flags.inline_frame) {
prefix = 'SPACE';
} else {
prefix = 'NEWLINE';
}
} else if (this._flags.last_token.type === TOKEN.END_EXPR) {
this._output.space_before_token = true;
prefix = 'NEWLINE';
}
if (reserved_array(current_token, line_starters) && this._flags.last_token.text !== ')') {
if (this._flags.inline_frame || this._flags.last_token.text === 'else' || this._flags.last_token.text === 'export') {
prefix = 'SPACE';
} else {
prefix = 'NEWLINE';
}
}
if (reserved_array(current_token, ['else', 'catch', 'finally'])) {
if ((!(this._flags.last_token.type === TOKEN.END_BLOCK && this._previous_flags.mode === MODE.BlockStatement) ||
this._options.brace_style === "expand" ||
this._options.brace_style === "end-expand" ||
(this._options.brace_style === "none" && current_token.newlines)) &&
!this._flags.inline_frame) {
this.print_newline();
} else {
this._output.trim(true);
var line = this._output.current_line;
// If we trimmed and there's something other than a close block before us
// put a newline back in. Handles '} // comment' scenario.
if (line.last() !== '}') {
this.print_newline();
}
this._output.space_before_token = true;
}
} else if (prefix === 'NEWLINE') {
if (reserved_array(this._flags.last_token, special_words)) {
// no newline between 'return nnn'
this._output.space_before_token = true;
} else if (this._flags.last_token.text === 'declare' && reserved_array(current_token, ['var', 'let', 'const'])) {
// accomodates Typescript declare formatting
this._output.space_before_token = true;
} else if (this._flags.last_token.type !== TOKEN.END_EXPR) {
if ((this._flags.last_token.type !== TOKEN.START_EXPR || !reserved_array(current_token, ['var', 'let', 'const'])) && this._flags.last_token.text !== ':') {
// no need to force newline on 'var': for (var x = 0...)
if (reserved_word(current_token, 'if') && reserved_word(current_token.previous, 'else')) {
// no newline for } else if {
this._output.space_before_token = true;
} else {
this.print_newline();
}
}
} else if (reserved_array(current_token, line_starters) && this._flags.last_token.text !== ')') {
this.print_newline();
}
} else if (this._flags.multiline_frame && is_array(this._flags.mode) && this._flags.last_token.text === ',' && this._last_last_text === '}') {
this.print_newline(); // }, in lists get a newline treatment
} else if (prefix === 'SPACE') {
this._output.space_before_token = true;
}
if (current_token.previous && (current_token.previous.type === TOKEN.WORD || current_token.previous.type === TOKEN.RESERVED)) {
this._output.space_before_token = true;
}
this.print_token(current_token);
this._flags.last_word = current_token.text;
if (current_token.type === TOKEN.RESERVED) {
if (current_token.text === 'do') {
this._flags.do_block = true;
} else if (current_token.text === 'if') {
this._flags.if_block = true;
} else if (current_token.text === 'import') {
this._flags.import_block = true;
} else if (this._flags.import_block && reserved_word(current_token, 'from')) {
this._flags.import_block = false;
}
}
};
Beautifier.prototype.handle_semicolon = function(current_token) {
if (this.start_of_statement(current_token)) {
// The conditional starts the statement if appropriate.
// Semicolon can be the start (and end) of a statement
this._output.space_before_token = false;
} else {
this.handle_whitespace_and_comments(current_token);
}
var next_token = this._tokens.peek();
while (this._flags.mode === MODE.Statement &&
!(this._flags.if_block && reserved_word(next_token, 'else')) &&
!this._flags.do_block) {
this.restore_mode();
}
// hacky but effective for the moment
if (this._flags.import_block) {
this._flags.import_block = false;
}
this.print_token(current_token);
};
Beautifier.prototype.handle_string = function(current_token) {
if (this.start_of_statement(current_token)) {
// The conditional starts the statement if appropriate.
// One difference - strings want at least a space before
this._output.space_before_token = true;
} else {
this.handle_whitespace_and_comments(current_token);
if (this._flags.last_token.type === TOKEN.RESERVED || this._flags.last_token.type === TOKEN.WORD || this._flags.inline_frame) {
this._output.space_before_token = true;
} else if (this._flags.last_token.type === TOKEN.COMMA || this._flags.last_token.type === TOKEN.START_EXPR || this._flags.last_token.type === TOKEN.EQUALS || this._flags.last_token.type === TOKEN.OPERATOR) {
if (!this.start_of_object_property()) {
this.allow_wrap_or_preserved_newline(current_token);
}
} else {
this.print_newline();
}
}
this.print_token(current_token);
};
Beautifier.prototype.handle_equals = function(current_token) {
if (this.start_of_statement(current_token)) {
// The conditional starts the statement if appropriate.
} else {
this.handle_whitespace_and_comments(current_token);
}
if (this._flags.declaration_statement) {
// just got an '=' in a var-line, different formatting/line-breaking, etc will now be done
this._flags.declaration_assignment = true;
}
this._output.space_before_token = true;
this.print_token(current_token);
this._output.space_before_token = true;
};
Beautifier.prototype.handle_comma = function(current_token) {
this.handle_whitespace_and_comments(current_token, true);
this.print_token(current_token);
this._output.space_before_token = true;
if (this._flags.declaration_statement) {
if (is_expression(this._flags.parent.mode)) {
// do not break on comma, for(var a = 1, b = 2)
this._flags.declaration_assignment = false;
}
if (this._flags.declaration_assignment) {
this._flags.declaration_assignment = false;
this.print_newline(false, true);
} else if (this._options.comma_first) {
// for comma-first, we want to allow a newline before the comma
// to turn into a newline after the comma, which we will fixup later
this.allow_wrap_or_preserved_newline(current_token);
}
} else if (this._flags.mode === MODE.ObjectLiteral ||
(this._flags.mode === MODE.Statement && this._flags.parent.mode === MODE.ObjectLiteral)) {
if (this._flags.mode === MODE.Statement) {
this.restore_mode();
}
if (!this._flags.inline_frame) {
this.print_newline();
}
} else if (this._options.comma_first) {
// EXPR or DO_BLOCK
// for comma-first, we want to allow a newline before the comma
// to turn into a newline after the comma, which we will fixup later
this.allow_wrap_or_preserved_newline(current_token);
}
};
Beautifier.prototype.handle_operator = function(current_token) {
var isGeneratorAsterisk = current_token.text === '*' &&
(reserved_array(this._flags.last_token, ['function', 'yield']) ||
(in_array(this._flags.last_token.type, [TOKEN.START_BLOCK, TOKEN.COMMA, TOKEN.END_BLOCK, TOKEN.SEMICOLON]))
);
var isUnary = in_array(current_token.text, ['-', '+']) && (
in_array(this._flags.last_token.type, [TOKEN.START_BLOCK, TOKEN.START_EXPR, TOKEN.EQUALS, TOKEN.OPERATOR]) ||
in_array(this._flags.last_token.text, line_starters) ||
this._flags.last_token.text === ','
);
if (this.start_of_statement(current_token)) {
// The conditional starts the statement if appropriate.
} else {
var preserve_statement_flags = !isGeneratorAsterisk;
this.handle_whitespace_and_comments(current_token, preserve_statement_flags);
}
if (reserved_array(this._flags.last_token, special_words)) {
// "return" had a special handling in TK_WORD. Now we need to return the favor
this._output.space_before_token = true;
this.print_token(current_token);
return;
}
// hack for actionscript's import .*;
if (current_token.text === '*' && this._flags.last_token.type === TOKEN.DOT) {
this.print_token(current_token);
return;
}
if (current_token.text === '::') {
// no spaces around exotic namespacing syntax operator
this.print_token(current_token);
return;
}
// Allow line wrapping between operators when operator_position is
// set to before or preserve
if (this._flags.last_token.type === TOKEN.OPERATOR && in_array(this._options.operator_position, OPERATOR_POSITION_BEFORE_OR_PRESERVE)) {
this.allow_wrap_or_preserved_newline(current_token);
}
if (current_token.text === ':' && this._flags.in_case) {
this._flags.case_body = true;
this.indent();
this.print_token(current_token);
this.print_newline();
this._flags.in_case = false;
return;
}
var space_before = true;
var space_after = true;
var in_ternary = false;
if (current_token.text === ':') {
if (this._flags.ternary_depth === 0) {
// Colon is invalid javascript outside of ternary and object, but do our best to guess what was meant.
space_before = false;
} else {
this._flags.ternary_depth -= 1;
in_ternary = true;
}
} else if (current_token.text === '?') {
this._flags.ternary_depth += 1;
}
// let's handle the operator_position option prior to any conflicting logic
if (!isUnary && !isGeneratorAsterisk && this._options.preserve_newlines && in_array(current_token.text, positionable_operators)) {
var isColon = current_token.text === ':';
var isTernaryColon = (isColon && in_ternary);
var isOtherColon = (isColon && !in_ternary);
switch (this._options.operator_position) {
case OPERATOR_POSITION.before_newline:
// if the current token is : and it's not a ternary statement then we set space_before to false
this._output.space_before_token = !isOtherColon;
this.print_token(current_token);
if (!isColon || isTernaryColon) {
this.allow_wrap_or_preserved_newline(current_token);
}
this._output.space_before_token = true;
return;
case OPERATOR_POSITION.after_newline:
// if the current token is anything but colon, or (via deduction) it's a colon and in a ternary statement,
// then print a newline.
this._output.space_before_token = true;
if (!isColon || isTernaryColon) {
if (this._tokens.peek().newlines) {
this.print_newline(false, true);
} else {
this.allow_wrap_or_preserved_newline(current_token);
}
} else {
this._output.space_before_token = false;
}
this.print_token(current_token);
this._output.space_before_token = true;
return;
case OPERATOR_POSITION.preserve_newline:
if (!isOtherColon) {
this.allow_wrap_or_preserved_newline(current_token);
}
// if we just added a newline, or the current token is : and it's not a ternary statement,
// then we set space_before to false
space_before = !(this._output.just_added_newline() || isOtherColon);
this._output.space_before_token = space_before;
this.print_token(current_token);
this._output.space_before_token = true;
return;
}
}
if (isGeneratorAsterisk) {
this.allow_wrap_or_preserved_newline(current_token);
space_before = false;
var next_token = this._tokens.peek();
space_after = next_token && in_array(next_token.type, [TOKEN.WORD, TOKEN.RESERVED]);
} else if (current_token.text === '...') {
this.allow_wrap_or_preserved_newline(current_token);
space_before = this._flags.last_token.type === TOKEN.START_BLOCK;
space_after = false;
} else if (in_array(current_token.text, ['--', '++', '!', '~']) || isUnary) {
// unary operators (and binary +/- pretending to be unary) special cases
if (this._flags.last_token.type === TOKEN.COMMA || this._flags.last_token.type === TOKEN.START_EXPR) {
this.allow_wrap_or_preserved_newline(current_token);
}
space_before = false;
space_after = false;
// http://www.ecma-international.org/ecma-262/5.1/#sec-7.9.1
// if there is a newline between -- or ++ and anything else we should preserve it.
if (current_token.newlines && (current_token.text === '--' || current_token.text === '++')) {
this.print_newline(false, true);
}
if (this._flags.last_token.text === ';' && is_expression(this._flags.mode)) {
// for (;; ++i)
// ^^^
space_before = true;
}
if (this._flags.last_token.type === TOKEN.RESERVED) {
space_before = true;
} else if (this._flags.last_token.type === TOKEN.END_EXPR) {
space_before = !(this._flags.last_token.text === ']' && (current_token.text === '--' || current_token.text === '++'));
} else if (this._flags.last_token.type === TOKEN.OPERATOR) {
// a++ + ++b;
// a - -b
space_before = in_array(current_token.text, ['--', '-', '++', '+']) && in_array(this._flags.last_token.text, ['--', '-', '++', '+']);
// + and - are not unary when preceeded by -- or ++ operator
// a-- + b
// a * +b
// a - -b
if (in_array(current_token.text, ['+', '-']) && in_array(this._flags.last_token.text, ['--', '++'])) {
space_after = true;
}
}
if (((this._flags.mode === MODE.BlockStatement && !this._flags.inline_frame) || this._flags.mode === MODE.Statement) &&
(this._flags.last_token.text === '{' || this._flags.last_token.text === ';')) {
// { foo; --i }
// foo(); --bar;
this.print_newline();
}
}
this._output.space_before_token = this._output.space_before_token || space_before;
this.print_token(current_token);
this._output.space_before_token = space_after;
};
Beautifier.prototype.handle_block_comment = function(current_token, preserve_statement_flags) {
if (this._output.raw) {
this._output.add_raw_token(current_token);
if (current_token.directives && current_token.directives.preserve === 'end') {
// If we're testing the raw output behavior, do not allow a directive to turn it off.
this._output.raw = this._options.test_output_raw;
}
return;
}
if (current_token.directives) {
this.print_newline(false, preserve_statement_flags);
this.print_token(current_token);
if (current_token.directives.preserve === 'start') {
this._output.raw = true;
}
this.print_newline(false, true);
return;
}
// inline block
if (!acorn.newline.test(current_token.text) && !current_token.newlines) {
this._output.space_before_token = true;
this.print_token(current_token);
this._output.space_before_token = true;
return;
}
var lines = split_linebreaks(current_token.text);
var j; // iterator for this case
var javadoc = false;
var starless = false;
var lastIndent = current_token.whitespace_before;
var lastIndentLength = lastIndent.length;
// block comment starts with a new line
this.print_newline(false, preserve_statement_flags);
// first line always indented
this.print_token(current_token, lines[0]);
this.print_newline(false, preserve_statement_flags);
if (lines.length > 1) {
lines = lines.slice(1);
javadoc = all_lines_start_with(lines, '*');
starless = each_line_matches_indent(lines, lastIndent);
if (javadoc) {
this._flags.alignment = 1;
}
for (j = 0; j < lines.length; j++) {
if (javadoc) {
// javadoc: reformat and re-indent
this.print_token(current_token, ltrim(lines[j]));
} else if (starless && lines[j]) {
// starless: re-indent non-empty content, avoiding trim
this.print_token(current_token, lines[j].substring(lastIndentLength));
} else {
// normal comments output raw
this._output.current_line.set_indent(-1);
this._output.add_token(lines[j]);
}
// for comments on their own line or more than one line, make sure there's a new line after
this.print_newline(false, preserve_statement_flags);
}
this._flags.alignment = 0;
}
};
Beautifier.prototype.handle_comment = function(current_token, preserve_statement_flags) {
if (current_token.newlines) {
this.print_newline(false, preserve_statement_flags);
} else {
this._output.trim(true);
}
this._output.space_before_token = true;
this.print_token(current_token);
this.print_newline(false, preserve_statement_flags);
};
Beautifier.prototype.handle_dot = function(current_token) {
if (this.start_of_statement(current_token)) {
// The conditional starts the statement if appropriate.
} else {
this.handle_whitespace_and_comments(current_token, true);
}
if (reserved_array(this._flags.last_token, special_words)) {
this._output.space_before_token = false;
} else {
// allow preserved newlines before dots in general
// force newlines on dots after close paren when break_chained - for bar().baz()
this.allow_wrap_or_preserved_newline(current_token,
this._flags.last_token.text === ')' && this._options.break_chained_methods);
}
// Only unindent chained method dot if this dot starts a new line.
// Otherwise the automatic extra indentation removal will handle the over indent
if (this._options.unindent_chained_methods && this._output.just_added_newline()) {
this.deindent();
}
this.print_token(current_token);
};
Beautifier.prototype.handle_unknown = function(current_token, preserve_statement_flags) {
this.print_token(current_token);
if (current_token.text[current_token.text.length - 1] === '\n') {
this.print_newline(false, preserve_statement_flags);
}
};
Beautifier.prototype.handle_eof = function(current_token) {
// Unwind any open statements
while (this._flags.mode === MODE.Statement) {
this.restore_mode();
}
this.handle_whitespace_and_comments(current_token);
};
module.exports.Beautifier = Beautifier;