mirror of
https://github.com/beautifier/js-beautify.git
synced 2024-11-27 14:40:23 +00:00
Extract TK_COMMA from TK_OPERATOR, fix #125
This commit is contained in:
parent
ec46241049
commit
29fffc280e
6
Makefile
6
Makefile
@ -34,8 +34,10 @@ edit:
|
||||
|
||||
gedit:
|
||||
gvim \
|
||||
beautify.js python/jsbeautifier/__init__.py \
|
||||
tests/beautify-tests.js python/jsbeautifier/tests/testjsbeautifier.py &
|
||||
beautify.js \
|
||||
tests/beautify-tests.js \
|
||||
python/jsbeautifier/__init__.py \
|
||||
python/jsbeautifier/tests/testjsbeautifier.py &
|
||||
|
||||
tests: testj testp
|
||||
|
||||
|
86
beautify.js
86
beautify.js
@ -649,7 +649,9 @@ function js_beautify(js_source_text, options) {
|
||||
}
|
||||
}
|
||||
|
||||
if (c === '=') {
|
||||
if (c === ',') {
|
||||
return [c, 'TK_COMMA'];
|
||||
} else if (c === '=') {
|
||||
return [c, 'TK_EQUALS'];
|
||||
} else {
|
||||
return [c, 'TK_OPERATOR'];
|
||||
@ -1083,18 +1085,12 @@ function js_beautify(js_source_text, options) {
|
||||
print_single_space();
|
||||
break;
|
||||
|
||||
case 'TK_OPERATOR':
|
||||
|
||||
var space_before = true;
|
||||
var space_after = true;
|
||||
|
||||
if (flags.var_line && token_text === ',' && (is_expression(flags.mode))) {
|
||||
case 'TK_COMMA':
|
||||
if (flags.var_line) {
|
||||
if (is_expression(flags.mode)) {
|
||||
// do not break on comma, for(var a = 1, b = 2)
|
||||
flags.var_line_tainted = false;
|
||||
}
|
||||
|
||||
if (flags.var_line) {
|
||||
if (token_text === ',') {
|
||||
if (flags.var_line_tainted) {
|
||||
print_token();
|
||||
flags.var_line_reindented = true;
|
||||
@ -1104,11 +1100,40 @@ function js_beautify(js_source_text, options) {
|
||||
} else {
|
||||
flags.var_line_tainted = false;
|
||||
}
|
||||
// } else if (token_text === ':') {
|
||||
// hmm, when does this happen? tests don't catch this
|
||||
// flags.var_line = false;
|
||||
|
||||
print_token();
|
||||
print_single_space();
|
||||
break;
|
||||
}
|
||||
|
||||
if (last_type == 'TK_COMMENT') {
|
||||
print_newline();
|
||||
}
|
||||
|
||||
if (last_type === 'TK_END_BLOCK' && flags.mode !== "(EXPRESSION)") {
|
||||
print_token();
|
||||
if (flags.mode === 'OBJECT' && last_text === '}') {
|
||||
print_newline();
|
||||
} else {
|
||||
print_single_space();
|
||||
}
|
||||
} else {
|
||||
if (flags.mode === 'OBJECT') {
|
||||
print_token();
|
||||
print_newline();
|
||||
} else {
|
||||
// EXPR or DO_BLOCK
|
||||
print_token();
|
||||
print_single_space();
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
|
||||
case 'TK_OPERATOR':
|
||||
|
||||
var space_before = true;
|
||||
var space_after = true;
|
||||
|
||||
if (is_special_word(last_text)) {
|
||||
// "return" had a special handling in TK_WORD. Now we need to return the favor
|
||||
@ -1138,36 +1163,7 @@ function js_beautify(js_source_text, options) {
|
||||
break;
|
||||
}
|
||||
|
||||
if (token_text === ',') {
|
||||
if (flags.var_line) {
|
||||
if (flags.var_line_tainted) {
|
||||
print_token();
|
||||
print_newline();
|
||||
flags.var_line_tainted = false;
|
||||
} else {
|
||||
print_token();
|
||||
print_single_space();
|
||||
}
|
||||
} else if (last_type === 'TK_END_BLOCK' && flags.mode !== "(EXPRESSION)") {
|
||||
print_token();
|
||||
if (flags.mode === 'OBJECT' && last_text === '}') {
|
||||
print_newline();
|
||||
} else {
|
||||
print_single_space();
|
||||
}
|
||||
} else {
|
||||
if (flags.mode === 'OBJECT') {
|
||||
print_token();
|
||||
print_newline();
|
||||
} else {
|
||||
// EXPR or DO_BLOCK
|
||||
print_token();
|
||||
print_single_space();
|
||||
}
|
||||
}
|
||||
break;
|
||||
// } 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, ['==', '!=', '+=', '-=', '*=', '/=', '+', '-'])))) {
|
||||
} 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)))) {
|
||||
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)))) {
|
||||
// unary operators (and binary +/- pretending to be unary) special cases
|
||||
|
||||
space_before = false;
|
||||
@ -1213,10 +1209,6 @@ function js_beautify(js_source_text, options) {
|
||||
print_single_space();
|
||||
}
|
||||
|
||||
if (token_text === '!') {
|
||||
// flags.eat_next_space = true;
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case 'TK_BLOCK_COMMENT':
|
||||
|
@ -233,6 +233,7 @@ class Beautifier:
|
||||
'TK_STRING': self.handle_string,
|
||||
'TK_EQUALS': self.handle_equals,
|
||||
'TK_OPERATOR': self.handle_operator,
|
||||
'TK_COMMA': self.handle_comma,
|
||||
'TK_BLOCK_COMMENT': self.handle_block_comment,
|
||||
'TK_INLINE_COMMENT': self.handle_inline_comment,
|
||||
'TK_COMMENT': self.handle_comment,
|
||||
@ -656,8 +657,11 @@ class Beautifier:
|
||||
break
|
||||
if c == '=':
|
||||
return c, 'TK_EQUALS'
|
||||
else:
|
||||
|
||||
if c == ',':
|
||||
return c, 'TK_COMMA'
|
||||
return c, 'TK_OPERATOR'
|
||||
|
||||
return c, 'TK_UNKNOWN'
|
||||
|
||||
|
||||
@ -942,6 +946,7 @@ class Beautifier:
|
||||
|
||||
self.append(token_text)
|
||||
|
||||
|
||||
def handle_equals(self, token_text):
|
||||
if self.flags.var_line:
|
||||
# just got an '=' in a var-line, different line breaking rules will apply
|
||||
@ -952,15 +957,16 @@ class Beautifier:
|
||||
self.append(' ')
|
||||
|
||||
|
||||
def handle_operator(self, token_text):
|
||||
space_before = True
|
||||
space_after = True
|
||||
def handle_comma(self, token_text):
|
||||
|
||||
if self.flags.var_line and token_text == ',' and self.is_expression(self.flags.mode):
|
||||
|
||||
if self.last_type == 'TK_COMMENT':
|
||||
self.append_newline();
|
||||
|
||||
if self.flags.var_line:
|
||||
if self.is_expression(self.flags.mode):
|
||||
# do not break on comma, for ( var a = 1, b = 2
|
||||
self.flags.var_line_tainted = False
|
||||
|
||||
if self.flags.var_line and token_text == ',':
|
||||
if self.flags.var_line_tainted:
|
||||
self.append(token_text)
|
||||
self.flags.var_line_reindented = True
|
||||
@ -970,6 +976,30 @@ class Beautifier:
|
||||
else:
|
||||
self.flags.var_line_tainted = False
|
||||
|
||||
self.append(token_text)
|
||||
self.append(' ');
|
||||
return
|
||||
|
||||
if self.last_type == 'TK_END_BLOCK' and self.flags.mode != '(EXPRESSION)':
|
||||
self.append(token_text)
|
||||
if self.flags.mode == 'OBJECT' and self.last_text == '}':
|
||||
self.append_newline()
|
||||
else:
|
||||
self.append(' ')
|
||||
else:
|
||||
if self.flags.mode == 'OBJECT':
|
||||
self.append(token_text)
|
||||
self.append_newline()
|
||||
else:
|
||||
# EXPR or DO_BLOCK
|
||||
self.append(token_text)
|
||||
self.append(' ')
|
||||
|
||||
|
||||
def handle_operator(self, token_text):
|
||||
space_before = True
|
||||
space_after = True
|
||||
|
||||
if self.is_special_word(self.last_text):
|
||||
# return had a special handling in TK_WORD
|
||||
self.append(' ')
|
||||
@ -994,33 +1024,7 @@ class Beautifier:
|
||||
return
|
||||
|
||||
|
||||
if token_text == ',':
|
||||
if self.flags.var_line:
|
||||
if self.flags.var_line_tainted:
|
||||
# This never happens, as it's handled previously, right?
|
||||
self.append(token_text)
|
||||
self.append_newline()
|
||||
self.flags.var_line_tainted = False
|
||||
else:
|
||||
self.append(token_text)
|
||||
self.append(' ')
|
||||
elif self.last_type == 'TK_END_BLOCK' and self.flags.mode != '(EXPRESSION)':
|
||||
self.append(token_text)
|
||||
if self.flags.mode == 'OBJECT' and self.last_text == '}':
|
||||
self.append_newline()
|
||||
else:
|
||||
self.append(' ')
|
||||
else:
|
||||
if self.flags.mode == 'OBJECT':
|
||||
self.append(token_text)
|
||||
self.append_newline()
|
||||
else:
|
||||
# EXPR or DO_BLOCK
|
||||
self.append(token_text)
|
||||
self.append(' ')
|
||||
# comma handled
|
||||
return
|
||||
elif token_text in ['--', '++', '!'] \
|
||||
if token_text in ['--', '++', '!'] \
|
||||
or (token_text in ['+', '-'] \
|
||||
and (self.last_type in ['TK_START_BLOCK', 'TK_START_EXPR', 'TK_EQUALS', 'TK_OPERATOR'] \
|
||||
or self.last_text in self.line_starters)):
|
||||
|
@ -288,7 +288,7 @@ class TestJSBeautifier(unittest.TestCase):
|
||||
bt("var a2, b2, c2, d2 = 0, c = function() {}, d = '';", "var a2, b2, c2, d2 = 0,\n c = function() {},\n d = '';");
|
||||
bt('var o2=$.extend(a);function(){alert(x);}', 'var o2 = $.extend(a);\n\nfunction() {\n alert(x);\n}');
|
||||
|
||||
bt('{"x":[{"a":1,"b":3},7,8,8,8,8,{"b":99},{"a":11}]}', '{\n "x": [{\n "a": 1,\n "b": 3\n },\n 7, 8, 8, 8, 8,\n {\n "b": 99\n }, {\n "a": 11\n }]\n}');
|
||||
bt('{"x":[{"a":1,"b":3},7,8,8,8,8,{"b":99},{"a":11}]}', '{\n "x": [{\n "a": 1,\n "b": 3\n },\n 7, 8, 8, 8, 8, {\n "b": 99\n }, {\n "a": 11\n }]\n}');
|
||||
|
||||
bt('{"1":{"1a":"1b"},"2"}', '{\n "1": {\n "1a": "1b"\n },\n "2"\n}');
|
||||
bt('{a:{a:b},c}', '{\n a: {\n a: b\n },\n c\n}');
|
||||
@ -444,6 +444,8 @@ class TestJSBeautifier(unittest.TestCase):
|
||||
bt('var a = function();')
|
||||
bt('var a = 5 + function();')
|
||||
|
||||
bt('{\n foo // something\n ,\n bar // something\n baz\n}')
|
||||
|
||||
|
||||
bt('3.*7;', '3. * 7;')
|
||||
bt('import foo.*;', 'import foo.*;') # actionscript's import
|
||||
|
@ -322,7 +322,7 @@ function run_beautifier_tests(test_obj)
|
||||
bt("var a2, b2, c2, d2 = 0, c = function() {}, d = '';", "var a2, b2, c2, d2 = 0,\n c = function() {},\n d = '';");
|
||||
bt('var o2=$.extend(a);function(){alert(x);}', 'var o2 = $.extend(a);\n\nfunction() {\n alert(x);\n}');
|
||||
|
||||
bt('{"x":[{"a":1,"b":3},7,8,8,8,8,{"b":99},{"a":11}]}', '{\n "x": [{\n "a": 1,\n "b": 3\n },\n 7, 8, 8, 8, 8,\n {\n "b": 99\n }, {\n "a": 11\n }]\n}');
|
||||
bt('{"x":[{"a":1,"b":3},7,8,8,8,8,{"b":99},{"a":11}]}', '{\n "x": [{\n "a": 1,\n "b": 3\n },\n 7, 8, 8, 8, 8, {\n "b": 99\n }, {\n "a": 11\n }]\n}');
|
||||
|
||||
bt('{"1":{"1a":"1b"},"2"}', '{\n "1": {\n "1a": "1b"\n },\n "2"\n}');
|
||||
bt('{a:{a:b},c}', '{\n a: {\n a: b\n },\n c\n}');
|
||||
@ -498,6 +498,8 @@ function run_beautifier_tests(test_obj)
|
||||
bt('import foo.*;', 'import foo.*;') // actionscript's import
|
||||
test_fragment('function f(a: a, b: b)') // actionscript
|
||||
|
||||
bt('{\n foo // something\n ,\n bar // something\n baz\n}');
|
||||
|
||||
return sanitytest;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user