Skip to content

Commit

Permalink
Revert "feat: automatically quote property keys if necessary when wri…
Browse files Browse the repository at this point in the history
…ting structures (#1451)"

This reverts commit 601f214.
  • Loading branch information
dsherret committed Sep 15, 2023
1 parent 601f214 commit 055bf21
Show file tree
Hide file tree
Showing 10 changed files with 20 additions and 68 deletions.
Original file line number Diff line number Diff line change
@@ -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";

Expand All @@ -20,8 +19,7 @@ export class GetAccessorDeclarationStructurePrinter extends NodePrinter<Optional
this.factory.forJSDoc().printDocs(writer, structure.docs);
this.factory.forDecorator().printTexts(writer, structure.decorators);
this.factory.forModifierableNode().printText(writer, structure);
writer.write("get ");
WriterUtils.writePropertyName(writer, structure.name);
writer.write(`get ${structure.name}`);
this.factory.forTypeParameterDeclaration().printTextsWithBrackets(writer, structure.typeParameters);
this.factory.forParameterDeclaration().printTextsWithParenthesis(writer, structure.parameters);
this.factory.forReturnTypedNode().printText(writer, structure);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { ObjectUtils } from "@ts-morph/common";
import { CodeBlockWriter } from "../../codeBlockWriter";
import { StructurePrinterFactory } from "../../factories";
import { MethodDeclarationOverloadStructure, MethodDeclarationStructure, OptionalKind } from "../../structures";
import { setValueIfUndefined, WriterUtils } from "../../utils";
import { setValueIfUndefined } from "../../utils";
import { NodePrinter } from "../NodePrinter";

export class MethodDeclarationStructurePrinter extends NodePrinter<OptionalKind<MethodDeclarationStructure>> {
Expand Down Expand Up @@ -81,7 +81,7 @@ export class MethodDeclarationStructurePrinter extends NodePrinter<OptionalKind<
this.factory.forDecorator().printTexts(writer, (structure as MethodDeclarationStructure).decorators);

this.factory.forModifierableNode().printText(writer, structure);
WriterUtils.writePropertyName(writer, name);
writer.write(name);
writer.conditionalWrite(structure.hasQuestionToken, "?");
this.factory.forTypeParameterDeclaration().printTextsWithBrackets(writer, structure.typeParameters);
this.factory.forParameterDeclaration().printTextsWithParenthesis(writer, structure.parameters);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { CodeBlockWriter } from "../../codeBlockWriter";
import { OptionalKind, PropertyDeclarationStructure } from "../../structures";
import { WriterUtils } from "../../utils";
import { NewLineFormattingStructuresPrinter } from "../formatting";
import { NodePrinter } from "../NodePrinter";

Expand All @@ -16,7 +15,7 @@ export class PropertyDeclarationStructurePrinter extends NodePrinter<OptionalKin
this.factory.forDecorator().printTexts(writer, structure.decorators);

this.factory.forModifierableNode().printText(writer, structure);
WriterUtils.writePropertyName(writer, structure.name);
writer.write(structure.name);
writer.conditionalWrite(structure.hasQuestionToken, "?");
writer.conditionalWrite(structure.hasExclamationToken && !structure.hasQuestionToken, "!");
this.factory.forTypedNode(":").printText(writer, structure);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { CodeBlockWriter } from "../../codeBlockWriter";
import { StructurePrinterFactory } from "../../factories";
import { OptionalKind, SetAccessorDeclarationStructure } from "../../structures";
import { WriterUtils } from "../../utils";
import { BlankLineFormattingStructuresPrinter } from "../formatting";
import { NodePrinter } from "../NodePrinter";

Expand All @@ -21,8 +20,7 @@ export class SetAccessorDeclarationStructurePrinter extends NodePrinter<Optional
this.factory.forDecorator().printTexts(writer, structure.decorators);

this.factory.forModifierableNode().printText(writer, structure);
writer.write("set ");
WriterUtils.writePropertyName(writer, structure.name);
writer.write(`set ${structure.name}`);
this.factory.forTypeParameterDeclaration().printTextsWithBrackets(writer, structure.typeParameters);
this.factory.forParameterDeclaration().printTextsWithParenthesis(writer, structure.parameters);
this.factory.forReturnTypedNode().printText(writer, structure);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { StringUtils } from "@ts-morph/common";
import { CodeBlockWriter } from "../../codeBlockWriter";
import { EnumMemberStructure, OptionalKind } from "../../structures";
import { WriterFunction } from "../../types";
import { isValidVariableName, WriterUtils } from "../../utils";
import { isValidVariableName } from "../../utils";
import { CommaNewLineSeparatedStructuresPrinter } from "../formatting";
import { NodePrinter } from "../NodePrinter";

Expand All @@ -26,7 +26,12 @@ export class EnumMemberStructurePrinter extends NodePrinter<OptionalKind<EnumMem
}

this.factory.forJSDoc().printDocs(writer, structure.docs);
WriterUtils.writePropertyName(writer, structure.name);
// Adds quotes if structure is not a valid variable name
// AND the string is not enclosed in quotation marks
if (isValidVariableName(structure.name) || StringUtils.isQuoted(structure.name))
writer.write(structure.name);
else
writer.quote(structure.name);

if (typeof structure.value === "string") {
const { value } = structure;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { CodeBlockWriter } from "../../codeBlockWriter";
import { MethodSignatureStructure, OptionalKind } from "../../structures";
import { WriterUtils } from "../../utils";
import { NewLineFormattingStructuresPrinter } from "../formatting";
import { NodePrinter } from "../NodePrinter";

Expand All @@ -13,7 +12,7 @@ export class MethodSignatureStructurePrinter extends NodePrinter<OptionalKind<Me

protected printTextInternal(writer: CodeBlockWriter, structure: OptionalKind<MethodSignatureStructure>) {
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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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";

Expand All @@ -14,7 +13,7 @@ export class PropertySignatureStructurePrinter extends NodePrinter<OptionalKind<
protected printTextInternal(writer: CodeBlockWriter, structure: OptionalKind<PropertySignatureStructure>) {
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...
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ describe("ClassDeclarationStructurePrinter", () => {
name: "pPrivate",
}, {
scope: Scope.Protected,
name: "p-Protected",
name: "pProtected",
}],
ctors: [{}],
methods: [{
Expand All @@ -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(
Expand All @@ -57,30 +57,24 @@ describe("ClassDeclarationStructurePrinter", () => {
pNoKeyword;
public pPublic;
private pPrivate;
protected "p-Protected";
protected pProtected;
constructor() {
}
get g() {
}
get "g-2"() {
}
set s() {
}
set "s-2"() {
}
private m1() {
}
protected m2() {
}
public "m3-2"() {
public m3() {
}
}`,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"();
}`,
);
});
});
});
});
11 changes: 0 additions & 11 deletions packages/ts-morph/src/utils/WriterUtils.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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);
}
}

0 comments on commit 055bf21

Please sign in to comment.