Skip to content

Commit f76892f

Browse files
committed
return and export default can now accept implicit objects (jashkenas#4532)
1 parent 96b6c5f commit f76892f

File tree

6 files changed

+45
-13
lines changed

6 files changed

+45
-13
lines changed

lib/coffee-script/lexer.js

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

lib/coffee-script/rewriter.js

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

src/lexer.coffee

+3-3
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ exports.Lexer = class Lexer
378378
return indent.length
379379

380380
if size > @indent
381-
if noNewlines
381+
if noNewlines or @tag() is 'RETURN'
382382
@indebt = size - @indent
383383
@suppressNewlines()
384384
return indent.length
@@ -430,7 +430,7 @@ exports.Lexer = class Lexer
430430
this
431431

432432
# Matches and consumes non-meaningful whitespace. Tag the previous token
433-
# as being "spaced", because there are some cases where it makes a difference.
433+
# as being spaced, because there are some cases where it makes a difference.
434434
whitespaceToken: ->
435435
return 0 unless (match = WHITESPACE.exec @chunk) or
436436
(nline = @chunk.charAt(0) is '\n')
@@ -761,7 +761,7 @@ exports.Lexer = class Lexer
761761
LINE_CONTINUER.test(@chunk) or
762762
@tag() in ['\\', '.', '?.', '?::', 'UNARY', 'MATH', 'UNARY_MATH', '+', '-',
763763
'**', 'SHIFT', 'RELATION', 'COMPARE', '&', '^', '|', '&&', '||',
764-
'BIN?', 'THROW', 'EXTENDS']
764+
'BIN?', 'THROW', 'EXTENDS', 'DEFAULT']
765765

766766
formatString: (str, options) ->
767767
@replaceUnicodeCodePointEscapes str.replace(STRING_OMIT, '$1'), options

src/rewriter.coffee

+4-6
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,16 @@ generate = (tag, value, origin) ->
1414

1515
# The **Rewriter** class is used by the [Lexer](lexer.html), directly against
1616
# its internal array of tokens.
17-
class exports.Rewriter
18-
19-
# Helpful snippet for debugging:
20-
#
21-
# console.log (t[0] + '/' + t[1] for t in @tokens).join ' '
17+
exports.Rewriter = class Rewriter
2218

2319
# Rewrite the token stream in multiple passes, one logical filter at
2420
# a time. This could certainly be changed into a single pass through the
2521
# stream, with a big ol' efficient switch, but it's much nicer to work with
2622
# like this. The order of these passes matters -- indentation must be
2723
# corrected before implicit parentheses can be wrapped around blocks of code.
2824
rewrite: (@tokens) ->
25+
# Helpful snippet for debugging:
26+
# console.log (t[0] + '/' + t[1] for t in @tokens).join ' '
2927
@removeLeadingNewlines()
3028
@closeOpenCalls()
3129
@closeOpenIndexes()
@@ -186,7 +184,7 @@ class exports.Rewriter
186184
# Don't end an implicit call on next indent if any of these are in an argument
187185
if inImplicitCall() and tag in ['IF', 'TRY', 'FINALLY', 'CATCH',
188186
'CLASS', 'SWITCH']
189-
stack.push ['CONTROL', i, ours: true]
187+
stack.push ['CONTROL', i, ours: yes]
190188
return forward(1)
191189

192190
if tag is 'INDENT' and inImplicit()

test/modules.coffee

+22
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,28 @@ test "export default object", ->
353353
};"""
354354
eq toJS(input), output
355355

356+
test "export default implicit object", ->
357+
input = "export default foo: 'bar', baz: 'qux'"
358+
output = """
359+
export default {
360+
foo: 'bar',
361+
baz: 'qux'
362+
};"""
363+
eq toJS(input), output
364+
365+
test "export default multiline implicit object", ->
366+
input = """
367+
export default
368+
foo: 'bar',
369+
baz: 'qux'
370+
"""
371+
output = """
372+
export default {
373+
foo: 'bar',
374+
baz: 'qux'
375+
};"""
376+
eq toJS(input), output
377+
356378
test "export default assignment expression", ->
357379
input = "export default foo = 'bar'"
358380
output = """

test/objects.coffee

+12
Original file line numberDiff line numberDiff line change
@@ -575,3 +575,15 @@ test "#4324: Shorthand after interpolated key", ->
575575
obj = {"#{1}": 1, a}
576576
eq 1, obj[1]
577577
eq 2, obj.a
578+
579+
test "#1263: Braceless object return", ->
580+
fn = ->
581+
return
582+
a: 1
583+
b: 2
584+
c: -> 3
585+
586+
obj = fn()
587+
eq 1, obj.a
588+
eq 2, obj.b
589+
eq 3, obj.c()

0 commit comments

Comments
 (0)