Skip to content
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
28 changes: 19 additions & 9 deletions lib/coffee-script/lexer.js

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

21 changes: 16 additions & 5 deletions src/lexer.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ exports.Lexer = class Lexer
@seenFor = no # Used to recognize FORIN and FOROF tokens.
@seenImport = no # Used to recognize IMPORT FROM? AS? tokens.
@seenExport = no # Used to recognize EXPORT FROM? AS? tokens.
@exportSpecifierList = no # Used to identify when in an EXPORT {...} FROM? ...

@chunkLine =
opts.line or 0 # The start line for the current @chunk.
Expand Down Expand Up @@ -115,10 +116,14 @@ exports.Lexer = class Lexer
if id is 'from' and @tag() is 'YIELD'
@token 'FROM', id
return id.length
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 @seenImport
if @value() is '*'
@tokens[@tokens.length - 1][0] = 'IMPORT_ALL'
else if @value() in COFFEE_KEYWORDS
@tokens[@tokens.length - 1][0] = 'IDENTIFIER'
if @tag() in ['IMPORT_ALL', 'IDENTIFIER']
@token 'AS', id
return id.length
if id is 'as' and @seenExport and @tag() is 'IDENTIFIER'
@token 'AS', id
return id.length
Expand All @@ -136,7 +141,8 @@ exports.Lexer = class Lexer
else
'IDENTIFIER'

if tag is 'IDENTIFIER' and (id in JS_KEYWORDS or id in COFFEE_KEYWORDS)
if tag is 'IDENTIFIER' and (id in JS_KEYWORDS or id in COFFEE_KEYWORDS) and
not (@exportSpecifierList and id in COFFEE_KEYWORDS)
tag = id.toUpperCase()
if tag is 'WHEN' and @tag() in LINE_BREAK
tag = 'LEADING_WHEN'
Expand Down Expand Up @@ -456,6 +462,11 @@ exports.Lexer = class Lexer
@error message, origin[2] if message
return value.length if skipToken

if value is '{' and prev?[0] is 'EXPORT'
@exportSpecifierList = yes
else if @exportSpecifierList and value is '}'
@exportSpecifierList = no

if value is ';'
@seenFor = @seenImport = @seenExport = no
tag = 'TERMINATOR'
Expand Down
20 changes: 20 additions & 0 deletions test/error_messages.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -1154,3 +1154,23 @@ test "imported members cannot be reassigned", ->
export foo = 'bar'
^^^
'''

test "CS only keywords can't be used as unaliased names in import lists", ->
assertErrorFormat """
import { unless, baz as bar } from 'lib'
bar.barMethod()
""", '''
[stdin]:1:10: error: unexpected unless
import { unless, baz as bar } from 'lib'
^^^^^^
'''

test "CS only keywords can't be used as local names in import list aliases", ->
assertErrorFormat """
import { bar as unless, baz as bar } from 'lib'
bar.barMethod()
""", '''
[stdin]:1:17: error: unexpected unless
import { bar as unless, baz as bar } from 'lib'
^^^^^^
'''
29 changes: 29 additions & 0 deletions test/modules.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,23 @@ test "export as aliases members imported from another module", ->
} from 'lib';"""
eq toJS(input), output

test "export list can contain CS only keywords", ->
input = "export { unless } from 'lib'"
output = """
export {
unless
} from 'lib';"""
eq toJS(input), output

test "export list can contain CS only keywords when aliasing", ->
input = "export { when as bar, baz as unless } from 'lib'"
output = """
export {
when as bar,
baz as unless
} from 'lib';"""
eq toJS(input), output


# Edge cases

Expand Down Expand Up @@ -608,6 +625,18 @@ test "`as` can be used as an alias name", ->
} from 'lib';"""
eq toJS(input), output

test "CS only keywords can be used as imported names in import lists", ->
input = """
import { unless as bar } from 'lib'
bar.barMethod()"""
output = """
import {
unless as bar
} from 'lib';

bar.barMethod();"""
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 = """
Expand Down