Skip to content
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

fix: allow writer functions in property named structures #1412

Open
wants to merge 3 commits into
base: latest
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ 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 ${structure.name}`);
writer.write("get ");
this.printTextOrWriterFunc(writer, 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,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";

Expand Down Expand Up @@ -54,7 +55,11 @@ export class MethodDeclarationStructurePrinter extends NodePrinter<OptionalKind<
}
}

private printOverloads(writer: CodeBlockWriter, name: string, structures: ReadonlyArray<OptionalKind<MethodDeclarationOverloadStructure>> | undefined) {
private printOverloads(
writer: CodeBlockWriter,
name: string | WriterFunction,
structures: ReadonlyArray<OptionalKind<MethodDeclarationOverloadStructure>> | undefined,
) {
if (structures == null || structures.length === 0)
return;

Expand All @@ -64,7 +69,7 @@ export class MethodDeclarationStructurePrinter extends NodePrinter<OptionalKind<
}
}

printOverload(writer: CodeBlockWriter, name: string, structure: OptionalKind<MethodDeclarationOverloadStructure>) {
printOverload(writer: CodeBlockWriter, name: string | WriterFunction, structure: OptionalKind<MethodDeclarationOverloadStructure>) {
this.printLeadingTrivia(writer, structure);
this.printHeader(writer, name, structure);
writer.write(";");
Expand All @@ -73,15 +78,15 @@ export class MethodDeclarationStructurePrinter extends NodePrinter<OptionalKind<

private printHeader(
writer: CodeBlockWriter,
name: string,
name: string | WriterFunction,
structure: OptionalKind<MethodDeclarationOverloadStructure> | OptionalKind<MethodDeclarationStructure>,
) {
this.factory.forJSDoc().printDocs(writer, structure.docs);
if ((structure as MethodDeclarationStructure).decorators != null)
this.factory.forDecorator().printTexts(writer, (structure as MethodDeclarationStructure).decorators);

this.factory.forModifierableNode().printText(writer, structure);
writer.write(name);
this.printTextOrWriterFunc(writer, 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
Expand Up @@ -15,7 +15,7 @@ export class PropertyDeclarationStructurePrinter extends NodePrinter<OptionalKin
this.factory.forDecorator().printTexts(writer, structure.decorators);

this.factory.forModifierableNode().printText(writer, structure);
writer.write(structure.name);
this.printTextOrWriterFunc(writer, 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
Expand Up @@ -20,7 +20,8 @@ export class SetAccessorDeclarationStructurePrinter extends NodePrinter<Optional
this.factory.forDecorator().printTexts(writer, structure.decorators);

this.factory.forModifierableNode().printText(writer, structure);
writer.write(`set ${structure.name}`);
writer.write("set ");
this.printTextOrWriterFunc(writer, 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 @@ -28,7 +28,9 @@ export class EnumMemberStructurePrinter extends NodePrinter<OptionalKind<EnumMem
this.factory.forJSDoc().printDocs(writer, structure.docs);
// 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))
if (structure.name instanceof Function)
structure.name(writer);
else if (isValidVariableName(structure.name) || StringUtils.isQuoted(structure.name))
writer.write(structure.name);
else
writer.quote(structure.name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import { NodePrinter } from "../../NodePrinter";
export class PropertyAssignmentStructurePrinter extends NodePrinter<OptionalKind<PropertyAssignmentStructure>> {
protected printTextInternal(writer: CodeBlockWriter, structure: OptionalKind<PropertyAssignmentStructure>) {
writer.hangingIndent(() => {
writer.write(`${structure.name}: `);
this.printTextOrWriterFunc(writer, structure.name);
writer.write(": ");
printTextFromStringOrWriter(writer, structure.initializer);
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export class MethodSignatureStructurePrinter extends NodePrinter<OptionalKind<Me

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

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { WriterFunction } from "../../../types";

export interface PropertyNamedNodeStructure {
name: string;
name: string | WriterFunction;
Copy link
Owner

Choose a reason for hiding this comment

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

I'm hesitant to merge this because people are likely doing comparisons on string. One of the major issues with structures is that changes that make it easier for writing can make it harder to use for reading. At this point, I think this might be too breaking.

}
1 change: 0 additions & 1 deletion packages/ts-morph/src/structures/base/name/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@ export * from "./BindingNamedNodeStructure";
export * from "./ModuleNamedNodeStructure";
export * from "./NameableNodeStructure";
export * from "./NamedNodeStructure";
export * from "./PropertyNameableNodeStructure";
export * from "./PropertyNamedNodeStructure";
Loading