Skip to content

Fixed issue with method called new being emitted as construct signature #55109

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/compiler/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2629,7 +2629,12 @@ export function createPrinter(printerOptions: PrinterOptions = {}, handlers: Pri
function emitMethodSignature(node: MethodSignature) {
pushNameGenerationScope(node);
emitModifierList(node, node.modifiers);
emit(node.name);
if (node.name.kind === SyntaxKind.Identifier && node.name.escapedText === "new") {
emit(factory.createStringLiteralFromNode(node.name));
}
else {
emit(node.name);
}
emit(node.questionToken);
emitTypeParameters(node, node.typeParameters);
emitParameters(node, node.parameters);
Expand Down
43 changes: 43 additions & 0 deletions tests/baselines/reference/emitMethodCalledNew.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//// [tests/cases/compiler/emitMethodCalledNew.ts] ////

//// [emitMethodCalledNew.ts]
// https://github.com/microsoft/TypeScript/issues/55075

export const a = {
new(x: number) { return x + 1 }
}
export const b = {
"new"(x: number) { return x + 1 }
}
export const c = {
["new"](x: number) { return x + 1 }
}


//// [emitMethodCalledNew.js]
"use strict";
// https://github.com/microsoft/TypeScript/issues/55075
var _a;
Object.defineProperty(exports, "__esModule", { value: true });
exports.c = exports.b = exports.a = void 0;
exports.a = {
new: function (x) { return x + 1; }
};
exports.b = {
"new": function (x) { return x + 1; }
};
exports.c = (_a = {},
_a["new"] = function (x) { return x + 1; },
_a);


//// [emitMethodCalledNew.d.ts]
export declare const a: {
"new"(x: number): number;
};
export declare const b: {
"new"(x: number): number;
};
export declare const c: {
"new"(x: number): number;
};
31 changes: 31 additions & 0 deletions tests/baselines/reference/emitMethodCalledNew.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//// [tests/cases/compiler/emitMethodCalledNew.ts] ////

=== emitMethodCalledNew.ts ===
// https://github.com/microsoft/TypeScript/issues/55075

export const a = {
>a : Symbol(a, Decl(emitMethodCalledNew.ts, 2, 12))

new(x: number) { return x + 1 }
>new : Symbol(new, Decl(emitMethodCalledNew.ts, 2, 18))
>x : Symbol(x, Decl(emitMethodCalledNew.ts, 3, 6))
>x : Symbol(x, Decl(emitMethodCalledNew.ts, 3, 6))
}
export const b = {
>b : Symbol(b, Decl(emitMethodCalledNew.ts, 5, 12))

"new"(x: number) { return x + 1 }
>"new" : Symbol("new", Decl(emitMethodCalledNew.ts, 5, 18))
>x : Symbol(x, Decl(emitMethodCalledNew.ts, 6, 8))
>x : Symbol(x, Decl(emitMethodCalledNew.ts, 6, 8))
}
export const c = {
>c : Symbol(c, Decl(emitMethodCalledNew.ts, 8, 12))

["new"](x: number) { return x + 1 }
>["new"] : Symbol(["new"], Decl(emitMethodCalledNew.ts, 8, 18))
>"new" : Symbol(["new"], Decl(emitMethodCalledNew.ts, 8, 18))
>x : Symbol(x, Decl(emitMethodCalledNew.ts, 9, 10))
>x : Symbol(x, Decl(emitMethodCalledNew.ts, 9, 10))
}

40 changes: 40 additions & 0 deletions tests/baselines/reference/emitMethodCalledNew.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//// [tests/cases/compiler/emitMethodCalledNew.ts] ////

=== emitMethodCalledNew.ts ===
// https://github.com/microsoft/TypeScript/issues/55075

export const a = {
>a : { "new"(x: number): number; }
>{ new(x: number) { return x + 1 }} : { "new"(x: number): number; }

new(x: number) { return x + 1 }
>new : (x: number) => number
>x : number
>x + 1 : number
>x : number
>1 : 1
}
export const b = {
>b : { "new"(x: number): number; }
>{ "new"(x: number) { return x + 1 }} : { "new"(x: number): number; }

"new"(x: number) { return x + 1 }
>"new" : (x: number) => number
>x : number
>x + 1 : number
>x : number
>1 : 1
}
export const c = {
>c : { "new"(x: number): number; }
>{ ["new"](x: number) { return x + 1 }} : { "new"(x: number): number; }

["new"](x: number) { return x + 1 }
>["new"] : (x: number) => number
>"new" : "new"
>x : number
>x + 1 : number
>x : number
>1 : 1
}

Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ var a: { new(x: Date): string }
>x : Date

var b = { new(x: RegExp) { return ''; } }; // not a construct signature, function called new
>b : { new(x: RegExp): string; }
>{ new(x: RegExp) { return ''; } } : { new(x: RegExp): string; }
>b : { "new"(x: RegExp): string; }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't seem right... aren't all of these cases changed explicitly construct signatures that shouldn't be quoted?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one definitely isnt wrong. Notice how this is inferred~ from an object literal and that cant even have a construct signature - it’s just a method called new

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agh, yeah. I think I skimmed this PR on my phone on the weekend and had it in my head that there was something going on.

>{ new(x: RegExp) { return ''; } } : { "new"(x: RegExp): string; }
>new : (x: RegExp) => string
>x : RegExp
>'' : ""
Expand Down Expand Up @@ -89,17 +89,17 @@ function foo3(x: any) { }
>x : any

function foo4(x: typeof b);
>foo4 : { (x: typeof b): any; (x: { new(x: RegExp): string; }): any; }
>x : { new(x: RegExp): string; }
>b : { new(x: RegExp): string; }
>foo4 : { (x: typeof b): any; (x: { "new"(x: RegExp): string; }): any; }
>x : { "new"(x: RegExp): string; }
>b : { "new"(x: RegExp): string; }

function foo4(x: typeof b); // error
>foo4 : { (x: { new(x: RegExp): string; }): any; (x: typeof b): any; }
>x : { new(x: RegExp): string; }
>b : { new(x: RegExp): string; }
>foo4 : { (x: { "new"(x: RegExp): string; }): any; (x: typeof b): any; }
>x : { "new"(x: RegExp): string; }
>b : { "new"(x: RegExp): string; }

function foo4(x: any) { }
>foo4 : { (x: { new(x: RegExp): string; }): any; (x: { new(x: RegExp): string; }): any; }
>foo4 : { (x: { "new"(x: RegExp): string; }): any; (x: { "new"(x: RegExp): string; }): any; }
>x : any

function foo8(x: B);
Expand Down Expand Up @@ -140,16 +140,16 @@ function foo10(x: any) { }
>x : any

function foo11(x: B);
>foo11 : { (x: B): any; (x: { new(x: RegExp): string; }): any; }
>foo11 : { (x: B): any; (x: { "new"(x: RegExp): string; }): any; }
>x : B

function foo11(x: typeof b); // ok
>foo11 : { (x: B): any; (x: typeof b): any; }
>x : { new(x: RegExp): string; }
>b : { new(x: RegExp): string; }
>x : { "new"(x: RegExp): string; }
>b : { "new"(x: RegExp): string; }

function foo11(x: any) { }
>foo11 : { (x: B): any; (x: { new(x: RegExp): string; }): any; }
>foo11 : { (x: B): any; (x: { "new"(x: RegExp): string; }): any; }
>x : any

function foo12(x: I);
Expand Down Expand Up @@ -190,16 +190,16 @@ function foo13(x: any) { }
>x : any

function foo14(x: I);
>foo14 : { (x: I): any; (x: { new(x: RegExp): string; }): any; }
>foo14 : { (x: I): any; (x: { "new"(x: RegExp): string; }): any; }
>x : I

function foo14(x: typeof b); // ok
>foo14 : { (x: I): any; (x: typeof b): any; }
>x : { new(x: RegExp): string; }
>b : { new(x: RegExp): string; }
>x : { "new"(x: RegExp): string; }
>b : { "new"(x: RegExp): string; }

function foo14(x: any) { }
>foo14 : { (x: I): any; (x: { new(x: RegExp): string; }): any; }
>foo14 : { (x: I): any; (x: { "new"(x: RegExp): string; }): any; }
>x : any

function foo15(x: I2<string>);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ var a: { new(x: string, y: string): string }
>y : string

var b = { new(x: string) { return ''; } }; // not a construct signature, function called new
>b : { new(x: string): string; }
>{ new(x: string) { return ''; } } : { new(x: string): string; }
>b : { "new"(x: string): string; }
>{ new(x: string) { return ''; } } : { "new"(x: string): string; }
>new : (x: string) => string
>x : string
>'' : ""
Expand Down Expand Up @@ -92,17 +92,17 @@ function foo3(x: any) { }
>x : any

function foo4(x: typeof b);
>foo4 : { (x: typeof b): any; (x: { new(x: string): string; }): any; }
>x : { new(x: string): string; }
>b : { new(x: string): string; }
>foo4 : { (x: typeof b): any; (x: { "new"(x: string): string; }): any; }
>x : { "new"(x: string): string; }
>b : { "new"(x: string): string; }

function foo4(x: typeof b); // error
>foo4 : { (x: { new(x: string): string; }): any; (x: typeof b): any; }
>x : { new(x: string): string; }
>b : { new(x: string): string; }
>foo4 : { (x: { "new"(x: string): string; }): any; (x: typeof b): any; }
>x : { "new"(x: string): string; }
>b : { "new"(x: string): string; }

function foo4(x: any) { }
>foo4 : { (x: { new(x: string): string; }): any; (x: { new(x: string): string; }): any; }
>foo4 : { (x: { "new"(x: string): string; }): any; (x: { "new"(x: string): string; }): any; }
>x : any

function foo8(x: B);
Expand Down Expand Up @@ -143,16 +143,16 @@ function foo10(x: any) { }
>x : any

function foo11(x: B);
>foo11 : { (x: B): any; (x: { new(x: string): string; }): any; }
>foo11 : { (x: B): any; (x: { "new"(x: string): string; }): any; }
>x : B

function foo11(x: typeof b); // ok
>foo11 : { (x: B): any; (x: typeof b): any; }
>x : { new(x: string): string; }
>b : { new(x: string): string; }
>x : { "new"(x: string): string; }
>b : { "new"(x: string): string; }

function foo11(x: any) { }
>foo11 : { (x: B): any; (x: { new(x: string): string; }): any; }
>foo11 : { (x: B): any; (x: { "new"(x: string): string; }): any; }
>x : any

function foo12(x: I);
Expand Down Expand Up @@ -193,16 +193,16 @@ function foo13(x: any) { }
>x : any

function foo14(x: I);
>foo14 : { (x: I): any; (x: { new(x: string): string; }): any; }
>foo14 : { (x: I): any; (x: { "new"(x: string): string; }): any; }
>x : I

function foo14(x: typeof b); // ok
>foo14 : { (x: I): any; (x: typeof b): any; }
>x : { new(x: string): string; }
>b : { new(x: string): string; }
>x : { "new"(x: string): string; }
>b : { "new"(x: string): string; }

function foo14(x: any) { }
>foo14 : { (x: I): any; (x: { new(x: string): string; }): any; }
>foo14 : { (x: I): any; (x: { "new"(x: string): string; }): any; }
>x : any

function foo15(x: I2<string>);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ var a: { new<T extends Array<string>>(x: T): string }
>x : T

var b = { new<T extends RegExp>(x: T) { return ''; } }; // not a construct signature, function called new
>b : { new<T extends RegExp>(x: T): string; }
>{ new<T extends RegExp>(x: T) { return ''; } } : { new<T extends RegExp>(x: T): string; }
>b : { "new"<T extends RegExp>(x: T): string; }
>{ new<T extends RegExp>(x: T) { return ''; } } : { "new"<T extends RegExp>(x: T): string; }
>new : <T extends RegExp>(x: T) => string
>x : T
>'' : ""
Expand Down Expand Up @@ -91,17 +91,17 @@ function foo3(x: any) { }
>x : any

function foo4(x: typeof b);
>foo4 : { (x: typeof b): any; (x: { new<T extends RegExp>(x: T): string; }): any; }
>x : { new<T extends RegExp>(x: T): string; }
>b : { new<T extends RegExp>(x: T): string; }
>foo4 : { (x: typeof b): any; (x: { "new"<T extends RegExp>(x: T): string; }): any; }
>x : { "new"<T extends RegExp>(x: T): string; }
>b : { "new"<T extends RegExp>(x: T): string; }

function foo4(x: typeof b); // error
>foo4 : { (x: { new<T extends RegExp>(x: T): string; }): any; (x: typeof b): any; }
>x : { new<T extends RegExp>(x: T): string; }
>b : { new<T extends RegExp>(x: T): string; }
>foo4 : { (x: { "new"<T extends RegExp>(x: T): string; }): any; (x: typeof b): any; }
>x : { "new"<T extends RegExp>(x: T): string; }
>b : { "new"<T extends RegExp>(x: T): string; }

function foo4(x: any) { }
>foo4 : { (x: { new<T extends RegExp>(x: T): string; }): any; (x: { new<T extends RegExp>(x: T): string; }): any; }
>foo4 : { (x: { "new"<T extends RegExp>(x: T): string; }): any; (x: { "new"<T extends RegExp>(x: T): string; }): any; }
>x : any

function foo8(x: B<Array<number>>);
Expand Down Expand Up @@ -142,16 +142,16 @@ function foo10(x: any) { }
>x : any

function foo11(x: B<Array<number>>);
>foo11 : { (x: B<Array<number>>): any; (x: { new<T extends RegExp>(x: T): string; }): any; }
>foo11 : { (x: B<Array<number>>): any; (x: { "new"<T extends RegExp>(x: T): string; }): any; }
>x : B<number[]>

function foo11(x: typeof b); // ok
>foo11 : { (x: B<number[]>): any; (x: typeof b): any; }
>x : { new<T extends RegExp>(x: T): string; }
>b : { new<T extends RegExp>(x: T): string; }
>x : { "new"<T extends RegExp>(x: T): string; }
>b : { "new"<T extends RegExp>(x: T): string; }

function foo11(x: any) { }
>foo11 : { (x: B<number[]>): any; (x: { new<T extends RegExp>(x: T): string; }): any; }
>foo11 : { (x: B<number[]>): any; (x: { "new"<T extends RegExp>(x: T): string; }): any; }
>x : any

function foo12(x: I<Number>);
Expand Down Expand Up @@ -192,15 +192,15 @@ function foo13(x: any) { }
>x : any

function foo14(x: I<Number>);
>foo14 : { (x: I<Number>): any; (x: { new<T extends RegExp>(x: T): string; }): any; }
>foo14 : { (x: I<Number>): any; (x: { "new"<T extends RegExp>(x: T): string; }): any; }
>x : I<Number>

function foo14(x: typeof b); // ok
>foo14 : { (x: I<Number>): any; (x: typeof b): any; }
>x : { new<T extends RegExp>(x: T): string; }
>b : { new<T extends RegExp>(x: T): string; }
>x : { "new"<T extends RegExp>(x: T): string; }
>b : { "new"<T extends RegExp>(x: T): string; }

function foo14(x: any) { }
>foo14 : { (x: I<Number>): any; (x: { new<T extends RegExp>(x: T): string; }): any; }
>foo14 : { (x: I<Number>): any; (x: { "new"<T extends RegExp>(x: T): string; }): any; }
>x : any

Loading