From 15d84dbb4ee2ad24bb98f28e2fb91afb697d048d Mon Sep 17 00:00:00 2001 From: Jeremy Ashkenas Date: Sun, 19 Sep 2010 09:04:38 -0400 Subject: [PATCH] Fixing issue #694. Destructuring assignment as first line of implicitly called block -- regression. --- lib/rewriter.js | 5 +++-- src/rewriter.coffee | 3 ++- test/test_pattern_matching.coffee | 10 ++++++++++ 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/lib/rewriter.js b/lib/rewriter.js index 451e740c..356c5417 100644 --- a/lib/rewriter.js +++ b/lib/rewriter.js @@ -206,14 +206,15 @@ if (prev && (prev.spaced && (include(IMPLICIT_FUNC, prev[0]) || prev.call) && include(IMPLICIT_CALL, token[0]) && !(token[0] === 'UNARY' && (('IN' === (_c = this.tag(i + 1)) || 'OF' === _c || 'INSTANCEOF' === _c)))) || callObject) { this.tokens.splice(i, 0, ['CALL_START', '(', token[2]]); condition = function(token, i) { - var _c; + var _c, post; if (!seenSingle && token.fromThen) { return true; } if (('IF' === (_c = token[0]) || 'ELSE' === _c || 'UNLESS' === _c || '->' === _c || '=>' === _c)) { seenSingle = true; } - return (!token.generated && this.tokens[i - 1][0] !== ',' && include(IMPLICIT_END, token[0]) && !(token[0] === 'INDENT' && (include(IMPLICIT_BLOCK, this.tag(i - 1)) || this.tag(i - 2) === 'CLASS' || this.tag(i + 1) === '{'))) || token[0] === 'PROPERTY_ACCESS' && this.tag(i - 1) === 'OUTDENT'; + post = this.tokens[i + 1]; + return (!token.generated && this.tokens[i - 1][0] !== ',' && include(IMPLICIT_END, token[0]) && !(token[0] === 'INDENT' && (include(IMPLICIT_BLOCK, this.tag(i - 1)) || this.tag(i - 2) === 'CLASS' || (post && post.generated && post[0] === '{')))) || token[0] === 'PROPERTY_ACCESS' && this.tag(i - 1) === 'OUTDENT'; }; action = function(token, i) { idx = token[0] === 'OUTDENT' ? i + 1 : i; diff --git a/src/rewriter.coffee b/src/rewriter.coffee index b65ddeb5..a8fee0f6 100644 --- a/src/rewriter.coffee +++ b/src/rewriter.coffee @@ -173,8 +173,9 @@ exports.Rewriter = class Rewriter condition = (token, i) -> return yes if not seenSingle and token.fromThen seenSingle = yes if token[0] in ['IF', 'ELSE', 'UNLESS', '->', '=>'] + post = @tokens[i + 1] (not token.generated and @tokens[i - 1][0] isnt ',' and include(IMPLICIT_END, token[0]) and - not (token[0] is 'INDENT' and (include(IMPLICIT_BLOCK, @tag(i - 1)) or @tag(i - 2) is 'CLASS' or @tag(i + 1) is '{'))) or + not (token[0] is 'INDENT' and (include(IMPLICIT_BLOCK, @tag(i - 1)) or @tag(i - 2) is 'CLASS' or (post and post.generated and post[0] is '{')))) or token[0] is 'PROPERTY_ACCESS' and @tag(i - 1) is 'OUTDENT' action = (token, i) -> idx = if token[0] is 'OUTDENT' then i + 1 else i diff --git a/test/test_pattern_matching.coffee b/test/test_pattern_matching.coffee index 9757bf8b..e9243386 100644 --- a/test/test_pattern_matching.coffee +++ b/test/test_pattern_matching.coffee @@ -137,3 +137,13 @@ persons = [['Bob', ['George']], ['Alice', ['Bob']], ['Stan', ['Christopher']]] join3 = "#{parent}: #{name}" for [name, [parent]] in persons deepEqual join2, join3 + + +# Pattern matching doesn't clash with implicit block objects. +obj = a: 101 +func -> true + +if func func + {a} = obj + +ok a is 101