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

Dynamic import #5169

Merged
merged 6 commits into from
Mar 20, 2019
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
12 changes: 11 additions & 1 deletion lib/coffeescript/grammar.js

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

5 changes: 4 additions & 1 deletion lib/coffeescript/lexer.js

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

19 changes: 18 additions & 1 deletion lib/coffeescript/nodes.js

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

366 changes: 185 additions & 181 deletions lib/coffeescript/parser.js

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion src/grammar.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ grammar =
o 'Super'
o 'This'
o 'SUPER Arguments', -> new SuperCall LOC(1)(new Super), $2, no, $1
o 'DYNAMIC_IMPORT Arguments', -> new DynamicImportCall LOC(1)(new DynamicImport), $2
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Technically it looks like dynamic imports are allowed to be object spread (eg {...import('module')} compiles in Babel)

Added a corresponding test

Copy link
Collaborator

Choose a reason for hiding this comment

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

It evaluates in browsers too:

image

Though it makes no sense, as the import() returns a Promise. Not sure how that’s supposed to interact with object spread, and apparently it doesn’t 😄but still the code compiles even if it’s useless.

o 'SimpleObjAssignable Arguments', -> new Call (new Value $1), $2
o 'ObjSpreadExpr Arguments', -> new Call $1, $2
]
Expand Down Expand Up @@ -485,6 +486,7 @@ grammar =
o 'Value OptFuncExist String', -> new TaggedTemplateCall $1, $3, $2
o 'Value OptFuncExist Arguments', -> new Call $1, $3, $2
o 'SUPER OptFuncExist Arguments', -> new SuperCall LOC(1)(new Super), $3, $2, $1
o 'DYNAMIC_IMPORT Arguments', -> new DynamicImportCall LOC(1)(new DynamicImport), $2
]

# An optional existence check on a function.
Expand Down Expand Up @@ -891,7 +893,7 @@ operators = [
['right', 'YIELD']
['right', '=', ':', 'COMPOUND_ASSIGN', 'RETURN', 'THROW', 'EXTENDS']
['right', 'FORIN', 'FOROF', 'FORFROM', 'BY', 'WHEN']
['right', 'IF', 'ELSE', 'FOR', 'WHILE', 'UNTIL', 'LOOP', 'SUPER', 'CLASS', 'IMPORT', 'EXPORT']
['right', 'IF', 'ELSE', 'FOR', 'WHILE', 'UNTIL', 'LOOP', 'SUPER', 'CLASS', 'IMPORT', 'EXPORT', 'DYNAMIC_IMPORT']
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I don't know if this is technically necessary but it looked like DYNAMIC_IMPORT belonged at this precedence level (with SUPER, IMPORT and EXPORT)

['left', 'POST_IF']
]

Expand Down
5 changes: 4 additions & 1 deletion src/lexer.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -667,6 +667,9 @@ exports.Lexer = class Lexer
@error message, origin[2] if message
return value.length if skipToken

if value is '(' and prev?[0] is 'IMPORT'
prev[0] = 'DYNAMIC_IMPORT'

if value is '{' and @seenImport
@importSpecifierList = yes
else if @importSpecifierList and value is '}'
Expand Down Expand Up @@ -1356,7 +1359,7 @@ BOOL = ['TRUE', 'FALSE']
# Tokens which could legitimately be invoked or indexed. An opening
# parentheses or bracket following these tokens will be recorded as the start
# of a function invocation or indexing operation.
CALLABLE = ['IDENTIFIER', 'PROPERTY', ')', ']', '?', '@', 'THIS', 'SUPER']
CALLABLE = ['IDENTIFIER', 'PROPERTY', ')', ']', '?', '@', 'THIS', 'SUPER', 'DYNAMIC_IMPORT']
INDEXABLE = CALLABLE.concat [
'NUMBER', 'INFINITY', 'NAN', 'STRING', 'STRING_END', 'REGEX', 'REGEX_END'
'BOOL', 'NULL', 'UNDEFINED', '}', '::'
Expand Down
10 changes: 10 additions & 0 deletions src/nodes.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -2172,6 +2172,16 @@ exports.ExportSpecifier = class ExportSpecifier extends ModuleSpecifier
constructor: (local, exported) ->
super local, exported, 'export'

exports.DynamicImport = class DynamicImport extends Base
compileNode: ->
[@makeCode 'import']

exports.DynamicImportCall = class DynamicImportCall extends Call
compileNode: (o) ->
unless @args.length is 1
@error 'import() requires exactly one argument'
super o

#### Assign

# The **Assign** is used to assign a local variable to value, or to set the
Expand Down
26 changes: 26 additions & 0 deletions test/error_messages.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -1925,3 +1925,29 @@ test "`new.target` is only allowed meta property", ->
-> new.something
^^^^^^^^^^^^^
'''

test "#4834: dynamic import requires exactly one argument", ->
assertErrorFormat '''
import()
''', '''
[stdin]:1:1: error: import() requires exactly one argument
import()
^^^^^^^^
'''

assertErrorFormat '''
import('x', {})
''', '''
[stdin]:1:1: error: import() requires exactly one argument
import('x', {})
^^^^^^^^^^^^^^^
'''

test "#4834: dynamic import requires explicit call parentheses", ->
assertErrorFormat '''
promise = import 'foo'
''', '''
[stdin]:1:23: error: unexpected end of input
GeoffreyBooth marked this conversation as resolved.
Show resolved Hide resolved
promise = import 'foo'
^
'''
21 changes: 21 additions & 0 deletions test/modules.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -920,3 +920,24 @@ test "#4874: backslash `export`", ->
min
} from 'underscore';
"""

test "#4834: dynamic import", ->
eqJS """
import('module').then ->
""",
"""
import('module').then(function() {});
"""

eqJS """
foo = ->
bar = await import('bar')
""",
"""
var foo;

foo = async function() {
var bar;
return bar = (await import('bar'));
};
"""
10 changes: 10 additions & 0 deletions test/object_rest_spread.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -418,3 +418,13 @@ test "#4673: complex destructured object spread variables", ->
g = ({@y...}) ->
eq @y.b, 1
g b: 1

test "#4834: dynamic import can technically be object spread", ->
eqJS """
x = {...import('module')}
""",
"""
var x;

x = {...import('module')};
"""