js-beautify 0.1.7

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 (74) hide show
  1. data/.document +5 -0
  2. data/.gitmodules +4 -0
  3. data/Gemfile +13 -0
  4. data/Gemfile.lock +27 -0
  5. data/LICENSE.txt +20 -0
  6. data/README.rdoc +13 -0
  7. data/Rakefile +29 -0
  8. data/VERSION +1 -0
  9. data/bin/js-beautify +5 -0
  10. data/init.sh +3 -0
  11. data/js-beautify-copy/.gitmodules +3 -0
  12. data/js-beautify-copy/Makefile +71 -0
  13. data/js-beautify-copy/README.md +39 -0
  14. data/js-beautify-copy/attic/beautify-cl/beautify-cl.js +142 -0
  15. data/js-beautify-copy/attic/bin/beautify_js +80 -0
  16. data/js-beautify-copy/attic/opera-userscript/beautifier.js +1087 -0
  17. data/js-beautify-copy/attic/opera-userscript/make_opera_userscript.sh +42 -0
  18. data/js-beautify-copy/attic/qtscript/jsbeautify.cpp +121 -0
  19. data/js-beautify-copy/attic/qtscript/jsbeautify.pro +5 -0
  20. data/js-beautify-copy/attic/qtscript/jsbeautify.qrc +6 -0
  21. data/js-beautify-copy/attic/qtscript/readme.txt +28 -0
  22. data/js-beautify-copy/attic/readme.txt +2 -0
  23. data/js-beautify-copy/attic/unmaintained/bbedit/jsBeautify_BBED.scpt +522 -0
  24. data/js-beautify-copy/attic/unmaintained/c-sharp/JSBeautify.cs +801 -0
  25. data/js-beautify-copy/attic/v8/README.txt +40 -0
  26. data/js-beautify-copy/attic/v8/beautify.h +2390 -0
  27. data/js-beautify-copy/attic/v8/jsbeautify.cpp +215 -0
  28. data/js-beautify-copy/beautify-css.js +198 -0
  29. data/js-beautify-copy/beautify-html.js +514 -0
  30. data/js-beautify-copy/beautify.js +1293 -0
  31. data/js-beautify-copy/favicon.png +0 -0
  32. data/js-beautify-copy/index.html +401 -0
  33. data/js-beautify-copy/jquery/jquery.cookie.js +96 -0
  34. data/js-beautify-copy/jquery/jquery.js +167 -0
  35. data/js-beautify-copy/license.txt +24 -0
  36. data/js-beautify-copy/php/jsbeautifier.php +1599 -0
  37. data/js-beautify-copy/php/test.php +476 -0
  38. data/js-beautify-copy/python/MANIFEST.in +2 -0
  39. data/js-beautify-copy/python/js-beautify +7 -0
  40. data/js-beautify-copy/python/js-beautify-profile +16 -0
  41. data/js-beautify-copy/python/js-beautify-test +10 -0
  42. data/js-beautify-copy/python/jsbeautifier/__init__.py +1166 -0
  43. data/js-beautify-copy/python/jsbeautifier/tests/__init__.py +1 -0
  44. data/js-beautify-copy/python/jsbeautifier/tests/testindentation.py +43 -0
  45. data/js-beautify-copy/python/jsbeautifier/tests/testjsbeautifier.py +464 -0
  46. data/js-beautify-copy/python/jsbeautifier/unpackers/README.specs.mkd +25 -0
  47. data/js-beautify-copy/python/jsbeautifier/unpackers/__init__.py +67 -0
  48. data/js-beautify-copy/python/jsbeautifier/unpackers/evalbased.py +39 -0
  49. data/js-beautify-copy/python/jsbeautifier/unpackers/javascriptobfuscator.py +58 -0
  50. data/js-beautify-copy/python/jsbeautifier/unpackers/myobfuscate.py +86 -0
  51. data/js-beautify-copy/python/jsbeautifier/unpackers/packer.py +104 -0
  52. data/js-beautify-copy/python/jsbeautifier/unpackers/tests/__init__.py +2 -0
  53. data/js-beautify-copy/python/jsbeautifier/unpackers/tests/test-myobfuscate-input.js +1 -0
  54. data/js-beautify-copy/python/jsbeautifier/unpackers/tests/test-myobfuscate-output.js +65 -0
  55. data/js-beautify-copy/python/jsbeautifier/unpackers/tests/test-packer-62-input.js +1 -0
  56. data/js-beautify-copy/python/jsbeautifier/unpackers/tests/test-packer-non62-input.js +1 -0
  57. data/js-beautify-copy/python/jsbeautifier/unpackers/tests/testjavascriptobfuscator.py +46 -0
  58. data/js-beautify-copy/python/jsbeautifier/unpackers/tests/testmyobfuscate.py +40 -0
  59. data/js-beautify-copy/python/jsbeautifier/unpackers/tests/testpacker.py +34 -0
  60. data/js-beautify-copy/python/jsbeautifier/unpackers/tests/testurlencode.py +36 -0
  61. data/js-beautify-copy/python/jsbeautifier/unpackers/urlencode.py +34 -0
  62. data/js-beautify-copy/python/setup.py +17 -0
  63. data/js-beautify-copy/tests/beautify-tests.js +489 -0
  64. data/js-beautify-copy/tests/run-tests +17 -0
  65. data/js-beautify-copy/tests/sanitytest.js +128 -0
  66. data/js-beautify-copy/unpackers/javascriptobfuscator_unpacker.js +103 -0
  67. data/js-beautify-copy/unpackers/myobfuscate_unpacker.js +81 -0
  68. data/js-beautify-copy/unpackers/p_a_c_k_e_r_unpacker.js +61 -0
  69. data/js-beautify-copy/unpackers/urlencode_unpacker.js +51 -0
  70. data/lib/js-beautify.rb +0 -0
  71. data/test/helper.rb +18 -0
  72. data/test/test_js-beautify.rb +7 -0
  73. data/update.sh +23 -0
  74. metadata +173 -0
