Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CS2] return and export default can now accept implicit objects #4532

Merged
merged 5 commits into from
May 2, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/coffeescript/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/coffeescript/rewriter.js

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

1 change: 0 additions & 1 deletion src/grammar.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,6 @@ grammar =
o 'AWAIT RETURN', -> new AwaitReturn
]


# A block comment.
Comment: [
o 'HERECOMMENT', -> new Comment $1
Expand Down
6 changes: 3 additions & 3 deletions src/lexer.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,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 @@ -455,7 +455,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 @@ -790,7 +790,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']
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Out of interest, but would adding 'RETURN" here achieve the same thing as 75f6912?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That was my first thought, but it's too permissive. This causes a return line continuation even if the line following the return is the same indentation, which we definitely don't want.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, good point, that makes sense!


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 @@ -353,6 +353,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()