Skip to content

Commit

Permalink
fix(parser): fix parsing nested class with constructor
Browse files Browse the repository at this point in the history
Fix parsing of nested class with constructor
  • Loading branch information
3cp committed May 12, 2023
2 parents 8de5c17 + c0856b9 commit 8f7124b
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 2 deletions.
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"
}
]
]);
});

0 comments on commit 8f7124b

Please sign in to comment.