Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix parsing of nested class with constructor #246

Merged
merged 1 commit into from
May 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
134 changes: 132 additions & 2 deletions test/parser/expressions/class.ts
Expand Up @@ -310,7 +310,20 @@ describe('Expressions - Class', () => {
'static constructor() {}',
'static get constructor() {}',
'static set constructor(_) {}',
'static *constructor() {}'
'static *constructor() {}',
`method() {
new class { constructor() {} }
}
constructor() {}`,
`method() {
new class {
method() {
new class { constructor() {} }
}
constructor() {}
}
}
constructor() {}`,
]) {
it(`class C {${arg}}`, () => {
t.doesNotThrow(() => {
Expand Down Expand Up @@ -799,7 +812,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],
['new class { constructor() {} start() { new class { } } constructor() {}}', Context.None]
]);

for (const arg of [
Expand Down Expand Up @@ -12789,6 +12804,121 @@ 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,
body: {
type: "ClassBody",
body: [
{
type: "MethodDefinition",
kind: "method",
static: false,
computed: false,
key: {
type: "Identifier",
name: "start"
},
value: {
type: "FunctionExpression",
params: [],
body: {
type: "BlockStatement",
body: [
{
type: "ExpressionStatement",
expression: {
type: "NewExpression",
callee: {
type: "ClassExpression",
id: null,
superClass: null,
body: {
type: "ClassBody",
body: [
{
type: "MethodDefinition",
kind: "constructor",
static: false,
computed: false,
key: {
type: "Identifier",
name: "constructor"
},
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"
},
value: {
type: "FunctionExpression",
params: [],
body: {
type: "BlockStatement",
body: []
},
async: false,
generator: false,
id: null
}
}
]
}
},
arguments: []
}
}
],
type: "Program",
sourceType: "script"
}
]
]);
});