grat 0.0.1 → 0.0.2

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.
@@ -0,0 +1,112 @@
1
+ /*
2
+
3
+ JS Beautifier Rhino command line script
4
+ ----------------------------------------
5
+
6
+ Written by Patrick Hof, <patrickhof@web.de>
7
+
8
+ This script is to be run with Rhino[1], the JavaScript Engine written in Java,
9
+ on the command line.
10
+
11
+ Usage:
12
+ java org.mozilla.javascript.tools.shell.Main beautify-cl.js
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
+ [1] http://www.mozilla.org/rhino/
17
+
18
+ */
19
+ load("beautify.js");
20
+ load("HTML-Beautify.js");
21
+
22
+
23
+ function print_usage() {
24
+ print("Usage: java org.mozilla.javascript.tools.shell.Main beautify-cl.js [options] [file || URL]\n");
25
+ print("Reads from standard input if no file or URL is specified.\n");
26
+ print("Options:");
27
+ print("-i NUM\tIndent size (1 for TAB)");
28
+ print("-n\tPreserve newlines");
29
+ print("-p\tJSLint-pedantic mode, currently only adds space between \"function ()\"");
30
+ print("-h\tPrint this help\n");
31
+ print("Examples:");
32
+ print("beautify-cl.js -i 2 example.js");
33
+ print("beautify-cl.js -i 1 http://www.example.org/example.js\n");
34
+ }
35
+
36
+
37
+ function parse_opts(args) {
38
+ var options = [];
39
+ while (args.length > 0) {
40
+ param = args.shift();
41
+ if (param.substr(0, 1) == '-') {
42
+ switch (param) {
43
+ case "-i":
44
+ options.indent = args.shift();
45
+ break;
46
+ case "-p":
47
+ options.jslint_pedantic = true;
48
+ break;
49
+ case "-n":
50
+ options.preserve_newlines = true;
51
+ break;
52
+ case "-h":
53
+ print_usage();
54
+ quit();
55
+ break;
56
+ default:
57
+ print("Unknown parameter: " + param + "\n");
58
+ print("Aborting.");
59
+ quit();
60
+ }
61
+ } else {
62
+ options.source = param;
63
+ }
64
+ }
65
+ return options;
66
+ }
67
+
68
+
69
+ function do_js_beautify() {
70
+ var js_source = '';
71
+ if (options.source) { // Check if source argument is an URL
72
+ if (options.source.substring(0, 4) === 'http') {
73
+ js_source = readUrl(options.source);
74
+ } else { // Otherwise, read from file
75
+ js_source = readFile(options.source);
76
+ }
77
+ } else { // read from stdin
78
+ importPackage(java.io);
79
+ importPackage(java.lang);
80
+ var stdin = new BufferedReader(new InputStreamReader(System['in']));
81
+ var lines = [];
82
+
83
+ // read stdin buffer until EOF
84
+ while (stdin.ready()) {
85
+ lines.push(stdin.readLine());
86
+ }
87
+ if (lines.length) js_source = lines.join("\n");
88
+ }
89
+ js_source = js_source.replace(/^\s+/, '');
90
+ var indent_size = options.indent || 2;
91
+ var preserve_newlines = options.preserve_newlines || false;
92
+ var indent_char = ' ';
93
+ var result;
94
+ if (indent_size == 1) {
95
+ indent_char = '\t';
96
+ }
97
+ if (js_source && js_source[0] === '<') {
98
+ result = style_html(js_source, indent_size, indent_char, 80);
99
+ } else {
100
+ result = js_beautify(js_source, {
101
+ indent_size: indent_size,
102
+ indent_char: indent_char,
103
+ preserve_newlines: preserve_newlines,
104
+ space_after_anon_function: options.jslint_pedantic
105
+ });
106
+ }
107
+ return result;
108
+ }
109
+
110
+
111
+ options = parse_opts(arguments);
112
+ print(do_js_beautify());
@@ -0,0 +1,273 @@
1
+ /*global js_beautify */
2
+
3
+
4
+ var indent_size = 4;
5
+ var indent_char = ' ';
6
+ var preserve_newlines = true;
7
+ var space_after_anon_function = true;
8
+
9
+ function test_beautifier(input)
10
+ {
11
+ return js_beautify(input, {
12
+ indent_size: indent_size,
13
+ indent_char: indent_char,
14
+ preserve_newlines: preserve_newlines,
15
+ space_after_anon_function: space_after_anon_function
16
+ });
17
+
18
+ }
19
+
20
+ var sanitytest;
21
+
22
+
23
+ function bt(input, expected)
24
+ {
25
+ var wrapped_input, wrapped_expectation;
26
+
27
+ expected = expected || input;
28
+ sanitytest.expect(input, expected);
29
+
30
+ // test also the returned indentation
31
+ // e.g if input = "asdf();"
32
+ // then test that this remains properly formatted as well:
33
+ // {
34
+ // asdf();
35
+ // indent;
36
+ // }
37
+
38
+ if (indent_size === 4 && input) {
39
+ wrapped_input = '{\n' + input + '\nindent;}';
40
+ //wrapped_expectation = '{\n ' + expected.replace(/^\s{4}/gm, 'g ') + '\n indent;\n}';
41
+ wrapped_expectation = '{\n' + expected.replace(/^(.+)$/mg, ' $1') + '\n indent;\n}';
42
+ sanitytest.expect(wrapped_input, wrapped_expectation);
43
+ }
44
+
45
+ }
46
+
47
+
48
+ function run_beautifier_tests(test_obj)
49
+ {
50
+ sanitytest = test_obj || new SanityTest();
51
+ sanitytest.test_function(test_beautifier, 'js_beautify');
52
+
53
+ indent_size = 4;
54
+ tests_passed = 0;
55
+ tests_failed = 0;
56
+ indent_char = ' ';
57
+ test_result = '';
58
+ preserve_newlines = true;
59
+ space_after_anon_function = true;
60
+
61
+ bt('');
62
+ bt('a = 1', 'a = 1');
63
+ bt('a=1', 'a = 1');
64
+ bt("a();\n\nb();", "a();\n\nb();");
65
+ bt('var a = 1 var b = 2', "var a = 1\nvar b = 2");
66
+ bt('var a=1, b=c[d], e=6;', 'var a = 1,\n b = c[d],\n e = 6;');
67
+ bt('a = " 12345 "');
68
+ bt("a = ' 12345 '");
69
+ bt('if (a == 1) b = 2;', "if (a == 1) b = 2;");
70
+ bt('if(1){2}else{3}', "if (1) {\n 2\n} else {\n 3\n}");
71
+ bt('if(1||2);', 'if (1 || 2);');
72
+ bt('(a==1)||(b==2)', '(a == 1) || (b == 2)');
73
+ bt('var a = 1 if (2) 3;', "var a = 1\nif (2) 3;");
74
+ bt('a = a + 1');
75
+ bt('a = a == 1');
76
+ bt('/12345[^678]*9+/.match(a)');
77
+ bt('a /= 5');
78
+ bt('a = 0.5 * 3');
79
+ bt('a *= 10.55');
80
+ bt('a < .5');
81
+ bt('a <= .5');
82
+ bt('a<.5', 'a < .5');
83
+ bt('a<=.5', 'a <= .5');
84
+ bt('a = 0xff;');
85
+ bt('a=0xff+4', 'a = 0xff + 4');
86
+ bt('a = [1, 2, 3, 4]');
87
+ bt('F*(g/=f)*g+b', 'F * (g /= f) * g + b');
88
+ bt('a.b({c:d})', "a.b({\n c: d\n})");
89
+ bt('a.b\n(\n{\nc:\nd\n}\n)', "a.b({\n c: d\n})");
90
+ bt('a=!b', 'a = !b');
91
+ bt('a?b:c', 'a ? b : c');
92
+ bt('a?1:2', 'a ? 1 : 2');
93
+ bt('a?(b):c', 'a ? (b) : c');
94
+ bt('x={a:1,b:w=="foo"?x:y,c:z}', 'x = {\n a: 1,\n b: w == "foo" ? x : y,\n c: z\n}');
95
+ bt('x=a?b?c?d:e:f:g;', 'x = a ? b ? c ? d : e : f : g;');
96
+ bt('x=a?b?c?d:{e1:1,e2:2}:f:g;', 'x = a ? b ? c ? d : {\n e1: 1,\n e2: 2\n} : f : g;');
97
+ bt('function void(void) {}');
98
+ bt('if(!a)foo();', 'if (!a) foo();');
99
+ bt('a=~a', 'a = ~a');
100
+ bt('a;/*comment*/b;', "a;\n/*comment*/\nb;");
101
+ bt('if(a)break;', "if (a) break;");
102
+ bt('if(a){break}', "if (a) {\n break\n}");
103
+ bt('if((a))foo();', 'if ((a)) foo();');
104
+ bt('for(var i=0;;)', 'for (var i = 0;;)');
105
+ bt('a++;', 'a++;');
106
+ bt('for(;;i++)', 'for (;; i++)');
107
+ bt('for(;;++i)', 'for (;; ++i)');
108
+ bt('return(1)', 'return (1)');
109
+ bt('try{a();}catch(b){c();}finally{d();}', "try {\n a();\n} catch(b) {\n c();\n} finally {\n d();\n}");
110
+ bt('(xx)()'); // magic function call
111
+ bt('a[1]()'); // another magic function call
112
+ bt('if(a){b();}else if(c) foo();', "if (a) {\n b();\n} else if (c) foo();");
113
+ bt('switch(x) {case 0: case 1: a(); break; default: break}', "switch (x) {\ncase 0:\ncase 1:\n a();\n break;\ndefault:\n break\n}");
114
+ bt('switch(x){case -1:break;case !y:break;}', 'switch (x) {\ncase -1:\n break;\ncase !y:\n break;\n}');
115
+ bt('a !== b');
116
+ bt('if (a) b(); else c();', "if (a) b();\nelse c();");
117
+ bt("// comment\n(function something() {})"); // typical greasemonkey start
118
+ bt("{\n\n x();\n\n}"); // was: duplicating newlines
119
+ bt('if (a in b) foo();');
120
+ //bt('var a, b');
121
+ bt('{a:1, b:2}', "{\n a: 1,\n b: 2\n}");
122
+ bt('a={1:[-1],2:[+1]}', 'a = {\n 1: [-1],\n 2: [+1]\n}');
123
+ bt('var l = {\'a\':\'1\', \'b\':\'2\'}', "var l = {\n 'a': '1',\n 'b': '2'\n}");
124
+ bt('if (template.user[n] in bk) foo();');
125
+ bt('{{}/z/}', "{\n {}\n /z/\n}");
126
+ bt('return 45', "return 45");
127
+ bt('If[1]', "If[1]");
128
+ bt('Then[1]', "Then[1]");
129
+ bt('a = 1e10', "a = 1e10");
130
+ bt('a = 1.3e10', "a = 1.3e10");
131
+ bt('a = 1.3e-10', "a = 1.3e-10");
132
+ bt('a = -1.3e-10', "a = -1.3e-10");
133
+ bt('a = 1e-10', "a = 1e-10");
134
+ bt('a = e - 10', "a = e - 10");
135
+ bt('a = 11-10', "a = 11 - 10");
136
+ bt("a = 1;// comment\n", "a = 1; // comment");
137
+ bt("a = 1; // comment\n", "a = 1; // comment");
138
+ bt("a = 1;\n // comment\n", "a = 1;\n// comment");
139
+
140
+ bt("if (a) {\n do();\n}"); // was: extra space appended
141
+ bt("if\n(a)\nb();", "if (a) b();"); // test for proper newline removal
142
+
143
+ bt("if (a) {\n// comment\n}else{\n// comment\n}", "if (a) {\n // comment\n} else {\n // comment\n}"); // if/else statement with empty body
144
+ bt("if (a) {\n// comment\n// comment\n}", "if (a) {\n // comment\n // comment\n}"); // multiple comments indentation
145
+ bt("if (a) b() else c();", "if (a) b()\nelse c();");
146
+ bt("if (a) b() else if c() d();", "if (a) b()\nelse if c() d();");
147
+
148
+ bt("{}");
149
+ bt("{\n\n}");
150
+ bt("do { a(); } while ( 1 );", "do {\n a();\n} while (1);");
151
+ bt("do {} while (1);");
152
+ bt("do {\n} while (1);", "do {} while (1);");
153
+ bt("do {\n\n} while (1);");
154
+ bt("var a = x(a, b, c)");
155
+ bt("delete x if (a) b();", "delete x\nif (a) b();");
156
+ bt("delete x[x] if (a) b();", "delete x[x]\nif (a) b();");
157
+ bt("for(var a=1,b=2)", "for (var a = 1, b = 2)");
158
+ bt("for(var a=1,b=2,c=3)", "for (var a = 1, b = 2, c = 3)");
159
+ bt("for(var a=1,b=2,c=3;d<3;d++)", "for (var a = 1, b = 2, c = 3; d < 3; d++)");
160
+ bt("function x(){(a||b).c()}", "function x() {\n (a || b).c()\n}");
161
+ bt("function x(){return - 1}", "function x() {\n return -1\n}");
162
+ bt("function x(){return ! a}", "function x() {\n return !a\n}");
163
+
164
+ bt("a = 'a'\nb = 'b'");
165
+ bt("a = /reg/exp");
166
+ bt("a = /reg/");
167
+ bt('/abc/.test()');
168
+ bt('/abc/i.test()');
169
+ bt("{/abc/i.test()}", "{\n /abc/i.test()\n}");
170
+
171
+ bt('{x=#1=[]}', '{\n x = #1=[]\n}');
172
+ bt('{a:#1={}}', '{\n a: #1={}\n}');
173
+ bt('{a:#1#}', '{\n a: #1#\n}');
174
+ test_beautifier('{a:#1', '{\n a: #1'); // incomplete
175
+ test_beautifier('{a:#', '{\n a: #'); // incomplete
176
+
177
+ test_beautifier('<!--\nvoid();\n// -->', '<!--\nvoid();\n// -->');
178
+
179
+ test_beautifier('a=/regexp', 'a = /regexp'); // incomplete regexp
180
+
181
+ bt('{a:#1=[],b:#1#,c:#999999#}', '{\n a: #1=[],\n b: #1#,\n c: #999999#\n}');
182
+
183
+ bt("a = 1e+2");
184
+ bt("a = 1e-2");
185
+ bt("do{x()}while(a>1)", "do {\n x()\n} while (a > 1)");
186
+
187
+ bt("x(); /reg/exp.match(something)", "x();\n/reg/exp.match(something)");
188
+
189
+ bt("something();(", "something();\n(");
190
+
191
+ bt("function namespace::something()");
192
+
193
+ test_beautifier("<!--\nsomething();\n-->", "<!--\nsomething();\n-->");
194
+ test_beautifier("<!--\nif(i<0){bla();}\n-->", "<!--\nif (i < 0) {\n bla();\n}\n-->");
195
+
196
+ test_beautifier("<!--\nsomething();\n-->\n<!--\nsomething();\n-->", "<!--\nsomething();\n-->\n<!--\nsomething();\n-->");
197
+ test_beautifier("<!--\nif(i<0){bla();}\n-->\n<!--\nif(i<0){bla();}\n-->", "<!--\nif (i < 0) {\n bla();\n}\n-->\n<!--\nif (i < 0) {\n bla();\n}\n-->");
198
+
199
+ bt('{foo();--bar;}', '{\n foo();\n --bar;\n}');
200
+ bt('{foo();++bar;}', '{\n foo();\n ++bar;\n}');
201
+ bt('{--bar;}', '{\n --bar;\n}');
202
+ bt('{++bar;}', '{\n ++bar;\n}');
203
+
204
+ // regexps
205
+ bt('a(/abc\\/\\/def/);b()', "a(/abc\\/\\/def/);\nb()");
206
+ bt('a(/a[b\\[\\]c]d/);b()', "a(/a[b\\[\\]c]d/);\nb()");
207
+ test_beautifier('a(/a[b\\[', "a(/a[b\\["); // incomplete char class
208
+ // allow unescaped / in char classes
209
+ bt('a(/[a/b]/);b()', "a(/[a/b]/);\nb()");
210
+
211
+ bt('a=[[1,2],[4,5],[7,8]]', "a = [\n [1, 2],\n [4, 5],\n [7, 8]]");
212
+ bt('a=[a[1],b[4],c[d[7]]]', "a = [a[1], b[4], c[d[7]]]");
213
+ bt('[1,2,[3,4,[5,6],7],8]', "[1, 2, [3, 4, [5, 6], 7], 8]");
214
+
215
+ bt('[[["1","2"],["3","4"]],[["5","6","7"],["8","9","0"]],[["1","2","3"],["4","5","6","7"],["8","9","0"]]]',
216
+ '[\n [\n ["1", "2"],\n ["3", "4"]],\n [\n ["5", "6", "7"],\n ["8", "9", "0"]],\n [\n ["1", "2", "3"],\n ["4", "5", "6", "7"],\n ["8", "9", "0"]]]');
217
+
218
+ bt('{[x()[0]];indent;}', '{\n [x()[0]];\n indent;\n}');
219
+
220
+ bt('return ++i', 'return ++i');
221
+ bt('return !!x', 'return !!x');
222
+ bt('return !x', 'return !x');
223
+ bt('return [1,2]', 'return [1, 2]');
224
+ bt('return;', 'return;');
225
+ bt('return\nfunc', 'return\nfunc');
226
+
227
+ // javadoc comment
228
+ bt('/**\n* foo\n*/', '/**\n * foo\n */');
229
+ bt(' /**\n * foo\n */', '/**\n * foo\n */');
230
+ bt('{\n/**\n* foo\n*/\n}', '{\n /**\n * foo\n */\n}');
231
+
232
+ bt('var a,b,c=1,d,e,f=2', 'var a, b, c = 1,\n d, e, f = 2');
233
+ bt('var a,b,c=[],d,e,f=2', 'var a, b, c = [],\n d, e, f = 2');
234
+ bt('function () {\n var a, b, c, d, e = [],\n f;\n}');
235
+
236
+ space_after_anon_function = true;
237
+
238
+ bt("// comment 1\n(function()", "// comment 1\n(function ()"); // typical greasemonkey start
239
+ bt("var a1, b1, c1, d1 = 0, c = function() {}, d = '';", "var a1, b1, c1, d1 = 0,\n c = function () {},\n d = '';");
240
+ bt('var o1=$.extend(a,function(){alert(x);}', 'var o1 = $.extend(a, function () {\n alert(x);\n}');
241
+ bt('var o1=$.extend(a);function(){alert(x);}', 'var o1 = $.extend(a);\n\nfunction () {\n alert(x);\n}');
242
+
243
+ space_after_anon_function = false;
244
+
245
+ bt("// comment 2\n(function()", "// comment 2\n(function()"); // typical greasemonkey start
246
+ bt("var a2, b2, c2, d2 = 0, c = function() {}, d = '';", "var a2, b2, c2, d2 = 0,\n c = function() {},\n d = '';");
247
+ bt('var o2=$.extend(a,function(){alert(x);}', 'var o2 = $.extend(a, function() {\n alert(x);\n}');
248
+ bt('var o2=$.extend(a);function(){alert(x);}', 'var o2 = $.extend(a);\n\nfunction() {\n alert(x);\n}');
249
+
250
+ bt('{[y[a]];keep_indent;}', '{\n [y[a]];\n keep_indent;\n}');
251
+
252
+ bt('if (x) {y} else { if (x) {y}}', 'if (x) {\n y\n} else {\n if (x) {\n y\n }\n}');
253
+
254
+ indent_size = 1;
255
+ indent_char = ' ';
256
+ bt('{ one_char() }', "{\n one_char()\n}");
257
+
258
+ indent_size = 4;
259
+ indent_char = ' ';
260
+ bt('{ one_char() }', "{\n one_char()\n}");
261
+
262
+ indent_size = 1;
263
+ indent_char = "\t";
264
+ bt('{ one_char() }', "{\n\tone_char()\n}");
265
+
266
+ preserve_newlines = false;
267
+ bt('var\na=dont_preserve_newlines', 'var a = dont_preserve_newlines');
268
+
269
+ preserve_newlines = true;
270
+ bt('var\na=do_preserve_newlines', 'var\na = do_preserve_newlines');
271
+
272
+ return sanitytest;
273
+ }
@@ -0,0 +1,891 @@
1
+ /*jslint onevar: false, plusplus: false */
2
+ /*
3
+
4
+ JS Beautifier
5
+ ---------------
6
+
7
+
8
+ Written by Einar Lielmanis, <einars@gmail.com>
9
+ http://jsbeautifier.org/
10
+
11
+ Originally converted to javascript by Vital, <vital76@gmail.com>
12
+
13
+ You are free to use this in any way you want, in case you find this useful or working for you.
14
+
15
+ Usage:
16
+ js_beautify(js_source_text);
17
+ js_beautify(js_source_text, options);
18
+
19
+ The options are:
20
+ indent_size (default 4) — indentation size,
21
+ indent_char (default space) — character to indent with,
22
+ preserve_newlines (default true) — whether existing line breaks should be preserved,
23
+ indent_level (default 0) — initial indentation level, you probably won't need this ever,
24
+
25
+ space_after_anon_function (default false) — if true, then space is added between "function ()"
26
+ (jslint is happy about this); if false, then the common "function()" output is used.
27
+
28
+ e.g
29
+
30
+ js_beautify(js_source_text, {indent_size: 1, indent_char: '\t'});
31
+
32
+
33
+ */
34
+
35
+
36
+
37
+ function js_beautify(js_source_text, options)
38
+ {
39
+
40
+ var input, output, token_text, last_type, last_text, last_last_text, last_word, flags, flag_store, indent_string;
41
+ var whitespace, wordchar, punct, parser_pos, line_starters, digits;
42
+ var prefix, token_type, do_block_just_closed;
43
+ var indent_level, wanted_newline, just_added_newline;
44
+
45
+
46
+ options = options || {};
47
+ var opt_indent_size = options.indent_size || 4;
48
+ var opt_indent_char = options.indent_char || ' ';
49
+ var opt_preserve_newlines =
50
+ typeof options.preserve_newlines === 'undefined' ? true : options.preserve_newlines;
51
+ var opt_indent_level = options.indent_level || 0; // starting indentation
52
+ var opt_space_after_anon_function = options.space_after_anon_function === 'undefined' ? false : options.space_after_anon_function;
53
+
54
+ just_added_newline = false;
55
+
56
+
57
+ function trim_output()
58
+ {
59
+ while (output.length && (output[output.length - 1] === ' ' || output[output.length - 1] === indent_string)) {
60
+ output.pop();
61
+ }
62
+ }
63
+
64
+ function print_newline(ignore_repeated)
65
+ {
66
+
67
+ ignore_repeated = typeof ignore_repeated === 'undefined' ? true: ignore_repeated;
68
+
69
+ flags.if_line = false;
70
+ trim_output();
71
+
72
+ if (!output.length) {
73
+ return; // no newline on start of file
74
+ }
75
+
76
+ if (output[output.length - 1] !== "\n" || !ignore_repeated) {
77
+ just_added_newline = true;
78
+ output.push("\n");
79
+ }
80
+ for (var i = 0; i < indent_level; i += 1) {
81
+ output.push(indent_string);
82
+ }
83
+ }
84
+
85
+
86
+
87
+ function print_space()
88
+ {
89
+ var last_output = ' ';
90
+ if (output.length) {
91
+ last_output = output[output.length - 1];
92
+ }
93
+ if (last_output !== ' ' && last_output !== '\n' && last_output !== indent_string) { // prevent occassional duplicate space
94
+ output.push(' ');
95
+ }
96
+ }
97
+
98
+
99
+ function print_token()
100
+ {
101
+ just_added_newline = false;
102
+ output.push(token_text);
103
+ }
104
+
105
+ function indent()
106
+ {
107
+ indent_level += 1;
108
+ }
109
+
110
+
111
+ function unindent()
112
+ {
113
+ if (indent_level) {
114
+ indent_level -= 1;
115
+ }
116
+ }
117
+
118
+
119
+ function remove_indent()
120
+ {
121
+ if (output.length && output[output.length - 1] === indent_string) {
122
+ output.pop();
123
+ }
124
+ }
125
+
126
+ function print_javadoc_comment()
127
+ {
128
+ var lines = token_text.split('\n');
129
+ output.push(lines[0]);
130
+ for (var i = 1 ; i < lines.length; i++) {
131
+ print_newline();
132
+ output.push(' ');
133
+ output.push(lines[i].replace(/^\s+/, ''));
134
+ }
135
+ }
136
+
137
+
138
+ function set_mode(mode)
139
+ {
140
+ if (flags) {
141
+ flag_store.push(flags);
142
+ }
143
+ flags = {
144
+ mode: mode,
145
+ var_line: false,
146
+ var_line_tainted: false,
147
+ if_line: false,
148
+ in_case: false
149
+ };
150
+ }
151
+
152
+ function is_expression(mode)
153
+ {
154
+ return mode === '[EXPRESSION]' || mode === '[INDENTED-EXPRESSION]' || mode === '(EXPRESSION)';
155
+ }
156
+
157
+
158
+ function restore_mode()
159
+ {
160
+ do_block_just_closed = flags.mode === 'DO_BLOCK';
161
+ flags = flag_store.pop();
162
+ }
163
+
164
+
165
+ function in_array(what, arr)
166
+ {
167
+ for (var i = 0; i < arr.length; i += 1)
168
+ {
169
+ if (arr[i] === what) {
170
+ return true;
171
+ }
172
+ }
173
+ return false;
174
+ }
175
+
176
+ // Walk backwards from the colon to find a '?' (colon is part of a ternary op)
177
+ // or a '{' (colon is part of a class literal). Along the way, keep track of
178
+ // the blocks and expressions we pass so we only trigger on those chars in our
179
+ // own level, and keep track of the colons so we only trigger on the matching '?'.
180
+ function is_ternary_op() {
181
+ var level = 0, colon_count = 0;
182
+ for (var i = output.length - 1; i >= 0; i--) {
183
+ switch (output[i]) {
184
+ case ':':
185
+ if (level === 0) {
186
+ colon_count++;
187
+ }
188
+ break;
189
+ case '?':
190
+ if (level === 0) {
191
+ if (colon_count === 0) {
192
+ return true;
193
+ } else {
194
+ colon_count--;
195
+ }
196
+ }
197
+ break;
198
+ case '{':
199
+ if (level === 0) {
200
+ return false;
201
+ }
202
+ level--;
203
+ break;
204
+ case '(':
205
+ case '[':
206
+ level--;
207
+ break;
208
+ case ')':
209
+ case ']':
210
+ case '}':
211
+ level++;
212
+ break;
213
+ }
214
+ }
215
+ }
216
+
217
+
218
+ function get_next_token()
219
+ {
220
+ var n_newlines = 0;
221
+
222
+ if (parser_pos >= input.length) {
223
+ return ['', 'TK_EOF'];
224
+ }
225
+
226
+ var c = input.charAt(parser_pos);
227
+ parser_pos += 1;
228
+
229
+ while (in_array(c, whitespace)) {
230
+ if (parser_pos >= input.length) {
231
+ return ['', 'TK_EOF'];
232
+ }
233
+
234
+ if (c === "\n") {
235
+ n_newlines += 1;
236
+ }
237
+
238
+ c = input.charAt(parser_pos);
239
+ parser_pos += 1;
240
+
241
+ }
242
+
243
+ wanted_newline = false;
244
+
245
+ if (opt_preserve_newlines) {
246
+ if (n_newlines > 1) {
247
+ for (var i = 0; i < 2; i += 1) {
248
+ print_newline(i === 0);
249
+ }
250
+ }
251
+ wanted_newline = (n_newlines === 1);
252
+ }
253
+
254
+
255
+ if (in_array(c, wordchar)) {
256
+ if (parser_pos < input.length) {
257
+ while (in_array(input.charAt(parser_pos), wordchar)) {
258
+ c += input.charAt(parser_pos);
259
+ parser_pos += 1;
260
+ if (parser_pos === input.length) {
261
+ break;
262
+ }
263
+ }
264
+ }
265
+
266
+ // small and surprisingly unugly hack for 1E-10 representation
267
+ if (parser_pos !== input.length && c.match(/^[0-9]+[Ee]$/) && (input.charAt(parser_pos) === '-' || input.charAt(parser_pos) === '+')) {
268
+
269
+ var sign = input.charAt(parser_pos);
270
+ parser_pos += 1;
271
+
272
+ var t = get_next_token(parser_pos);
273
+ c += sign + t[0];
274
+ return [c, 'TK_WORD'];
275
+ }
276
+
277
+ if (c === 'in') { // hack for 'in' operator
278
+ return [c, 'TK_OPERATOR'];
279
+ }
280
+ if (wanted_newline && last_type !== 'TK_OPERATOR' && !flags.if_line) {
281
+ print_newline();
282
+ }
283
+ return [c, 'TK_WORD'];
284
+ }
285
+
286
+ if (c === '(' || c === '[') {
287
+ return [c, 'TK_START_EXPR'];
288
+ }
289
+
290
+ if (c === ')' || c === ']') {
291
+ return [c, 'TK_END_EXPR'];
292
+ }
293
+
294
+ if (c === '{') {
295
+ return [c, 'TK_START_BLOCK'];
296
+ }
297
+
298
+ if (c === '}') {
299
+ return [c, 'TK_END_BLOCK'];
300
+ }
301
+
302
+ if (c === ';') {
303
+ return [c, 'TK_SEMICOLON'];
304
+ }
305
+
306
+ if (c === '/') {
307
+ var comment = '';
308
+ // peek for comment /* ... */
309
+ if (input.charAt(parser_pos) === '*') {
310
+ parser_pos += 1;
311
+ if (parser_pos < input.length) {
312
+ while (! (input.charAt(parser_pos) === '*' && input.charAt(parser_pos + 1) && input.charAt(parser_pos + 1) === '/') && parser_pos < input.length) {
313
+ comment += input.charAt(parser_pos);
314
+ parser_pos += 1;
315
+ if (parser_pos >= input.length) {
316
+ break;
317
+ }
318
+ }
319
+ }
320
+ parser_pos += 2;
321
+ return ['/*' + comment + '*/', 'TK_BLOCK_COMMENT'];
322
+ }
323
+ // peek for comment // ...
324
+ if (input.charAt(parser_pos) === '/') {
325
+ comment = c;
326
+ while (input.charAt(parser_pos) !== "\x0d" && input.charAt(parser_pos) !== "\x0a") {
327
+ comment += input.charAt(parser_pos);
328
+ parser_pos += 1;
329
+ if (parser_pos >= input.length) {
330
+ break;
331
+ }
332
+ }
333
+ parser_pos += 1;
334
+ if (wanted_newline) {
335
+ print_newline();
336
+ }
337
+ return [comment, 'TK_COMMENT'];
338
+ }
339
+
340
+ }
341
+
342
+ if (c === "'" || // string
343
+ c === '"' || // string
344
+ (c === '/' &&
345
+ ((last_type === 'TK_WORD' && last_text === 'return') || (last_type === 'TK_START_EXPR' || last_type === 'TK_START_BLOCK' || last_type === 'TK_END_BLOCK' || last_type === 'TK_OPERATOR' || last_type === 'TK_EOF' || last_type === 'TK_SEMICOLON')))) { // regexp
346
+ var sep = c;
347
+ var esc = false;
348
+ var resulting_string = c;
349
+
350
+ if (parser_pos < input.length) {
351
+ if (sep === '/') {
352
+ //
353
+ // handle regexp separately...
354
+ //
355
+
356
+ var in_char_class = false;
357
+ while (esc || in_char_class || input.charAt(parser_pos) !== sep) {
358
+ resulting_string += input.charAt(parser_pos);
359
+ if (!esc) {
360
+ esc = input.charAt(parser_pos) === '\\';
361
+ if (input.charAt(parser_pos) === '[') {
362
+ in_char_class = true;
363
+ } else if (input.charAt(parser_pos) === ']') {
364
+ in_char_class = false;
365
+ }
366
+ } else {
367
+ esc = false;
368
+ }
369
+ parser_pos += 1;
370
+ if (parser_pos >= input.length) {
371
+ // incomplete string/rexp when end-of-file reached.
372
+ // bail out with what had been received so far.
373
+ return [resulting_string, 'TK_STRING'];
374
+ }
375
+ }
376
+
377
+ } else {
378
+ //
379
+ // and handle string also separately
380
+ //
381
+ while (esc || input.charAt(parser_pos) !== sep) {
382
+ resulting_string += input.charAt(parser_pos);
383
+ if (!esc) {
384
+ esc = input.charAt(parser_pos) === '\\';
385
+ } else {
386
+ esc = false;
387
+ }
388
+ parser_pos += 1;
389
+ if (parser_pos >= input.length) {
390
+ // incomplete string/rexp when end-of-file reached.
391
+ // bail out with what had been received so far.
392
+ return [resulting_string, 'TK_STRING'];
393
+ }
394
+ }
395
+ }
396
+
397
+
398
+
399
+ }
400
+
401
+ parser_pos += 1;
402
+
403
+ resulting_string += sep;
404
+
405
+ if (sep === '/') {
406
+ // regexps may have modifiers /regexp/MOD , so fetch those, too
407
+ while (parser_pos < input.length && in_array(input.charAt(parser_pos), wordchar)) {
408
+ resulting_string += input.charAt(parser_pos);
409
+ parser_pos += 1;
410
+ }
411
+ }
412
+ return [resulting_string, 'TK_STRING'];
413
+ }
414
+
415
+ if (c === '#') {
416
+ // Spidermonkey-specific sharp variables for circular references
417
+ // https://developer.mozilla.org/En/Sharp_variables_in_JavaScript
418
+ // http://mxr.mozilla.org/mozilla-central/source/js/src/jsscan.cpp around line 1935
419
+ var sharp = '#';
420
+ if (parser_pos < input.length && in_array(input.charAt(parser_pos), digits)) {
421
+ do {
422
+ c = input.charAt(parser_pos);
423
+ sharp += c;
424
+ parser_pos += 1;
425
+ } while (parser_pos < input.length && c !== '#' && c !== '=');
426
+ if (c === '#') {
427
+ return [sharp, 'TK_WORD'];
428
+ } else {
429
+ return [sharp, 'TK_OPERATOR'];
430
+ }
431
+ }
432
+ }
433
+
434
+ if (c === '<' && input.substring(parser_pos - 1, parser_pos + 3) === '<!--') {
435
+ parser_pos += 3;
436
+ return ['<!--', 'TK_COMMENT'];
437
+ }
438
+
439
+ if (c === '-' && input.substring(parser_pos - 1, parser_pos + 2) === '-->') {
440
+ parser_pos += 2;
441
+ if (wanted_newline) {
442
+ print_newline();
443
+ }
444
+ return ['-->', 'TK_COMMENT'];
445
+ }
446
+
447
+ if (in_array(c, punct)) {
448
+ while (parser_pos < input.length && in_array(c + input.charAt(parser_pos), punct)) {
449
+ c += input.charAt(parser_pos);
450
+ parser_pos += 1;
451
+ if (parser_pos >= input.length) {
452
+ break;
453
+ }
454
+ }
455
+
456
+ return [c, 'TK_OPERATOR'];
457
+ }
458
+
459
+ return [c, 'TK_UNKNOWN'];
460
+ }
461
+
462
+
463
+ //----------------------------------
464
+
465
+ indent_string = '';
466
+ while (opt_indent_size > 0) {
467
+ indent_string += opt_indent_char;
468
+ opt_indent_size -= 1;
469
+ }
470
+
471
+ indent_level = opt_indent_level;
472
+
473
+ input = js_source_text;
474
+
475
+ last_word = ''; // last 'TK_WORD' passed
476
+ last_type = 'TK_START_EXPR'; // last token type
477
+ last_text = ''; // last token text
478
+ last_last_text = ''; // pre-last token text
479
+ output = [];
480
+
481
+ do_block_just_closed = false;
482
+
483
+ whitespace = "\n\r\t ".split('');
484
+ wordchar = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_$'.split('');
485
+ digits = '0123456789'.split('');
486
+
487
+ // <!-- is a special case (ok, it's a minor hack actually)
488
+ punct = '+ - * / % & ++ -- = += -= *= /= %= == === != !== > < >= <= >> << >>> >>>= >>= <<= && &= | || ! !! , : ? ^ ^= |= ::'.split(' ');
489
+
490
+ // words which should always start on new line.
491
+ line_starters = 'continue,try,throw,return,var,if,switch,case,default,for,while,break,function'.split(',');
492
+
493
+ // states showing if we are currently in expression (i.e. "if" case) - 'EXPRESSION', or in usual block (like, procedure), 'BLOCK'.
494
+ // some formatting depends on that.
495
+ flag_store = [];
496
+ set_mode('BLOCK');
497
+
498
+
499
+ parser_pos = 0;
500
+ while (true) {
501
+ var t = get_next_token(parser_pos);
502
+ token_text = t[0];
503
+ token_type = t[1];
504
+ if (token_type === 'TK_EOF') {
505
+ break;
506
+ }
507
+
508
+ switch (token_type) {
509
+
510
+ case 'TK_START_EXPR':
511
+
512
+ if (token_text === '[') {
513
+
514
+ if (last_type === 'TK_WORD' || last_text === ')') {
515
+ // this is array index specifier, break immediately
516
+ // a[x], fn()[x]
517
+ if (last_word === 'return' || last_word === 'throw') {
518
+ print_space();
519
+ }
520
+ set_mode('(EXPRESSION)');
521
+ print_token();
522
+ break;
523
+ }
524
+
525
+ if (flags.mode === '[EXPRESSION]' || flags.mode === '[INDENTED-EXPRESSION]') {
526
+ if (last_last_text === ']' && last_text === ',') {
527
+ // ], [ goes to new line
528
+ indent();
529
+ print_newline();
530
+ set_mode('[INDENTED-EXPRESSION]');
531
+ } else if (last_text === '[') {
532
+ indent();
533
+ print_newline();
534
+ set_mode('[INDENTED-EXPRESSION]');
535
+ } else {
536
+ set_mode('[EXPRESSION]');
537
+ }
538
+ } else {
539
+ set_mode('[EXPRESSION]');
540
+ }
541
+
542
+
543
+
544
+ } else {
545
+ set_mode('(EXPRESSION)');
546
+ }
547
+
548
+ if (last_text === ';' || last_type === 'TK_START_BLOCK') {
549
+ print_newline();
550
+ } else if (last_type === 'TK_END_EXPR' || last_type === 'TK_START_EXPR') {
551
+ // do nothing on (( and )( and ][ and ]( ..
552
+ } else if (last_type !== 'TK_WORD' && last_type !== 'TK_OPERATOR') {
553
+ print_space();
554
+ } else if (last_word === 'function') {
555
+ // function() vs function ()
556
+ if (opt_space_after_anon_function) {
557
+ print_space();
558
+ }
559
+ } else if (in_array(last_word, line_starters)) {
560
+ print_space();
561
+ }
562
+ print_token();
563
+
564
+ break;
565
+
566
+ case 'TK_END_EXPR':
567
+ if (token_text === ']' && flags.mode === '[INDENTED-EXPRESSION]') {
568
+ unindent();
569
+ }
570
+ restore_mode();
571
+ print_token();
572
+ break;
573
+
574
+ case 'TK_START_BLOCK':
575
+
576
+ if (last_word === 'do') {
577
+ set_mode('DO_BLOCK');
578
+ } else {
579
+ set_mode('BLOCK');
580
+ }
581
+ if (last_type !== 'TK_OPERATOR' && last_type !== 'TK_START_EXPR') {
582
+ if (last_type === 'TK_START_BLOCK') {
583
+ print_newline();
584
+ } else {
585
+ print_space();
586
+ }
587
+ }
588
+ print_token();
589
+ indent();
590
+ break;
591
+
592
+ case 'TK_END_BLOCK':
593
+ if (last_type === 'TK_START_BLOCK') {
594
+ // nothing
595
+ if (just_added_newline) {
596
+ remove_indent();
597
+ // {
598
+ //
599
+ // }
600
+ } else {
601
+ // {}
602
+ trim_output();
603
+ }
604
+ unindent();
605
+ } else {
606
+ unindent();
607
+ print_newline();
608
+ }
609
+ print_token();
610
+ restore_mode();
611
+ break;
612
+
613
+ case 'TK_WORD':
614
+
615
+ // no, it's not you. even I have problems understanding how this works
616
+ // and what does what.
617
+
618
+ if (do_block_just_closed) {
619
+ // do {} ## while ()
620
+ print_space();
621
+ print_token();
622
+ print_space();
623
+ do_block_just_closed = false;
624
+ break;
625
+ }
626
+
627
+ if (token_text === 'function') {
628
+ if (last_text === ';' || last_text === '}') {
629
+ print_newline(false);
630
+ print_newline(false);
631
+ }
632
+ }
633
+ if (token_text === 'case' || token_text === 'default') {
634
+ if (last_text === ':') {
635
+ // switch cases following one another
636
+ remove_indent();
637
+ } else {
638
+ // case statement starts in the same line where switch
639
+ unindent();
640
+ print_newline();
641
+ indent();
642
+ }
643
+ print_token();
644
+ flags.in_case = true;
645
+ break;
646
+ }
647
+
648
+ prefix = 'NONE';
649
+
650
+ if (last_type === 'TK_END_BLOCK') {
651
+ if (!in_array(token_text.toLowerCase(), ['else', 'catch', 'finally'])) {
652
+ prefix = 'NEWLINE';
653
+ } else {
654
+ prefix = 'SPACE';
655
+ print_space();
656
+ }
657
+ } else if (last_type === 'TK_SEMICOLON' && (flags.mode === 'BLOCK' || flags.mode === 'DO_BLOCK')) {
658
+ prefix = 'NEWLINE';
659
+ } else if (last_type === 'TK_SEMICOLON' && is_expression(flags.mode)) {
660
+ prefix = 'SPACE';
661
+ } else if (last_type === 'TK_STRING') {
662
+ prefix = 'NEWLINE';
663
+ } else if (last_type === 'TK_WORD') {
664
+ prefix = 'SPACE';
665
+ } else if (last_type === 'TK_START_BLOCK') {
666
+ prefix = 'NEWLINE';
667
+ } else if (last_type === 'TK_END_EXPR') {
668
+ print_space();
669
+ prefix = 'NEWLINE';
670
+ }
671
+
672
+ if (last_type !== 'TK_END_BLOCK' && in_array(token_text.toLowerCase(), ['else', 'catch', 'finally'])) {
673
+ print_newline();
674
+ } else if (in_array(token_text, line_starters) || prefix === 'NEWLINE') {
675
+ if (last_text === 'else') {
676
+ // no need to force newline on else break
677
+ print_space();
678
+ } else if ((last_type === 'TK_START_EXPR' || last_text === '=' || last_text === ',') && token_text === 'function') {
679
+ // no need to force newline on 'function': (function
680
+ // DONOTHING
681
+ } else if (last_text === 'return' || last_text === 'throw') {
682
+ // no newline between 'return nnn'
683
+ print_space();
684
+ } else if (last_type !== 'TK_END_EXPR') {
685
+ if ((last_type !== 'TK_START_EXPR' || token_text !== 'var') && last_text !== ':') {
686
+ // no need to force newline on 'var': for (var x = 0...)
687
+ if (token_text === 'if' && last_word === 'else' && last_text !== '{') {
688
+ // no newline for } else if {
689
+ print_space();
690
+ } else {
691
+ print_newline();
692
+ }
693
+ }
694
+ } else {
695
+ if (in_array(token_text, line_starters) && last_text !== ')') {
696
+ print_newline();
697
+ }
698
+ }
699
+ } else if (prefix === 'SPACE') {
700
+ print_space();
701
+ }
702
+ print_token();
703
+ last_word = token_text;
704
+
705
+ if (token_text === 'var') {
706
+ flags.var_line = true;
707
+ flags.var_line_tainted = false;
708
+ }
709
+
710
+ if (token_text === 'if' || token_text === 'else') {
711
+ flags.if_line = true;
712
+ }
713
+
714
+ break;
715
+
716
+ case 'TK_SEMICOLON':
717
+
718
+ print_token();
719
+ flags.var_line = false;
720
+ break;
721
+
722
+ case 'TK_STRING':
723
+
724
+ if (last_type === 'TK_START_BLOCK' || last_type === 'TK_END_BLOCK' || last_type === 'TK_SEMICOLON') {
725
+ print_newline();
726
+ } else if (last_type === 'TK_WORD') {
727
+ print_space();
728
+ }
729
+ print_token();
730
+ break;
731
+
732
+ case 'TK_OPERATOR':
733
+
734
+ var start_delim = true;
735
+ var end_delim = true;
736
+ if (flags.var_line && token_text === ',' && (is_expression(flags.mode))) {
737
+ // do not break on comma, for(var a = 1, b = 2)
738
+ flags.var_line_tainted = false;
739
+ }
740
+
741
+ if (flags.var_line) {
742
+ if (token_text === ',') {
743
+ if (flags.var_line_tainted) {
744
+ print_token();
745
+ print_newline();
746
+ output.push(indent_string);
747
+ flags.var_line_tainted = false;
748
+ break;
749
+ } else {
750
+ flags.var_line_tainted = false;
751
+ }
752
+ } else {
753
+ flags.var_line_tainted = true;
754
+ if (token_text === ':') {
755
+ flags.var_line = false;
756
+ }
757
+ }
758
+ }
759
+
760
+ if (last_text === 'return' || last_text === 'throw') {
761
+ // "return" had a special handling in TK_WORD. Now we need to return the favor
762
+ print_space();
763
+ print_token();
764
+ break;
765
+ }
766
+
767
+ if (token_text === ':' && flags.in_case) {
768
+ print_token(); // colon really asks for separate treatment
769
+ print_newline();
770
+ flags.in_case = false;
771
+ break;
772
+ }
773
+
774
+ if (token_text === '::') {
775
+ // no spaces around exotic namespacing syntax operator
776
+ print_token();
777
+ break;
778
+ }
779
+
780
+ if (token_text === ',') {
781
+ if (flags.var_line) {
782
+ if (flags.var_line_tainted) {
783
+ print_token();
784
+ print_newline();
785
+ flags.var_line_tainted = false;
786
+ } else {
787
+ print_token();
788
+ print_space();
789
+ }
790
+ } else if (last_type === 'TK_END_BLOCK') {
791
+ print_token();
792
+ print_newline();
793
+ } else {
794
+ if (flags.mode === 'BLOCK') {
795
+ print_token();
796
+ print_newline();
797
+ } else {
798
+ // EXPR or DO_BLOCK
799
+ print_token();
800
+ print_space();
801
+ }
802
+ }
803
+ break;
804
+ } else if (token_text === '--' || token_text === '++') { // unary operators special case
805
+ if (last_text === ';') {
806
+ if (flags.mode === 'BLOCK') {
807
+ // { foo; --i }
808
+ print_newline();
809
+ start_delim = true;
810
+ end_delim = false;
811
+ } else {
812
+ // space for (;; ++i)
813
+ start_delim = true;
814
+ end_delim = false;
815
+ }
816
+ } else {
817
+ if (last_text === '{') {
818
+ // {--i
819
+ print_newline();
820
+ }
821
+ start_delim = false;
822
+ end_delim = false;
823
+ }
824
+ } else if ((token_text === '!' || token_text === '+' || token_text === '-') && (last_text === 'return' || last_text === 'case')) {
825
+ start_delim = true;
826
+ end_delim = false;
827
+ } else if ((token_text === '!' || token_text === '+' || token_text === '-') && last_type === 'TK_START_EXPR') {
828
+ // special case handling: if (!a)
829
+ start_delim = false;
830
+ end_delim = false;
831
+ } else if (last_type === 'TK_OPERATOR') {
832
+ start_delim = false;
833
+ end_delim = false;
834
+ } else if (last_type === 'TK_END_EXPR') {
835
+ start_delim = true;
836
+ end_delim = true;
837
+ } else if (token_text === '.') {
838
+ // decimal digits or object.property
839
+ start_delim = false;
840
+ end_delim = false;
841
+
842
+ } else if (token_text === ':') {
843
+ if (is_ternary_op()) {
844
+ start_delim = true;
845
+ } else {
846
+ start_delim = false;
847
+ }
848
+ }
849
+ if (start_delim) {
850
+ print_space();
851
+ }
852
+
853
+ print_token();
854
+
855
+ if (end_delim) {
856
+ print_space();
857
+ }
858
+ break;
859
+
860
+ case 'TK_BLOCK_COMMENT':
861
+
862
+ print_newline();
863
+ if (token_text.substring(0, 3) == '/**') {
864
+ print_javadoc_comment();
865
+ } else {
866
+ print_token();
867
+ }
868
+ print_newline();
869
+ break;
870
+
871
+ case 'TK_COMMENT':
872
+
873
+ // print_newline();
874
+ print_space();
875
+ print_token();
876
+ print_newline();
877
+ break;
878
+
879
+ case 'TK_UNKNOWN':
880
+ print_token();
881
+ break;
882
+ }
883
+
884
+ last_last_text = last_text;
885
+ last_type = token_type;
886
+ last_text = token_text;
887
+ }
888
+
889
+ return output.join('').replace(/\n+$/, '');
890
+
891
+ }