Skip to content

Commit

Permalink
feat: ArrayLiteralExpression - Add ability to add elements with write…
Browse files Browse the repository at this point in the history
…r for each element.
  • Loading branch information
dsherret committed Oct 6, 2018
1 parent 7b1261e commit 1f8b1cb
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 17 deletions.
12 changes: 6 additions & 6 deletions lib/ts-simple-ast.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5014,18 +5014,18 @@ export declare class ArrayLiteralExpression extends PrimaryExpression<ts.ArrayLi
getElements(): Expression[];
/**
* Adds an element to the array.
* @param text - Text to add as an element.
* @param textOrWriterFunction - Text to add as an element.
* @param options - Options.
*/
addElement(text: string, options?: {
addElement(textOrWriterFunction: string | WriterFunction, options?: {
useNewLines?: boolean;
}): Expression<ts.Expression>;
/**
* Adds elements to the array.
* @param texts - Texts to add as elements.
* @param textsOrWriterFunction - Texts to add as elements.
* @param options - Options.
*/
addElements(texts: ReadonlyArray<string>, options?: {
addElements(textsOrWriterFunction: ReadonlyArray<string | WriterFunction> | WriterFunction, options?: {
useNewLines?: boolean;
}): Expression<ts.Expression>[];
/**
Expand All @@ -5034,7 +5034,7 @@ export declare class ArrayLiteralExpression extends PrimaryExpression<ts.ArrayLi
* @param text - Text to insert as an element.
* @param options - Options.
*/
insertElement(index: number, text: string, options?: {
insertElement(index: number, textOrWriterFunction: string | WriterFunction, options?: {
useNewLines?: boolean;
}): Expression<ts.Expression>;
/**
Expand All @@ -5043,7 +5043,7 @@ export declare class ArrayLiteralExpression extends PrimaryExpression<ts.ArrayLi
* @param textsOrWriterFunction - Texts to insert as elements.
* @param options - Options.
*/
insertElements(index: number, textsOrWriterFunction: ReadonlyArray<string> | WriterFunction, options?: {
insertElements(index: number, textsOrWriterFunction: ReadonlyArray<string | WriterFunction> | WriterFunction, options?: {
useNewLines?: boolean;
}): Expression<ts.Expression>[];
/**
Expand Down
18 changes: 9 additions & 9 deletions src/compiler/ast/expression/array/ArrayLiteralExpression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,20 @@ export class ArrayLiteralExpression extends PrimaryExpression<ts.ArrayLiteralExp

/**
* Adds an element to the array.
* @param text - Text to add as an element.
* @param textOrWriterFunction - Text to add as an element.
* @param options - Options.
*/
addElement(text: string, options?: { useNewLines?: boolean; }) {
return this.addElements([text], options)[0];
addElement(textOrWriterFunction: string | WriterFunction, options?: { useNewLines?: boolean; }) {
return this.addElements([textOrWriterFunction], options)[0];
}

/**
* Adds elements to the array.
* @param texts - Texts to add as elements.
* @param textsOrWriterFunction - Texts to add as elements.
* @param options - Options.
*/
addElements(texts: ReadonlyArray<string>, options?: { useNewLines?: boolean; }) {
return this.insertElements(this.compilerNode.elements.length, texts, options);
addElements(textsOrWriterFunction: ReadonlyArray<string | WriterFunction> | WriterFunction, options?: { useNewLines?: boolean; }) {
return this.insertElements(this.compilerNode.elements.length, textsOrWriterFunction, options);
}

/**
Expand All @@ -38,8 +38,8 @@ export class ArrayLiteralExpression extends PrimaryExpression<ts.ArrayLiteralExp
* @param text - Text to insert as an element.
* @param options - Options.
*/
insertElement(index: number, text: string, options?: { useNewLines?: boolean; }) {
return this.insertElements(index, [text], options)[0];
insertElement(index: number, textOrWriterFunction: string | WriterFunction, options?: { useNewLines?: boolean; }) {
return this.insertElements(index, [textOrWriterFunction], options)[0];
}

/**
Expand All @@ -48,7 +48,7 @@ export class ArrayLiteralExpression extends PrimaryExpression<ts.ArrayLiteralExp
* @param textsOrWriterFunction - Texts to insert as elements.
* @param options - Options.
*/
insertElements(index: number, textsOrWriterFunction: ReadonlyArray<string> | WriterFunction, options: { useNewLines?: boolean; } = {}) {
insertElements(index: number, textsOrWriterFunction: ReadonlyArray<string | WriterFunction> | WriterFunction, options: { useNewLines?: boolean; } = {}) {
const elements = this.getElements();
index = verifyAndGetIndex(index, elements.length);
const useNewLines = getUseNewLines(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,21 @@ describe(nameof(ArrayLiteralExpression), () => {
doTest("var t = [2, 3, 4]", 0, ["1"], `var t = [\n 1,\n 2, 3, 4]`, { useNewLines: true });
});

function doWriterTest(text: string, index: number, writerFunction: WriterFunction, expectedText: string, options?: { useNewLines?: boolean; }) {
function doWriterTest(text: string, index: number, elements: WriterFunction | ReadonlyArray<string | WriterFunction>, expectedText: string,
options?: { useNewLines?: boolean; })
{
const {arrayLiteralExpression, sourceFile} = getArrayLiteralExpression(text);
const result = arrayLiteralExpression.insertElements(index, writerFunction, options);
arrayLiteralExpression.insertElements(index, elements, options);
expect(sourceFile.getFullText()).to.equal(expectedText);
}

it("should support writing with a writer", () => {
doWriterTest("var t = [1, 4, 5]", 1, writer => writer.writeLine("2,").write("3"), `var t = [1,\n 2,\n 3,\n 4, 5]`, { useNewLines: true });
});

it("should support passing in an array of writer functions and/or strings", () => {
doWriterTest("var t = [1, 4, 5]", 1, [writer => writer.write("2"), "3"], `var t = [1, 2, 3, 4, 5]`);
});
});

describe(nameof<ArrayLiteralExpression>(e => e.insertElement), () => {
Expand Down

0 comments on commit 1f8b1cb

Please sign in to comment.