From 13b2dc8d31d6d52d5bcd217c5d95f8c3b0e556d3 Mon Sep 17 00:00:00 2001 From: Jeremy Ashkenas Date: Sat, 13 Feb 2010 09:39:25 -0500 Subject: [PATCH] subtle bug in the order of the rewriter rules was causing some if/else chains to get confused by implicit indentation --- lib/coffee_script/command_line.js | 14 ++++++++++++-- lib/coffee_script/grammar.js | 8 ++++---- lib/coffee_script/nodes.js | 2 -- lib/coffee_script/rewriter.js | 2 +- src/rewriter.coffee | 2 +- 5 files changed, 18 insertions(+), 10 deletions(-) diff --git a/lib/coffee_script/command_line.js b/lib/coffee_script/command_line.js index d4e287c2..51700d8f 100644 --- a/lib/coffee_script/command_line.js +++ b/lib/coffee_script/command_line.js @@ -56,9 +56,19 @@ var js; if (opts.tokens) { puts(coffee.tokenize(code).join(' ')); + } else if (opts.tree) { + puts(coffee.tree(code).toString()); } else { - opts.tree ? puts(coffee.tree(code).toString()) : (js = coffee.compile(code)); - opts.run ? eval(js) : opts.print ? puts(js) : opts.lint ? exports.lint(js) : exports.write_js(source, coffee.compile(code)); + js = coffee.compile(code); + if (opts.run) { + eval(js); + } else if (opts.print) { + puts(js); + } else if (opts.lint) { + exports.lint(js); + } else { + exports.write_js(source, coffee.compile(code)); + } } return exports.compile_scripts(); }); diff --git a/lib/coffee_script/grammar.js b/lib/coffee_script/grammar.js index 0a8ea5a8..7fcf62ab 100644 --- a/lib/coffee_script/grammar.js +++ b/lib/coffee_script/grammar.js @@ -118,13 +118,13 @@ return new OpNode('!!', $2); }), o("- Expression", (function() { return new OpNode('-', $2); - }), { + })), { prec: 'UMINUS' - }), o("+ Expression", (function() { + }, o("+ Expression", (function() { return new OpNode('+', $2); - }), { + })), { prec: 'UPLUS' - }), o("NOT Expression", function() { + }, o("NOT Expression", function() { return new OpNode('not', $2); }), o("~ Expression", function() { return new OpNode('~', $2); diff --git a/lib/coffee_script/nodes.js b/lib/coffee_script/nodes.js index 30c4ec09..3d2d8946 100644 --- a/lib/coffee_script/nodes.js +++ b/lib/coffee_script/nodes.js @@ -807,9 +807,7 @@ toString: function toString(idt) { var children; idt = (idt || '') + TAB; - puts(this.body.expressions.length); children = _.flatten([this.params, this.body.expressions]); - puts(children.length); return this.type + '\n' + _.map(children, function(child) { return idt + child.toString(idt); }).join(''); diff --git a/lib/coffee_script/rewriter.js b/lib/coffee_script/rewriter.js index 945884a7..ebabed72 100644 --- a/lib/coffee_script/rewriter.js +++ b/lib/coffee_script/rewriter.js @@ -53,8 +53,8 @@ this.remove_mid_expression_newlines(); this.move_commas_outside_outdents(); this.close_open_calls_and_indexes(); - this.add_implicit_parentheses(); this.add_implicit_indentation(); + this.add_implicit_parentheses(); this.ensure_balance(BALANCED_PAIRS); this.rewrite_closing_parens(); return this.tokens; diff --git a/src/rewriter.coffee b/src/rewriter.coffee index b1d99491..b9d8dd61 100644 --- a/src/rewriter.coffee +++ b/src/rewriter.coffee @@ -45,8 +45,8 @@ re::rewrite: (tokens) -> this.remove_mid_expression_newlines() this.move_commas_outside_outdents() this.close_open_calls_and_indexes() - this.add_implicit_parentheses() this.add_implicit_indentation() + this.add_implicit_parentheses() this.ensure_balance(BALANCED_PAIRS) this.rewrite_closing_parens() this.tokens