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

consider es6 module syntax to be plain JS [fixes #3162] #4160

Closed
wants to merge 1 commit into from
Closed
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
15 changes: 13 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.

9 changes: 9 additions & 0 deletions src/lexer.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ exports.Lexer = class Lexer
i = 0
while @chunk = code[i..]
consumed = \
@moduleToken() or
@identifierToken() or
@commentToken() or
@whitespaceToken() or
Expand Down Expand Up @@ -255,6 +256,12 @@ exports.Lexer = class Lexer
@token 'JS', (script = match[0])[1...-1], 0, script.length
script.length

# Matches JavaScript interpolated directly into the source via module syntax.
moduleToken: ->
return 0 unless match = MODULETOKEN.exec @chunk
@token 'JS', (script = match[0]), 0, script.length
script.length

# Matches regular expression literals, as well as multiline extended ones.
# Lexing regular expressions is difficult to distinguish from division, so we
# borrow some basic heuristics from JavaScript and Ruby.
Expand Down Expand Up @@ -824,6 +831,8 @@ MULTI_DENT = /^(?:\n[^\n\S]*)+/

JSTOKEN = /^`[^\\`]*(?:\\.[^\\`]*)*`/

MODULETOKEN = /^(import|export)\s+[^#\\\n]*/

# String-matching-regexes.
STRING_START = /^(?:'''|"""|'|")/

Expand Down
49 changes: 49 additions & 0 deletions test/javascript_literals.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,52 @@ eq '\\`', `
// Inline JS
"\\\`"
`

toJS = (str)->
CoffeeScript.compile str, bare: true
.replace /^\s+|\s+$/g, ""

test "regular JS literal import statement", ->
input = '`import { member as alias } from "module-name"`'
output = 'import { member as alias } from "module-name";'
eq toJS(input), output

test "module import test, syntax #1", ->
input = "import foo from 'lib'"
output = "import foo from 'lib';"
eq toJS(input), output

test "module import test, syntax #2", ->
input = "import { foo } from 'lib'"
output = "import { foo } from 'lib';"
eq toJS(input), output

test "module import test, syntax #3", ->
input = "import { default as foo } from 'lib'"
output = "import { default as foo } from 'lib';"
eq toJS(input), output

test "module import test, syntax #4", ->
input = "import { square, diag } from 'lib'"
output = "import { square, diag } from 'lib';"
eq toJS(input), output

test "module import test, syntax #5", ->
input = "import { foo } from 'lib' # with a comment"
output = "import { foo } from 'lib' ;"
eq toJS(input), output

test "module export test, syntax #1", ->
input = "export default mixin"
output = "export default mixin;"
eq toJS(input), output

test "module export test, syntax #2", ->
input = "export { D as default }"
output = "export { D as default };"
eq toJS(input), output

test "module export test, syntax #3", ->
input = "export const sqrt = Math.sqrt"
output = "export const sqrt = Math.sqrt;"
eq toJS(input), output