forked from dsherret/ts-morph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ObjectLiteralExpression.ts
302 lines (264 loc) · 12.8 KB
/
ObjectLiteralExpression.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
import * as errors from "../../../../errors";
import { getNodesToReturn, insertIntoCommaSeparatedNodes, verifyAndGetIndex } from "../../../../manipulation";
import { CommaNewLineSeparatedStructuresPrinter, StructurePrinter } from "../../../../structurePrinters";
import { GetAccessorDeclarationStructure, MethodDeclarationStructure, PropertyAssignmentStructure, SetAccessorDeclarationStructure,
ShorthandPropertyAssignmentStructure, SpreadAssignmentStructure } from "../../../../structures";
import { SyntaxKind, ts } from "../../../../typescript";
import { ArrayUtils } from "../../../../utils";
import { ObjectLiteralElementLike } from "../../aliases";
import { GetAccessorDeclaration, MethodDeclaration, SetAccessorDeclaration } from "../../class";
import { PrimaryExpression } from "../PrimaryExpression";
import { PropertyAssignment } from "./PropertyAssignment";
import { ShorthandPropertyAssignment } from "./ShorthandPropertyAssignment";
import { SpreadAssignment } from "./SpreadAssignment";
import { callBaseGetStructure } from "../../callBaseGetStructure";
export const ObjectLiteralExpressionBase = PrimaryExpression;
export class ObjectLiteralExpression extends ObjectLiteralExpressionBase<ts.ObjectLiteralExpression> {
/**
* Gets the first property by the provided name or throws.
* @param name - Name of the property.
*/
getPropertyOrThrow(name: string): ObjectLiteralElementLike;
/**
* Gets the first property that matches the provided find function or throws.
* @param findFunction - Find function.
*/
getPropertyOrThrow(findFunction: (property: ObjectLiteralElementLike) => boolean): ObjectLiteralElementLike;
getPropertyOrThrow(nameOrFindFunction: string | ((property: ObjectLiteralElementLike) => boolean)): ObjectLiteralElementLike {
return errors.throwIfNullOrUndefined(this.getProperty(nameOrFindFunction), "Expected to find a property.");
}
/**
* Gets the first property by the provided name or returns undefined.
* @param name - Name of the property.
*/
getProperty(name: string): ObjectLiteralElementLike | undefined;
/**
* Gets the first property that matches the provided find function or returns undefined.
* @param findFunction - Find function.
*/
getProperty(findFunction: (property: ObjectLiteralElementLike) => boolean): ObjectLiteralElementLike | undefined;
/** @internal */
getProperty(nameOrFindFunction: string | ((property: ObjectLiteralElementLike) => boolean)): ObjectLiteralElementLike | undefined;
getProperty(nameOrFindFunction: string | ((property: ObjectLiteralElementLike) => boolean)): ObjectLiteralElementLike | undefined {
let findFunc: (property: ObjectLiteralElementLike) => boolean;
if (typeof nameOrFindFunction === "string")
findFunc = prop => {
if ((prop as any)[nameof<PropertyAssignment>(o => o.getName)] == null)
return false;
return (prop as PropertyAssignment).getName() === nameOrFindFunction;
};
else
findFunc = nameOrFindFunction;
return ArrayUtils.find(this.getProperties(), findFunc);
}
/**
* Gets the properties.
*/
getProperties(): ObjectLiteralElementLike[] {
const properties: ts.NodeArray<ts.ObjectLiteralElementLike> = this.compilerNode.properties; // explicit type for validation
return properties.map(p => this._getNodeFromCompilerNode(p));
}
/* Property Assignments */
/**
* Adds a property assignment.
* @param structure - Structure that represents the property assignment to add.
*/
addPropertyAssignment(structure: PropertyAssignmentStructure) {
return this.addPropertyAssignments([structure])[0];
}
/**
* Adds property assignments.
* @param structures - Structure that represents the property assignments to add.
*/
addPropertyAssignments(structures: ReadonlyArray<PropertyAssignmentStructure>) {
return this.insertPropertyAssignments(this.compilerNode.properties.length, structures);
}
/**
* Inserts a property assignment at the specified index.
* @param index - Child index to insert at.
* @param structure - Structure that represents the property assignment to insert.
*/
insertPropertyAssignment(index: number, structure: PropertyAssignmentStructure) {
return this.insertPropertyAssignments(index, [structure])[0];
}
/**
* Inserts property assignments at the specified index.
* @param index - Child index to insert at.
* @param structures - Structures that represent the property assignments to insert.
*/
insertPropertyAssignments(index: number, structures: ReadonlyArray<PropertyAssignmentStructure>) {
return this._insertProperty(index, structures, () => this._context.structurePrinterFactory.forPropertyAssignment()) as PropertyAssignment[];
}
/* Shorthand Property Assignments */
/**
* Adds a shorthand property assignment.
* @param structure - Structure that represents the shorthand property assignment to add.
*/
addShorthandPropertyAssignment(structure: ShorthandPropertyAssignmentStructure) {
return this.addShorthandPropertyAssignments([structure])[0];
}
/**
* Adds shorthand property assignments.
* @param structures - Structure that represents the shorthand property assignments to add.
*/
addShorthandPropertyAssignments(structures: ReadonlyArray<ShorthandPropertyAssignmentStructure>) {
return this.insertShorthandPropertyAssignments(this.compilerNode.properties.length, structures);
}
/**
* Inserts a shorthand property assignment at the specified index.
* @param index - Child index to insert at.
* @param structure - Structure that represents the shorthand property assignment to insert.
*/
insertShorthandPropertyAssignment(index: number, structure: ShorthandPropertyAssignmentStructure) {
return this.insertShorthandPropertyAssignments(index, [structure])[0];
}
/**
* Inserts shorthand property assignments at the specified index.
* @param index - Child index to insert at.
* @param structures - Structures that represent the shorthand property assignments to insert.
*/
insertShorthandPropertyAssignments(index: number, structures: ReadonlyArray<ShorthandPropertyAssignmentStructure>) {
return this._insertProperty(index, structures, () => this._context.structurePrinterFactory.forShorthandPropertyAssignment()) as ShorthandPropertyAssignment[];
}
/* Spread Assignments */
/**
* Adds a spread assignment.
* @param structure - Structure that represents the spread assignment to add.
*/
addSpreadAssignment(structure: SpreadAssignmentStructure) {
return this.addSpreadAssignments([structure])[0];
}
/**
* Adds spread assignments.
* @param structures - Structure that represents the spread assignments to add.
*/
addSpreadAssignments(structures: ReadonlyArray<SpreadAssignmentStructure>) {
return this.insertSpreadAssignments(this.compilerNode.properties.length, structures);
}
/**
* Inserts a spread assignment at the specified index.
* @param index - Child index to insert at.
* @param structure - Structure that represents the spread assignment to insert.
*/
insertSpreadAssignment(index: number, structure: SpreadAssignmentStructure) {
return this.insertSpreadAssignments(index, [structure])[0];
}
/**
* Inserts spread assignments at the specified index.
* @param index - Child index to insert at.
* @param structures - Structures that represent the spread assignments to insert.
*/
insertSpreadAssignments(index: number, structures: ReadonlyArray<SpreadAssignmentStructure>) {
return this._insertProperty(index, structures, () => this._context.structurePrinterFactory.forSpreadAssignment()) as SpreadAssignment[];
}
/* Method Declarations */
/**
* Adds a method.
* @param structure - Structure that represents the method to add.
*/
addMethod(structure: MethodDeclarationStructure) {
return this.addMethods([structure])[0];
}
/**
* Adds methods.
* @param structures - Structure that represents the methods to add.
*/
addMethods(structures: ReadonlyArray<MethodDeclarationStructure>) {
return this.insertMethods(this.compilerNode.properties.length, structures);
}
/**
* Inserts a method at the specified index.
* @param index - Child index to insert at.
* @param structure - Structure that represents the method to insert.
*/
insertMethod(index: number, structure: MethodDeclarationStructure) {
return this.insertMethods(index, [structure])[0];
}
/**
* Inserts methods at the specified index.
* @param index - Child index to insert at.
* @param structures - Structures that represent the methods to insert.
*/
insertMethods(index: number, structures: ReadonlyArray<MethodDeclarationStructure>) {
return this._insertProperty(index, structures, () => this._context.structurePrinterFactory.forMethodDeclaration({ isAmbient: false })) as MethodDeclaration[];
}
/* Get Accessor Declarations */
/**
* Adds a get accessor.
* @param structure - Structure that represents the property assignment to add.
*/
addGetAccessor(structure: GetAccessorDeclarationStructure) {
return this.addGetAccessors([structure])[0];
}
/**
* Adds get accessors.
* @param structures - Structure that represents the get accessors to add.
*/
addGetAccessors(structures: ReadonlyArray<GetAccessorDeclarationStructure>) {
return this.insertGetAccessors(this.compilerNode.properties.length, structures);
}
/**
* Inserts a get accessor at the specified index.
* @param index - Child index to insert at.
* @param structure - Structure that represents the get accessor to insert.
*/
insertGetAccessor(index: number, structure: GetAccessorDeclarationStructure) {
return this.insertGetAccessors(index, [structure])[0];
}
/**
* Inserts get accessors at the specified index.
* @param index - Child index to insert at.
* @param structures - Structures that represent the get accessors to insert.
*/
insertGetAccessors(index: number, structures: ReadonlyArray<GetAccessorDeclarationStructure>) {
return this._insertProperty(index, structures, () => this._context.structurePrinterFactory.forGetAccessorDeclaration({ isAmbient: false })) as GetAccessorDeclaration[];
}
/* Set Accessor Declarations */
/**
* Adds a set accessor.
* @param structure - Structure that represents the property assignment to add.
*/
addSetAccessor(structure: SetAccessorDeclarationStructure) {
return this.addSetAccessors([structure])[0];
}
/**
* Adds set accessors.
* @param structures - Structure that represents the set accessors to add.
*/
addSetAccessors(structures: ReadonlyArray<SetAccessorDeclarationStructure>) {
return this.insertSetAccessors(this.compilerNode.properties.length, structures);
}
/**
* Inserts a set accessor at the specified index.
* @param index - Child index to insert at.
* @param structure - Structure that represents the set accessor to insert.
*/
insertSetAccessor(index: number, structure: SetAccessorDeclarationStructure) {
return this.insertSetAccessors(index, [structure])[0];
}
/**
* Inserts set accessors at the specified index.
* @param index - Child index to insert at.
* @param structures - Structures that represent the set accessors to insert.
*/
insertSetAccessors(index: number, structures: ReadonlyArray<SetAccessorDeclarationStructure>) {
return this._insertProperty(index, structures, () => this._context.structurePrinterFactory.forSetAccessorDeclaration({ isAmbient: false })) as SetAccessorDeclaration[];
}
/**
* @internal
*/
private _insertProperty<T>(index: number, structures: ReadonlyArray<T>, createStructurePrinter: () => StructurePrinter<T>) {
index = verifyAndGetIndex(index, this.compilerNode.properties.length);
const writer = this._getWriterWithChildIndentation();
const structurePrinter = new CommaNewLineSeparatedStructuresPrinter(createStructurePrinter());
structurePrinter.printText(writer, structures);
insertIntoCommaSeparatedNodes({
parent: this.getFirstChildByKindOrThrow(SyntaxKind.SyntaxList),
currentNodes: this.getProperties(),
insertIndex: index,
newText: writer.toString(),
useNewLines: true
});
return getNodesToReturn(this.getProperties(), index, structures.length);
}
}