Skip to content

Commit

Permalink
fix(parser): fixed __proto__ edge cases
Browse files Browse the repository at this point in the history
  • Loading branch information
KFlash committed May 8, 2019
1 parent f7f339e commit 91cdefd
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 18 deletions.
14 changes: 3 additions & 11 deletions src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,23 +279,15 @@ export function validateIdentifier(parser: ParserState, context: Context, type:

export function isStrictReservedWord(parser: ParserState, context: Context, t: Token): boolean {
if (t === Token.AwaitKeyword) {
if (context & (Context.InAwaitContext | Context.Module)) {
report(parser, Errors.AwaitOutsideAsync);
}
if (context & (Context.InAwaitContext | Context.Module)) report(parser, Errors.AwaitOutsideAsync);
parser.flags |= Flags.SeenAwait;
}

if (t === Token.YieldKeyword) {
if (context & (Context.InYieldContext | Context.Strict)) {
report(parser, Errors.DisallowedInContext, 'yield');
}
}
if (t === Token.GetKeyword) return true;
if (t === Token.SetKeyword) return true;
if (t === Token.YieldKeyword && context & Context.InYieldContext) report(parser, Errors.DisallowedInContext, 'yield');

return (
(t & Token.Reserved) === Token.Reserved ||
(t & Token.FutureReserved) === Token.FutureReserved ||
t == Token.EscapedReserved ||
t == Token.EscapedFutureReserved
);
}
Expand Down
19 changes: 13 additions & 6 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3002,12 +3002,14 @@ function parseArrayLiteral(parser: ParserState, context: Context, skipInitialize
*/
const expr = parseArrayExpressionOrPattern(parser, context, skipInitializer, BindingType.None);

if (parser.destructible & DestructuringKind.Required) {
report(parser, Errors.InvalidShorthandPropInit);
}
if (context & Context.OptionsWebCompat && parser.destructible & DestructuringKind.SeenProto) {
report(parser, Errors.DuplicateProto);
}

if (parser.destructible & DestructuringKind.Required) {
report(parser, Errors.InvalidShorthandPropInit);
}

parser.assignable =
parser.destructible & DestructuringKind.NotDestructible ? AssignmentKind.NotAssignable : AssignmentKind.Assignable;
return expr as ESTree.ArrayExpression;
Expand Down Expand Up @@ -3488,13 +3490,14 @@ function parseObjectLiteral(parser: ParserState, context: Context, skipInitializ
*/
const expr = parseObjectLiteralOrPattern(parser, context, skipInitializer, BindingType.None);

if (parser.destructible & DestructuringKind.Required) {
report(parser, Errors.InvalidShorthandPropInit);
}
if (context & Context.OptionsWebCompat && parser.destructible & DestructuringKind.SeenProto) {
report(parser, Errors.DuplicateProto);
}

if (parser.destructible & DestructuringKind.Required) {
report(parser, Errors.InvalidShorthandPropInit);
}

parser.assignable =
parser.destructible & DestructuringKind.NotDestructible ? AssignmentKind.NotAssignable : AssignmentKind.Assignable;

Expand Down Expand Up @@ -4271,6 +4274,8 @@ export function parseParenthesizedExpression(parser: ParserState, context: Conte
return parseArrowFunctionExpression(parser, context, toplevelComma ? expressions : [expr], /* isAsync */ 0);
} else if (destructible & DestructuringKind.Required) {
report(parser, Errors.InvalidShorthandPropInit);
} else if (context & Context.OptionsWebCompat && parser.destructible & DestructuringKind.SeenProto) {
report(parser, Errors.DuplicateProto);
}

parser.destructible = destructible;
Expand Down Expand Up @@ -4714,6 +4719,8 @@ export function parseAsyncArrowOrCallExpression(
return parseArrowFunctionExpression(parser, context, params as any, /* isAsync */ 1) as any;
} else if (destructible & DestructuringKind.Required) {
report(parser, Errors.InvalidShorthandPropInit);
} else if (context & Context.OptionsWebCompat && parser.destructible & DestructuringKind.SeenProto) {
report(parser, Errors.DuplicateProto);
}

return {
Expand Down
1 change: 1 addition & 0 deletions test/parser/expressions/class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,7 @@ describe('Expressions - Class', () => {
['function () { class A { constructor() { A = 0; } }; new A(); }', Context.None],
['(class { adf&/()})', Context.None],
['(class { adf &/()})', Context.None],
//['class aw\\u0061it {}', Context.Strict | Context.Module],
['(class b {)', Context.None],
['(class b )', Context.None],
['(class b {-})', Context.None],
Expand Down
70 changes: 70 additions & 0 deletions test/parser/expressions/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1084,6 +1084,13 @@ describe('Expressions - Object', () => {
['var x = 012;', Context.Strict],
['({b}) = b;', Context.None],
['([b]) = b;', Context.None],
['({ __proto__: null, other: null, "__proto__": null });', Context.OptionsWebCompat],
['foo({ __proto__: null, other: null, "__proto__": null });', Context.OptionsWebCompat],
['({ __proto__: null, other: null, "__proto__": null }) => foo;', Context.OptionsWebCompat],
['async ({ __proto__: null, other: null, "__proto__": null }) => foo;', Context.OptionsWebCompat],
['({ __proto__: null, other: null, "__proto__": null });', Context.OptionsWebCompat | Context.Strict],
['[{ __proto__: null, other: null, "__proto__": null }];', Context.OptionsWebCompat],
['x = { __proto__: null, other: null, "__proto__": null };', Context.OptionsWebCompat],
['[...a, ] = b', Context.None],
['obj = {x = 0}', Context.None],
['({ obj:20 }) = 42', Context.None],
Expand Down Expand Up @@ -1447,6 +1454,69 @@ describe('Expressions - Object', () => {
]
}
],
[
'({ __proto__: null, other: null, "__proto__": null });',
Context.None,
{
body: [
{
expression: {
properties: [
{
computed: false,
key: {
name: '__proto__',
type: 'Identifier'
},
kind: 'init',
method: false,
shorthand: false,
type: 'Property',
value: {
type: 'Literal',
value: null
}
},
{
computed: false,
key: {
name: 'other',
type: 'Identifier'
},
kind: 'init',
method: false,
shorthand: false,
type: 'Property',
value: {
type: 'Literal',
value: null
}
},
{
computed: false,
key: {
type: 'Literal',
value: '__proto__'
},
kind: 'init',
method: false,
shorthand: false,
type: 'Property',
value: {
type: 'Literal',
value: null
}
}
],
type: 'ObjectExpression'
},
type: 'ExpressionStatement'
}
],
sourceType: 'script',
type: 'Program'
}
],
[
'({...x = y, y})',
Context.None,
Expand Down
1 change: 0 additions & 1 deletion test/parser/miscellaneous/grammar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1861,7 +1861,6 @@ describe('Miscellaneous - Cover grammar', () => {
'({...x})',
'({x, ...y})',
'({[x] : z, ...y})',
'({ __proto__: x, __proto__: y, ...z})',
'([{x:x, y:y}, [a,b,c]])',
'([{x:x, y:y}, [a,b,c]])',
'([{x:x, y:y, ...z}, [a,b,c]] = {})',
Expand Down

0 comments on commit 91cdefd

Please sign in to comment.