Skip to content

Commit

Permalink
Fix parsing of nested class with constructor
Browse files Browse the repository at this point in the history
Co-authored-by: DimaIT <dima@surfly.com>
  • Loading branch information
pmenichelli and DimaIT committed May 10, 2023
1 parent 8de5c17 commit ae643fd
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/parser.ts
Expand Up @@ -8204,6 +8204,8 @@ export function parseClassBody(

consume(parser, context | Context.AllowRegExp, Token.LeftBrace);
context = (context | Context.DisallowIn) ^ Context.DisallowIn;

let hasConstr = parser.flags & Flags.HasConstructor;
parser.flags = (parser.flags | Flags.HasConstructor) ^ Flags.HasConstructor;

const body: (ESTree.MethodDefinition | ESTree.PropertyDefinition | ESTree.StaticBlock)[] = [];
Expand Down Expand Up @@ -8245,6 +8247,7 @@ export function parseClassBody(
);
}
consume(parser, origin & Origin.Declaration ? context | Context.AllowRegExp : context, Token.RightBrace);
parser.flags = (parser.flags & ~Flags.HasConstructor) | hasConstr;

return finishNode(parser, context, tokenPos, linePos, colPos, {
type: 'ClassBody',
Expand Down
124 changes: 123 additions & 1 deletion test/parser/expressions/class.ts
Expand Up @@ -799,7 +799,9 @@ describe('Expressions - Class', () => {
['(class A { static prototype() {} })', Context.None],
['(class A { static *get [x](){} })', Context.None],
['(class A { static *set [x](y){}})', Context.None],
['async function f(foo = class y extends (await f) {}){}', Context.None]
['async function f(foo = class y extends (await f) {}){}', Context.None],
['new class { constructor() {} start() { new class { constructor() {}}} constructor() {}}', Context.None]

Check failure on line 803 in test/parser/expressions/class.ts

View workflow job for this annotation

GitHub Actions / build (18)

Type 'string | Context' is not assignable to type '[string, Context]'.

Check failure on line 803 in test/parser/expressions/class.ts

View workflow job for this annotation

GitHub Actions / build (14)

Type 'string | Context' is not assignable to type '[string, Context]'.
['new class { constructor() {} start() { new class { } } constructor() {}}', Context.None]

Check failure on line 804 in test/parser/expressions/class.ts

View workflow job for this annotation

GitHub Actions / build (18)

Left side of comma operator is unused and has no side effects.

Check failure on line 804 in test/parser/expressions/class.ts

View workflow job for this annotation

GitHub Actions / build (14)

Left side of comma operator is unused and has no side effects.
]);

for (const arg of [
Expand Down Expand Up @@ -12789,6 +12791,126 @@ describe('Expressions - Class', () => {
sourceType: 'module',
type: 'Program'
}
],
[
`new class {
start() {
new class {
constructor() {}
}
}
constructor() {}
}`,
Context.None,
{
body: [
{
type: "ExpressionStatement",
expression: {
type: "NewExpression",
callee: {
type: "ClassExpression",
id: null,
superClass: null,
decorators: [],
body: {
type: "ClassBody",
body: [
{
type: "MethodDefinition",
kind: "method",
static: false,
computed: false,
key: {
type: "Identifier",
name: "start"
},
decorators: [],
value: {
type: "FunctionExpression",
params: [],
body: {
type: "BlockStatement",
body: [
{
type: "ExpressionStatement",
expression: {
type: "NewExpression",
callee: {
type: "ClassExpression",
id: null,
superClass: null,
decorators: [],
body: {
type: "ClassBody",
body: [
{
type: "MethodDefinition",
kind: "constructor",
static: false,
computed: false,
key: {
type: "Identifier",
name: "constructor"
},
decorators: [],
value: {
type: "FunctionExpression",
params: [],
body: {
type: "BlockStatement",
body: []
},
async: false,
generator: false,
id: null
}
}
]
}
},
arguments: []
}
}
]
},
async: false,
generator: false,
id: null
}
},
{
type: "MethodDefinition",
kind: "constructor",
static: false,
computed: false,
key: {
type: "Identifier",
name: "constructor"
},
decorators: [],
value: {
type: "FunctionExpression",
params: [],
body: {
type: "BlockStatement",
body: []
},
async: false,
generator: false,
id: null
}
}
]
}
},
arguments: []
}
}
],
type: "Program",
sourceType: "script"
}
]
]);
});

0 comments on commit ae643fd

Please sign in to comment.