Skip to content

Commit 510fb59

Browse files
committed
Fixes #1871, allows single-line implicit ends to close implicit objects.
1 parent e4b3e83 commit 510fb59

File tree

3 files changed

+24
-4
lines changed

3 files changed

+24
-4
lines changed

lib/coffee-script/rewriter.js

+5-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/rewriter.coffee

+5-2
Original file line numberDiff line numberDiff line change
@@ -103,13 +103,15 @@ class exports.Rewriter
103103
stack = []
104104
start = null
105105
startsLine = null
106+
sameLine = yes
106107
startIndent = 0
107108

108109
condition = (token, i) ->
109110
[one, two, three] = @tokens[i + 1 .. i + 3]
110-
return false if 'HERECOMMENT' is one?[0]
111+
return no if 'HERECOMMENT' is one?[0]
111112
[tag] = token
112-
(tag in ['TERMINATOR', 'OUTDENT'] and
113+
sameLine = no if tag in LINEBREAKS
114+
((tag in ['TERMINATOR', 'OUTDENT'] or (tag in IMPLICIT_END and sameLine)) and
113115
((!startsLine and @tag(i - 1) isnt ',') or
114116
not (two?[0] is ':' or one?[0] is '@' and three?[0] is ':'))) or
115117
(tag is ',' and one and
@@ -129,6 +131,7 @@ class exports.Rewriter
129131
return 1
130132
return 1 unless tag is ':' and
131133
((ago = @tag i - 2) is ':' or stack[stack.length - 1]?[0] isnt '{')
134+
sameLine = yes
132135
stack.push ['{']
133136
idx = if ago is '@' then i - 2 else i - 1
134137
idx -= 2 while @tag(idx - 2) is 'HERECOMMENT'

test/objects.coffee

+14
Original file line numberDiff line numberDiff line change
@@ -237,3 +237,17 @@ test "#1322: implicit call against implicit object with block comments", ->
237237
test "#1513: Top level bare objs need to be wrapped in parens for unary and existence ops", ->
238238
doesNotThrow -> CoffeeScript.run "{}?", bare: true
239239
doesNotThrow -> CoffeeScript.run "{}.a++", bare: true
240+
241+
test "#1871: Special case for IMPLICIT_END in the middle of an implicit object", ->
242+
result = 'result'
243+
ident = (x) -> x
244+
245+
result = ident one: 1 if false
246+
247+
eq result, 'result'
248+
249+
result = ident
250+
one: 1
251+
two: 2 for i in [1..3]
252+
253+
eq result.two.join(' '), '2 2 2'

0 commit comments

Comments
 (0)