Skip to content

Commit cf3dcdc

Browse files
Merge pull request #77 from awerlang/master
Create property declarations from constructor parameter properties
2 parents e677fe4 + db61dbd commit cf3dcdc

File tree

4 files changed

+168
-13
lines changed

4 files changed

+168
-13
lines changed

src/td/converter/converters/convertNode.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,36 @@ module td.converter
431431
return member;
432432
}
433433

434+
/**
435+
* Analyze parameters in given constructor declaration node and create a suitable reflection.
436+
*
437+
* @param context The context object describing the current state the converter is in.
438+
* @param node The constructor declaration node that should be analyzed.
439+
* @return The resulting reflection or NULL.
440+
*/
441+
function visitConstructorModifiers(context:Context, node:ts.ConstructorDeclaration) {
442+
node.parameters.forEach(param => {
443+
var visibility = param.flags & (ts.NodeFlags.Public | ts.NodeFlags.Protected | ts.NodeFlags.Private);
444+
if (!visibility) return;
445+
446+
var property = createDeclaration(context, param, models.ReflectionKind.Property);
447+
if (!property) return;
448+
449+
property.setFlag(models.ReflectionFlag.Static, false);
450+
property.type = convertType(context, param.type, context.getTypeAtLocation(param));
451+
452+
var sourceComment = CommentPlugin.getComment(node);
453+
if (sourceComment) {
454+
var constructorComment = CommentPlugin.parseComment(sourceComment);
455+
if (constructorComment) {
456+
var tag = constructorComment.getTag('param', property.name);
457+
if (tag && tag.text) {
458+
property.comment = CommentPlugin.parseComment(tag.text);
459+
}
460+
}
461+
}
462+
});
463+
}
434464

435465
/**
436466
* Analyze the given constructor declaration node and create a suitable reflection.
@@ -444,6 +474,7 @@ module td.converter
444474
var hasBody = !!node.body;
445475
var method = createDeclaration(context, node, models.ReflectionKind.Constructor, 'constructor');
446476

477+
visitConstructorModifiers(context, node);
447478
context.withScope(method, () => {
448479
if (!hasBody || !method.signatures) {
449480
var name = 'new ' + parent.name;

test/converter/class/class.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,16 @@ export class TestSubClass extends TestClass
6363
* protectedMethod short text.
6464
*/
6565
protected protectedMethod() {}
66+
67+
/**
68+
* Constructor short text.
69+
*
70+
* @param p1 Constructor param
71+
* @param p2 Private string property
72+
* @param p3 Public number property
73+
* @param p4 Public implicit any property
74+
*/
75+
constructor(p1, private p2: string, public p3: number, public p4) {
76+
super();
77+
}
6678
}

test/converter/class/specs.json

