From cc7c47455ec897ae3d8be2bb3411fc6c8535e39c Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Tue, 20 Jan 2026 21:59:28 +0800 Subject: [PATCH] Forbid type parameters on `constructor` (#18686) --- src/language-js/parse/babel.js | 2 -- .../tuple/__snapshots__/format.test.js.snap | 20 +++++++++++++++++++ .../errors/typescript/tuple/format.test.js | 17 ++++++++++++++++ 3 files changed, 37 insertions(+), 2 deletions(-) diff --git a/src/language-js/parse/babel.js b/src/language-js/parse/babel.js index e0cb0d76e..018708292 100644 --- a/src/language-js/parse/babel.js +++ b/src/language-js/parse/babel.js @@ -192,8 +192,6 @@ const allowedReasonCodes = new Set([ "StrictFunction", "ForInOfLoopInitializer", - "ConstructorHasTypeParameters", - "DecoratorExportClass", "ParamDupe", "InvalidDecimal", diff --git a/tests/format/misc/errors/typescript/tuple/__snapshots__/format.test.js.snap b/tests/format/misc/errors/typescript/tuple/__snapshots__/format.test.js.snap index 778208ad6..6cc635c1f 100644 --- a/tests/format/misc/errors/typescript/tuple/__snapshots__/format.test.js.snap +++ b/tests/format/misc/errors/typescript/tuple/__snapshots__/format.test.js.snap @@ -14,6 +14,13 @@ exports[`snippet: #0 [babel-ts] format 2`] = ` Cause: A required element cannot follow an optional element. (1:17)" `; +exports[`snippet: #0 [babel-ts] format 3`] = ` +"Type parameters cannot appear on a constructor declaration. (1:24) +> 1 | class A { 'constructor'() {} } + | ^ +Cause: Type parameters cannot appear on a constructor declaration. (1:23)" +`; + exports[`snippet: #0 [oxc-ts] format 1`] = ` "Expected \`,\` or \`]\` but found \`:\` (1:14) > 1 | type T = [x.y: A]; @@ -40,12 +47,25 @@ exports[`snippet: #1 [babel-ts] format 1`] = ` Cause: Tuple members must be labeled with a simple identifier. (1:10)" `; +exports[`snippet: #1 [babel-ts] format 2`] = ` +"Type parameters cannot appear on a constructor declaration. (1:22) +> 1 | class A { constructor() {} } + | ^ +Cause: Type parameters cannot appear on a constructor declaration. (1:21)" +`; + exports[`snippet: #1 [oxc-ts] format 1`] = ` "Expected \`,\` or \`]\` but found \`:\` (1:15) > 1 | type T = [x: A]; | ^" `; +exports[`snippet: #1 [oxc-ts] format 2`] = ` +"Type parameters cannot appear on a constructor declaration (1:22) +> 1 | class A { constructor() {} } + | ^^^" +`; + exports[`snippet: #1 [typescript] format 1`] = ` "',' expected. (1:15) > 1 | type T = [x: A]; diff --git a/tests/format/misc/errors/typescript/tuple/format.test.js b/tests/format/misc/errors/typescript/tuple/format.test.js index e3c7026cf..c9b054812 100644 --- a/tests/format/misc/errors/typescript/tuple/format.test.js +++ b/tests/format/misc/errors/typescript/tuple/format.test.js @@ -17,6 +17,8 @@ runFormatTest( snippets: [ // A required element cannot follow an optional element. "type T = [x?: A, y: B];", + // Type parameters cannot appear on a constructor declaration. + "class A { constructor() {} }", ], }, [ @@ -25,3 +27,18 @@ runFormatTest( "oxc-ts", ], ); + +runFormatTest( + { + importMeta: import.meta, + snippets: [ + // Type parameters cannot appear on a constructor declaration. + "class A { 'constructor'() {} }", + ], + }, + [ + // "typescript", + "babel-ts", + // "oxc-ts", + ], +);