js-beautify 1.7.0 → 1.7.4

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