Skip to content

Commit

Permalink
Fix 'super' usage in arrow functions
Browse files Browse the repository at this point in the history
  • Loading branch information
ypapouski committed May 22, 2024
1 parent b4617e2 commit 7801336
Show file tree
Hide file tree
Showing 4 changed files with 577 additions and 6 deletions.
20 changes: 17 additions & 3 deletions dist/meriyah.umd.js
Original file line number Diff line number Diff line change
Expand Up @@ -6266,17 +6266,19 @@
case 67174411: {
if ((context & 524288) === 0)
report(parser, 26);
if (context & 16384)
if (context & 16384 && !(context & 33554432)) {
report(parser, 27);
}
parser.assignable = 2;
break;
}
case 69271571:
case 67108877: {
if ((context & 262144) === 0)
report(parser, 27);
if (context & 16384)
if (context & 16384 && !(context & 33554432)) {
report(parser, 27);
}
parser.assignable = 1;
break;
}
Expand Down Expand Up @@ -6313,6 +6315,11 @@
switch (parser.token) {
case 67108877: {
nextToken(parser, (context | 1073741824 | 8192) ^ 8192);
if (context & 16384 &&
(parser.token & 131) === 131 &&
parser.tokenValue === 'super') {
report(parser, 27);
}
parser.assignable = 1;
const property = parsePropertyOrPrivatePropertyName(parser, context | 65536);
expr = finishNode(parser, context, start, line, column, {
Expand Down Expand Up @@ -8023,7 +8030,7 @@
reportScopeError(scope.scopeError);
}
if (expression) {
body = parseExpression(parser, context, 1, 0, 0, parser.tokenPos, parser.linePos, parser.colPos);
body = parseExpression(parser, context & 16384 ? context | 33554432 : context, 1, 0, 0, parser.tokenPos, parser.linePos, parser.colPos);
}
else {
if (scope)
Expand Down Expand Up @@ -8695,6 +8702,13 @@
const { tokenPos, linePos, colPos } = parser;
if (parser.token === 537079928)
report(parser, 116);
const modifierFlags = (state & 64) === 0
? 31981568
: 14680064;
context =
((context | modifierFlags) ^ modifierFlags) |
((state & 88) << 18) |
100925440;
value = parsePrimaryExpression(parser, context | 16384, 2, 0, 1, 0, 0, 1, tokenPos, linePos, colPos);
if ((parser.token & 1073741824) !== 1073741824 ||
(parser.token & 4194304) === 4194304) {
Expand Down
37 changes: 34 additions & 3 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3916,7 +3916,9 @@ export function parseSuperExpression(
case Token.LeftParen: {
// The super property has to be within a class constructor
if ((context & Context.SuperCall) === 0) report(parser, Errors.SuperNoConstructor);
if (context & Context.InClass) report(parser, Errors.InvalidSuperProperty);
if (context & Context.InClass && !(context & Context.InMethod)) {
report(parser, Errors.InvalidSuperProperty);
}
parser.assignable = AssignmentKind.CannotAssign;
break;
}
Expand All @@ -3925,7 +3927,9 @@ export function parseSuperExpression(
// new super() is never allowed.
// super() is only allowed in derived constructor
if ((context & Context.SuperProperty) === 0) report(parser, Errors.InvalidSuperProperty);
if (context & Context.InClass) report(parser, Errors.InvalidSuperProperty);
if (context & Context.InClass && !(context & Context.InMethod)) {
report(parser, Errors.InvalidSuperProperty);
}
parser.assignable = AssignmentKind.Assignable;
break;
}
Expand Down Expand Up @@ -4041,6 +4045,14 @@ export function parseMemberOrUpdateExpression(
case Token.Period: {
nextToken(parser, (context | Context.AllowEscapedKeyword | Context.InGlobal) ^ Context.InGlobal);

if (
context & Context.InClass &&
(parser.token & Token.PrivateField) === Token.PrivateField &&
parser.tokenValue === 'super'
) {
report(parser, Errors.InvalidSuperProperty);
}

parser.assignable = AssignmentKind.Assignable;

const property = parsePropertyOrPrivatePropertyName(parser, context | Context.TaggedTemplate);
Expand Down Expand Up @@ -7347,7 +7359,16 @@ export function parseArrowFunctionExpression(

if (expression) {
// Single-expression body
body = parseExpression(parser, context, 1, 0, 0, parser.tokenPos, parser.linePos, parser.colPos);
body = parseExpression(
parser,
context & Context.InClass ? context | Context.InMethod : context,
1,
0,
0,
parser.tokenPos,
parser.linePos,
parser.colPos
);
} else {
if (scope) scope = addChildScope(scope, ScopeKind.FunctionBody);

Expand Down Expand Up @@ -8704,6 +8725,16 @@ export function parsePropertyDefinition(

if (parser.token === Token.Arguments) report(parser, Errors.StrictEvalArguments);

const modifierFlags =
(state & PropertyKind.Constructor) === 0
? 0b0000001111010000000_0000_00000000
: 0b0000000111000000000_0000_00000000;

context =
((context | modifierFlags) ^ modifierFlags) |
((state & 0b0000000000000000000_0000_01011000) << 18) |
0b0000110000001000000_0000_00000000;

value = parsePrimaryExpression(
parser,
context | Context.InClass,
Expand Down
Loading

0 comments on commit 7801336

Please sign in to comment.