Skip to content

Commit

Permalink
Merge pull request #4308 from GeoffreyBooth/import-export
Browse files Browse the repository at this point in the history
Be much more careful about parsing `*` in import and export statements
  • Loading branch information
lydell authored Sep 15, 2016
2 parents 9ae377b + 51f24e0 commit 7667cb2
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 11 deletions.
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

0 comments on commit 7667cb2

Please sign in to comment.