Skip to content

Commit 37c6361

Browse files
committed
fix(parser): allow top level await in expressions
Such as foo[await 1], foo(await 1). closes #212
1 parent 6a386a2 commit 37c6361

4 files changed

Lines changed: 63 additions & 4 deletions

File tree

src/parser.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3859,12 +3859,12 @@ export function parseMemberOrUpdateExpression(
38593859
if ((parser.token & Token.IsUpdateOp) === Token.IsUpdateOp && (parser.flags & Flags.NewLine) < 1) {
38603860
expr = parseUpdateExpression(parser, context, expr, start, line, column);
38613861
} else if ((parser.token & Token.IsMemberOrCallExpression) === Token.IsMemberOrCallExpression) {
3862-
context = (context | Context.DisallowIn | Context.InGlobal) ^ (Context.DisallowIn | Context.InGlobal);
3862+
context = (context | Context.DisallowIn) ^ Context.DisallowIn;
38633863

38643864
switch (parser.token) {
38653865
/* Property */
38663866
case Token.Period: {
3867-
nextToken(parser, context | Context.AllowEscapedKeyword);
3867+
nextToken(parser, (context | Context.AllowEscapedKeyword | Context.InGlobal) ^ Context.InGlobal);
38683868

38693869
parser.assignable = AssignmentKind.Assignable;
38703870

@@ -3940,7 +3940,7 @@ export function parseMemberOrUpdateExpression(
39403940

39413941
/* Optional chaining */
39423942
case Token.QuestionMarkPeriod: {
3943-
nextToken(parser, context); // skips: '?.'
3943+
nextToken(parser, (context | Context.AllowEscapedKeyword | Context.InGlobal) ^ Context.InGlobal); // skips: '?.'
39443944
parser.flags |= Flags.HasOptionalChaining;
39453945
parser.assignable = AssignmentKind.CannotAssign;
39463946
expr = parseOptionalChain(parser, context, expr, start, line, column);

test/parser/expressions/await.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3282,6 +3282,63 @@ describe('Expressions - Await', () => {
32823282
}
32833283
]
32843284
}
3285+
],
3286+
[
3287+
'foo[await 1]',
3288+
Context.Module,
3289+
{
3290+
type: 'Program',
3291+
sourceType: 'module',
3292+
body: [
3293+
{
3294+
type: 'ExpressionStatement',
3295+
expression: {
3296+
type: 'MemberExpression',
3297+
object: {
3298+
type: 'Identifier',
3299+
name: 'foo'
3300+
},
3301+
computed: true,
3302+
property: {
3303+
type: 'AwaitExpression',
3304+
argument: {
3305+
type: 'Literal',
3306+
value: 1
3307+
}
3308+
}
3309+
}
3310+
}
3311+
]
3312+
}
3313+
],
3314+
[
3315+
'foo(await bar)',
3316+
Context.Module,
3317+
{
3318+
type: 'Program',
3319+
sourceType: 'module',
3320+
body: [
3321+
{
3322+
type: 'ExpressionStatement',
3323+
expression: {
3324+
type: 'CallExpression',
3325+
callee: {
3326+
type: 'Identifier',
3327+
name: 'foo'
3328+
},
3329+
arguments: [
3330+
{
3331+
type: 'AwaitExpression',
3332+
argument: {
3333+
type: 'Identifier',
3334+
name: 'bar'
3335+
}
3336+
}
3337+
]
3338+
}
3339+
}
3340+
]
3341+
}
32853342
]
32863343
]);
32873344
});

test/parser/expressions/optional-chaining.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import { parseSource } from '../../../src/parser';
66
describe('Optional chaining', () => {
77
for (const arg of [
88
'func?.()',
9-
'obj?.prop ',
9+
'obj?.prop',
10+
'obj?.def\\u{61}ult',
1011
'func?.(...args)',
1112
'a?.[x]',
1213
'a?.()',

test/parser/miscellaneous/escaped-keyword.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ describe('Miscellaneous - Escaped keywords', () => {
1111
'(\\u0069nterface = 1);',
1212
'({ def\\u0061ult: 0 })',
1313
'({ def\\u{61}ult: 0 })',
14+
'foo = {}; foo?.def\\u{61}ult + 3;',
1415
'foo = {}; foo.def\\u{61}ult = 3;',
1516
'var int\\u0065rface = 1;',
1617
'var { int\\u0065rface } = {};',

0 commit comments

Comments
 (0)