Skip to content

Commit 238fa0f

Browse files
authored
test: add tests for array and object destructure in class methods (buehler#72)
Adding tests for object and array destructuring patterns. Now there are tests for methods, functions and constructors. Closes buehler#70.
1 parent b96650f commit 238fa0f

File tree

3 files changed

+162
-1
lines changed

3 files changed

+162
-1
lines changed

test/TypescriptParser.spec.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ describe('TypescriptParser', () => {
363363
});
364364

365365
it('should parse a file', () => {
366-
expect(parsed.declarations).toHaveLength(7);
366+
expect(parsed.declarations).toHaveLength(8);
367367
});
368368

369369
it('should parse an abstract class', () => {
@@ -432,6 +432,18 @@ describe('TypescriptParser', () => {
432432
expect(parsedClass.accessors).toMatchSnapshot();
433433
});
434434

435+
it('should parse object and array destructure pattern in a class method', () => {
436+
const parsedClass = parsed.declarations[7] as ClassDeclaration;
437+
438+
expect(parsedClass.methods).toMatchSnapshot();
439+
});
440+
441+
it('should parse object and array destructure pattern in a class constructor', () => {
442+
const parsedClass = parsed.declarations[7] as ClassDeclaration;
443+
444+
expect(parsedClass.ctor).toMatchSnapshot();
445+
});
446+
435447
});
436448

437449
describe('Modules', () => {

test/__snapshots__/TypescriptParser.spec.ts.snap

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,145 @@ ClassDeclaration {
195195
}
196196
`;
197197

198+
exports[`TypescriptParser Declaration parsing Classes should parse object and array destructure pattern in a class constructor 1`] = `
199+
ConstructorDeclaration {
200+
"end": 1198,
201+
"name": "ObjAndArrDestruct",
202+
"parameters": Array [
203+
ParameterDeclaration {
204+
"end": 1167,
205+
"name": "p1",
206+
"start": 1165,
207+
"type": undefined,
208+
},
209+
ParameterDeclaration {
210+
"end": 1171,
211+
"name": "p2",
212+
"start": 1169,
213+
"type": undefined,
214+
},
215+
ParameterDeclaration {
216+
"end": 1183,
217+
"name": "p3",
218+
"start": 1181,
219+
"type": undefined,
220+
},
221+
ParameterDeclaration {
222+
"end": 1187,
223+
"name": "p4",
224+
"start": 1185,
225+
"type": undefined,
226+
},
227+
],
228+
"start": 1151,
229+
"variables": Array [],
230+
}
231+
`;
232+
233+
exports[`TypescriptParser Declaration parsing Classes should parse object and array destructure pattern in a class method 1`] = `
234+
Array [
235+
MethodDeclaration {
236+
"end": 1251,
237+
"isAbstract": false,
238+
"name": "objMethod",
239+
"parameters": Array [
240+
ParameterDeclaration {
241+
"end": 1225,
242+
"name": "p1",
243+
"start": 1223,
244+
"type": undefined,
245+
},
246+
ParameterDeclaration {
247+
"end": 1229,
248+
"name": "p2",
249+
"start": 1227,
250+
"type": undefined,
251+
},
252+
ParameterDeclaration {
253+
"end": 1233,
254+
"name": "p3",
255+
"start": 1231,
256+
"type": undefined,
257+
},
258+
],
259+
"start": 1204,
260+
"type": "void",
261+
"variables": Array [],
262+
"visibility": 2,
263+
},
264+
MethodDeclaration {
265+
"end": 1307,
266+
"isAbstract": false,
267+
"name": "arrMethod",
268+
"parameters": Array [
269+
ParameterDeclaration {
270+
"end": 1277,
271+
"name": "p1",
272+
"start": 1275,
273+
"type": undefined,
274+
},
275+
ParameterDeclaration {
276+
"end": 1281,
277+
"name": "p2",
278+
"start": 1279,
279+
"type": undefined,
280+
},
281+
ParameterDeclaration {
282+
"end": 1285,
283+
"name": "p3",
284+
"start": 1283,
285+
"type": undefined,
286+
},
287+
],
288+
"start": 1257,
289+
"type": "void",
290+
"variables": Array [],
291+
"visibility": 2,
292+
},
293+
MethodDeclaration {
294+
"end": 1386,
295+
"isAbstract": false,
296+
"name": "objAndArrMethod",
297+
"parameters": Array [
298+
ParameterDeclaration {
299+
"end": 1339,
300+
"name": "p1",
301+
"start": 1337,
302+
"type": undefined,
303+
},
304+
ParameterDeclaration {
305+
"end": 1343,
306+
"name": "p2",
307+
"start": 1341,
308+
"type": undefined,
309+
},
310+
ParameterDeclaration {
311+
"end": 1347,
312+
"name": "p3",
313+
"start": 1345,
314+
"type": undefined,
315+
},
316+
ParameterDeclaration {
317+
"end": 1364,
318+
"name": "p4",
319+
"start": 1362,
320+
"type": undefined,
321+
},
322+
ParameterDeclaration {
323+
"end": 1368,
324+
"name": "p5",
325+
"start": 1366,
326+
"type": undefined,
327+
},
328+
],
329+
"start": 1313,
330+
"type": "void",
331+
"variables": Array [],
332+
"visibility": 2,
333+
},
334+
]
335+
`;
336+
198337
exports[`TypescriptParser Declaration parsing Classes should parse property accessors 1`] = `
199338
Array [
200339
GetterDeclaration {

test/_workspace/typescript-parser/class.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,13 @@ abstract class AbstractPropertyAccessors {
5454
public abstract get getAndSet(): string;
5555
public abstract set getAndSet(value: string);
5656
}
57+
58+
class ObjAndArrDestruct {
59+
constructor({ p1, p2 }: any, [p3, p4]: any) { }
60+
61+
public objMethod({ p1, p2, p3 }: any): void { }
62+
63+
public arrMethod([p1, p2, p3]: string[]): void { }
64+
65+
public objAndArrMethod([p1, p2, p3]: string[], { p4, p5 }: any): void { }
66+
}

0 commit comments

Comments
 (0)