Browse files

Fix some hypothetical bugs with wrong line breaks

  • Loading branch information...
1 parent 59dd46f commit 0ea87daa0d864fe537f5d0d07635f25cb7e510cd @einars einars committed Mar 7, 2012
Showing with 16 additions and 10 deletions.
  1. +10 −5 beautify.js
  2. +6 −5 python/jsbeautifier/__init__.py
View
15 beautify.js
@@ -236,6 +236,11 @@ function js_beautify(js_source_text, options) {
return true;
}
+ function is_special_word(word)
+ {
+ return in_array(word, ['case', 'return', 'do', 'if', 'throw', 'else']);
+ }
+
function in_array(what, arr) {
for (var i = 0; i < arr.length; i += 1) {
if (arr[i] === what) {
@@ -459,7 +464,7 @@ function js_beautify(js_source_text, options) {
if (c === "'" || // string
c === '"' || // string
(c === '/' &&
- ((last_type === 'TK_WORD' && in_array(last_text, ['case', 'return', 'do', 'else'])) ||
+ ((last_type === 'TK_WORD' && is_special_word(last_text)) ||
(last_text === ')' && in_array(flags.previous_mode, ['(COND-EXPRESSION)', '(FOR-EXPRESSION)'])) ||
(last_type === 'TK_COMMENT' || last_type === 'TK_START_EXPR' || last_type === 'TK_START_BLOCK' || last_type === 'TK_END_BLOCK' || last_type === 'TK_OPERATOR' || last_type === 'TK_EQUALS' || last_type === 'TK_EOF' || last_type === 'TK_SEMICOLON')))) { // regexp
var sep = c;
@@ -783,7 +788,7 @@ function js_beautify(js_source_text, options) {
}
} else {
if (last_type !== 'TK_OPERATOR') {
- if (last_text === 'return' || last_text === '=' || last_text === 'throw') {
+ if (last_text === '=' || (is_special_word(last_text) && last_text !== 'else')) {
print_single_space();
} else {
print_newline(true);
@@ -959,7 +964,7 @@ function js_beautify(js_source_text, options) {
// DONOTHING
} else if (token_text === 'function' && last_text == 'new') {
print_single_space();
- } else if (last_text === 'return' || last_text === 'throw') {
+ } else if (is_special_word(last_text)) {
// no newline between 'return nnn'
print_single_space();
} else if (last_type !== 'TK_END_EXPR') {
@@ -1062,7 +1067,7 @@ function js_beautify(js_source_text, options) {
}
}
- if (last_text === 'return' || last_text === 'throw') {
+ if (is_special_word(last_text)) {
// "return" had a special handling in TK_WORD. Now we need to return the favor
print_single_space();
print_token();
@@ -1227,7 +1232,7 @@ function js_beautify(js_source_text, options) {
break;
case 'TK_UNKNOWN':
- if (last_text === 'return' || last_text === 'throw') {
+ if (is_special_word(last_text)) {
print_single_space();
}
print_token();
View
11 python/jsbeautifier/__init__.py
@@ -92,7 +92,6 @@ def beautify(string, opts = default_options() ):
b = Beautifier()
return b.beautify(string, opts)
-
def beautify_file(file_name, opts = default_options() ):
if file_name == '-': # stdin
@@ -261,6 +260,8 @@ def trim_output(self, eat_newlines = False):
or (eat_newlines and self.output[-1] in ['\n', '\r'])):
self.output.pop()
+ def is_special_word(self, s):
+ return s in ['case', 'return', 'do', 'if', 'throw', 'else'];
def is_array(self, mode):
return mode in ['[EXPRESSION]', '[INDENDED-EXPRESSION]']
@@ -517,7 +518,7 @@ def get_next_token(self):
if c == "'" or c == '"' or \
- (c == '/' and ((self.last_type == 'TK_WORD' and self.last_text in ['case', 'return', 'do', 'else']) or \
+ (c == '/' and ((self.last_type == 'TK_WORD' and self.is_special_word(self.last_text)) or \
(self.last_type == 'TK_END_EXPR' and self.flags.previous_mode in ['(FOR-EXPRESSION)', '(COND-EXPRESSION)']) or \
(self.last_type in ['TK_COMMENT', 'TK_START_EXPR', 'TK_START_BLOCK', 'TK_END_BLOCK', 'TK_OPERATOR',
'TK_EQUALS', 'TK_EOF', 'TK_SEMICOLON']))):
@@ -720,7 +721,7 @@ def handle_start_block(self, token_text):
if self.opts.brace_style == 'expand':
if self.last_type != 'TK_OPERATOR':
- if self.last_text in ['return', '=', 'throw']:
+ if self.last_text == '=' or (self.is_special_word(self.last_text) and self.last_text != 'else'):
self.append(' ')
else:
self.append_newline(True)
@@ -857,7 +858,7 @@ def handle_word(self, token_text):
pass
elif token_text == 'function' and self.last_text == 'new':
self.append(' ')
- elif self.last_text in ['return', 'throw']:
+ elif self.is_special_word(self.last_text):
# no newline between return nnn
self.append(' ')
elif self.last_type != 'TK_END_EXPR':
@@ -954,7 +955,7 @@ def handle_operator(self, token_text):
else:
self.flags.var_line_tainted = False
- if self.last_text in ['return', 'throw']:
+ if self.is_special_word(self.last_text):
# return had a special handling in TK_WORD
self.append(' ')
self.append(token_text)

0 comments on commit 0ea87da

Please sign in to comment.