Skip to content

Commit

Permalink
feat(parser): support top-level await
Browse files Browse the repository at this point in the history
closes #186
  • Loading branch information
3cp committed Jul 11, 2021
1 parent a915866 commit 7b2a5bd
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 17 deletions.
1 change: 0 additions & 1 deletion src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,6 @@ export const errorMessages: {
[Errors.InvalidDefaultImport]: "Only '*' or '{...}' can be imported after default",
[Errors.TrailingDecorators]: 'Trailing decorator may be followed by method',
[Errors.GeneratorConstructor]: "Decorators can't be used with a constructor",
[Errors.AwaitOrYieldIdentInModule]: "'%0' may not be used as an identifier in this context",
[Errors.HtmlCommentInWebCompat]: 'HTML comments are only allowed with web compatibility (Annex B)',
[Errors.StrictInvalidLetInExprPos]: "The identifier 'let' must not be in expression position in strict mode",
[Errors.NotAssignableLetArgs]: 'Cannot assign to `eval` and `arguments` in strict mode',
Expand Down
5 changes: 2 additions & 3 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3593,7 +3593,7 @@ export function parseAwaitExpression(
column: number
): ESTree.IdentifierOrExpression | ESTree.AwaitExpression {
if (inGroup) parser.destructible |= DestructuringKind.Await;
if (context & Context.InAwaitContext) {
if (context & Context.InAwaitContext || (context & Context.Module && context & Context.InGlobal)) {
if (inNew) report(parser, Errors.Unexpected);

if (context & Context.InArgumentList) {
Expand Down Expand Up @@ -3623,8 +3623,7 @@ export function parseAwaitExpression(
});
}

if (context & Context.Module) report(parser, Errors.AwaitOrYieldIdentInModule, 'Await');

if (context & Context.Module) report(parser, Errors.AwaitOutsideAsync);
return parseIdentifierOrArrow(parser, context, start, line, column);
}

Expand Down
2 changes: 1 addition & 1 deletion test/parser/expressions/async-arrow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ describe('Expressions - Async arrow', () => {
['(async function() { } => 1)', Context.None],
['async(...a,) => b', Context.None],
['async(...a, b) => b', Context.None],
['async (a = b => await (0)) => {}', Context.Strict | Context.Module],
// ['async (a = b => await (0)) => {}', Context.Strict | Context.Module],
['async(...a,) => b', Context.None],
['async(...a, b) => b', Context.None],
["var asyncFn = async () => var await = 'test';", Context.None],
Expand Down
46 changes: 44 additions & 2 deletions test/parser/expressions/await.ts
Original file line number Diff line number Diff line change
Expand Up @@ -389,10 +389,8 @@ describe('Expressions - Await', () => {
}`,
Context.None
],

['var await = 5;', Context.Module | Context.Strict],
['await;', Context.Module | Context.Strict],
['await 5;', Context.Module | Context.Strict],
['function f() { await 5; }', Context.Module | Context.Strict],
['() => { await 5; }', Context.Module | Context.Strict],
['export var await;', Context.Module | Context.Strict],
Expand Down Expand Up @@ -797,6 +795,50 @@ describe('Expressions - Await', () => {
}

pass('Expressions - Await (pass)', [
[
'await f();',
Context.Module,
{
type: 'Program',
sourceType: 'module',
body: [
{
type: 'ExpressionStatement',
expression: {
type: 'AwaitExpression',
argument: {
type: 'CallExpression',
callee: {
type: 'Identifier',
name: 'f'
},
arguments: []
}
}
}
]
}
],
[
'await 5;',
Context.Module | Context.Strict,
{
type: 'Program',
sourceType: 'module',
body: [
{
type: 'ExpressionStatement',
expression: {
type: 'AwaitExpression',
argument: {
type: 'Literal',
value: 5
}
}
}
]
}
],
[
'async function f(){ if (await \n x) {} }',
Context.None,
Expand Down
20 changes: 10 additions & 10 deletions test/parser/miscellaneous/failure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ describe('Miscellaneous - Failure', () => {
'arguments => {"use strict";}',
'async arguments => {"use strict";}',
'function f(x=(yield z)=y){}',
'`a${await foo}d`',
// '`a${await foo}d`',
`for (a+b in c) d;`,
`for (a+b of c) d;`,
'for ({x = y} ;;) {}',
Expand Down Expand Up @@ -268,7 +268,7 @@ describe('Miscellaneous - Failure', () => {
`for (let + in y);`,
`for (let() in y);`,
`"use strict"; for (let + of y);`,
`async ({x} = await bar);`,
// `async ({x} = await bar);`,
`async ({} + 1) => x;`,
`({x: y}.length) => x;`,
`({x = y}.z) => obj`,
Expand Down Expand Up @@ -321,7 +321,7 @@ describe('Miscellaneous - Failure', () => {
`({x} = await bar) => {}`,
`async ({x} = await bar) => {}`,
`let z = async ({x} = await bar) => {}`,
`async ({x} = await bar);`,
// `async ({x} = await bar);`,
'function foo(a, ...b, c) { }',
'function foo(a, ...b, ) { }',
'function foo(a, ...[b], ) { }',
Expand Down Expand Up @@ -674,7 +674,7 @@ describe('Miscellaneous - Failure', () => {
'for (var [ a ]; a; ) {}',
'var { a, b, c };',
'for (var { a, b, c }; a && b && c; ) {}',
'let b = async () => []; for (a in await b());',
// 'let b = async () => []; for (a in await b());',
'async ((a)) => {}',
'({ async\nf(){} })',
'for(let.a of 0);',
Expand Down Expand Up @@ -838,7 +838,7 @@ describe('Miscellaneous - Failure', () => {
'"use strict";\n"\\011"',
'"use strict"; "\\08"',
'"use strict"; "\\09"',
'await ~123',
// 'await ~123',
'async () => class await {}',
'{_ => {}/123/g;}',
'({a}) = 1;',
Expand Down Expand Up @@ -942,7 +942,7 @@ describe('Miscellaneous - Failure', () => {
'throw',
'if(false)',
'async ((x, y, z)) => 0 ',
'const {params:{callFrames:[{callFrameId}]}} = await Protocol.Debugger.oncePaused();',
// 'const {params:{callFrames:[{callFrameId}]}} = await Protocol.Debugger.oncePaused();',
'‿ = 10',
'if(true) let a = 1;',
'/*',
Expand Down Expand Up @@ -1088,7 +1088,7 @@ describe('Miscellaneous - Failure', () => {
'if (true) function* g() { }',
'if (false) ; else function* g() { }',
'foo: function* g() { }',
'`x${await x}y`',
// '`x${await x}y`',
`async () \n => x`,
`(async () \n => x)`,
`1.a`,
Expand Down Expand Up @@ -1203,7 +1203,7 @@ describe('Miscellaneous - Failure', () => {
`({async foo() { var await }})`,
`class A {async get foo() { }}`,
`class A {async foo() { return {await} }}`,
`await a`,
// `await a`,
`async () => await`,
`async function foo(a = await b) {}`,
`([a.a]) => 42`,
Expand Down Expand Up @@ -1300,7 +1300,7 @@ describe('Miscellaneous - Failure', () => {
'({async foo(a = await b) {}})',
'(class {async foo(a = await b) {}})',
'async function foo(a = class extends (await b) {}) {}',
'await a',
// 'await a',
'async await => 1',
'async ([await]) => 1',
'async a\n=> a',
Expand Down Expand Up @@ -2106,7 +2106,7 @@ describe('Miscellaneous - Failure', () => {
'class x {async *f(foo = [{m: t(await bar)}]){}}',
'function f(foo = [{m: t(+await bar)}]){}',
'async function f(foo = [{m: t(+await bar)}]){}',
'async ([x] = await bar);',
// 'async ([x] = await bar);',
'function *a({yield}){}',
'function *a({yield = 0}){}',
'(foo = +await bar) => {}',
Expand Down

0 comments on commit 7b2a5bd

Please sign in to comment.