Skip to content

Commit

Permalink
fix(parser): fixed issue with private field didn't pass the 'kind' state
Browse files Browse the repository at this point in the history
  • Loading branch information
KFlash committed May 11, 2019
1 parent c08d907 commit bd6ec68
Show file tree
Hide file tree
Showing 5 changed files with 904 additions and 67 deletions.
16 changes: 8 additions & 8 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4365,9 +4365,6 @@ export function parseParenthesizedExpression(parser: ParserState, context: Conte
} else if (destructible & DestructuringKind.Required) {
report(parser, Errors.InvalidShorthandPropInit);
}
if (context & Context.OptionsWebCompat && parser.destructible & DestructuringKind.SeenProto) {
report(parser, Errors.DuplicateProto);
}

parser.destructible = destructible;

Expand Down Expand Up @@ -4821,8 +4818,6 @@ 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 Expand Up @@ -5167,7 +5162,6 @@ function parseClassElementList(
} else if (token === Token.Multiply) {
kind |= PropertyKind.Generator;
nextToken(parser, context);
if (parser.token === Token.LeftParen) report(parser, Errors.Unexpected);
} else if (context & Context.OptionsNext && parser.token === Token.PrivateField) {
return parsePrivateFieldsOrMethod(parser, context, decorators, kind);
} else if (context & Context.OptionsNext && parser.token === Token.RightBrace) {
Expand All @@ -5179,6 +5173,11 @@ function parseClassElementList(
if (kind & PropertyKind.Computed) {
key = parseComputedPropertyName(parser, context);
} else if (kind & (PropertyKind.Generator | PropertyKind.Async | PropertyKind.Getter | PropertyKind.Setter)) {
if (kind & PropertyKind.Generator) {
if (context & Context.OptionsNext && parser.token === Token.PrivateField) {
return parsePrivateFieldsOrMethod(parser, context, decorators, kind);
} else if (parser.token === Token.LeftParen) report(parser, Errors.Unexpected);
}
if (parser.token & Token.IsIdentifier) {
key = parseIdentifier(parser, context);
} else if ((parser.token & Token.IsStringOrNumber) === Token.IsStringOrNumber) {
Expand Down Expand Up @@ -5287,6 +5286,7 @@ export function parseFieldDefinition(
): any {
let value: ESTree.Expression | null = null;
if (state & PropertyKind.Generator) report(parser, Errors.Unexpected);
// if (parser.tokenValue === 'prototype') report(parser, Errors.Unexpected); // static ptype
if (parser.token === Token.Assign) {
nextToken(parser, context | Context.AllowRegExp);
if ((parser.token & Token.IsEvalOrArguments) === Token.IsEvalOrArguments)
Expand Down Expand Up @@ -5320,7 +5320,7 @@ function parsePrivateFieldsOrMethod(
state: PropertyKind
): any {
let value: ESTree.Expression | null = null;
if (state & PropertyKind.Generator) report(parser, Errors.Unexpected);

const key = parsePrivateName(parser, context);

if (parser.token === Token.LeftParen) {
Expand All @@ -5337,7 +5337,7 @@ function parsePrivateFieldsOrMethod(
static: (state & PropertyKind.Static) > 0,
computed: (state & PropertyKind.Computed) > 0,
key,
value: parseMethodDefinition(parser, context, PropertyKind.None)
value: parseMethodDefinition(parser, context, state)
};
}

Expand Down
6 changes: 5 additions & 1 deletion test/parser/expressions/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,11 @@ describe('Expressions - Array', () => {
'[(a) = (b)]',
'[(x) = y = (z)]',
'[(x) = y = (z) => (a)]',
'[(x) => y = (z)]'
'[(x) => y = (z)]',
'[(x), y = x] = x;',
'[(x), y] = x;',
'[(a), ] = x;',
'([(x), y] = x);'
]) {
it(`${arg}`, () => {
t.doesNotThrow(() => {
Expand Down
120 changes: 118 additions & 2 deletions test/parser/expressions/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1100,11 +1100,9 @@ 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],
Expand Down Expand Up @@ -1528,6 +1526,124 @@ describe('Expressions - Object', () => {
]
}
],
[
'var a = { __proto__: { abc: 123 } };',
Context.None,
{
type: 'Program',
sourceType: 'script',
body: [
{
type: 'VariableDeclaration',
kind: 'var',
declarations: [
{
type: 'VariableDeclarator',
init: {
type: 'ObjectExpression',
properties: [
{
type: 'Property',
key: {
type: 'Identifier',
name: '__proto__'
},
value: {
type: 'ObjectExpression',
properties: [
{
type: 'Property',
key: {
type: 'Identifier',
name: 'abc'
},
value: {
type: 'Literal',
value: 123
},
kind: 'init',
computed: false,
method: false,
shorthand: false
}
]
},
kind: 'init',
computed: false,
method: false,
shorthand: false
}
]
},
id: {
type: 'Identifier',
name: 'a'
}
}
]
}
]
}
],
[
'var b = { ["__proto__"]: { abc: 123 }};',
Context.None,
{
type: 'Program',
sourceType: 'script',
body: [
{
type: 'VariableDeclaration',
kind: 'var',
declarations: [
{
type: 'VariableDeclarator',
init: {
type: 'ObjectExpression',
properties: [
{
type: 'Property',
key: {
type: 'Literal',
value: '__proto__'
},
value: {
type: 'ObjectExpression',
properties: [
{
type: 'Property',
key: {
type: 'Identifier',
name: 'abc'
},
value: {
type: 'Literal',
value: 123
},
kind: 'init',
computed: false,
method: false,
shorthand: false
}
]
},
kind: 'init',
computed: true,
method: false,
shorthand: false
}
]
},
id: {
type: 'Identifier',
name: 'b'
}
}
]
}
]
}
],
[
'({ __proto__: null, other: null, "__proto__": null });',
Context.None,
Expand Down
Loading

0 comments on commit bd6ec68

Please sign in to comment.