Skip to content

Commit

Permalink
feat: Ability to set a IndexSignatureDeclaration's return type using …
Browse files Browse the repository at this point in the history
…a writer function.
  • Loading branch information
dsherret committed Apr 30, 2018
1 parent 792c530 commit 0d6526d
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 8 deletions.
15 changes: 13 additions & 2 deletions src/compiler/interface/IndexSignatureDeclaration.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { ts } from "../../typescript";
import { IndexSignatureDeclarationStructure } from "../../structures";
import { CodeBlockWriter } from "../../codeBlockWriter";
import { getTextFromStringOrWriter } from "../../utils";
import { removeInterfaceMember } from "../../manipulation";
import { callBaseFill } from "../callBaseFill";
import { Node, Identifier } from "../common";
Expand Down Expand Up @@ -93,10 +95,19 @@ export class IndexSignatureDeclaration extends IndexSignatureDeclarationBase<ts.

/**
* Sets the return type.
* @param text
* @param text - Text of the return type.
*/
setReturnType(text: string) {
setReturnType(text: string): this;
/**
* Sets the return type.
* @param writerFunction - Writer function to write the return type with.
*/
setReturnType(writerFunction: (writer: CodeBlockWriter) => void): this;
/** @internal */
setReturnType(textOrWriterFunction: string | ((writer: CodeBlockWriter) => void)): this;
setReturnType(textOrWriterFunction: string | ((writer: CodeBlockWriter) => void)) {
const returnTypeNode = this.getReturnTypeNode();
const text = getTextFromStringOrWriter(this.getWriterWithQueuedChildIndentation(), textOrWriterFunction);
if (returnTypeNode.getText() === text)
return this;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { CodeBlockWriter } from "../../codeBlockWriter";
import { printTextFromStringOrWriter } from "../../utils";
import { IndexSignatureDeclarationStructure } from "../../structures";
import { FactoryStructurePrinter } from "../FactoryStructurePrinter";
import { NewLineFormattingStructuresPrinter } from "../formatting";
Expand All @@ -13,6 +14,8 @@ export class IndexSignatureDeclarationStructurePrinter extends FactoryStructureP
printText(writer: CodeBlockWriter, structure: IndexSignatureDeclarationStructure) {
this.factory.forJSDoc().printDocs(writer, structure.docs);
this.factory.forModifierableNode().printText(writer, structure);
writer.write(`[${structure.keyName || "key"}: ${structure.keyType || "string"}]: ${structure.returnType};`);
writer.write(`[${structure.keyName || "key"}: ${structure.keyType || "string"}]: `);
printTextFromStringOrWriter(writer, structure.returnType);
writer.write(";");
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { JSDocableNodeStructure, ReadonlyableNodeStructure } from "../base";
import { CodeBlockWriter } from "../../codeBlockWriter";
import { JSDocableNodeStructure, ReadonlyableNodeStructure } from "../base";

export interface IndexSignatureDeclarationStructure extends JSDocableNodeStructure, ReadonlyableNodeStructure {
keyName?: string;
keyType?: string;
returnType: string;
returnType: string | ((writer: CodeBlockWriter) => void);
}
2 changes: 1 addition & 1 deletion src/tests/compiler/base/typeElementMemberedNodeTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ describe(nameof(TypeElementMemberedNode), () => {

it("should insert multiple into other", () => {
doTest("interface i {\n method1();\n method2();\n}", 1,
[{ returnType: "number" }, { keyName: "key2", keyType: "number", returnType: "Date" }],
[{ returnType: "number" }, { keyName: "key2", keyType: "number", returnType: writer => writer.write("Date") }],
"interface i {\n method1();\n [key: string]: number;\n [key2: number]: Date;\n method2();\n}");
});

Expand Down
14 changes: 12 additions & 2 deletions src/tests/compiler/interface/indexSignatureDeclarationTests.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { expect } from "chai";
import { CodeBlockWriter } from "../../../codeBlockWriter";
import { IndexSignatureDeclaration, InterfaceDeclaration } from "../../../compiler";
import { IndexSignatureDeclarationStructure } from "../../../structures";
import { getInfoFromText } from "../testHelpers";
Expand Down Expand Up @@ -31,6 +32,11 @@ describe(nameof(IndexSignatureDeclaration), () => {
doTest("interface Identifier {\n [key: string]: SomeReference;\n}", allProps,
"interface Identifier {\n /**\n * Desc\n */\n readonly [newKeyName: number]: Date;\n}");
});

it("should change the return type when using a writer", () => {
doTest("interface Identifier { [key: string]: number; }", { returnType: writer => writer.write("string") },
"interface Identifier { [key: string]: string; }");
});
});

describe(nameof<IndexSignatureDeclaration>(n => n.getKeyName), () => {
Expand Down Expand Up @@ -124,15 +130,19 @@ describe(nameof(IndexSignatureDeclaration), () => {
});

describe(nameof<IndexSignatureDeclaration>(n => n.setReturnType), () => {
function doTest(code: string, name: string, expectedCode: string) {
function doTest(code: string, textOrWriterFunction: string | ((writer: CodeBlockWriter) => void), expectedCode: string) {
const {firstIndexSignature, sourceFile} = getFirstIndexSignatureWithInfo(code);
firstIndexSignature.setReturnType(name);
firstIndexSignature.setReturnType(textOrWriterFunction);
expect(sourceFile.getFullText()).to.equal(expectedCode);
}

it("should set the return type", () => {
doTest("interface Identifier { [keyName: string]: number; }", "Date", "interface Identifier { [keyName: string]: Date; }");
});

it("should set the return type with a writer function", () => {
doTest("interface Identifier { [keyName: string]: number; }", writer => writer.write("Date"), "interface Identifier { [keyName: string]: Date; }");
});
});

describe(nameof<IndexSignatureDeclaration>(n => n.setIsReadonly), () => {
Expand Down

0 comments on commit 0d6526d

Please sign in to comment.