Permalink
Browse files

fix strings starting after a comment (+fix #167)

  • Loading branch information...
1 parent 67fcbb5 commit 8daa7c7b276c49119f3fd630b740f9771e2200ae @einars einars committed Dec 27, 2012
Showing with 19 additions and 11 deletions.
  1. +3 −3 beautify.js
  2. +12 −8 python/jsbeautifier/__init__.py
  3. +2 −0 python/jsbeautifier/tests/testjsbeautifier.py
  4. +2 −0 tests/beautify-tests.js
View
@@ -1076,15 +1076,15 @@ function js_beautify(js_source_text, options) {
if (last_type === 'TK_END_EXPR' && in_array(flags.previous_mode, ['(COND-EXPRESSION)', '(FOR-EXPRESSION)'])) {
print_single_space();
- } else if (last_type === 'TK_COMMENT' || last_type === 'TK_STRING' || last_type === 'TK_START_BLOCK' || last_type === 'TK_END_BLOCK' || last_type === 'TK_SEMICOLON') {
- print_newline();
} else if (last_type === 'TK_WORD') {
print_single_space();
- } else {
+ } else if (last_type === 'TK_COMMA' || last_type === 'TK_START_EXPR' || last_type === 'TK_EQUALS' || last_type === 'TK_OPERATOR') {
if (opt_preserve_newlines && wanted_newline && flags.mode !== 'OBJECT') {
print_newline();
output.push(indent_string);
}
+ } else {
+ print_newline();
}
print_token();
break;
@@ -475,7 +475,6 @@ def get_next_token(self):
if c == '/':
comment = ''
inline_comment = True
- comment_mode = 'TK_INLINE_COMMENT'
if self.input[self.parser_pos] == '*': # peek /* .. */ comment
self.parser_pos += 1
if self.parser_pos < len(self.input):
@@ -486,12 +485,16 @@ def get_next_token(self):
c = self.input[self.parser_pos]
comment += c
if c in '\r\n':
- comment_mode = 'TK_BLOCK_COMMENT'
+ inline_comment = False
self.parser_pos += 1
if self.parser_pos >= len(self.input):
break
self.parser_pos += 2
- return '/*' + comment + '*/', comment_mode
+ if inline_comment and self.n_newlines == 0:
+ return '/*' + comment + '*/', 'TK_INLINE_COMMENT'
+ else:
+ return '/*' + comment + '*/', 'TK_BLOCK_COMMENT'
+
if self.input[self.parser_pos] == '/': # peek // comment
comment = c
while self.input[self.parser_pos] not in '\r\n':
@@ -938,13 +941,14 @@ def handle_semicolon(self, token_text):
def handle_string(self, token_text):
if self.last_type == 'TK_END_EXPR' and self.flags.previous_mode in ['(COND-EXPRESSION)', '(FOR-EXPRESSION)']:
self.append(' ')
- if self.last_type in ['TK_COMMENT', 'TK_STRING', 'TK_START_BLOCK', 'TK_END_BLOCK', 'TK_SEMICOLON']:
- self.append_newline()
elif self.last_type == 'TK_WORD':
self.append(' ')
- elif self.opts.preserve_newlines and self.wanted_newline and self.flags.mode != 'OBJECT':
- self.append_newline();
- self.append(self.indent_string);
+ elif self.last_type in ['TK_COMMA', 'TK_START_EXPR', 'TK_EQUALS', 'TK_OPERATOR']:
+ if self.opts.preserve_newlines and self.wanted_newline and self.flags.mode != 'OBJECT':
+ self.append_newline()
+ self.append(self.indent_string)
+ else:
+ self.append_newline()
self.append(token_text)
@@ -468,6 +468,8 @@ def test_beautifier(self):
bt('if (foo) // comment\n(bar());');
bt('if (foo) // comment\n/asdf/;');
+ bt('/* foo */\n"x"');
+
bt("var a = 'foo' +\n 'bar';");
bt("var a = \"foo\" +\n \"bar\";");
View
@@ -528,6 +528,8 @@ function run_beautifier_tests(test_obj)
bt('if(foo) // comment\n(bar());');
bt('if(foo) // comment\n/asdf/;');
+ bt('/* foo */\n"x"');
+
opts.break_chained_methods = true;
bt('foo.bar().baz().cucumber(fat)', 'foo.bar()\n .baz()\n .cucumber(fat)');
bt('foo.bar().baz().cucumber(fat); foo.bar().baz().cucumber(fat)', 'foo.bar()\n .baz()\n .cucumber(fat);\nfoo.bar()\n .baz()\n .cucumber(fat)');

0 comments on commit 8daa7c7

Please sign in to comment.