@@ -0,0 +1,1293 @@
1
+ /*jslint onevar: false, plusplus: false */
2
+ /*
3
+
4
+ JS Beautifier
5
+ ---------------
6
+
7
+
8
+ Written by Einar Lielmanis, <einar@jsbeautifier.org>
9
+ http://jsbeautifier.org/
10
+
11
+ Originally converted to javascript by Vital, <vital76@gmail.com>
12
+ "End braces on own line" added by Chris J. Shull, <chrisjshull@gmail.com>
13
+
14
+ You are free to use this in any way you want, in case you find this useful or working for you.
15
+
16
+ Usage:
17
+ js_beautify(js_source_text);
18
+ js_beautify(js_source_text, options);
19
+
20
+ The options are:
21
+ indent_size (default 4) — indentation size,
22
+ indent_char (default space) — character to indent with,
23
+ preserve_newlines (default true) — whether existing line breaks should be preserved,
24
+ preserve_max_newlines (default unlimited) - maximum number of line breaks to be preserved in one chunk,
25
+
26
+ jslint_happy (default false) — if true, then jslint-stricter mode is enforced.
27
+
28
+ jslint_happy !jslint_happy
29
+ ---------------------------------
30
+ function () function()
31
+
32
+ brace_style (default "collapse") - "collapse" | "expand" | "end-expand" | "expand-strict"
33
+ put braces on the same line as control statements (default), or put braces on own line (Allman / ANSI style), or just put end braces on own line.
34
+
35
+ expand-strict: put brace on own line even in such cases:
36
+
37
+ var a =
38
+ {
39
+ a: 5,
40
+ b: 6
41
+ }
42
+ This mode may break your scripts - e.g "return { a: 1 }" will be broken into two lines, so beware.
43
+
44
+ space_before_conditional (default true) - should the space before conditional statement be added, "if(true)" vs "if (true)"
45
+
46
+ e.g
47
+
48
+ js_beautify(js_source_text, {
49
+ 'indent_size': 1,
50
+ 'indent_char': '\t'
51
+ });
52
+
53
+
54
+ */
55
+
56
+
57
+
58
+ function js_beautify(js_source_text, options) {
59
+
60
+ var input, output, token_text, last_type, last_text, last_last_text, last_word, flags, flag_store, indent_string;
61
+ var whitespace, wordchar, punct, parser_pos, line_starters, digits;
62
+ var prefix, token_type, do_block_just_closed;
63
+ var wanted_newline, just_added_newline, n_newlines;
64
+ var preindent_string = '';
65
+
66
+
67
+ // Some interpreters have unexpected results with foo = baz || bar;
68
+ options = options ? options : {};
69
+
70
+ var opt_brace_style;
71
+
72
+ // compatibility
73
+ if (options.space_after_anon_function !== undefined && options.jslint_happy === undefined) {
74
+ options.jslint_happy = options.space_after_anon_function;
75
+ }
76
+ if (options.braces_on_own_line !== undefined) { //graceful handling of deprecated option
77
+ opt_brace_style = options.braces_on_own_line ? "expand" : "collapse";
78
+ }
79
+ opt_brace_style = options.brace_style ? options.brace_style : (opt_brace_style ? opt_brace_style : "collapse");
80
+
81
+
82
+ var opt_indent_size = options.indent_size ? options.indent_size : 4;
83
+ var opt_indent_char = options.indent_char ? options.indent_char : ' ';
84
+ var opt_preserve_newlines = typeof options.preserve_newlines === 'undefined' ? true : options.preserve_newlines;
85
+ var opt_max_preserve_newlines = typeof options.max_preserve_newlines === 'undefined' ? false : options.max_preserve_newlines;
86
+ var opt_jslint_happy = options.jslint_happy === 'undefined' ? false : options.jslint_happy;
87
+ var opt_keep_array_indentation = typeof options.keep_array_indentation === 'undefined' ? false : options.keep_array_indentation;
88
+ var opt_space_before_conditional = typeof options.space_before_conditional === 'undefined' ? true : options.space_before_conditional;
89
+ var opt_indent_case = typeof options.indent_case === 'undefined' ? false : options.indent_case;
90
+
91
+ just_added_newline = false;
92
+
93
+ // cache the source's length.
94
+ var input_length = js_source_text.length;
95
+
96
+ function trim_output(eat_newlines) {
97
+ eat_newlines = typeof eat_newlines === 'undefined' ? false : eat_newlines;
98
+ while (output.length && (output[output.length - 1] === ' '
99
+ || output[output.length - 1] === indent_string
100
+ || output[output.length - 1] === preindent_string
101
+ || (eat_newlines && (output[output.length - 1] === '\n' || output[output.length - 1] === '\r')))) {
102
+ output.pop();
103
+ }
104
+ }
105
+
106
+ function trim(s) {
107
+ return s.replace(/^\s\s*|\s\s*$/, '');
108
+ }
109
+
110
+ // we could use just string.split, but
111
+ // IE doesn't like returning empty strings
112
+ function split_newlines(s)
113
+ {
114
+ //return s.split(/\x0d\x0a|\x0a/);
115
+
116
+ s = s.replace(/\x0d/g, '');
117
+ var out = [],
118
+ idx = s.indexOf("\n");
119
+ while (idx != -1) {
120
+ out.push(s.substring(0, idx));
121
+ s = s.substring(idx + 1);
122
+ idx = s.indexOf("\n");
123
+ }
124
+ if (s.length) {
125
+ out.push(s);
126
+ }
127
+ return out;
128
+ }
129
+
130
+ function force_newline()
131
+ {
132
+ var old_keep_array_indentation = opt_keep_array_indentation;
133
+ opt_keep_array_indentation = false;
134
+ print_newline()
135
+ opt_keep_array_indentation = old_keep_array_indentation;
136
+ }
137
+
138
+ function print_newline(ignore_repeated) {
139
+
140
+ flags.eat_next_space = false;
141
+ if (opt_keep_array_indentation && is_array(flags.mode)) {
142
+ return;
143
+ }
144
+
145
+ ignore_repeated = typeof ignore_repeated === 'undefined' ? true : ignore_repeated;
146
+
147
+ flags.if_line = false;
148
+ trim_output();
149
+
150
+ if (!output.length) {
151
+ return; // no newline on start of file
152
+ }
153
+
154
+ if (output[output.length - 1] !== "\n" || !ignore_repeated) {
155
+ just_added_newline = true;
156
+ output.push("\n");
157
+ }
158
+ if (preindent_string) {
159
+ output.push(preindent_string);
160
+ }
161
+ for (var i = 0; i < flags.indentation_level; i += 1) {
162
+ output.push(indent_string);
163
+ }
164
+ if (flags.var_line && flags.var_line_reindented) {
165
+ output.push(indent_string); // skip space-stuffing, if indenting with a tab
166
+ }
167
+ if (flags.case_body) {
168
+ output.push(indent_string);
169
+ }
170
+ }
171
+
172
+
173
+
174
+ function print_single_space() {
175
+
176
+ if (last_type === 'TK_COMMENT') {
177
+ // no you will not print just a space after a comment
178
+ return print_newline(true);
179
+ }
180
+
181
+ if (flags.eat_next_space) {
182
+ flags.eat_next_space = false;
183
+ return;
184
+ }
185
+ var last_output = ' ';
186
+ if (output.length) {
187
+ last_output = output[output.length - 1];
188
+ }
189
+ if (last_output !== ' ' && last_output !== '\n' && last_output !== indent_string) { // prevent occassional duplicate space
190
+ output.push(' ');
191
+ }
192
+ }
193
+
194
+
195
+ function print_token() {
196
+ just_added_newline = false;
197
+ flags.eat_next_space = false;
198
+ output.push(token_text);
199
+ }
200
+
201
+ function indent() {
202
+ flags.indentation_level += 1;
203
+ }
204
+
205
+
206
+ function remove_indent() {
207
+ if (output.length && output[output.length - 1] === indent_string) {
208
+ output.pop();
209
+ }
210
+ }
211
+
212
+ function set_mode(mode) {
213
+ if (flags) {
214
+ flag_store.push(flags);
215
+ }
216
+ flags = {
217
+ previous_mode: flags ? flags.mode : 'BLOCK',
218
+ mode: mode,
219
+ var_line: false,
220
+ var_line_tainted: false,
221
+ var_line_reindented: false,
222
+ in_html_comment: false,
223
+ if_line: false,
224
+ in_case_statement: false, // switch(..){ INSIDE HERE }
225
+ in_case: false, // we're on the exact line with "case 0:"
226
+ case_body: false, // the indented case-action block
227
+ eat_next_space: false,
228
+ indentation_baseline: -1,
229
+ indentation_level: (flags ? flags.indentation_level + (flags.case_body?1:0) + ((flags.var_line && flags.var_line_reindented) ? 1 : 0) : 0),
230
+ ternary_depth: 0
231
+ };
232
+ }
233
+
234
+ function is_array(mode) {
235
+ return mode === '[EXPRESSION]' || mode === '[INDENTED-EXPRESSION]';
236
+ }
237
+
238
+ function is_expression(mode) {
239
+ return in_array(mode, ['[EXPRESSION]', '(EXPRESSION)', '(FOR-EXPRESSION)', '(COND-EXPRESSION)']);
240
+ }
241
+
242
+ function restore_mode() {
243
+ do_block_just_closed = flags.mode === 'DO_BLOCK';
244
+ if (flag_store.length > 0) {
245
+ var mode = flags.mode;
246
+ flags = flag_store.pop();
247
+ flags.previous_mode = mode;
248
+ }
249
+ }
250
+
251
+ function all_lines_start_with(lines, c) {
252
+ for (var i = 0; i < lines.length; i++) {
253
+ var line = trim(lines[i]);
254
+ if (line.charAt(0) !== c) {
255
+ return false;
256
+ }
257
+ }
258
+ return true;
259
+ }
260
+
261
+ function is_special_word(word)
262
+ {
263
+ return in_array(word, ['case', 'return', 'do', 'if', 'throw', 'else']);
264
+ }
265
+
266
+ function in_array(what, arr) {
267
+ for (var i = 0; i < arr.length; i += 1) {
268
+ if (arr[i] === what) {
269
+ return true;
270
+ }
271
+ }
272
+ return false;
273
+ }
274
+
275
+ function look_up(exclude) {
276
+ var local_pos = parser_pos;
277
+ var c = input.charAt(local_pos);
278
+ while (in_array(c, whitespace) && c != exclude) {
279
+ local_pos++;
280
+ if (local_pos >= input_length) return 0;
281
+ c = input.charAt(local_pos);
282
+ }
283
+ return c;
284
+ }
285
+
286
+ function get_next_token() {
287
+ n_newlines = 0;
288
+
289
+ if (parser_pos >= input_length) {
290
+ return ['', 'TK_EOF'];
291
+ }
292
+
293
+ wanted_newline = false;
294
+
295
+ var c = input.charAt(parser_pos);
296
+ parser_pos += 1;
297
+
298
+
299
+ var keep_whitespace = opt_keep_array_indentation && is_array(flags.mode);
300
+
301
+ if (keep_whitespace) {
302
+
303
+ //
304
+ // slight mess to allow nice preservation of array indentation and reindent that correctly
305
+ // first time when we get to the arrays:
306
+ // var a = [
307
+ // ....'something'
308
+ // we make note of whitespace_count = 4 into flags.indentation_baseline
309
+ // so we know that 4 whitespaces in original source match indent_level of reindented source
310
+ //
311
+ // and afterwards, when we get to
312
+ // 'something,
313
+ // .......'something else'
314
+ // we know that this should be indented to indent_level + (7 - indentation_baseline) spaces
315
+ //
316
+ var whitespace_count = 0;
317
+
318
+ while (in_array(c, whitespace)) {
319
+
320
+ if (c === "\n") {
321
+ trim_output();
322
+ output.push("\n");
323
+ just_added_newline = true;
324
+ whitespace_count = 0;
325
+ } else {
326
+ if (c === '\t') {
327
+ whitespace_count += 4;
328
+ } else if (c === '\r') {
329
+ // nothing
330
+ } else {
331
+ whitespace_count += 1;
332
+ }
333
+ }
334
+
335
+ if (parser_pos >= input_length) {
336
+ return ['', 'TK_EOF'];
337
+ }
338
+
339
+ c = input.charAt(parser_pos);
340
+ parser_pos += 1;
341
+
342
+ }
343
+ if (flags.indentation_baseline === -1) {
344
+ flags.indentation_baseline = whitespace_count;
345
+ }
346
+
347
+ if (just_added_newline) {
348
+ var i;
349
+ for (i = 0; i < flags.indentation_level + 1; i += 1) {
350
+ output.push(indent_string);
351
+ }
352
+ if (flags.indentation_baseline !== -1) {
353
+ for (i = 0; i < whitespace_count - flags.indentation_baseline; i++) {
354
+ output.push(' ');
355
+ }
356
+ }
357
+ }
358
+
359
+ } else {
360
+ while (in_array(c, whitespace)) {
361
+
362
+ if (c === "\n") {
363
+ n_newlines += ( (opt_max_preserve_newlines) ? (n_newlines <= opt_max_preserve_newlines) ? 1: 0: 1 );
364
+ }
365
+
366
+
367
+ if (parser_pos >= input_length) {
368
+ return ['', 'TK_EOF'];
369
+ }
370
+
371
+ c = input.charAt(parser_pos);
372
+ parser_pos += 1;
373
+
374
+ }
375
+
376
+ if (opt_preserve_newlines) {
377
+ if (n_newlines > 1) {
378
+ for (i = 0; i < n_newlines; i += 1) {
379
+ print_newline(i === 0);
380
+ just_added_newline = true;
381
+ }
382
+ }
383
+ }
384
+ wanted_newline = n_newlines > 0;
385
+ }
386
+
387
+
388
+ if (in_array(c, wordchar)) {
389
+ if (parser_pos < input_length) {
390
+ while (in_array(input.charAt(parser_pos), wordchar)) {
391
+ c += input.charAt(parser_pos);
392
+ parser_pos += 1;
393
+ if (parser_pos === input_length) {
394
+ break;
395
+ }
396
+ }
397
+ }
398
+
399
+ // small and surprisingly unugly hack for 1E-10 representation
400
+ if (parser_pos !== input_length && c.match(/^[0-9]+[Ee]$/) && (input.charAt(parser_pos) === '-' || input.charAt(parser_pos) === '+')) {
401
+
402
+ var sign = input.charAt(parser_pos);
403
+ parser_pos += 1;
404
+
405
+ var t = get_next_token(parser_pos);
406
+ c += sign + t[0];
407
+ return [c, 'TK_WORD'];
408
+ }
409
+
410
+ if (c === 'in') { // hack for 'in' operator
411
+ return [c, 'TK_OPERATOR'];
412
+ }
413
+ if (wanted_newline && last_type !== 'TK_OPERATOR'
414
+ && last_type !== 'TK_EQUALS'
415
+ && !flags.if_line && (opt_preserve_newlines || last_text !== 'var')) {
416
+ print_newline();
417
+ }
418
+ return [c, 'TK_WORD'];
419
+ }
420
+
421
+ if (c === '(' || c === '[') {
422
+ return [c, 'TK_START_EXPR'];
423
+ }
424
+
425
+ if (c === ')' || c === ']') {
426
+ return [c, 'TK_END_EXPR'];
427
+ }
428
+
429
+ if (c === '{') {
430
+ return [c, 'TK_START_BLOCK'];
431
+ }
432
+
433
+ if (c === '}') {
434
+ return [c, 'TK_END_BLOCK'];
435
+ }
436
+
437
+ if (c === ';') {
438
+ return [c, 'TK_SEMICOLON'];
439
+ }
440
+
441
+ if (c === '/') {
442
+ var comment = '';
443
+ // peek for comment /* ... */
444
+ var inline_comment = true;
445
+ if (input.charAt(parser_pos) === '*') {
446
+ parser_pos += 1;
447
+ if (parser_pos < input_length) {
448
+ while (parser_pos < input_length &&
449
+ ! (input.charAt(parser_pos) === '*' && input.charAt(parser_pos + 1) && input.charAt(parser_pos + 1) === '/')) {
450
+ c = input.charAt(parser_pos);
451
+ comment += c;
452
+ if (c === "\n" || c === "\r") {
453
+ inline_comment = false;
454
+ }
455
+ parser_pos += 1;
456
+ if (parser_pos >= input_length) {
457
+ break;
458
+ }
459
+ }
460
+ }
461
+ parser_pos += 2;
462
+ if (inline_comment && n_newlines == 0) {
463
+ return ['/*' + comment + '*/', 'TK_INLINE_COMMENT'];
464
+ } else {
465
+ return ['/*' + comment + '*/', 'TK_BLOCK_COMMENT'];
466
+ }
467
+ }
468
+ // peek for comment // ...
469
+ if (input.charAt(parser_pos) === '/') {
470
+ comment = c;
471
+ while (input.charAt(parser_pos) !== '\r' && input.charAt(parser_pos) !== '\n') {
472
+ comment += input.charAt(parser_pos);
473
+ parser_pos += 1;
474
+ if (parser_pos >= input_length) {
475
+ break;
476
+ }
477
+ }
478
+ parser_pos += 1;
479
+ if (wanted_newline) {
480
+ print_newline();
481
+ }
482
+ return [comment, 'TK_COMMENT'];
483
+ }
484
+
485
+ }
486
+
487
+ if (c === "'" || // string
488
+ c === '"' || // string
489
+ (c === '/' &&
490
+ ((last_type === 'TK_WORD' && is_special_word(last_text)) ||
491
+ (last_text === ')' && in_array(flags.previous_mode, ['(COND-EXPRESSION)', '(FOR-EXPRESSION)'])) ||
492
+ (last_type === 'TK_COMMENT' || last_type === 'TK_START_EXPR' || last_type === 'TK_START_BLOCK' || last_type === 'TK_END_BLOCK' || last_type === 'TK_OPERATOR' || last_type === 'TK_EQUALS' || last_type === 'TK_EOF' || last_type === 'TK_SEMICOLON')))) { // regexp
493
+ var sep = c;
494
+ var esc = false;
495
+ var resulting_string = c;
496
+
497
+ if (parser_pos < input_length) {
498
+ if (sep === '/') {
499
+ //
500
+ // handle regexp separately...
501
+ //
502
+ var in_char_class = false;
503
+ while (esc || in_char_class || input.charAt(parser_pos) !== sep) {
504
+ resulting_string += input.charAt(parser_pos);
505
+ if (!esc) {
506
+ esc = input.charAt(parser_pos) === '\\';
507
+ if (input.charAt(parser_pos) === '[') {
508
+ in_char_class = true;
509
+ } else if (input.charAt(parser_pos) === ']') {
510
+ in_char_class = false;
511
+ }
512
+ } else {
513
+ esc = false;
514
+ }
515
+ parser_pos += 1;
516
+ if (parser_pos >= input_length) {
517
+ // incomplete string/rexp when end-of-file reached.
518
+ // bail out with what had been received so far.
519
+ return [resulting_string, 'TK_STRING'];
520
+ }
521
+ }
522
+
523
+ } else {
524
+ //
525
+ // and handle string also separately
526
+ //
527
+ while (esc || input.charAt(parser_pos) !== sep) {
528
+ resulting_string += input.charAt(parser_pos);
529
+ if (!esc) {
530
+ esc = input.charAt(parser_pos) === '\\';
531
+ } else {
532
+ esc = false;
533
+ }
534
+ parser_pos += 1;
535
+ if (parser_pos >= input_length) {
536
+ // incomplete string/rexp when end-of-file reached.
537
+ // bail out with what had been received so far.
538
+ return [resulting_string, 'TK_STRING'];
539
+ }
540
+ }
541
+ }
542
+
543
+
544
+
545
+ }
546
+
547
+ parser_pos += 1;
548
+
549
+ resulting_string += sep;
550
+
551
+ if (sep === '/') {
552
+ // regexps may have modifiers /regexp/MOD , so fetch those, too
553
+ while (parser_pos < input_length && in_array(input.charAt(parser_pos), wordchar)) {
554
+ resulting_string += input.charAt(parser_pos);
555
+ parser_pos += 1;
556
+ }
557
+ }
558
+ return [resulting_string, 'TK_STRING'];
559
+ }
560
+
561
+ if (c === '#') {
562
+
563
+
564
+ if (output.length === 0 && input.charAt(parser_pos) === '!') {
565
+ // shebang
566
+ resulting_string = c;
567
+ while (parser_pos < input_length && c != '\n') {
568
+ c = input.charAt(parser_pos);
569
+ resulting_string += c;
570
+ parser_pos += 1;
571
+ }
572
+ output.push(trim(resulting_string) + '\n');
573
+ print_newline();
574
+ return get_next_token();
575
+ }
576
+
577
+
578
+
579
+ // Spidermonkey-specific sharp variables for circular references
580
+ // https://developer.mozilla.org/En/Sharp_variables_in_JavaScript
581
+ // http://mxr.mozilla.org/mozilla-central/source/js/src/jsscan.cpp around line 1935
582
+ var sharp = '#';
583
+ if (parser_pos < input_length && in_array(input.charAt(parser_pos), digits)) {
584
+ do {
585
+ c = input.charAt(parser_pos);
586
+ sharp += c;
587
+ parser_pos += 1;
588
+ } while (parser_pos < input_length && c !== '#' && c !== '=');
589
+ if (c === '#') {
590
+ //
591
+ } else if (input.charAt(parser_pos) === '[' && input.charAt(parser_pos + 1) === ']') {
592
+ sharp += '[]';
593
+ parser_pos += 2;
594
+ } else if (input.charAt(parser_pos) === '{' && input.charAt(parser_pos + 1) === '}') {
595
+ sharp += '{}';
596
+ parser_pos += 2;
597
+ }
598
+ return [sharp, 'TK_WORD'];
599
+ }
600
+ }
601
+
602
+ if (c === '<' && input.substring(parser_pos - 1, parser_pos + 3) === '<!--') {
603
+ parser_pos += 3;
604
+ c = '<!--';
605
+ while (input.charAt(parser_pos) != '\n' && parser_pos < input_length) {
606
+ c += input.charAt(parser_pos);
607
+ parser_pos++;
608
+ }
609
+ flags.in_html_comment = true;
610
+ return [c, 'TK_COMMENT'];
611
+ }
612
+
613
+ if (c === '-' && flags.in_html_comment && input.substring(parser_pos - 1, parser_pos + 2) === '-->') {
614
+ flags.in_html_comment = false;
615
+ parser_pos += 2;
616
+ if (wanted_newline) {
617
+ print_newline();
618
+ }
619
+ return ['-->', 'TK_COMMENT'];
620
+ }
621
+
622
+ if (in_array(c, punct)) {
623
+ while (parser_pos < input_length && in_array(c + input.charAt(parser_pos), punct)) {
624
+ c += input.charAt(parser_pos);
625
+ parser_pos += 1;
626
+ if (parser_pos >= input_length) {
627
+ break;
628
+ }
629
+ }
630
+
631
+ if (c === '=') {
632
+ return [c, 'TK_EQUALS'];
633
+ } else {
634
+ return [c, 'TK_OPERATOR'];
635
+ }
636
+ }
637
+
638
+ return [c, 'TK_UNKNOWN'];
639
+ }
640
+
641
+ //----------------------------------
642
+ indent_string = '';
643
+ while (opt_indent_size > 0) {
644
+ indent_string += opt_indent_char;
645
+ opt_indent_size -= 1;
646
+ }
647
+
648
+ while (js_source_text && (js_source_text.charAt(0) === ' ' || js_source_text.charAt(0) === '\t')) {
649
+ preindent_string += js_source_text.charAt(0);
650
+ js_source_text = js_source_text.substring(1);
651
+ }
652
+ input = js_source_text;
653
+
654
+ last_word = ''; // last 'TK_WORD' passed
655
+ last_type = 'TK_START_EXPR'; // last token type
656
+ last_text = ''; // last token text
657
+ last_last_text = ''; // pre-last token text
658
+ output = [];
659
+
660
+ do_block_just_closed = false;
661
+
662
+ whitespace = "\n\r\t ".split('');
663
+ wordchar = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_$'.split('');
664
+ digits = '0123456789'.split('');
665
+
666
+ punct = '+ - * / % & ++ -- = += -= *= /= %= == === != !== > < >= <= >> << >>> >>>= >>= <<= && &= | || ! !! , : ? ^ ^= |= ::';
667
+ punct += ' <%= <% %> <?= <? ?>'; // try to be a good boy and try not to break the markup language identifiers
668
+ punct = punct.split(' ');
669
+
670
+ // words which should always start on new line.
671
+ line_starters = 'continue,try,throw,return,var,if,switch,case,default,for,while,break,function'.split(',');
672
+
673
+ // states showing if we are currently in expression (i.e. "if" case) - 'EXPRESSION', or in usual block (like, procedure), 'BLOCK'.
674
+ // some formatting depends on that.
675
+ flag_store = [];
676
+ set_mode('BLOCK');
677
+
678
+ parser_pos = 0;
679
+ while (true) {
680
+ var t = get_next_token(parser_pos);
681
+ token_text = t[0];
682
+ token_type = t[1];
683
+ if (token_type === 'TK_EOF') {
684
+ break;
685
+ }
686
+
687
+ switch (token_type) {
688
+
689
+ case 'TK_START_EXPR':
690
+
691
+ if (token_text === '[') {
692
+
693
+ if (last_type === 'TK_WORD' || last_text === ')') {
694
+ // this is array index specifier, break immediately
695
+ // a[x], fn()[x]
696
+ if (in_array(last_text, line_starters)) {
697
+ print_single_space();
698
+ }
699
+ set_mode('(EXPRESSION)');
700
+ print_token();
701
+ break;
702
+ }
703
+
704
+ if (flags.mode === '[EXPRESSION]' || flags.mode === '[INDENTED-EXPRESSION]') {
705
+ if (last_last_text === ']' && last_text === ',') {
706
+ // ], [ goes to new line
707
+ if (flags.mode === '[EXPRESSION]') {
708
+ flags.mode = '[INDENTED-EXPRESSION]';
709
+ if (!opt_keep_array_indentation) {
710
+ indent();
711
+ }
712
+ }
713
+ set_mode('[EXPRESSION]');
714
+ if (!opt_keep_array_indentation) {
715
+ print_newline();
716
+ }
717
+ } else if (last_text === '[') {
718
+ if (flags.mode === '[EXPRESSION]') {
719
+ flags.mode = '[INDENTED-EXPRESSION]';
720
+ if (!opt_keep_array_indentation) {
721
+ indent();
722
+ }
723
+ }
724
+ set_mode('[EXPRESSION]');
725
+
726
+ if (!opt_keep_array_indentation) {
727
+ print_newline();
728
+ }
729
+ } else {
730
+ set_mode('[EXPRESSION]');
731
+ }
732
+ } else {
733
+ set_mode('[EXPRESSION]');
734
+ }
735
+
736
+
737
+
738
+ } else {
739
+ if (last_word === 'for') {
740
+ set_mode('(FOR-EXPRESSION)');
741
+ } else if (in_array(last_word, ['if', 'while'])) {
742
+ set_mode('(COND-EXPRESSION)');
743
+ } else {
744
+ set_mode('(EXPRESSION)');
745
+ }
746
+ }
747
+
748
+ if (last_text === ';' || last_type === 'TK_START_BLOCK') {
749
+ print_newline();
750
+ } else if (last_type === 'TK_END_EXPR' || last_type === 'TK_START_EXPR' || last_type === 'TK_END_BLOCK' || last_text === '.') {
751
+ if (wanted_newline) {
752
+ print_newline();
753
+ }
754
+ // do nothing on (( and )( and ][ and ]( and .(
755
+ } else if (last_type !== 'TK_WORD' && last_type !== 'TK_OPERATOR') {
756
+ print_single_space();
757
+ } else if (last_word === 'function' || last_word === 'typeof') {
758
+ // function() vs function ()
759
+ if (opt_jslint_happy) {
760
+ print_single_space();
761
+ }
762
+ } else if (in_array(last_text, line_starters) || last_text === 'catch') {
763
+ if (opt_space_before_conditional) {
764
+ print_single_space();
765
+ }
766
+ }
767
+ print_token();
768
+
769
+ break;
770
+
771
+ case 'TK_END_EXPR':
772
+ if (token_text === ']') {
773
+ if (opt_keep_array_indentation) {
774
+ if (last_text === '}') {
775
+ // trim_output();
776
+ // print_newline(true);
777
+ remove_indent();
778
+ print_token();
779
+ restore_mode();
780
+ break;
781
+ }
782
+ } else {
783
+ if (flags.mode === '[INDENTED-EXPRESSION]') {
784
+ if (last_text === ']') {
785
+ restore_mode();
786
+ print_newline();
787
+ print_token();
788
+ break;
789
+ }
790
+ }
791
+ }
792
+ }
793
+ restore_mode();
794
+ print_token();
795
+ break;
796
+
797
+ case 'TK_START_BLOCK':
798
+
799
+ if (last_word === 'do') {
800
+ set_mode('DO_BLOCK');
801
+ } else {
802
+ set_mode('BLOCK');
803
+ }
804
+ if (opt_brace_style=="expand" || opt_brace_style=="expand-strict") {
805
+ var empty_braces = false;
806
+ if (opt_brace_style == "expand-strict")
807
+ {
808
+ empty_braces = (look_up() == '}');
809
+ if (!empty_braces) {
810
+ print_newline(true);
811
+ }
812
+ } else {
813
+ if (last_type !== 'TK_OPERATOR') {
814
+ if (last_text === '=' || (is_special_word(last_text) && last_text !== 'else')) {
815
+ print_single_space();
816
+ } else {
817
+ print_newline(true);
818
+ }
819
+ }
820
+ }
821
+ print_token();
822
+ if (!empty_braces) indent();
823
+ } else {
824
+ if (last_type !== 'TK_OPERATOR' && last_type !== 'TK_START_EXPR') {
825
+ if (last_type === 'TK_START_BLOCK') {
826
+ print_newline();
827
+ } else {
828
+ print_single_space();
829
+ }
830
+ } else {
831
+ // if TK_OPERATOR or TK_START_EXPR
832
+ if (is_array(flags.previous_mode) && last_text === ',') {
833
+ if (last_last_text === '}') {
834
+ // }, { in array context
835
+ print_single_space();
836
+ } else {
837
+ print_newline(); // [a, b, c, {
838
+ }
839
+ }
840
+ }
841
+ indent();
842
+ print_token();
843
+ }
844
+
845
+ break;
846
+
847
+ case 'TK_END_BLOCK':
848
+ restore_mode();
849
+ if (opt_brace_style=="expand" || opt_brace_style == "expand-strict") {
850
+ if (last_text !== '{') {
851
+ print_newline();
852
+ }
853
+ print_token();
854
+ } else {
855
+ if (last_type === 'TK_START_BLOCK') {
856
+ // nothing
857
+ if (just_added_newline) {
858
+ remove_indent();
859
+ } else {
860
+ // {}
861
+ trim_output();
862
+ }
863
+ } else {
864
+ if (is_array(flags.mode) && opt_keep_array_indentation) {
865
+ // we REALLY need a newline here, but newliner would skip that
866
+ opt_keep_array_indentation = false;
867
+ print_newline();
868
+ opt_keep_array_indentation = true;
869
+
870
+ } else {
871
+ print_newline();
872
+ }
873
+ }
874
+ print_token();
875
+ }
876
+ break;
877
+
878
+ case 'TK_WORD':
879
+
880
+ // no, it's not you. even I have problems understanding how this works
881
+ // and what does what.
882
+ if (do_block_just_closed) {
883
+ // do {} ## while ()
884
+ print_single_space();
885
+ print_token();
886
+ print_single_space();
887
+ do_block_just_closed = false;
888
+ break;
889
+ }
890
+
891
+ if (token_text === 'function') {
892
+ if (flags.var_line) {
893
+ flags.var_line_reindented = true;
894
+ }
895
+ if ((just_added_newline || last_text === ';') && last_text !== '{'
896
+ && last_type != 'TK_BLOCK_COMMENT' && last_type != 'TK_COMMENT') {
897
+ // make sure there is a nice clean space of at least one blank line
898
+ // before a new function definition
899
+ n_newlines = just_added_newline ? n_newlines : 0;
900
+ if ( ! opt_preserve_newlines) {
901
+ n_newlines = 1;
902
+ }
903
+
904
+ for (var i = 0; i < 2 - n_newlines; i++) {
905
+ print_newline(false);
906
+ }
907
+ }
908
+ }
909
+
910
+ if (token_text === 'case' || (token_text === 'default' && flags.in_case_statement)) {
911
+ if (last_text === ':' || flags.case_body) {
912
+ // switch cases following one another
913
+ remove_indent();
914
+ } else {
915
+ // case statement starts in the same line where switch
916
+ if (!opt_indent_case)
917
+ flags.indentation_level--;
918
+ print_newline();
919
+ if (!opt_indent_case)
920
+ flags.indentation_level++;
921
+ }
922
+ print_token();
923
+ flags.in_case = true;
924
+ flags.in_case_statement = true;
925
+ flags.case_body = false;
926
+ break;
927
+ }
928
+
929
+ prefix = 'NONE';
930
+
931
+ if (last_type === 'TK_END_BLOCK') {
932
+
933
+ if (!in_array(token_text.toLowerCase(), ['else', 'catch', 'finally'])) {
934
+ prefix = 'NEWLINE';
935
+ } else {
936
+ if (opt_brace_style=="expand" || opt_brace_style=="end-expand" || opt_brace_style == "expand-strict") {
937
+ prefix = 'NEWLINE';
938
+ } else {
939
+ prefix = 'SPACE';
940
+ print_single_space();
941
+ }
942
+ }
943
+ } else if (last_type === 'TK_SEMICOLON' && (flags.mode === 'BLOCK' || flags.mode === 'DO_BLOCK')) {
944
+ prefix = 'NEWLINE';
945
+ } else if (last_type === 'TK_SEMICOLON' && is_expression(flags.mode)) {
946
+ prefix = 'SPACE';
947
+ } else if (last_type === 'TK_STRING') {
948
+ prefix = 'NEWLINE';
949
+ } else if (last_type === 'TK_WORD') {
950
+ if (last_text === 'else') {
951
+ // eat newlines between ...else *** some_op...
952
+ // won't preserve extra newlines in this place (if any), but don't care that much
953
+ trim_output(true);
954
+ }
955
+ prefix = 'SPACE';
956
+ } else if (last_type === 'TK_START_BLOCK') {
957
+ prefix = 'NEWLINE';
958
+ } else if (last_type === 'TK_END_EXPR') {
959
+ print_single_space();
960
+ prefix = 'NEWLINE';
961
+ }
962
+
963
+ if (in_array(token_text, line_starters) && last_text !== ')') {
964
+ if (last_text == 'else') {
965
+ prefix = 'SPACE';
966
+ } else {
967
+ prefix = 'NEWLINE';
968
+ }
969
+
970
+ if (token_text === 'function' && (last_text === 'get' || last_text === 'set')) {
971
+ prefix = 'SPACE';
972
+ }
973
+ }
974
+
975
+ if (flags.if_line && last_type === 'TK_END_EXPR') {
976
+ flags.if_line = false;
977
+ }
978
+ if (in_array(token_text.toLowerCase(), ['else', 'catch', 'finally'])) {
979
+ if (last_type !== 'TK_END_BLOCK' || opt_brace_style=="expand" || opt_brace_style=="end-expand" || opt_brace_style == "expand-strict") {
980
+ print_newline();
981
+ } else {
982
+ trim_output(true);
983
+ print_single_space();
984
+ }
985
+ } else if (prefix === 'NEWLINE') {
986
+ if ((last_type === 'TK_START_EXPR' || last_text === '=' || last_text === ',') && token_text === 'function') {
987
+ // no need to force newline on 'function': (function
988
+ // DONOTHING
989
+ } else if (token_text === 'function' && last_text == 'new') {
990
+ print_single_space();
991
+ } else if (is_special_word(last_text)) {
992
+ // no newline between 'return nnn'
993
+ print_single_space();
994
+ } else if (last_type !== 'TK_END_EXPR') {
995
+ if ((last_type !== 'TK_START_EXPR' || token_text !== 'var') && last_text !== ':') {
996
+ // no need to force newline on 'var': for (var x = 0...)
997
+ if (token_text === 'if' && last_word === 'else' && last_text !== '{') {
998
+ // no newline for } else if {
999
+ print_single_space();
1000
+ } else {
1001
+ flags.var_line = false;
1002
+ flags.var_line_reindented = false;
1003
+ print_newline();
1004
+ }
1005
+ }
1006
+ } else if (in_array(token_text, line_starters) && last_text != ')') {
1007
+ flags.var_line = false;
1008
+ flags.var_line_reindented = false;
1009
+ print_newline();
1010
+ }
1011
+ } else if (is_array(flags.mode) && last_text === ',' && last_last_text === '}') {
1012
+ print_newline(); // }, in lists get a newline treatment
1013
+ } else if (prefix === 'SPACE') {
1014
+ print_single_space();
1015
+ }
1016
+ print_token();
1017
+ last_word = token_text;
1018
+
1019
+ if (token_text === 'var') {
1020
+ flags.var_line = true;
1021
+ flags.var_line_reindented = false;
1022
+ flags.var_line_tainted = false;
1023
+ }
1024
+
1025
+ if (token_text === 'if') {
1026
+ flags.if_line = true;
1027
+ }
1028
+ if (token_text === 'else') {
1029
+ flags.if_line = false;
1030
+ }
1031
+
1032
+ break;
1033
+
1034
+ case 'TK_SEMICOLON':
1035
+
1036
+ print_token();
1037
+ flags.var_line = false;
1038
+ flags.var_line_reindented = false;
1039
+ if (flags.mode == 'OBJECT') {
1040
+ // OBJECT mode is weird and doesn't get reset too well.
1041
+ flags.mode = 'BLOCK';
1042
+ }
1043
+ break;
1044
+
1045
+ case 'TK_STRING':
1046
+
1047
+ if (last_type === 'TK_END_EXPR' && in_array(flags.previous_mode, ['(COND-EXPRESSION)', '(FOR-EXPRESSION)'])) {
1048
+ print_single_space();
1049
+ } else if (last_type == 'TK_STRING' || last_type === 'TK_START_BLOCK' || last_type === 'TK_END_BLOCK' || last_type === 'TK_SEMICOLON') {
1050
+ print_newline();
1051
+ } else if (last_type === 'TK_WORD') {
1052
+ print_single_space();
1053
+ }
1054
+ print_token();
1055
+ break;
1056
+
1057
+ case 'TK_EQUALS':
1058
+ if (flags.var_line) {
1059
+ // just got an '=' in a var-line, different formatting/line-breaking, etc will now be done
1060
+ flags.var_line_tainted = true;
1061
+ }
1062
+ print_single_space();
1063
+ print_token();
1064
+ print_single_space();
1065
+ break;
1066
+
1067
+ case 'TK_OPERATOR':
1068
+
1069
+ var space_before = true;
1070
+ var space_after = true;
1071
+
1072
+ if (flags.var_line && token_text === ',' && (is_expression(flags.mode))) {
1073
+ // do not break on comma, for(var a = 1, b = 2)
1074
+ flags.var_line_tainted = false;
1075
+ }
1076
+
1077
+ if (flags.var_line) {
1078
+ if (token_text === ',') {
1079
+ if (flags.var_line_tainted) {
1080
+ print_token();
1081
+ flags.var_line_reindented = true;
1082
+ flags.var_line_tainted = false;
1083
+ print_newline();
1084
+ break;
1085
+ } else {
1086
+ flags.var_line_tainted = false;
1087
+ }
1088
+ // } else if (token_text === ':') {
1089
+ // hmm, when does this happen? tests don't catch this
1090
+ // flags.var_line = false;
1091
+ }
1092
+ }
1093
+
1094
+ if (is_special_word(last_text)) {
1095
+ // "return" had a special handling in TK_WORD. Now we need to return the favor
1096
+ print_single_space();
1097
+ print_token();
1098
+ break;
1099
+ }
1100
+
1101
+ // hack for actionscript's import .*;
1102
+ if (token_text == '*' && last_type == 'TK_UNKNOWN' && ! last_last_text.match(/^\d+$/)) {
1103
+ print_token();
1104
+ break;
1105
+ }
1106
+
1107
+ if (token_text === ':' && flags.in_case) {
1108
+ if (opt_indent_case)
1109
+ flags.case_body = true;
1110
+ print_token(); // colon really asks for separate treatment
1111
+ print_newline();
1112
+ flags.in_case = false;
1113
+ break;
1114
+ }
1115
+
1116
+ if (token_text === '::') {
1117
+ // no spaces around exotic namespacing syntax operator
1118
+ print_token();
1119
+ break;
1120
+ }
1121
+
1122
+ if (token_text === ',') {
1123
+ if (flags.var_line) {
1124
+ if (flags.var_line_tainted) {
1125
+ print_token();
1126
+ print_newline();
1127
+ flags.var_line_tainted = false;
1128
+ } else {
1129
+ print_token();
1130
+ print_single_space();
1131
+ }
1132
+ } else if (last_type === 'TK_END_BLOCK' && flags.mode !== "(EXPRESSION)") {
1133
+ print_token();
1134
+ if (flags.mode === 'OBJECT' && last_text === '}') {
1135
+ print_newline();
1136
+ } else {
1137
+ print_single_space();
1138
+ }
1139
+ } else {
1140
+ if (flags.mode === 'OBJECT') {
1141
+ print_token();
1142
+ print_newline();
1143
+ } else {
1144
+ // EXPR or DO_BLOCK
1145
+ print_token();
1146
+ print_single_space();
1147
+ }
1148
+ }
1149
+ break;
1150
+ // } else if (in_array(token_text, ['--', '++', '!']) || (in_array(token_text, ['-', '+']) && (in_array(last_type, ['TK_START_BLOCK', 'TK_START_EXPR', 'TK_EQUALS']) || in_array(last_text, line_starters) || in_array(last_text, ['==', '!=', '+=', '-=', '*=', '/=', '+', '-'])))) {
1151
+ } else if (in_array(token_text, ['--', '++', '!']) || (in_array(token_text, ['-', '+']) && (in_array(last_type, ['TK_START_BLOCK', 'TK_START_EXPR', 'TK_EQUALS', 'TK_OPERATOR']) || in_array(last_text, line_starters)))) {
1152
+ // unary operators (and binary +/- pretending to be unary) special cases
1153
+
1154
+ space_before = false;
1155
+ space_after = false;
1156
+
1157
+ if (last_text === ';' && is_expression(flags.mode)) {
1158
+ // for (;; ++i)
1159
+ // ^^^
1160
+ space_before = true;
1161
+ }
1162
+ if (last_type === 'TK_WORD' && in_array(last_text, line_starters)) {
1163
+ space_before = true;
1164
+ }
1165
+
1166
+ if (flags.mode === 'BLOCK' && (last_text === '{' || last_text === ';')) {
1167
+ // { foo; --i }
1168
+ // foo(); --bar;
1169
+ print_newline();
1170
+ }
1171
+ } else if (token_text === '.') {
1172
+ // decimal digits or object.property
1173
+ space_before = false;
1174
+
1175
+ } else if (token_text === ':') {
1176
+ if (flags.ternary_depth == 0) {
1177
+ if (flags.mode == 'BLOCK') {
1178
+ flags.mode = 'OBJECT';
1179
+ }
1180
+ space_before = false;
1181
+ } else {
1182
+ flags.ternary_depth -= 1;
1183
+ }
1184
+ } else if (token_text === '?') {
1185
+ flags.ternary_depth += 1;
1186
+ }
1187
+ if (space_before) {
1188
+ print_single_space();
1189
+ }
1190
+
1191
+ print_token();
1192
+
1193
+ if (space_after) {
1194
+ print_single_space();
1195
+ }
1196
+
1197
+ if (token_text === '!') {
1198
+ // flags.eat_next_space = true;
1199
+ }
1200
+
1201
+ break;
1202
+
1203
+ case 'TK_BLOCK_COMMENT':
1204
+
1205
+ var lines = split_newlines(token_text);
1206
+
1207
+ if (all_lines_start_with(lines.slice(1), '*')) {
1208
+ // javadoc: reformat and reindent
1209
+ print_newline();
1210
+ output.push(lines[0]);
1211
+ for (i = 1; i < lines.length; i++) {
1212
+ print_newline();
1213
+ output.push(' ');
1214
+ output.push(trim(lines[i]));
1215
+ }
1216
+
1217
+ } else {
1218
+
1219
+ // simple block comment: leave intact
1220
+ if (lines.length > 1) {
1221
+ // multiline comment block starts with a new line
1222
+ print_newline();
1223
+ } else {
1224
+ // single-line /* comment */ stays where it is
1225
+ if (last_type === 'TK_END_BLOCK') {
1226
+ print_newline();
1227
+ } else {
1228
+ print_single_space();
1229
+ }
1230
+
1231
+ }
1232
+
1233
+ for (i = 0; i < lines.length; i++) {
1234
+ output.push(lines[i]);
1235
+ output.push("\n");
1236
+ }
1237
+
1238
+ }
1239
+ if(look_up('\n') != '\n')
1240
+ print_newline();
1241
+ break;
1242
+
1243
+ case 'TK_INLINE_COMMENT':
1244
+ print_single_space();
1245
+ print_token();
1246
+ if (is_expression(flags.mode)) {
1247
+ print_single_space();
1248
+ } else {
1249
+ force_newline();
1250
+ }
1251
+ break;
1252
+
1253
+ case 'TK_COMMENT':
1254
+
1255
+ if (last_type == 'TK_COMMENT') {
1256
+ print_newline();
1257
+ if (wanted_newline) {
1258
+ print_newline(false);
1259
+ }
1260
+ } else {
1261
+ if (wanted_newline) {
1262
+ print_newline();
1263
+ } else {
1264
+ print_single_space();
1265
+ }
1266
+ }
1267
+ print_token();
1268
+ if(look_up('\n') != '\n')
1269
+ force_newline();
1270
+ break;
1271
+
1272
+ case 'TK_UNKNOWN':
1273
+ if (is_special_word(last_text)) {
1274
+ print_single_space();
1275
+ }
1276
+ print_token();
1277
+ break;
1278
+ }
1279
+
1280
+ last_last_text = last_text;
1281
+ last_type = token_type;
1282
+ last_text = token_text;
1283
+ }
1284
+
1285
+ var sweet_code = preindent_string + output.join('').replace(/[\r\n ]+$/, '');
1286
+ return sweet_code;
1287
+
1288
+ }
1289
+
1290
+ // Add support for CommonJS. Just put this file somewhere on your require.paths
1291
+ // and you will be able to `var js_beautify = require("beautify").js_beautify`.
1292
+ if (typeof exports !== "undefined")
1293
+ exports.js_beautify = js_beautify;