forked from dsherret/ts-morph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ArrayLiteralExpression.ts
121 lines (106 loc) · 4.97 KB
/
ArrayLiteralExpression.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import * as errors from "../../../../errors";
import { getNodesToReturn, insertIntoCommaSeparatedNodes, removeCommaSeparatedChild, verifyAndGetIndex } from "../../../../manipulation";
import { CommaNewLineSeparatedStructuresPrinter, CommaSeparatedStructuresPrinter, StringStructurePrinter } from "../../../../structurePrinters";
import { WriterFunction } from "../../../../types";
import { SyntaxKind, ts } from "../../../../typescript";
import { Expression } from "../Expression";
import { PrimaryExpression } from "../PrimaryExpression";
export class ArrayLiteralExpression extends PrimaryExpression<ts.ArrayLiteralExpression> {
/**
* Gets the array's elements.
*/
getElements(): Expression[] {
return this.compilerNode.elements.map(e => this._getNodeFromCompilerNode(e));
}
/**
* Adds an element to the array.
* @param textOrWriterFunction - Text to add as an element.
* @param options - Options.
*/
addElement(textOrWriterFunction: string | WriterFunction, options?: { useNewLines?: boolean; }) {
return this.addElements([textOrWriterFunction], options)[0];
}
/**
* Adds elements to the array.
* @param textsOrWriterFunction - Texts to add as elements.
* @param options - Options.
*/
addElements(textsOrWriterFunction: ReadonlyArray<string | WriterFunction> | WriterFunction, options?: { useNewLines?: boolean; }) {
return this.insertElements(this.compilerNode.elements.length, textsOrWriterFunction, options);
}
/**
* Insert an element into the array.
* @param index - Child index to insert at.
* @param text - Text to insert as an element.
* @param options - Options.
*/
insertElement(index: number, textOrWriterFunction: string | WriterFunction, options?: { useNewLines?: boolean; }) {
return this.insertElements(index, [textOrWriterFunction], options)[0];
}
/**
* Insert elements into the array.
* @param index - Child index to insert at.
* @param textsOrWriterFunction - Texts to insert as elements.
* @param options - Options.
*/
insertElements(index: number, textsOrWriterFunction: ReadonlyArray<string | WriterFunction> | WriterFunction, options: { useNewLines?: boolean; } = {}) {
const elements = this.getElements();
index = verifyAndGetIndex(index, elements.length);
const useNewLines = getUseNewLines(this);
const writer = useNewLines ? this._getWriterWithChildIndentation() : this._getWriterWithQueuedChildIndentation();
const stringStructurePrinter = new StringStructurePrinter();
const structurePrinter = useNewLines ?
new CommaNewLineSeparatedStructuresPrinter(stringStructurePrinter) :
new CommaSeparatedStructuresPrinter(stringStructurePrinter);
structurePrinter.printText(writer, textsOrWriterFunction);
return insertTexts(this);
function insertTexts(node: ArrayLiteralExpression) {
insertIntoCommaSeparatedNodes({
parent: node.getFirstChildByKindOrThrow(SyntaxKind.SyntaxList),
currentNodes: elements,
insertIndex: index,
newText: writer.toString(),
useNewLines
});
const newElements = node.getElements();
return getNodesToReturn(newElements, index, newElements.length - elements.length);
}
function getUseNewLines(node: ArrayLiteralExpression) {
if (options.useNewLines != null)
return options.useNewLines;
if (elements.length > 1)
return allElementsOnDifferentLines();
return node.getStartLineNumber() !== node.getEndLineNumber();
function allElementsOnDifferentLines() {
let previousLine = elements[0].getStartLineNumber();
for (let i = 1; i < elements.length; i++) {
const currentLine = elements[i].getStartLineNumber();
if (previousLine === currentLine)
return false;
previousLine = currentLine;
}
return true;
}
}
}
/**
* Removes an element from the array.
* @param index - Index to remove from.
*/
removeElement(index: number): void;
/**
* Removes an element from the array.
* @param element - Element to remove.
*/
removeElement(element: Expression): void;
removeElement(elementOrIndex: Expression | number) {
const elements = this.getElements();
if (elements.length === 0)
throw new errors.InvalidOperationError("Cannot remove an element when none exist.");
const elementToRemove = typeof elementOrIndex === "number" ? getElementFromIndex(elementOrIndex) : elementOrIndex;
removeCommaSeparatedChild(elementToRemove);
function getElementFromIndex(index: number) {
return elements[verifyAndGetIndex(index, elements.length - 1)];
}
}
}