Skip to content

Commit

Permalink
feat: #636 - WriterFunctions.returnStatement should do hanging indent…
Browse files Browse the repository at this point in the history
… in certain cases.
  • Loading branch information
dsherret committed Jun 1, 2019
1 parent deef3d6 commit db6602d
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
9 changes: 7 additions & 2 deletions lib/ts-morph.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1943,17 +1943,22 @@ export declare class WriterFunctions {
*/
static objectType(structure: TypeElementMemberedNodeStructure): WriterFunction;
/**
* Gets a writer function for writing a union type.
* Gets a writer function for writing a union type (ex. `FirstType | SecondType`).
*/
static unionType(firstType: WriterFunctionOrValue, secondType: WriterFunctionOrValue, ...additionalTypes: WriterFunctionOrValue[]): (writer: CodeBlockWriter) => void;
/**
* Gets a writer function for writing an intersection type.
* Gets a writer function for writing an intersection type (ex. `FirstType & SecondType`).
*/
static intersectionType(firstType: WriterFunctionOrValue, secondType: WriterFunctionOrValue, ...additionalTypes: WriterFunctionOrValue[]): (writer: CodeBlockWriter) => void;
/**
* Gets a writer function for writing a type assertion (ex. `type as assertionType`).
*/
static assertion(type: WriterFunctionOrValue, assertionType: WriterFunctionOrValue): (writer: CodeBlockWriter) => void;
/**
* Gets a writer function for writing a return statement returning the provided value (ex. `return value;`).
* @param value - Value to be returned.
*/
static returnStatement(value: WriterFunctionOrValue): WriterFunction;
}

export declare type WriterFunctionOrValue = string | number | WriterFunction;
Expand Down
12 changes: 7 additions & 5 deletions src/structurePrinters/WriterFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,12 @@ export class WriterFunctions {
};
}

/** Gets a writer function for writing a union type. */
/** Gets a writer function for writing a union type (ex. `FirstType | SecondType`). */
static unionType(firstType: WriterFunctionOrValue, secondType: WriterFunctionOrValue, ...additionalTypes: WriterFunctionOrValue[]) {
return getWriteFunctionForUnionOrIntersectionType("|", [firstType, secondType, ...additionalTypes]);
}

/** Gets a writer function for writing an intersection type. */
/** Gets a writer function for writing an intersection type (ex. `FirstType & SecondType`). */
static intersectionType(firstType: WriterFunctionOrValue, secondType: WriterFunctionOrValue, ...additionalTypes: WriterFunctionOrValue[]) {
return getWriteFunctionForUnionOrIntersectionType("&", [firstType, secondType, ...additionalTypes]);
}
Expand All @@ -86,14 +86,16 @@ export class WriterFunctions {
}

/**
* Gets a writer function for writing a return statement returning the provided value.
* Gets a writer function for writing a return statement returning the provided value (ex. `return value;`).
* @param value - Value to be returned.
*/
static returnStatement(value: WriterFunctionOrValue): WriterFunction {
return (writer: CodeBlockWriter) => {
writer.write("return ");
writeValue(writer, value);
writer.write(";");
writer.withHangingIndentationUnlessBlock(() => {
writeValue(writer, value);
writer.write(";");
});
};
}
}
Expand Down
8 changes: 8 additions & 0 deletions src/tests/structurePrinters/writerFunctionsTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,5 +143,13 @@ describe(nameof<WriterFunctions>(), () => {
it("should write when specifying some value", () => {
doWriterTest((writer, { returnStatement }) => returnStatement("A")(writer), "return A;");
});

it("should write with hanging indentation when not using a block", () => {
doWriterTest((writer, { returnStatement }) => returnStatement("A\n&& B")(writer), "return A\n && B;");
});

it("should not write with hanging indentation when using a block", () => {
doWriterTest((writer, { returnStatement }) => returnStatement(() => writer.inlineBlock(() => writer.write("prop")))(writer), "return {\n prop\n};");
});
});
});

0 comments on commit db6602d

Please sign in to comment.