Skip to content

Commit

Permalink
Extract TK_COMMA from TK_OPERATOR, fix #125
Browse files Browse the repository at this point in the history
  • Loading branch information
einars committed Jul 5, 2012
1 parent ec46241 commit 29fffc2
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 95 deletions.
6 changes: 4 additions & 2 deletions Makefile
Expand Up @@ -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

Expand Down
102 changes: 47 additions & 55 deletions beautify.js
Expand Up @@ -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'];
Expand Down Expand Up @@ -1083,32 +1085,55 @@ function js_beautify(js_source_text, options) {
print_single_space();
break;

case 'TK_OPERATOR':
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_tainted) {
print_token();
flags.var_line_reindented = true;
flags.var_line_tainted = false;
print_newline();
break;
} else {
flags.var_line_tainted = false;
}

var space_before = true;
var space_after = true;
print_token();
print_single_space();
break;
}

if (flags.var_line && token_text === ',' && (is_expression(flags.mode))) {
// do not break on comma, for(var a = 1, b = 2)
flags.var_line_tainted = false;
if (last_type == 'TK_COMMENT') {
print_newline();
}

if (flags.var_line) {
if (token_text === ',') {
if (flags.var_line_tainted) {
print_token();
flags.var_line_reindented = true;
flags.var_line_tainted = false;
print_newline();
break;
} else {
flags.var_line_tainted = false;
}
// } else if (token_text === ':') {
// hmm, when does this happen? tests don't catch this
// flags.var_line = false;
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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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':
Expand Down
76 changes: 40 additions & 36 deletions python/jsbeautifier/__init__.py
Expand Up @@ -233,6 +233,7 @@ def beautify(self, s, opts = None ):
'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,
Expand Down Expand Up @@ -656,8 +657,11 @@ def get_next_token(self):
break
if c == '=':
return c, 'TK_EQUALS'
else:
return c, 'TK_OPERATOR'

if c == ',':
return c, 'TK_COMMA'
return c, 'TK_OPERATOR'

return c, 'TK_UNKNOWN'


Expand Down Expand Up @@ -942,6 +946,7 @@ def handle_string(self, token_text):

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
Expand All @@ -952,15 +957,16 @@ def handle_equals(self, token_text):
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):
# 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.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_tainted:
self.append(token_text)
self.flags.var_line_reindented = True
Expand All @@ -970,6 +976,30 @@ def handle_operator(self, token_text):
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(' ')
Expand All @@ -994,33 +1024,7 @@ def handle_operator(self, token_text):
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)):
Expand Down
4 changes: 3 additions & 1 deletion python/jsbeautifier/tests/testjsbeautifier.py
Expand Up @@ -288,7 +288,7 @@ def test_beautifier(self):
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}');
Expand Down Expand Up @@ -444,6 +444,8 @@ def test_beautifier(self):
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
Expand Down
4 changes: 3 additions & 1 deletion tests/beautify-tests.js
Expand Up @@ -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}');
Expand Down Expand Up @@ -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;
}

0 comments on commit 29fffc2

Please sign in to comment.