From 055bf2158539d0b32c71141ca7c5e5213b85a611 Mon Sep 17 00:00:00 2001 From: David Sherret Date: Fri, 15 Sep 2023 10:40:31 -0400 Subject: [PATCH] Revert "feat: automatically quote property keys if necessary when writing structures (#1451)" This reverts commit 601f214b876697ee1143575b4b2fff2b2214fa11. --- .../GetAccessorDeclarationStructurePrinter.ts | 4 +-- .../MethodDeclarationStructurePrinter.ts | 4 +-- .../PropertyDeclarationStructurePrinter.ts | 3 +- .../SetAccessorDeclarationStructurePrinter.ts | 4 +-- .../enum/EnumMemberStructurePrinter.ts | 9 ++++-- .../MethodSignatureStructurePrinter.ts | 3 +- .../PropertySignatureStructurePrinter.ts | 3 +- .../classDeclarationStructurePrinterTests.ts | 18 ++++-------- ...terfaceDeclarationStructurePrinterTests.ts | 29 ------------------- packages/ts-morph/src/utils/WriterUtils.ts | 11 ------- 10 files changed, 20 insertions(+), 68 deletions(-) diff --git a/packages/ts-morph/src/structurePrinters/class/GetAccessorDeclarationStructurePrinter.ts b/packages/ts-morph/src/structurePrinters/class/GetAccessorDeclarationStructurePrinter.ts index bc5497d75..5be2087fe 100644 --- a/packages/ts-morph/src/structurePrinters/class/GetAccessorDeclarationStructurePrinter.ts +++ b/packages/ts-morph/src/structurePrinters/class/GetAccessorDeclarationStructurePrinter.ts @@ -1,7 +1,6 @@ import { CodeBlockWriter } from "../../codeBlockWriter"; import { StructurePrinterFactory } from "../../factories"; import { GetAccessorDeclarationStructure, OptionalKind } from "../../structures"; -import { WriterUtils } from "../../utils"; import { BlankLineFormattingStructuresPrinter } from "../formatting"; import { NodePrinter } from "../NodePrinter"; @@ -20,8 +19,7 @@ export class GetAccessorDeclarationStructurePrinter extends NodePrinter> { @@ -81,7 +81,7 @@ export class MethodDeclarationStructurePrinter extends NodePrinter) { this.factory.forJSDoc().printDocs(writer, structure.docs); - WriterUtils.writePropertyName(writer, structure.name); + writer.write(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 8c04a40ae..587dcb0a0 100644 --- a/packages/ts-morph/src/structurePrinters/interface/PropertySignatureStructurePrinter.ts +++ b/packages/ts-morph/src/structurePrinters/interface/PropertySignatureStructurePrinter.ts @@ -1,6 +1,5 @@ import { CodeBlockWriter } from "../../codeBlockWriter"; import { OptionalKind, PropertySignatureStructure } from "../../structures"; -import { WriterUtils } from "../../utils"; import { NewLineFormattingStructuresPrinter } from "../formatting"; import { NodePrinter } from "../NodePrinter"; @@ -14,7 +13,7 @@ export class PropertySignatureStructurePrinter extends NodePrinter) { this.factory.forJSDoc().printDocs(writer, structure.docs); this.factory.forModifierableNode().printText(writer, structure); - WriterUtils.writePropertyName(writer, structure.name); + writer.write(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/tests/structurePrinters/class/classDeclarationStructurePrinterTests.ts b/packages/ts-morph/src/tests/structurePrinters/class/classDeclarationStructurePrinterTests.ts index 4d4b192dc..fd74d23be 100644 --- a/packages/ts-morph/src/tests/structurePrinters/class/classDeclarationStructurePrinterTests.ts +++ b/packages/ts-morph/src/tests/structurePrinters/class/classDeclarationStructurePrinterTests.ts @@ -34,7 +34,7 @@ describe("ClassDeclarationStructurePrinter", () => { name: "pPrivate", }, { scope: Scope.Protected, - name: "p-Protected", + name: "pProtected", }], ctors: [{}], methods: [{ @@ -45,10 +45,10 @@ describe("ClassDeclarationStructurePrinter", () => { name: "m2", }, { scope: Scope.Public, - name: "m3-2", + name: "m3", }], - getAccessors: [{ name: "g" }, { name: "g-2" }], - setAccessors: [{ name: "s" }, { name: "s-2" }], + getAccessors: [{ name: "g" }], + setAccessors: [{ name: "s" }], }; doTest( @@ -57,7 +57,7 @@ describe("ClassDeclarationStructurePrinter", () => { pNoKeyword; public pPublic; private pPrivate; - protected "p-Protected"; + protected pProtected; constructor() { } @@ -65,22 +65,16 @@ describe("ClassDeclarationStructurePrinter", () => { get g() { } - get "g-2"() { - } - set s() { } - set "s-2"() { - } - private m1() { } protected m2() { } - public "m3-2"() { + public m3() { } }`, ); diff --git a/packages/ts-morph/src/tests/structurePrinters/interface/interfaceDeclarationStructurePrinterTests.ts b/packages/ts-morph/src/tests/structurePrinters/interface/interfaceDeclarationStructurePrinterTests.ts index 5ecf3fd83..352c6fc0c 100644 --- a/packages/ts-morph/src/tests/structurePrinters/interface/interfaceDeclarationStructurePrinterTests.ts +++ b/packages/ts-morph/src/tests/structurePrinters/interface/interfaceDeclarationStructurePrinterTests.ts @@ -32,34 +32,5 @@ describe("InterfaceDeclarationStructurePrinter", () => { doTest({ name: "I", extends: _ => {} }, `interface I {\n}`); }); }); - - describe("props and methods", () => { - it("should write properties and methods", () => { - // todo: more tests - doTest( - { - name: "I", - properties: [{ - name: "prop", - type: "string", - }, { - name: "prop-1", - type: "string", - }], - methods: [{ - name: "method", - }, { - name: "method-1", - }], - }, - `interface I { - prop: string; - "prop-1": string; - method(); - "method-1"(); -}`, - ); - }); - }); }); }); diff --git a/packages/ts-morph/src/utils/WriterUtils.ts b/packages/ts-morph/src/utils/WriterUtils.ts index 335270ebc..73dabc34b 100644 --- a/packages/ts-morph/src/utils/WriterUtils.ts +++ b/packages/ts-morph/src/utils/WriterUtils.ts @@ -1,6 +1,4 @@ -import { StringUtils } from "@ts-morph/common"; import { CodeBlockWriter } from "../codeBlockWriter"; -import { isValidVariableName } from "./namingValidator"; /** Utilities for code block writer. */ export class WriterUtils { @@ -21,13 +19,4 @@ export class WriterUtils { }); return chars.join(""); } - - /* Adds quotes if structure is not a valid variable name - * AND the string is not enclosed in quotation marks */ - static writePropertyName(writer: CodeBlockWriter, text: string) { - if (isValidVariableName(text) || StringUtils.isQuoted(text)) - writer.write(text); - else - writer.quote(text); - } }