js-beautify 0.1.7

Sign up to get free protection for your applications and to get access to all the features.
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,801 @@
1
+ //
2
+ // converted from javascript by rekna anker <rekna1@gmail.com>
3
+ //
4
+
5
+ using System;
6
+ using System.Collections.Generic;
7
+ using System.Linq;
8
+ using System.Text;
9
+ using System.Collections;
10
+ using System.Text.RegularExpressions;
11
+
12
+ namespace JSBeautifyLib
13
+ {
14
+
15
+ public class JSBeautifyOptions
16
+ {
17
+ public int? indent_size { get; set; }
18
+ public char? indent_char { get; set; }
19
+ public int? indent_level { get; set; }
20
+ public bool? preserve_newlines { get; set; }
21
+
22
+
23
+ }
24
+ public class JSBeautify
25
+ {
26
+ private StringBuilder output;
27
+ private string indent_string;
28
+ private int indent_level;
29
+ private string token_text;
30
+ private Stack<string> modes;
31
+ private string current_mode;
32
+ private int opt_indent_size;
33
+ private char opt_indent_char;
34
+ private int opt_indent_level;
35
+ private bool opt_preserve_newlines;
36
+ private bool if_line_flag;
37
+ private bool do_block_just_closed;
38
+ private string input;
39
+
40
+
41
+ private void trim_output()
42
+ {
43
+ while ((output.Length>0) && ((output[output.Length-1]==' ')||(output[output.Length-1].ToString()==indent_string)))
44
+ {
45
+ output.Remove(output.Length-1,1);
46
+ }
47
+ }
48
+
49
+ private void print_newline(bool? ignore_repeated)
50
+ {
51
+ ignore_repeated = ignore_repeated ?? true;
52
+
53
+ if_line_flag = false;
54
+ trim_output();
55
+
56
+ if (output.Length == 0)
57
+ return;
58
+
59
+ if ((output[output.Length - 1] != '\n') || !ignore_repeated.Value)
60
+ {
61
+ output.Append(Environment.NewLine);
62
+ }
63
+
64
+ for (var i = 0; i < indent_level; i++)
65
+ {
66
+ output.Append(indent_string);
67
+ }
68
+ }
69
+
70
+ private void print_space()
71
+ {
72
+ var last_output = " ";
73
+ if (output.Length > 0)
74
+ last_output = output[output.Length - 1].ToString();
75
+ if ((last_output != " ") && (last_output != "\n") && (last_output != indent_string))
76
+ {
77
+ output.Append(' ');
78
+ }
79
+ }
80
+
81
+
82
+ private void print_token()
83
+ {
84
+ output.Append(token_text);
85
+ }
86
+
87
+ private void indent()
88
+ {
89
+ indent_level++;
90
+ }
91
+
92
+ private void unindent()
93
+ {
94
+ if (indent_level > 0)
95
+ indent_level--;
96
+ }
97
+
98
+ private void remove_indent()
99
+ {
100
+ if ((output.Length>0) && (output[output.Length-1].ToString()==indent_string))
101
+ {
102
+ output.Remove(output.Length-1,1);
103
+ }
104
+ }
105
+
106
+ private void set_mode(string mode)
107
+ {
108
+ modes.Push(current_mode);
109
+ current_mode = mode;
110
+ }
111
+
112
+ private void restore_mode()
113
+ {
114
+ do_block_just_closed = (current_mode == "DO_BLOCK");
115
+ current_mode = modes.Pop();
116
+ }
117
+
118
+ private bool in_array(object what, ArrayList arr)
119
+ {
120
+ return arr.Contains(what);
121
+
122
+ }
123
+
124
+ private bool is_ternary_op()
125
+ {
126
+ int level = 0;
127
+ int colon_count = 0;
128
+ for (var i = output.Length - 1; i >= 0; i--)
129
+ {
130
+ switch (output[i])
131
+ {
132
+ case ':':
133
+ if (level == 0)
134
+ colon_count++;
135
+ break;
136
+ case '?':
137
+ if (level == 0)
138
+ {
139
+ if (colon_count == 0)
140
+ {
141
+ return true;
142
+ }
143
+ else
144
+ {
145
+ colon_count--;
146
+ }
147
+ }
148
+ break;
149
+ case '{':
150
+ if (level == 0) return false;
151
+ level--;
152
+ break;
153
+ case '(':
154
+ case '[':
155
+ level--;
156
+ break;
157
+ case ')':
158
+ case ']':
159
+ case '}':
160
+ level++;
161
+ break;
162
+ }
163
+ }
164
+ return false;
165
+ }
166
+
167
+ private string whitespace;
168
+ private string wordchar;
169
+ private int parser_pos;
170
+ private string last_type;
171
+ private string last_text;
172
+ private string digits;
173
+ private string[] punct;
174
+ private string prefix;
175
+ private string[] get_next_token(ref int parser_pos)
176
+ {
177
+ var n_newlines = 0;
178
+
179
+ if (parser_pos >= input.Length)
180
+ {
181
+ return new string[] { "", "TK_EOF" };
182
+ }
183
+
184
+ string c = input[parser_pos].ToString();
185
+ parser_pos++;
186
+
187
+ while (whitespace.Contains(c))
188
+ {
189
+ if (parser_pos >= input.Length)
190
+ {
191
+ return new string[] { "", "TK_EOF" };
192
+ }
193
+
194
+ if (c == "\n")
195
+ n_newlines++;
196
+
197
+ c = input[parser_pos].ToString();
198
+ parser_pos++;
199
+ }
200
+
201
+ var wanted_newline = false;
202
+
203
+ if (opt_preserve_newlines)
204
+ {
205
+ if (n_newlines > 1)
206
+ {
207
+ for (var i = 0; i < 2; i++)
208
+ {
209
+ print_newline(i == 0);
210
+ }
211
+ }
212
+ wanted_newline = (n_newlines == 1);
213
+
214
+ }
215
+
216
+ if (wordchar.Contains(c))
217
+ {
218
+ if (parser_pos < input.Length)
219
+ {
220
+ while (wordchar.Contains(input[parser_pos]))
221
+ {
222
+ c += input[parser_pos];
223
+ parser_pos++;
224
+ if (parser_pos == input.Length)
225
+ break;
226
+ }
227
+ }
228
+
229
+
230
+ if ((parser_pos!=input.Length) && (Regex.IsMatch(c,"^[0-9]+[Ee]$")) && ((input[parser_pos] == '-') || (input[parser_pos]=='+')))
231
+ {
232
+ var sign = input[parser_pos];
233
+ parser_pos++;
234
+
235
+ var t = get_next_token(ref parser_pos);
236
+ c += sign + t[0];
237
+ return new string[] { c, "TK_WORD" };
238
+ }
239
+
240
+ if (c == "in")
241
+ {
242
+ return new string[] { c, "TK_OPERATOR" };
243
+ }
244
+
245
+ if (wanted_newline && last_type != "TK_OPERATOR" && !if_line_flag)
246
+ {
247
+ print_newline(null);
248
+ }
249
+ return new string[] { c, "TK_WORD" };
250
+
251
+ }
252
+
253
+ if ((c == "(") || (c == "["))
254
+ return new string[] {c,"TK_START_EXPR"};
255
+
256
+ if (c == ")" || c == "]") {
257
+ return new string[] {c, "TK_END_EXPR"};
258
+ }
259
+
260
+ if (c == "{") {
261
+ return new string[] {c, "TK_START_BLOCK"};
262
+ }
263
+
264
+ if (c == "}") {
265
+ return new string[] {c, "TK_END_BLOCK"};
266
+ }
267
+
268
+ if (c == ";") {
269
+ return new string[] {c, "TK_SEMICOLON"};
270
+ }
271
+
272
+ if (c=="/") {
273
+ var comment = "";
274
+ if (input[parser_pos]=='*') {
275
+ parser_pos++;
276
+ if (parser_pos<input.Length){
277
+ while (!((input[parser_pos]=='*') && (input[parser_pos+1]>'\0') && (input[parser_pos+1]=='/') && (parser_pos<input.Length)))
278
+ {
279
+ comment+=input[parser_pos];
280
+ parser_pos++;
281
+ if (parser_pos>=input.Length) {
282
+ break;
283
+ }
284
+ }
285
+ }
286
+
287
+ parser_pos+=2;
288
+ return new string[] { "/*"+comment+"*/", "TK_BLOCK_COMMENT"};
289
+ }
290
+
291
+ if (input[parser_pos]=='/') {
292
+ comment = c;
293
+ while ((input[parser_pos]!='\x0d') && (input[parser_pos]!='\x0a')) {
294
+ comment+=input[parser_pos];
295
+ parser_pos++;
296
+ if (parser_pos>=input.Length){
297
+ break;
298
+ }
299
+ }
300
+
301
+ parser_pos++;
302
+ if (wanted_newline){
303
+ print_newline(null);
304
+ }
305
+ return new string[] {comment,"TK_COMMENT"};
306
+
307
+ }
308
+ }
309
+
310
+ if ( (c=="'") || (c=="\"") || ((c=="/")
311
+ && ((last_type=="TK_WORD" && last_text=="return") || ((last_type=="TK_START_EXPR") || (last_type=="TK_START_BLOCK") || (last_type=="TK_END_BLOCK")
312
+ || (last_type=="TK_OPERATOR") || (last_type=="TK_EOF") || (last_type=="TK_SEMICOLON"))))
313
+ ) {
314
+ var sep = c;
315
+ var esc = false;
316
+ var resulting_string = c;
317
+
318
+ if (parser_pos<input.Length) {
319
+ if (sep=="/") {
320
+ var in_char_class=false;
321
+ while ((esc) || (in_char_class) || (input[parser_pos].ToString()!=sep)) {
322
+ resulting_string+=input[parser_pos];
323
+ if (!esc) {
324
+ esc = input[parser_pos]=='\\';
325
+ if (input[parser_pos]=='['){
326
+ in_char_class=true;
327
+ }
328
+ else if (input[parser_pos]==']') {
329
+ in_char_class=false;
330
+ }
331
+ }
332
+ else {
333
+ esc=false;
334
+ }
335
+ parser_pos++;
336
+ if (parser_pos>=input.Length) {
337
+ return new string[] { resulting_string, "TK_STRING"};
338
+ }
339
+ }
340
+ }
341
+ else {
342
+ while ((esc) || (input[parser_pos].ToString()!=sep)) {
343
+ resulting_string += input[parser_pos];
344
+ if (!esc) {
345
+ esc = input[parser_pos]=='\\';
346
+ } else {
347
+ esc=false;
348
+ }
349
+ parser_pos++;
350
+ if (parser_pos>=input.Length){
351
+ return new string[] {resulting_string,"TK_STRING"};
352
+ }
353
+ }
354
+ }
355
+ }
356
+
357
+ parser_pos += 1;
358
+
359
+ resulting_string += sep;
360
+
361
+ if (sep == "/") {
362
+ // regexps may have modifiers /regexp/MOD , so fetch those, too
363
+ while ((parser_pos < input.Length) && (wordchar.Contains(input[parser_pos]))) {
364
+ resulting_string += input[parser_pos];
365
+ parser_pos += 1;
366
+ }
367
+ }
368
+ return new string [] {resulting_string, "TK_STRING"};
369
+
370
+
371
+ }
372
+
373
+ if (c == "#") {
374
+ var sharp = "#";
375
+ if ((parser_pos < input.Length) && (digits.Contains(input[parser_pos]))) {
376
+ do {
377
+ c = input[parser_pos].ToString();
378
+ sharp += c;
379
+ parser_pos += 1;
380
+ } while ((parser_pos < input.Length ) && (c != "#") && (c != "="));
381
+ if (c == "#") {
382
+ return new string[] {sharp, "TK_WORD"};;
383
+ } else {
384
+ return new string[] {sharp, "TK_OPERATOR"};;
385
+ }
386
+ }
387
+ }
388
+
389
+
390
+ if ((c == "<") && (input.Substring(parser_pos - 1, 3) == "<!--")) {
391
+ parser_pos += 3;
392
+ return new string[] {"<!--", "TK_COMMENT"};;
393
+ }
394
+
395
+ if ((c == "-") && (input.Substring(parser_pos - 1, 2) == "-->")) {
396
+ parser_pos += 2;
397
+ if (wanted_newline) {
398
+ print_newline(null);
399
+ }
400
+ return new string[] {"-->", "TK_COMMENT"};
401
+ }
402
+
403
+ if (punct.Contains(c)) {
404
+ while ((parser_pos < input.Length) && (punct.Contains(c + input[parser_pos]))) {
405
+ c += input[parser_pos];
406
+ parser_pos += 1;
407
+ if (parser_pos >= input.Length) {
408
+ break;
409
+ }
410
+ }
411
+
412
+ return new string[] {c, "TK_OPERATOR"};
413
+ }
414
+
415
+ return new string[] {c, "TK_UNKNOWN"};
416
+
417
+
418
+ }
419
+
420
+ private string last_word;
421
+ private bool var_line;
422
+ private bool var_line_tainted;
423
+ private string[] line_starters;
424
+ private bool in_case;
425
+ private string token_type;
426
+
427
+ public string GetResult()
428
+ {
429
+ if (add_script_tags)
430
+ {
431
+ output.AppendLine().AppendLine("</script>");
432
+ }
433
+
434
+ return output.ToString();
435
+ }
436
+
437
+ private bool add_script_tags;
438
+ public JSBeautify(string js_source_text, JSBeautifyOptions options)
439
+ {
440
+ opt_indent_size = options.indent_size ?? 4;
441
+ opt_indent_char = options.indent_char ?? ' ';
442
+ opt_indent_level = options.indent_level ?? 0;
443
+ opt_preserve_newlines = options.preserve_newlines ?? true;
444
+ output = new StringBuilder();
445
+ modes = new Stack<string>();
446
+
447
+
448
+
449
+ indent_string = "";
450
+
451
+ while (opt_indent_size > 0) {
452
+ indent_string += opt_indent_char;
453
+ opt_indent_size -= 1;
454
+ }
455
+
456
+ indent_level = opt_indent_level;
457
+
458
+
459
+
460
+ input = js_source_text.Replace("<script type=\"text/javascript\">","").Replace("</script>","");
461
+ if (input.Length != js_source_text.Length)
462
+ {
463
+ output.AppendLine("<script type=\"text/javascript\">");
464
+ add_script_tags = true;
465
+ }
466
+
467
+ last_word = ""; // last 'TK_WORD' passed
468
+ last_type = "TK_START_EXPR"; // last token type
469
+ last_text = ""; // last token text
470
+
471
+ do_block_just_closed = false;
472
+ var_line = false; // currently drawing var .... ;
473
+ var_line_tainted = false; // false: var a = 5; true: var a = 5, b = 6
474
+
475
+ whitespace = "\n\r\t ";
476
+ wordchar = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_$";
477
+ digits = "0123456789";
478
+
479
+ // <!-- is a special case (ok, it's a minor hack actually)
480
+ punct = "+ - * / % & ++ -- = += -= *= /= %= == === != !== > < >= <= >> << >>> >>>= >>= <<= && &= | || ! !! , : ? ^ ^= |= ::".Split(' ');
481
+
482
+ // words which should always start on new line.
483
+ line_starters = "continue,try,throw,return,var,if,switch,case,default,for,while,break,function".Split(',');
484
+
485
+ // states showing if we are currently in expression (i.e. "if" case) - 'EXPRESSION', or in usual block (like, procedure), 'BLOCK'.
486
+ // some formatting depends on that.
487
+ current_mode = "BLOCK";
488
+ modes.Push(current_mode);
489
+
490
+ parser_pos = 0;
491
+ in_case = false;
492
+
493
+ while (true) {
494
+ var t = get_next_token(ref parser_pos);
495
+ token_text = t[0];
496
+ token_type = t[1];
497
+ if (token_type == "TK_EOF") {
498
+ break;
499
+ }
500
+
501
+ switch (token_type) {
502
+
503
+ case "TK_START_EXPR":
504
+ var_line = false;
505
+ set_mode("EXPRESSION");
506
+ if ((last_text == ";") || (last_type == "TK_START_BLOCK")) {
507
+ print_newline(null);
508
+ } else if ((last_type == "TK_END_EXPR") ||( last_type == "TK_START_EXPR")) {
509
+ // do nothing on (( and )( and ][ and ]( ..
510
+ } else if ((last_type != "TK_WORD") && (last_type != "TK_OPERATOR")) {
511
+ print_space();
512
+ } else if (line_starters.Contains(last_word)) {
513
+ print_space();
514
+ }
515
+ print_token();
516
+ break;
517
+
518
+ case "TK_END_EXPR":
519
+ print_token();
520
+ restore_mode();
521
+ break;
522
+
523
+ case "TK_START_BLOCK":
524
+
525
+ if (last_word == "do") {
526
+ set_mode("DO_BLOCK");
527
+ } else {
528
+ set_mode("BLOCK");
529
+ }
530
+ if ((last_type != "TK_OPERATOR") && (last_type != "TK_START_EXPR")) {
531
+ if (last_type == "TK_START_BLOCK") {
532
+ print_newline(null);
533
+ } else {
534
+ print_space();
535
+ }
536
+ }
537
+ print_token();
538
+ indent();
539
+ break;
540
+
541
+ case "TK_END_BLOCK":
542
+ if (last_type == "TK_START_BLOCK") {
543
+ // nothing
544
+ trim_output();
545
+ unindent();
546
+ } else {
547
+ unindent();
548
+ print_newline(null);
549
+ }
550
+ print_token();
551
+ restore_mode();
552
+ break;
553
+
554
+ case "TK_WORD":
555
+
556
+ if (do_block_just_closed) {
557
+ // do {} ## while ()
558
+ print_space();
559
+ print_token();
560
+ print_space();
561
+ do_block_just_closed = false;
562
+ break;
563
+ }
564
+
565
+ if ((token_text == "case" )|| (token_text == "default")) {
566
+ if (last_text == ":") {
567
+ // switch cases following one another
568
+ remove_indent();
569
+ } else {
570
+ // case statement starts in the same line where switch
571
+ unindent();
572
+ print_newline(null);
573
+ indent();
574
+ }
575
+ print_token();
576
+ in_case = true;
577
+ break;
578
+ }
579
+
580
+ prefix = "NONE";
581
+
582
+ if (last_type == "TK_END_BLOCK") {
583
+ if (!(new string[] {"else", "catch", "finally" }).Contains(token_text.ToLower())) {
584
+ prefix = "NEWLINE";
585
+ } else {
586
+ prefix = "SPACE";
587
+ print_space();
588
+ }
589
+ } else if ((last_type == "TK_SEMICOLON") && ((current_mode == "BLOCK") ||( current_mode == "DO_BLOCK"))) {
590
+ prefix = "NEWLINE";
591
+ } else if ((last_type == "TK_SEMICOLON") && (current_mode == "EXPRESSION")) {
592
+ prefix = "SPACE";
593
+ } else if (last_type == "TK_STRING") {
594
+ prefix = "NEWLINE";
595
+ } else if (last_type == "TK_WORD") {
596
+ prefix = "SPACE";
597
+ } else if (last_type == "TK_START_BLOCK") {
598
+ prefix = "NEWLINE";
599
+ } else if (last_type == "TK_END_EXPR") {
600
+ print_space();
601
+ prefix = "NEWLINE";
602
+ }
603
+
604
+ if ((last_type != "TK_END_BLOCK") && ((new string[] {"else", "catch", "finally"}).Contains(token_text.ToLower()))) {
605
+ print_newline(null);
606
+ } else if ((line_starters.Contains(token_text)) ||( prefix == "NEWLINE")) {
607
+ if (last_text == "else") {
608
+ // no need to force newline on else break
609
+ print_space();
610
+ } else if (((last_type == "TK_START_EXPR") ||( last_text == "=") || (last_text == ",")) && (token_text == "function")) {
611
+ // no need to force newline on "function": (function
612
+ // DONOTHING
613
+ } else if ((last_type == "TK_WORD") && ((last_text == "return") || (last_text == "throw"))) {
614
+ // no newline between "return nnn"
615
+ print_space();
616
+ } else if (last_type != "TK_END_EXPR") {
617
+ if (((last_type != "TK_START_EXPR") || (token_text != "var")) && (last_text != ":")) {
618
+ // no need to force newline on "var": for (var x = 0...)
619
+ if ((token_text == "if") && (last_type == "TK_WORD") && (last_word == "else")) {
620
+ // no newline for } else if {
621
+ print_space();
622
+ } else {
623
+ print_newline(null);
624
+ }
625
+ }
626
+ } else {
627
+ if ((line_starters.Contains(token_text)) && (last_text != ")")) {
628
+ print_newline(null);
629
+ }
630
+ }
631
+ } else if (prefix == "SPACE") {
632
+ print_space();
633
+ }
634
+ print_token();
635
+ last_word = token_text;
636
+
637
+ if (token_text == "var") {
638
+ var_line = true;
639
+ var_line_tainted = false;
640
+ }
641
+
642
+ if (token_text == "if" || token_text == "else") {
643
+ if_line_flag = true;
644
+ }
645
+
646
+ break;
647
+
648
+ case "TK_SEMICOLON":
649
+
650
+ print_token();
651
+ var_line = false;
652
+ break;
653
+
654
+ case "TK_STRING":
655
+
656
+ if ((last_type == "TK_START_BLOCK" )||( last_type == "TK_END_BLOCK") || (last_type == "TK_SEMICOLON")) {
657
+ print_newline(null);
658
+ } else if (last_type == "TK_WORD") {
659
+ print_space();
660
+ }
661
+ print_token();
662
+ break;
663
+
664
+ case "TK_OPERATOR":
665
+
666
+ var start_delim = true;
667
+ var end_delim = true;
668
+ if (var_line && (token_text != ",")) {
669
+ var_line_tainted = true;
670
+ if (token_text == ":") {
671
+ var_line = false;
672
+ }
673
+ }
674
+ if (var_line && (token_text == ",") && (current_mode == "EXPRESSION")) {
675
+ // do not break on comma, for(var a = 1, b = 2)
676
+ var_line_tainted = false;
677
+ }
678
+
679
+ if (token_text == ":" && in_case) {
680
+ print_token(); // colon really asks for separate treatment
681
+ print_newline(null);
682
+ in_case = false;
683
+ break;
684
+ }
685
+
686
+ if (token_text == "::") {
687
+ // no spaces around exotic namespacing syntax operator
688
+ print_token();
689
+ break;
690
+ }
691
+
692
+ if (token_text == ",") {
693
+ if (var_line) {
694
+ if (var_line_tainted) {
695
+ print_token();
696
+ print_newline(null);
697
+ var_line_tainted = false;
698
+ } else {
699
+ print_token();
700
+ print_space();
701
+ }
702
+ } else if (last_type == "TK_END_BLOCK") {
703
+ print_token();
704
+ print_newline(null);
705
+ } else {
706
+ if (current_mode == "BLOCK") {
707
+ print_token();
708
+ print_newline(null);
709
+ } else {
710
+ // EXPR od DO_BLOCK
711
+ print_token();
712
+ print_space();
713
+ }
714
+ }
715
+ break;
716
+ } else if ((token_text == "--" )|| (token_text == "++")) { // unary operators special case
717
+ if (last_text == ";") {
718
+ if (current_mode == "BLOCK") {
719
+ // { foo; --i }
720
+ print_newline(null);
721
+ start_delim = true;
722
+ end_delim = false;
723
+ } else {
724
+ // space for (;; ++i)
725
+ start_delim = true;
726
+ end_delim = false;
727
+ }
728
+ } else {
729
+ if (last_text == "{") {
730
+ // {--i
731
+ print_newline(null);
732
+ }
733
+ start_delim = false;
734
+ end_delim = false;
735
+ }
736
+ } else if (((token_text == "!") ||( token_text == "+") || (token_text == "-")) && ((last_text == "return" )|| (last_text == "case"))) {
737
+ start_delim = true;
738
+ end_delim = false;
739
+ } else if (((token_text == "!" )||( token_text == "+") || (token_text == "-")) && (last_type == "TK_START_EXPR")) {
740
+ // special case handling: if (!a)
741
+ start_delim = false;
742
+ end_delim = false;
743
+ } else if (last_type == "TK_OPERATOR") {
744
+ start_delim = false;
745
+ end_delim = false;
746
+ } else if (last_type == "TK_END_EXPR") {
747
+ start_delim = true;
748
+ end_delim = true;
749
+ } else if (token_text == ".") {
750
+ // decimal digits or object.property
751
+ start_delim = false;
752
+ end_delim = false;
753
+
754
+ } else if (token_text == ":") {
755
+ if (is_ternary_op()) {
756
+ start_delim = true;
757
+ } else {
758
+ start_delim = false;
759
+ }
760
+ }
761
+ if (start_delim) {
762
+ print_space();
763
+ }
764
+
765
+ print_token();
766
+
767
+ if (end_delim) {
768
+ print_space();
769
+ }
770
+ break;
771
+
772
+ case "TK_BLOCK_COMMENT":
773
+
774
+ print_newline(null);
775
+ print_token();
776
+ print_newline(null);
777
+ break;
778
+
779
+ case "TK_COMMENT":
780
+
781
+ // print_newline();
782
+ print_space();
783
+ print_token();
784
+ print_newline(null);
785
+ break;
786
+
787
+ case "TK_UNKNOWN":
788
+ print_token();
789
+ break;
790
+ }
791
+
792
+ last_type = token_type;
793
+ last_text = token_text;
794
+ }
795
+
796
+
797
+ }
798
+
799
+
800
+ }
801
+ }