Skip to content

Commit

Permalink
fix(parser): support top level for-await in module context
Browse files Browse the repository at this point in the history
related to #214
  • Loading branch information
3cp committed May 28, 2022
1 parent fd54074 commit 69761bf
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 5 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ jobs:

strategy:
matrix:
node-version: [12.x, 14.x, 16.x]
node-version: [12, 14, 16, 18]
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v2
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
- run: npm i
Expand Down
4 changes: 3 additions & 1 deletion src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2163,7 +2163,9 @@ export function parseForStatement(
): ESTree.ForStatement | ESTree.ForInStatement | ESTree.ForOfStatement {
nextToken(parser, context);

const forAwait = (context & Context.InAwaitContext) > 0 && consumeOpt(parser, context, Token.AwaitKeyword);
const forAwait =
((context & Context.InAwaitContext) > 0 || ((context & Context.Module) > 0 && (context & Context.InGlobal) > 0)) &&
consumeOpt(parser, context, Token.AwaitKeyword);

consume(parser, context | Context.AllowRegExp, Token.LeftParen);

Expand Down
3 changes: 2 additions & 1 deletion test/parser/miscellaneous/regex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ describe('Miscellaneous - Regular expressions', () => {
['/(?<𝟐>foo)/', Context.OptionsNext],
['/(?<\\uD835\\uDFD0rest>foo)/', Context.OptionsWebCompat],
['/(?<abc\\uD835\\uDFD0def>foo\\k<abc\\uD835def>)/', Context.OptionsNext | Context.Module | Context.Strict],
['/(?<\\ud87e\\udddfrest>foo)/', Context.OptionsNext | Context.OptionsWebCompat],
// Nodejs v18 now accepts unicode in capture group name
// ['/(?<\\ud87e\\udddfrest>foo)/', Context.OptionsNext | Context.OptionsWebCompat],
[
`function* f(){ yield
/foo }`,
Expand Down
14 changes: 14 additions & 0 deletions test/parser/statements/for-await-of.ts
Original file line number Diff line number Diff line change
Expand Up @@ -529,4 +529,18 @@ describe('Statements - For await of', () => {
});
});
}

it('accepts top level for await in module context', () => {
t.doesNotThrow(() => {
parseSource('for await (const a of b) {}', undefined, Context.Module);
});

t.throws(() => {
parseSource('for await (const a of b) {}', undefined, Context.None);
});

t.throws(() => {
parseSource('function c() { for await (const a of b) {} }', undefined, Context.Module);
});
});
});

0 comments on commit 69761bf

Please sign in to comment.