Skip to content

Commit e15bcd6

Browse files
committed
fix: Missing comments on variable functions
Closes #1421
1 parent d1f4bf9 commit e15bcd6

File tree

12 files changed

+311
-107
lines changed

12 files changed

+311
-107
lines changed

src/lib/converter/factories/comment.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -211,9 +211,6 @@ export function parseComment(
211211
}
212212

213213
currentTag = new CommentTag(tagName, paramName, line);
214-
if (!comment.tags) {
215-
comment.tags = [];
216-
}
217214
comment.tags.push(currentTag);
218215
}
219216

src/lib/converter/factories/signature.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ export function createSignature(
2323
| ReflectionKind.GetSignature
2424
| ReflectionKind.SetSignature,
2525
signature: ts.Signature,
26-
declaration?: ts.SignatureDeclaration
26+
declaration?: ts.SignatureDeclaration,
27+
commentDeclaration?: ts.Node
2728
) {
2829
assert(context.scope instanceof DeclarationReflection);
2930
// signature.getDeclaration might return undefined.
@@ -32,14 +33,10 @@ export function createSignature(
3233
| ts.SignatureDeclaration
3334
| undefined;
3435

35-
let commentDeclaration: ts.Node | undefined = declaration;
36-
if (
37-
commentDeclaration &&
38-
(ts.isArrowFunction(commentDeclaration) ||
39-
ts.isFunctionExpression(commentDeclaration))
40-
) {
41-
commentDeclaration = commentDeclaration.parent;
36+
if (!commentDeclaration && declaration && (ts.isArrowFunction(declaration) || ts.isFunctionExpression(declaration)) {
37+
commentDeclaration = declaration.parent;
4238
}
39+
commentDeclaration ??= declaration;
4340

4441
const sigRef = new SignatureReflection(
4542
context.scope.name,

src/lib/converter/plugins/CategoryPlugin.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -177,15 +177,13 @@ export class CategoryPlugin extends ConverterComponent {
177177
static getCategory(reflection: Reflection): string {
178178
function extractCategoryTag(comment: Comment) {
179179
const tags = comment.tags;
180-
if (tags) {
181-
const tagIndex = tags.findIndex(
182-
(tag) => tag.tagName === "category"
183-
);
184-
if (tagIndex >= 0) {
185-
const tag = tags[tagIndex].text;
186-
tags.splice(tagIndex, 1);
187-
return tag.trim();
188-
}
180+
const tagIndex = tags.findIndex(
181+
(tag) => tag.tagName === "category"
182+
);
183+
if (tagIndex >= 0) {
184+
const tag = tags[tagIndex].text;
185+
tags.splice(tagIndex, 1);
186+
return tag.trim();
189187
}
190188
return "";
191189
}

src/lib/converter/symbols.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -748,7 +748,9 @@ function convertVariableAsFunction(
748748
createSignature(
749749
reflectionContext,
750750
ReflectionKind.CallSignature,
751-
signature
751+
signature,
752+
void 0,
753+
declaration
752754
)
753755
);
754756
}

src/lib/models/comments/comment.ts

Lines changed: 13 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
import { removeIf } from "../../utils";
12
import { CommentTag } from "./tag";
23

34
/**
4-
* A model that represents a javadoc comment.
5+
* A model that represents a comment.
56
*
6-
* Instances of this model are created by the [[CommentHandler]]. You can retrieve comments
7-
* through the [[BaseReflection.comment]] property.
7+
* Instances of this model are created by the [[CommentPlugin]]. You can retrieve comments
8+
* through the [[DeclarationReflection.comment]] property.
89
*/
910
export class Comment {
1011
/**
@@ -24,9 +25,9 @@ export class Comment {
2425
returns?: string;
2526

2627
/**
27-
* All associated javadoc tags.
28+
* All associated tags.
2829
*/
29-
tags?: CommentTag[];
30+
tags: CommentTag[] = [];
3031

3132
/**
3233
* Creates a new Comment instance.
@@ -42,7 +43,7 @@ export class Comment {
4243
* @returns TRUE when this comment has a visible component.
4344
*/
4445
hasVisibleComponent(): boolean {
45-
return !!this.shortText || !!this.text || !!this.tags;
46+
return !!this.shortText || !!this.text || this.tags.length > 0;
4647
}
4748

4849
/**
@@ -52,15 +53,7 @@ export class Comment {
5253
* @returns TRUE when this comment contains a tag with the given name, otherwise FALSE.
5354
*/
5455
hasTag(tagName: string): boolean {
55-
if (!this.tags) {
56-
return false;
57-
}
58-
for (let i = 0, c = this.tags.length; i < c; i++) {
59-
if (this.tags[i].tagName === tagName) {
60-
return true;
61-
}
62-
}
63-
return false;
56+
return this.tags.some((tag) => tag.tagName === tagName);
6457
}
6558

6659
/**
@@ -73,7 +66,7 @@ export class Comment {
7366
* @returns The found tag or undefined.
7467
*/
7568
getTag(tagName: string, paramName?: string): CommentTag | undefined {
76-
return (this.tags || []).find((tag) => {
69+
return this.tags.find((tag) => {
7770
return (
7871
tag.tagName === tagName &&
7972
(paramName === void 0 || tag.paramName === paramName)
@@ -86,20 +79,7 @@ export class Comment {
8679
* @param tagName
8780
*/
8881
removeTags(tagName: string) {
89-
if (!this.tags) {
90-
return;
91-
}
92-
93-
let i = 0,
94-
c = this.tags.length ?? 0;
95-
while (i < c) {
96-
if (this.tags[i].tagName === tagName) {
97-
this.tags.splice(i, 1);
98-
c--;
99-
} else {
100-
i++;
101-
}
102-
}
82+
removeIf(this.tags, (tag) => tag.tagName === tagName);
10383
}
10484

10585
/**
@@ -111,10 +91,8 @@ export class Comment {
11191
this.shortText = comment.shortText;
11292
this.text = comment.text;
11393
this.returns = comment.returns;
114-
this.tags = comment.tags
115-
? comment.tags.map(
116-
(tag) => new CommentTag(tag.tagName, tag.paramName, tag.text)
117-
)
118-
: undefined;
94+
this.tags = comment.tags.map(
95+
(tag) => new CommentTag(tag.tagName, tag.paramName, tag.text)
96+
);
11997
}
12098
}

src/lib/serialization/serializers/comments/comment.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export class CommentSerializer extends SerializerComponent<Comment> {
2727
if (comment.returns) {
2828
obj.returns = comment.returns;
2929
}
30-
if (comment.tags && comment.tags.length) {
30+
if (comment.tags.length) {
3131
obj.tags = comment.tags.map((tag) => this.owner.toObject(tag));
3232
}
3333

src/lib/utils/array.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,20 @@ export function removeIfPresent<T>(arr: T[] | undefined, item: T) {
5959
}
6060
}
6161

62+
/**
63+
* Remove items in an array which match a predicate.
64+
* @param arr
65+
* @param predicate
66+
*/
67+
export function removeIf<T>(arr: T[], predicate: (item: T) => boolean) {
68+
const indices = filterMap(arr, (item, index) =>
69+
predicate(item) ? index : void 0
70+
);
71+
for (const index of indices.reverse()) {
72+
arr.splice(index, 1);
73+
}
74+
}
75+
6276
/**
6377
* Filters out duplicate values from the given iterable.
6478
* @param arr

src/lib/utils/index.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,14 @@ export type IfInternal<T, F> = InternalOnly extends true ? T : F;
3131
export type NeverIfInternal<T> = IfInternal<never, T>;
3232

3333
export { Options, ParameterType, ParameterHint, BindOption } from "./options";
34-
export { insertPrioritySorted, removeIfPresent } from "./array";
34+
export {
35+
insertPrioritySorted,
36+
removeIfPresent,
37+
removeIf,
38+
filterMap,
39+
unique,
40+
uniqueByEquals,
41+
} from "./array";
3542
export { Component, AbstractComponent, ChildableComponent } from "./component";
3643
export { Event, EventDispatcher } from "./events";
3744
export {

src/test/converter/function/function.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,3 +188,13 @@ export class Predicates {
188188
static assert(x: unknown): asserts x {}
189189
assertString(): asserts this is string {}
190190
}
191+
192+
/**
193+
* Returns true if fn returns true for every item in the iterator
194+
*
195+
* Returns true if the iterator is empty
196+
*/
197+
export const all: {
198+
<T>(fn: (item: T) => boolean, iterator: Iterable<T>): boolean;
199+
<T>(fn: (item: T) => boolean): (iterator: Iterable<T>) => boolean;
200+
} = () => false as any;

0 commit comments

Comments
 (0)