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

Be much more careful about parsing * in import and export statements #4308

Merged
merged 1 commit into from
Sep 15, 2016
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
23 changes: 15 additions & 8 deletions lib/coffee-script/lexer.js

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

10 changes: 7 additions & 3 deletions src/lexer.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,11 @@ exports.Lexer = class Lexer
if id is 'from' and @tag() is 'YIELD'
@token 'FROM', id
return id.length
if id is 'as' and (@seenImport or @seenExport) and @tag() in ['IDENTIFIER', 'IMPORT_ALL', 'EXPORT_ALL']
if id is 'as' and @seenImport and (@tag() is 'IDENTIFIER' or @value() is '*')
@tokens[@tokens.length - 1][0] = 'IMPORT_ALL' if @value() is '*'
@token 'AS', id
return id.length
if id is 'as' and @seenExport and @tag() is 'IDENTIFIER'
@token 'AS', id
return id.length
if id is 'default' and @seenExport
Expand Down Expand Up @@ -450,8 +454,8 @@ exports.Lexer = class Lexer
if value is ';'
@seenFor = @seenImport = @seenExport = no
tag = 'TERMINATOR'
else if value is '*' and @indent is 0 and (@seenImport or @seenExport)
tag = if @seenImport then 'IMPORT_ALL' else 'EXPORT_ALL'
else if value is '*' and prev[0] is 'EXPORT'
tag = 'EXPORT_ALL'
else if value in MATH then tag = 'MATH'
else if value in COMPARE then tag = 'COMPARE'
else if value in COMPOUND_ASSIGN then tag = 'COMPOUND_ASSIGN'
Expand Down
16 changes: 16 additions & 0 deletions test/modules.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,22 @@ test "`as` can be used as an alias name", ->
} from 'lib';"""
eq toJS(input), output

test "`*` can be used in an expression on the same line as an export keyword", ->
input = "export foo = (x) -> x * x"
output = """
export var foo = function(x) {
return x * x;
};"""
eq toJS(input), output
input = "export default foo = (x) -> x * x"
output = """
var foo;

export default foo = function(x) {
return x * x;
};"""
eq toJS(input), output

test "`*` and `from` can be used in an export default expression", ->
input = """
export default foo.extend
Expand Down