Skip to content

Commit 656b53f

Browse files
Allow to modify the access level via doc comment tags, resolves #25
1 parent 6ed7691 commit 656b53f

File tree

15 files changed

+512
-62
lines changed

15 files changed

+512
-62
lines changed

bin/typedoc.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,7 @@ declare module td {
524524
* @param state The state that describes the current declaration and reflection.
525525
*/
526526
private onDeclaration(event);
527+
private applyAccessModifiers(reflection, comment);
527528
private onFunctionImplementation(event);
528529
/**
529530
* Triggered when the dispatcher enters the resolving phase.

bin/typedoc.js

Lines changed: 43 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1321,7 +1321,7 @@ var td;
13211321
return new td.UnionType(types);
13221322
}
13231323
function extractTypeParameterType(node, type) {
1324-
if (node) {
1324+
if (node && node.typeName) {
13251325
var name = node.typeName['text'];
13261326
if (typeParameters[name]) {
13271327
return typeParameters[name];
@@ -1955,8 +1955,6 @@ var td;
19551955
function visitTypeAliasDeclaration(node, scope) {
19561956
var alias = createDeclaration(scope, node, 4194304 /* TypeAlias */);
19571957
alias.type = extractType(alias, node.type, checker.getTypeAtLocation(node.type));
1958-
if (alias.name == 'Callback') {
1959-
}
19601958
return alias;
19611959
}
19621960
function visitExportAssignment(node, scope) {
@@ -2170,17 +2168,34 @@ var td;
21702168
* @param state The state that describes the current declaration and reflection.
21712169
*/
21722170
CommentPlugin.prototype.onDeclaration = function (event) {
2173-
if (event.reflection.kindOf(td.ReflectionKind.FunctionOrMethod)) {
2171+
var rawComment = CommentPlugin.getComment(event.node);
2172+
if (!rawComment)
21742173
return;
2174+
if (event.reflection.kindOf(td.ReflectionKind.FunctionOrMethod)) {
2175+
var comment = CommentPlugin.parseComment(rawComment, event.reflection.comment);
2176+
this.applyAccessModifiers(event.reflection, comment);
21752177
}
2176-
var comment = CommentPlugin.getComment(event.node);
2177-
if (comment) {
2178-
if (event.reflection.kindOf(2 /* Module */)) {
2179-
this.storeModuleComment(comment, event.reflection);
2180-
}
2181-
else {
2182-
event.reflection.comment = CommentPlugin.parseComment(comment, event.reflection.comment);
2183-
}
2178+
else if (event.reflection.kindOf(2 /* Module */)) {
2179+
this.storeModuleComment(rawComment, event.reflection);
2180+
}
2181+
else {
2182+
var comment = CommentPlugin.parseComment(rawComment, event.reflection.comment);
2183+
this.applyAccessModifiers(event.reflection, comment);
2184+
event.reflection.comment = comment;
2185+
}
2186+
};
2187+
CommentPlugin.prototype.applyAccessModifiers = function (reflection, comment) {
2188+
if (comment.hasTag('private')) {
2189+
reflection.setFlag(1 /* Private */);
2190+
CommentPlugin.removeTags(comment, 'private');
2191+
}
2192+
if (comment.hasTag('protected')) {
2193+
reflection.setFlag(2 /* Protected */);
2194+
CommentPlugin.removeTags(comment, 'protected');
2195+
}
2196+
if (comment.hasTag('public')) {
2197+
reflection.setFlag(4 /* Public */);
2198+
CommentPlugin.removeTags(comment, 'public');
21842199
}
21852200
};
21862201
CommentPlugin.prototype.onFunctionImplementation = function (event) {
@@ -2810,7 +2825,7 @@ var td;
28102825
group.children.forEach(function (child) {
28112826
someExported = child.flags.isExported || someExported;
28122827
allPrivate = child.flags.isPrivate && allPrivate;
2813-
allProtected = (child.flags.isPrivate || child.flags.isPrivate) && allProtected;
2828+
allProtected = (child.flags.isPrivate || child.flags.isProtected) && allProtected;
28142829
allExternal = child.flags.isExternal && allExternal;
28152830
allInherited = child.inheritedFrom && allInherited;
28162831
});
@@ -3685,12 +3700,24 @@ var td;
36853700
switch (flag) {
36863701
case 1 /* Private */:
36873702
this.flags.isPrivate = value;
3703+
if (value) {
3704+
this.setFlag(2 /* Protected */, false);
3705+
this.setFlag(4 /* Public */, false);
3706+
}
36883707
break;
36893708
case 2 /* Protected */:
36903709
this.flags.isProtected = value;
3710+
if (value) {
3711+
this.setFlag(1 /* Private */, false);
3712+
this.setFlag(4 /* Public */, false);
3713+
}
36913714
break;
36923715
case 4 /* Public */:
36933716
this.flags.isPublic = value;
3717+
if (value) {
3718+
this.setFlag(1 /* Private */, false);
3719+
this.setFlag(2 /* Protected */, false);
3720+
}
36943721
break;
36953722
case 8 /* Static */:
36963723
this.flags.isStatic = value;
@@ -4498,7 +4525,9 @@ var td;
44984525
var result = _super.prototype.toObject.call(this);
44994526
result.type = 'reference';
45004527
result.name = this.name;
4501-
result.symbolID = this.symbolID;
4528+
if (this.reflection) {
4529+
result.id = this.reflection.id;
4530+
}
45024531
return result;
45034532
};
45044533
/**

examples/basic/src/access.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/**
2+
* A variable that is made private via comment.
3+
* @private
4+
*/
5+
export var fakePrivateVariable = 'test';
6+
7+
/**
8+
* A variable that is made protected via comment.
9+
* @protected
10+
*/
11+
export var fakeProtectedVariable = 'test';
12+
13+
/**
14+
* A function that is made private via comment.
15+
* @private
16+
*/
17+
export function fakePrivateFunction() {}
18+
19+
/**
20+
* A function that is made protected via comment.
21+
* @protected
22+
*/
23+
export function fakeProtectedFunction() {}
24+
25+
/**
26+
* A class that is documented as being private.
27+
* @private
28+
*/
29+
export class PrivateClass
30+
{
31+
/**
32+
* A variable that is made private via comment.
33+
* @private
34+
*/
35+
fakePrivateVariable:string;
36+
37+
/**
38+
* A variable that is made protected via comment.
39+
* @protected
40+
*/
41+
fakeProtectedVariable:string;
42+
43+
/**
44+
* A function that is made private via comment.
45+
* @private
46+
*/
47+
fakePrivateFunction() {}
48+
49+
/**
50+
* A function that is made protected via comment.
51+
* @protected
52+
*/
53+
fakeProtectedFunction() {}
54+
}

examples/basic/src/typescript-1.4.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ class SimpleClass
9191
/**
9292
* A generic function using a generic type alias.
9393
*
94+
* Uses [[GenericCallback]] instead of [[Callback]].
95+
*
9496
* @param T Some type argument.
9597
* @param arr A generic array.
9698
* @param callback Some generic type alias callback.

src/td/converter/Converter.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1050,9 +1050,6 @@ module td
10501050
function visitTypeAliasDeclaration(node:ts.TypeAliasDeclaration, scope:ContainerReflection):Reflection {
10511051
var alias = createDeclaration(scope, node, ReflectionKind.TypeAlias);
10521052
alias.type = extractType(alias, node.type, checker.getTypeAtLocation(node.type));
1053-
if (alias.name == 'Callback') {
1054-
// console.log(alias);
1055-
}
10561053
return alias;
10571054
}
10581055

src/td/converter/plugins/CommentPlugin.ts

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -103,17 +103,36 @@ module td
103103
* @param state The state that describes the current declaration and reflection.
104104
*/
105105
private onDeclaration(event:CompilerEvent) {
106+
var rawComment = CommentPlugin.getComment(event.node);
107+
if (!rawComment) return;
108+
106109
if (event.reflection.kindOf(ReflectionKind.FunctionOrMethod)) {
107-
return;
110+
var comment = CommentPlugin.parseComment(rawComment, event.reflection.comment);
111+
this.applyAccessModifiers(event.reflection, comment);
112+
} else if (event.reflection.kindOf(ReflectionKind.Module)) {
113+
this.storeModuleComment(rawComment, event.reflection);
114+
} else {
115+
var comment = CommentPlugin.parseComment(rawComment, event.reflection.comment);
116+
this.applyAccessModifiers(event.reflection, comment);
117+
event.reflection.comment = comment;
108118
}
119+
}
109120

110-
var comment = CommentPlugin.getComment(event.node);
111-
if (comment) {
112-
if (event.reflection.kindOf(ReflectionKind.Module)) {
113-
this.storeModuleComment(comment, event.reflection);
114-
} else {
115-
event.reflection.comment = CommentPlugin.parseComment(comment, event.reflection.comment);
116-
}
121+
122+
private applyAccessModifiers(reflection:Reflection, comment:Comment) {
123+
if (comment.hasTag('private')) {
124+
reflection.setFlag(ReflectionFlag.Private);
125+
CommentPlugin.removeTags(comment, 'private');
126+
}
127+
128+
if (comment.hasTag('protected')) {
129+
reflection.setFlag(ReflectionFlag.Protected);
130+
CommentPlugin.removeTags(comment, 'protected');
131+
}
132+
133+
if (comment.hasTag('public')) {
134+
reflection.setFlag(ReflectionFlag.Public);
135+
CommentPlugin.removeTags(comment, 'public');
117136
}
118137
}
119138

src/td/converter/plugins/GroupPlugin.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ module td
146146
group.children.forEach((child) => {
147147
someExported = child.flags.isExported || someExported;
148148
allPrivate = child.flags.isPrivate && allPrivate;
149-
allProtected = (child.flags.isPrivate || child.flags.isPrivate) && allProtected;
149+
allProtected = (child.flags.isPrivate || child.flags.isProtected) && allProtected;
150150
allExternal = child.flags.isExternal && allExternal;
151151
allInherited = child.inheritedFrom && allInherited;
152152
});

src/td/models/Reflection.ts

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -347,15 +347,45 @@ module td
347347
}
348348

349349
switch (flag) {
350-
case ReflectionFlag.Private: this.flags.isPrivate = value; break;
351-
case ReflectionFlag.Protected: this.flags.isProtected = value; break;
352-
case ReflectionFlag.Public: this.flags.isPublic = value; break;
353-
case ReflectionFlag.Static: this.flags.isStatic = value; break;
354-
case ReflectionFlag.Exported: this.flags.isExported = value; break;
355-
case ReflectionFlag.External: this.flags.isExternal = value; break;
356-
case ReflectionFlag.Optional: this.flags.isOptional = value; break;
357-
case ReflectionFlag.Rest: this.flags.isRest = value; break;
358-
case ReflectionFlag.ExportAssignment: this.flags.hasExportAssignment = value; break;
350+
case ReflectionFlag.Private:
351+
this.flags.isPrivate = value;
352+
if (value) {
353+
this.setFlag(ReflectionFlag.Protected, false);
354+
this.setFlag(ReflectionFlag.Public, false);
355+
}
356+
break;
357+
case ReflectionFlag.Protected:
358+
this.flags.isProtected = value;
359+
if (value) {
360+
this.setFlag(ReflectionFlag.Private, false);
361+
this.setFlag(ReflectionFlag.Public, false);
362+
}
363+
break;
364+
case ReflectionFlag.Public:
365+
this.flags.isPublic = value;
366+
if (value) {
367+
this.setFlag(ReflectionFlag.Private, false);
368+
this.setFlag(ReflectionFlag.Protected, false);
369+
}
370+
break;
371+
case ReflectionFlag.Static:
372+
this.flags.isStatic = value;
373+
break;
374+
case ReflectionFlag.Exported:
375+
this.flags.isExported = value;
376+
break;
377+
case ReflectionFlag.External:
378+
this.flags.isExternal = value;
379+
break;
380+
case ReflectionFlag.Optional:
381+
this.flags.isOptional = value;
382+
break;
383+
case ReflectionFlag.Rest:
384+
this.flags.isRest = value;
385+
break;
386+
case ReflectionFlag.ExportAssignment:
387+
this.flags.hasExportAssignment = value;
388+
break;
359389
}
360390
}
361391

test/converter/access/access.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/**
2+
* A variable that is made private via comment.
3+
* @private
4+
*/
5+
export var fakePrivateVariable = 'test';
6+
7+
/**
8+
* A variable that is made protected via comment.
9+
* @protected
10+
*/
11+
export var fakeProtectedVariable = 'test';
12+
13+
/**
14+
* A function that is made private via comment.
15+
* @private
16+
*/
17+
export function fakePrivateFunction() {}
18+
19+
/**
20+
* A function that is made protected via comment.
21+
* @protected
22+
*/
23+
export function fakeProtectedFunction() {}
24+
25+
/**
26+
* A class that is documented as being private.
27+
* @private
28+
*/
29+
export class PrivateClass
30+
{
31+
/**
32+
* A variable that is made private via comment.
33+
* @private
34+
*/
35+
fakePrivateVariable:string;
36+
37+
/**
38+
* A variable that is made protected via comment.
39+
* @protected
40+
*/
41+
fakeProtectedVariable:string;
42+
43+
/**
44+
* A function that is made private via comment.
45+
* @private
46+
*/
47+
fakePrivateFunction() {}
48+
49+
/**
50+
* A function that is made protected via comment.
51+
* @protected
52+
*/
53+
fakeProtectedFunction() {}
54+
}

0 commit comments

Comments
 (0)