From 023996e32564191100fe3321c88f36e3e4f7eb9a Mon Sep 17 00:00:00 2001 From: Emily M Klassen Date: Thu, 11 May 2023 20:43:21 -0700 Subject: [PATCH 1/2] fix: allow writer functions in property named structures --- .../class/MethodDeclarationStructurePrinter.ts | 13 +++++++++---- .../class/PropertyDeclarationStructurePrinter.ts | 2 +- .../class/SetAccessorDeclarationStructurePrinter.ts | 3 ++- .../enum/EnumMemberStructurePrinter.ts | 4 +++- .../object/PropertyAssignmentStructurePrinter.ts | 3 ++- .../interface/MethodSignatureStructurePrinter.ts | 2 +- .../interface/PropertySignatureStructurePrinter.ts | 2 +- .../base/name/PropertyNameableNodeStructure.ts | 4 +++- .../base/name/PropertyNamedNodeStructure.ts | 4 +++- 9 files changed, 25 insertions(+), 12 deletions(-) diff --git a/packages/ts-morph/src/structurePrinters/class/MethodDeclarationStructurePrinter.ts b/packages/ts-morph/src/structurePrinters/class/MethodDeclarationStructurePrinter.ts index 1278ba0a3..7ba250709 100644 --- a/packages/ts-morph/src/structurePrinters/class/MethodDeclarationStructurePrinter.ts +++ b/packages/ts-morph/src/structurePrinters/class/MethodDeclarationStructurePrinter.ts @@ -2,6 +2,7 @@ import { ObjectUtils } from "@ts-morph/common"; import { CodeBlockWriter } from "../../codeBlockWriter"; import { StructurePrinterFactory } from "../../factories"; import { MethodDeclarationOverloadStructure, MethodDeclarationStructure, OptionalKind } from "../../structures"; +import { WriterFunction } from "../../types"; import { setValueIfUndefined } from "../../utils"; import { NodePrinter } from "../NodePrinter"; @@ -54,7 +55,11 @@ export class MethodDeclarationStructurePrinter extends NodePrinter> | undefined) { + private printOverloads( + writer: CodeBlockWriter, + name: string | WriterFunction, + structures: ReadonlyArray> | undefined, + ) { if (structures == null || structures.length === 0) return; @@ -64,7 +69,7 @@ export class MethodDeclarationStructurePrinter extends NodePrinter) { + printOverload(writer: CodeBlockWriter, name: string | WriterFunction, structure: OptionalKind) { this.printLeadingTrivia(writer, structure); this.printHeader(writer, name, structure); writer.write(";"); @@ -73,7 +78,7 @@ export class MethodDeclarationStructurePrinter extends NodePrinter | OptionalKind, ) { this.factory.forJSDoc().printDocs(writer, structure.docs); @@ -81,7 +86,7 @@ export class MethodDeclarationStructurePrinter extends NodePrinter> { protected printTextInternal(writer: CodeBlockWriter, structure: OptionalKind) { writer.hangingIndent(() => { - writer.write(`${structure.name}: `); + this.printTextOrWriterFunc(writer, structure.name); + writer.write(": "); printTextFromStringOrWriter(writer, structure.initializer); }); } diff --git a/packages/ts-morph/src/structurePrinters/interface/MethodSignatureStructurePrinter.ts b/packages/ts-morph/src/structurePrinters/interface/MethodSignatureStructurePrinter.ts index eee24388b..f75a0b671 100644 --- a/packages/ts-morph/src/structurePrinters/interface/MethodSignatureStructurePrinter.ts +++ b/packages/ts-morph/src/structurePrinters/interface/MethodSignatureStructurePrinter.ts @@ -12,7 +12,7 @@ export class MethodSignatureStructurePrinter extends NodePrinter) { this.factory.forJSDoc().printDocs(writer, structure.docs); - writer.write(structure.name); + this.printTextOrWriterFunc(writer, structure.name); writer.conditionalWrite(structure.hasQuestionToken, "?"); this.factory.forTypeParameterDeclaration().printTextsWithBrackets(writer, structure.typeParameters); this.factory.forParameterDeclaration().printTextsWithParenthesis(writer, structure.parameters); diff --git a/packages/ts-morph/src/structurePrinters/interface/PropertySignatureStructurePrinter.ts b/packages/ts-morph/src/structurePrinters/interface/PropertySignatureStructurePrinter.ts index 587dcb0a0..a92acad27 100644 --- a/packages/ts-morph/src/structurePrinters/interface/PropertySignatureStructurePrinter.ts +++ b/packages/ts-morph/src/structurePrinters/interface/PropertySignatureStructurePrinter.ts @@ -13,7 +13,7 @@ export class PropertySignatureStructurePrinter extends NodePrinter) { this.factory.forJSDoc().printDocs(writer, structure.docs); this.factory.forModifierableNode().printText(writer, structure); - writer.write(structure.name); + this.printTextOrWriterFunc(writer, structure.name); writer.conditionalWrite(structure.hasQuestionToken, "?"); this.factory.forTypedNode(":").printText(writer, structure); // why would someone write an initializer? I guess let them do it... diff --git a/packages/ts-morph/src/structures/base/name/PropertyNameableNodeStructure.ts b/packages/ts-morph/src/structures/base/name/PropertyNameableNodeStructure.ts index 0f1d2cde8..9b12a8a0d 100644 --- a/packages/ts-morph/src/structures/base/name/PropertyNameableNodeStructure.ts +++ b/packages/ts-morph/src/structures/base/name/PropertyNameableNodeStructure.ts @@ -1,3 +1,5 @@ +import { WriterFunction } from "../../../types"; + export interface PropertyNameableNodeStructure { - name?: string; + name?: string | WriterFunction; } diff --git a/packages/ts-morph/src/structures/base/name/PropertyNamedNodeStructure.ts b/packages/ts-morph/src/structures/base/name/PropertyNamedNodeStructure.ts index a640cb394..263674f8e 100644 --- a/packages/ts-morph/src/structures/base/name/PropertyNamedNodeStructure.ts +++ b/packages/ts-morph/src/structures/base/name/PropertyNamedNodeStructure.ts @@ -1,3 +1,5 @@ +import { WriterFunction } from "../../../types"; + export interface PropertyNamedNodeStructure { - name: string; + name: string | WriterFunction; } From 3c85ae9de8227e1ad3e1a27d47612498218aebd2 Mon Sep 17 00:00:00 2001 From: David Sherret Date: Mon, 11 Sep 2023 18:04:48 -0400 Subject: [PATCH 2/2] Fixes and remove unused interface --- .../class/GetAccessorDeclarationStructurePrinter.ts | 3 ++- .../structures/base/name/PropertyNameableNodeStructure.ts | 5 ----- packages/ts-morph/src/structures/base/name/index.ts | 1 - 3 files changed, 2 insertions(+), 7 deletions(-) delete mode 100644 packages/ts-morph/src/structures/base/name/PropertyNameableNodeStructure.ts diff --git a/packages/ts-morph/src/structurePrinters/class/GetAccessorDeclarationStructurePrinter.ts b/packages/ts-morph/src/structurePrinters/class/GetAccessorDeclarationStructurePrinter.ts index 5be2087fe..de6a53717 100644 --- a/packages/ts-morph/src/structurePrinters/class/GetAccessorDeclarationStructurePrinter.ts +++ b/packages/ts-morph/src/structurePrinters/class/GetAccessorDeclarationStructurePrinter.ts @@ -19,7 +19,8 @@ export class GetAccessorDeclarationStructurePrinter extends NodePrinter