Skip to content

Commit

Permalink
Fixed issue with method called new being emitted as construct signa…
Browse files Browse the repository at this point in the history
…ture
  • Loading branch information
Andarist committed Jul 22, 2023
1 parent 2ac22c1 commit 0e8e53d
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/compiler/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2618,7 +2618,11 @@ 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 {

Check failure on line 2623 in src/compiler/emitter.ts

View workflow job for this annotation

GitHub Actions / lint

Closing curly brace appears on the same line as the subsequent block
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
}

13 changes: 13 additions & 0 deletions tests/cases/compiler/emitMethodCalledNew.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// @declaration: true

// 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 }
}

0 comments on commit 0e8e53d

Please sign in to comment.