Lines changed: 124 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@
266266
},
267267
"children": [
268268
{
269-
"id": 23,
269+
"id": 21,
270270
"name": "constructor",
271271
"kind": 512,
272272
"kindString": "Constructor",
@@ -278,34 +278,143 @@
278278
},
279279
"signatures": [
280280
{
281-
"id": 24,
281+
"id": 25,
282282
"name": "new TestSubClass",
283283
"kind": 16384,
284284
"kindString": "Constructor signature",
285285
"flags": {},
286286
"comment": {
287287
"shortText": "Constructor short text."
288288
},
289+
"parameters": [
290+
{
291+
"id": 26,
292+
"name": "p1",
293+
"kind": 32768,
294+
"kindString": "Parameter",
295+
"flags": {},
296+
"comment": {
297+
"shortText": "Constructor param"
298+
},
299+
"type": {
300+
"type": "instrinct",
301+
"name": "any"
302+
}
303+
},
304+
{
305+
"id": 27,
306+
"name": "p2",
307+
"kind": 32768,
308+
"kindString": "Parameter",
309+
"flags": {},
310+
"comment": {
311+
"shortText": "Private string property"
312+
},
313+
"type": {
314+
"type": "instrinct",
315+
"name": "string"
316+
}
317+
},
318+
{
319+
"id": 28,
320+
"name": "p3",
321+
"kind": 32768,
322+
"kindString": "Parameter",
323+
"flags": {},
324+
"comment": {
325+
"shortText": "Public number property"
326+
},
327+
"type": {
328+
"type": "instrinct",
329+
"name": "number"
330+
}
331+
},
332+
{
333+
"id": 29,
334+
"name": "p4",
335+
"kind": 32768,
336+
"kindString": "Parameter",
337+
"flags": {},
338+
"comment": {
339+
"shortText": "Public implicit any property\n"
340+
},
341+
"type": {
342+
"type": "instrinct",
343+
"name": "any"
344+
}
345+
}
346+
],
289347
"type": {
290348
"type": "reference",
291349
"name": "TestSubClass",
292350
"id": 16
293351
},
294-
"inheritedFrom": {
352+
"overwrites": {
295353
"type": "reference",
296354
"name": "TestClass.__constructor",
297355
"id": 6
298356
}
299357
}
300358
],
301-
"inheritedFrom": {
359+
"overwrites": {
302360
"type": "reference",
303361
"name": "TestClass.__constructor",
304362
"id": 6
305363
}
306364
},
307365
{
308-
"id": 21,
366+
"id": 22,
367+
"name": "p2",
368+
"kind": 1024,
369+
"kindString": "Property",
370+
"flags": {
371+
"isPrivate": true,
372+
"isExported": true
373+
},
374+
"comment": {
375+
"shortText": "Private string property"
376+
},
377+
"type": {
378+
"type": "instrinct",
379+
"name": "string"
380+
}
381+
},
382+
{
383+
"id": 23,
384+
"name": "p3",
385+
"kind": 1024,
386+
"kindString": "Property",
387+
"flags": {
388+
"isPublic": true,
389+
"isExported": true
390+
},
391+
"comment": {
392+
"shortText": "Public number property"
393+
},
394+
"type": {
395+
"type": "instrinct",
396+
"name": "number"
397+
}
398+
},
399+
{
400+
"id": 24,
401+
"name": "p4",
402+
"kind": 1024,
403+
"kindString": "Property",
404+
"flags": {
405+
"isPublic": true,
406+
"isExported": true
407+
},
408+
"comment": {
409+
"shortText": "Public implicit any property"
410+
},
411+
"type": {
412+
"type": "instrinct",
413+
"name": "any"
414+
}
415+
},
416+
{
417+
"id": 30,
309418
"name": "publicProperty",
310419
"kind": 1024,
311420
"kindString": "Property",
@@ -327,7 +436,7 @@
327436
}
328437
},
329438
{
330-
"id": 22,
439+
"id": 31,
331440
"name": "staticProperty",
332441
"kind": 1024,
333442
"kindString": "Property",
@@ -422,7 +531,7 @@
422531
}
423532
},
424533
{
425-
"id": 25,
534+
"id": 32,
426535
"name": "staticMethod",
427536
"kind": 2048,
428537
"kindString": "Method",
@@ -432,7 +541,7 @@
432541
},
433542
"signatures": [
434543
{
435-
"id": 26,
544+
"id": 33,
436545
"name": "staticMethod",
437546
"kind": 4096,
438547
"kindString": "Call signature",
@@ -463,15 +572,18 @@
463572
"title": "Constructors",
464573
"kind": 512,
465574
"children": [
466-
23
575+
21
467576
]
468577
},
469578
{
470579
"title": "Properties",
471580
"kind": 1024,
472581
"children": [
473-
21,
474-
22
582+
22,
583+
23,
584+
24,
585+
30,
586+
31
475587
]
476588
},
477589
{
@@ -480,7 +592,7 @@
480592
"children": [
481593
19,
482594
17,
483-
25
595+
32
484596
]
485597
}
486598
],

test/renderer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ function compareDirectories(a, b) {
2626
var bFiles = getFileIndex(b);
2727
Assert.deepEqual(aFiles, bFiles, "Generated files differ.");
2828

29-
var gitHubRegExp = /https:\/\/github.com\/sebastian-lenz\/typedoc\/blob\/[^\/]*\/examples/g;
29+
var gitHubRegExp = /https:\/\/github.com\/[A-Za-z0-9\-]+\/typedoc\/blob\/[^\/]*\/examples/g;
3030
aFiles.forEach(function (file) {
3131
var aSrc = FS.readFileSync(Path.join(a, file), {encoding:'utf-8'})
3232
.replace("\r", '')

0 commit comments

Comments
 (0)