Skip to content

Commit c4f1fe7

Browse files
authored
Support top-level await (#5371)
* Support top-level await * Remove code duplication * Avoid use of trimEnd so tests pass in old Node * Proposed rewrite of tests * startsWith tests; revert eqJS * build:browser
1 parent b0946c3 commit c4f1fe7

File tree

8 files changed

+45
-19
lines changed

8 files changed

+45
-19
lines changed

docs/v2/browser-compiler-legacy/coffeescript.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/v2/browser-compiler-modern/coffeescript.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/coffeescript-browser-compiler-legacy/coffeescript.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/coffeescript-browser-compiler-modern/coffeescript.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/coffeescript/nodes.js

Lines changed: 15 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/nodes.coffee

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,8 @@ exports.Root = class Root extends Base
513513
constructor: (@body) ->
514514
super()
515515

516+
@isAsync = (new Code [], @body).isAsync
517+
516518
children: ['body']
517519

518520
# Wrap everything in a safety closure, unless requested not to. It would be
@@ -525,7 +527,13 @@ exports.Root = class Root extends Base
525527
@initializeScope o
526528
fragments = @body.compileRoot o
527529
return fragments if o.bare
528-
[].concat @makeCode("(function() {\n"), fragments, @makeCode("\n}).call(this);\n")
530+
parts = []
531+
parts.push @makeCode '('
532+
parts.push @makeCode 'async ' if @isAsync
533+
parts.push @makeCode 'function() {\n'
534+
parts.push ...fragments
535+
parts.push @makeCode '\n}).call(this);\n'
536+
[].concat ...parts
529537

530538
initializeScope: (o) ->
531539
o.scope = new Scope null, @body, null, o.referencedVars ? []
@@ -4782,7 +4790,7 @@ exports.Op = class Op extends Base
47824790
compileContinuation: (o) ->
47834791
parts = []
47844792
op = @operator
4785-
@checkContinuation o
4793+
@checkContinuation o unless @isAwait()
47864794
if 'expression' in Object.keys(@first) and not (@first instanceof Throw)
47874795
parts.push @first.expression.compileToFragments o, LEVEL_OP if @first.expression?
47884796
else
@@ -4817,7 +4825,7 @@ exports.Op = class Op extends Base
48174825
@error 'delete operand may not be argument or var'
48184826

48194827
astNode: (o) ->
4820-
@checkContinuation o if @isYield() or @isAwait()
4828+
@checkContinuation o if @isYield()
48214829
@checkDeleteOperand o
48224830
super o
48234831

test/async.coffee

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,3 +204,17 @@ test "#3199: await multiline implicit object", ->
204204
type: 'a'
205205
msg: 'b'
206206
eq undefined, y
207+
208+
test "top-level await", ->
209+
eqJS 'await null', 'await null;'
210+
211+
test "top-level wrapper has correct async attribute", ->
212+
starts = (code, prefix) ->
213+
compiled = CoffeeScript.compile code
214+
unless compiled.startsWith prefix
215+
fail """Expected generated JavaScript to start with:
216+
#{reset}#{prefix}#{red}
217+
but instead it was:
218+
#{reset}#{compiled}#{red}"""
219+
starts 'await null', '(async function'
220+
starts 'do -> await null', '(function'

test/error_messages.coffee

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1257,14 +1257,7 @@ test "CoffeeScript keywords cannot be used as local names in import list aliases
12571257
^^^^^^
12581258
'''
12591259

1260-
test "cannot have `await` outside a function", ->
1261-
assertErrorFormat '''
1262-
await 1
1263-
''', '''
1264-
[stdin]:1:1: error: await can only occur inside functions
1265-
await 1
1266-
^^^^^^^
1267-
'''
1260+
test "cannot have `await return` outside a function", ->
12681261
assertErrorFormat '''
12691262
await return
12701263
''', '''

0 commit comments

Comments
 (0)