Skip to content

Commit

Permalink
return and export default can now accept implicit objects (#4532)
Browse files Browse the repository at this point in the history
  • Loading branch information
GeoffreyBooth committed May 2, 2017
1 parent ac1b2b5 commit 26cb24a
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 13 deletions.
4 changes: 2 additions & 2 deletions lib/coffee-script/lexer.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions lib/coffee-script/rewriter.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions src/lexer.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ exports.Lexer = class Lexer
return indent.length

if size > @indent
if noNewlines
if noNewlines or @tag() is 'RETURN'
@indebt = size - @indent
@suppressNewlines()
return indent.length
Expand Down Expand Up @@ -430,7 +430,7 @@ exports.Lexer = class Lexer
this

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

formatString: (str, options) ->
@replaceUnicodeCodePointEscapes str.replace(STRING_OMIT, '$1'), options
Expand Down
10 changes: 4 additions & 6 deletions src/rewriter.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,16 @@ generate = (tag, value, origin) ->

# The **Rewriter** class is used by the [Lexer](lexer.html), directly against
# its internal array of tokens.
class exports.Rewriter

# Helpful snippet for debugging:
#
# console.log (t[0] + '/' + t[1] for t in @tokens).join ' '
exports.Rewriter = class Rewriter

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

if tag is 'INDENT' and inImplicit()
Expand Down
22 changes: 22 additions & 0 deletions test/modules.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,28 @@ test "export default object", ->
};"""
eq toJS(input), output

test "export default implicit object", ->
input = "export default foo: 'bar', baz: 'qux'"
output = """
export default {
foo: 'bar',
baz: 'qux'
};"""
eq toJS(input), output

test "export default multiline implicit object", ->
input = """
export default
foo: 'bar',
baz: 'qux'
"""
output = """
export default {
foo: 'bar',
baz: 'qux'
};"""
eq toJS(input), output

test "export default assignment expression", ->
input = "export default foo = 'bar'"
output = """
Expand Down
12 changes: 12 additions & 0 deletions test/objects.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -575,3 +575,15 @@ test "#4324: Shorthand after interpolated key", ->
obj = {"#{1}": 1, a}
eq 1, obj[1]
eq 2, obj.a

test "#1263: Braceless object return", ->
fn = ->
return
a: 1
b: 2
c: -> 3

obj = fn()
eq 1, obj.a
eq 2, obj.b
eq 3, obj.c()

0 comments on commit 26cb24a

Please sign in to comment.