diff --git a/lib/coffee-script.js b/lib/coffee-script.js index aef25e38..5f9d9f01 100644 --- a/lib/coffee-script.js +++ b/lib/coffee-script.js @@ -54,8 +54,8 @@ exports.run = (function(code, options) { var __dirname, __filename; module.filename = (__filename = options.source); - __dirname = path.dirname(__filename); - return eval(exports.compile(code, options)); + __dirname = path.dirname(__filename, eval(exports.compile(code, options))); + return __dirname; }); // Extend CoffeeScript with a custom language extension. It should hook in to // the **Lexer** (as a peer of any of the lexer's tokenizing methods), and diff --git a/lib/rewriter.js b/lib/rewriter.js index 154371ce..5dbbfee0 100644 --- a/lib/rewriter.js +++ b/lib/rewriter.js @@ -150,9 +150,10 @@ // Insert the implicit parentheses here, so that the parser doesn't have to // deal with them. Rewriter.prototype.add_implicit_parentheses = function add_implicit_parentheses() { - var calls, stack; + var calls, parens, stack; stack = [0]; calls = 0; + parens = 0; return this.scan_tokens((function(__this) { var __func = function(prev, token, post, i) { var _a, _b, _c, idx, last, open, size, stack_pointer, tag, tmp; @@ -161,6 +162,10 @@ calls += 1; } else if (tag === 'CALL_END') { calls -= 1; + } else if (tag === '(') { + parens += 1; + } else if (tag === ')') { + parens -= 1; } else if (tag === 'INDENT') { stack.push(0); } else if (tag === 'OUTDENT') { @@ -173,7 +178,7 @@ this.tokens.splice(i, 0, ['CALL_END', ')', token[2]]); return 2; } - if (!(typeof post !== "undefined" && post !== null) || include(IMPLICIT_END, tag)) { + if (!(typeof post !== "undefined" && post !== null) || (parens === 0 && include(IMPLICIT_END, tag))) { if (tag === 'INDENT' && prev && include(IMPLICIT_BLOCK, prev[0])) { return 1; } diff --git a/src/rewriter.coffee b/src/rewriter.coffee index 8df2b323..da75d34b 100644 --- a/src/rewriter.coffee +++ b/src/rewriter.coffee @@ -109,12 +109,15 @@ exports.Rewriter: class Rewriter # deal with them. add_implicit_parentheses: -> stack: [0] - calls: 0 + calls: 0 + parens: 0 @scan_tokens (prev, token, post, i) => tag: token[0] switch tag when 'CALL_START' then calls: + 1 when 'CALL_END' then calls: - 1 + when '(' then parens: + 1 + when ')' then parens: - 1 when 'INDENT' then stack.push(0) when 'OUTDENT' last: stack.pop() @@ -124,7 +127,7 @@ exports.Rewriter: class Rewriter stack[stack.length - 1]: - 1 @tokens.splice(i, 0, ['CALL_END', ')', token[2]]) return 2 - if !post? or include IMPLICIT_END, tag + if !post? or (parens is 0 and include IMPLICIT_END, tag) return 1 if tag is 'INDENT' and prev and include IMPLICIT_BLOCK, prev[0] return 1 if tag is 'OUTDENT' and token.generated if open or tag is 'INDENT' @@ -160,7 +163,7 @@ exports.Rewriter: class Rewriter pre: @tokens[idx - 1] if (not tok or (include(SINGLE_CLOSERS, tok[0]) and tok[1] isnt ';') or - (tok[0] is ')' && parens is 0)) and + (tok[0] is ')' and parens is 0)) and not (starter is 'ELSE' and tok[0] is 'ELSE') insertion: if pre[0] is "," then idx - 1 else idx outdent: ['OUTDENT', 2, token[2]] diff --git a/test/test_nested_comprehensions.coffee b/test/test_nested_comprehensions.coffee index 66d14128..301af11b 100644 --- a/test/test_nested_comprehensions.coffee +++ b/test/test_nested_comprehensions.coffee @@ -9,3 +9,12 @@ single_liner: ok multi_liner.length is single_liner.length ok 5 is multi_liner[2][2][1] ok 5 is single_liner[2][2][1] + + +# Test comprehensions within parentheses. +result: null +store: (obj) -> result: obj +store (x * 2 for x in [3, 2, 1]) + +ok result.join(' ') is '6 4 2' +