Skip to content

Commit

Permalink
fix(parser): fixed a few edgy cases for escape keywords
Browse files Browse the repository at this point in the history
  • Loading branch information
KFlash committed Aug 8, 2019
1 parent 17d4649 commit 5165c2e
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 26 deletions.
33 changes: 16 additions & 17 deletions src/lexer/identifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,33 +74,32 @@ export function scanIdentifierSlowCase(
const token: Token | undefined = descKeywordTable[parser.tokenValue];
if (token === void 0) return Token.Identifier;
if (!hasEscape) return token;
if (context & Context.Strict) {
if (token === Token.AwaitKeyword)
return context & (Context.Module | Context.InAwaitContext) ? Token.EscapedReserved : token;

return token === Token.StaticKeyword
if (context & Context.Strict) {
return token === Token.AwaitKeyword && (context & (Context.Module | Context.InAwaitContext)) === 0
? token
: token === Token.StaticKeyword
? Token.EscapedFutureReserved
: (token & Token.FutureReserved) === Token.FutureReserved
? Token.EscapedFutureReserved
: Token.EscapedReserved;
}

if (token === Token.YieldKeyword) {
if (context & Context.AllowEscapedKeyword) return Token.ReservedIfStrict;

return context & Context.InYieldContext ? Token.EscapedReserved : token;
}
if (token === Token.AwaitKeyword) return context & Context.InAwaitContext ? Token.EscapedReserved : token;
if (token === Token.AsyncKeyword) {
if (context & Context.AllowEscapedKeyword) return Token.Identifier;
}

if ((token & Token.FutureReserved) === Token.FutureReserved) {
if (context & Context.Strict) return Token.EscapedFutureReserved;
return token;
return context & Context.AllowEscapedKeyword
? Token.ReservedIfStrict
: context & Context.InYieldContext
? Token.EscapedReserved
: token;
}

return Token.EscapedReserved;
return token === Token.AsyncKeyword && context & Context.AllowEscapedKeyword
? Token.Identifier
: (token & Token.FutureReserved) === Token.FutureReserved
? token
: token === Token.AwaitKeyword && (context & Context.InAwaitContext) === 0
? token
: Token.EscapedReserved;
}
return Token.Identifier;
}
Expand Down
28 changes: 25 additions & 3 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5628,7 +5628,7 @@ export function parseObjectLiteralOrPattern(
* MethodDefinition
*/

nextToken(parser, context | Context.AllowEscapedKeyword);
nextToken(parser, context);

const properties: (ESTree.Property | ESTree.SpreadElement)[] = [];
let destructible: DestructuringKind | AssignmentKind = 0;
Expand Down Expand Up @@ -6548,8 +6548,30 @@ export function parseParenthesizedExpression(
} else if ((token & Token.IsPatternStart) === Token.IsPatternStart) {
expr =
token === Token.LeftBrace
? parseObjectLiteralOrPattern(parser, context, scope, 0, 1, kind, origin, tokenPos, linePos, colPos)
: parseArrayExpressionOrPattern(parser, context, scope, 0, 1, kind, origin, tokenPos, linePos, colPos);
? parseObjectLiteralOrPattern(
parser,
context | Context.AllowEscapedKeyword,
scope,
0,
1,
kind,
origin,
tokenPos,
linePos,
colPos
)
: parseArrayExpressionOrPattern(
parser,
context | Context.AllowEscapedKeyword,
scope,
0,
1,
kind,
origin,
tokenPos,
linePos,
colPos
);

destructible |= parser.destructible;

Expand Down
12 changes: 6 additions & 6 deletions test/parser/miscellaneous/escaped-keyword.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ describe('Miscellaneous - Escaped keywords', () => {
'aw\\u0061it: 1;',
'function *a(){({yi\\u0065ld: 0})}',
'\\u0061sync',
// '({i\\u0066: 0})',
'l\\u0065t\na',
'l\\u0065t',
`function a() {
\\u0061sync
p => {}
Expand Down Expand Up @@ -181,15 +184,13 @@ describe('Miscellaneous - Escaped keywords', () => {
['var gen = async function *g() { yi\\u0065ld: ; };', Context.None],
['function *gen() { yi\\u0065ld: ; }', Context.None],
['(function *gen() { yi\\u0065ld: ; })', Context.None],

['i\\u0066 (0)', Context.None],
['var i\\u0066', Context.None],
['export {a \\u0061s b} from "";', Context.Strict | Context.Module],
['export {} fr\\u006fm "";', Context.Strict | Context.Module],
['for (a o\\u0066 b);', Context.None],
['class a { st\\u0061tic m(){} }', Context.None],
['var \\u{64}\\u{6f} = 123;', Context.None],
['(async function() { aw\\u0061it x })', Context.None],
['(\\u0061sync function() { await x })', Context.None],
['(\\u0061sync () => { await x })', Context.None],
['\\u0061sync x => { await x }', Context.None],
Expand All @@ -200,7 +201,7 @@ describe('Miscellaneous - Escaped keywords', () => {
['({ \\u0061sync x() { await x } })', Context.None],
['for (x \\u006ff y) {}', Context.None],
['(async function() { aw\\u0061it x })', Context.None],
// ['(function*() { var y\\u0069eld = 1; })()', Context.None],
['(function*() { var y\\u0069eld = 1; })()', Context.None],
['(function*() { y\\u0069eld 1; })()', Context.None],
['w\\u0069th (this.scope) { }', Context.None],
['wh\\u0069le (true) { }', Context.None],
Expand Down Expand Up @@ -230,9 +231,8 @@ describe('Miscellaneous - Escaped keywords', () => {
['var C = class aw\\u0061it {};', Context.Strict | Context.Module],
['var C = class aw\\u0061it {};', Context.Strict | Context.Module],
['class aw\\u0061it {}', Context.Strict | Context.Module],

['var gen = async function *() { var yi\\u0065ld; };', Context.None],
['var obj = { *method() { void yi\\u0065ld; } };', Context.None]
// ['var gen = function *g() { yi\\u0065ld: ; };', Context.None],
['var obj = { *method() { void yi\\u0065ld; } };', Context.None],
['var gen = function *g() { yi\\u0065ld: ; };', Context.None]
]);
});

0 comments on commit 5165c2e

Please sign in to comment.