Skip to content

Commit

Permalink
fix: Missing comments on variable functions
Browse files Browse the repository at this point in the history
Closes #1421
  • Loading branch information
Gerrit0 committed Dec 26, 2020
1 parent d1f4bf9 commit e15bcd6
Show file tree
Hide file tree
Showing 12 changed files with 311 additions and 107 deletions.
3 changes: 0 additions & 3 deletions src/lib/converter/factories/comment.ts
Expand Up @@ -211,9 +211,6 @@ export function parseComment(
}

currentTag = new CommentTag(tagName, paramName, line);
if (!comment.tags) {
comment.tags = [];
}
comment.tags.push(currentTag);
}

Expand Down
13 changes: 5 additions & 8 deletions src/lib/converter/factories/signature.ts
Expand Up @@ -23,7 +23,8 @@ export function createSignature(
| ReflectionKind.GetSignature
| ReflectionKind.SetSignature,
signature: ts.Signature,
declaration?: ts.SignatureDeclaration
declaration?: ts.SignatureDeclaration,
commentDeclaration?: ts.Node
) {
assert(context.scope instanceof DeclarationReflection);
// signature.getDeclaration might return undefined.
Expand All @@ -32,14 +33,10 @@ export function createSignature(
| ts.SignatureDeclaration
| undefined;

let commentDeclaration: ts.Node | undefined = declaration;
if (
commentDeclaration &&
(ts.isArrowFunction(commentDeclaration) ||
ts.isFunctionExpression(commentDeclaration))
) {
commentDeclaration = commentDeclaration.parent;
if (!commentDeclaration && declaration && (ts.isArrowFunction(declaration) || ts.isFunctionExpression(declaration)) {
commentDeclaration = declaration.parent;
}
commentDeclaration ??= declaration;

const sigRef = new SignatureReflection(
context.scope.name,
Expand Down
16 changes: 7 additions & 9 deletions src/lib/converter/plugins/CategoryPlugin.ts
Expand Up @@ -177,15 +177,13 @@ export class CategoryPlugin extends ConverterComponent {
static getCategory(reflection: Reflection): string {
function extractCategoryTag(comment: Comment) {
const tags = comment.tags;
if (tags) {
const tagIndex = tags.findIndex(
(tag) => tag.tagName === "category"
);
if (tagIndex >= 0) {
const tag = tags[tagIndex].text;
tags.splice(tagIndex, 1);
return tag.trim();
}
const tagIndex = tags.findIndex(
(tag) => tag.tagName === "category"
);
if (tagIndex >= 0) {
const tag = tags[tagIndex].text;
tags.splice(tagIndex, 1);
return tag.trim();
}
return "";
}
Expand Down
4 changes: 3 additions & 1 deletion src/lib/converter/symbols.ts
Expand Up @@ -748,7 +748,9 @@ function convertVariableAsFunction(
createSignature(
reflectionContext,
ReflectionKind.CallSignature,
signature
signature,
void 0,
declaration
)
);
}
Expand Down
48 changes: 13 additions & 35 deletions src/lib/models/comments/comment.ts
@@ -1,10 +1,11 @@
import { removeIf } from "../../utils";
import { CommentTag } from "./tag";

/**
* A model that represents a javadoc comment.
* A model that represents a comment.
*
* Instances of this model are created by the [[CommentHandler]]. You can retrieve comments
* through the [[BaseReflection.comment]] property.
* Instances of this model are created by the [[CommentPlugin]]. You can retrieve comments
* through the [[DeclarationReflection.comment]] property.
*/
export class Comment {
/**
Expand All @@ -24,9 +25,9 @@ export class Comment {
returns?: string;

/**
* All associated javadoc tags.
* All associated tags.
*/
tags?: CommentTag[];
tags: CommentTag[] = [];

/**
* Creates a new Comment instance.
Expand All @@ -42,7 +43,7 @@ export class Comment {
* @returns TRUE when this comment has a visible component.
*/
hasVisibleComponent(): boolean {
return !!this.shortText || !!this.text || !!this.tags;
return !!this.shortText || !!this.text || this.tags.length > 0;
}

/**
Expand All @@ -52,15 +53,7 @@ export class Comment {
* @returns TRUE when this comment contains a tag with the given name, otherwise FALSE.
*/
hasTag(tagName: string): boolean {
if (!this.tags) {
return false;
}
for (let i = 0, c = this.tags.length; i < c; i++) {
if (this.tags[i].tagName === tagName) {
return true;
}
}
return false;
return this.tags.some((tag) => tag.tagName === tagName);
}

/**
Expand All @@ -73,7 +66,7 @@ export class Comment {
* @returns The found tag or undefined.
*/
getTag(tagName: string, paramName?: string): CommentTag | undefined {
return (this.tags || []).find((tag) => {
return this.tags.find((tag) => {
return (
tag.tagName === tagName &&
(paramName === void 0 || tag.paramName === paramName)
Expand All @@ -86,20 +79,7 @@ export class Comment {
* @param tagName
*/
removeTags(tagName: string) {
if (!this.tags) {
return;
}

let i = 0,
c = this.tags.length ?? 0;
while (i < c) {
if (this.tags[i].tagName === tagName) {
this.tags.splice(i, 1);
c--;
} else {
i++;
}
}
removeIf(this.tags, (tag) => tag.tagName === tagName);
}

/**
Expand All @@ -111,10 +91,8 @@ export class Comment {
this.shortText = comment.shortText;
this.text = comment.text;
this.returns = comment.returns;
this.tags = comment.tags
? comment.tags.map(
(tag) => new CommentTag(tag.tagName, tag.paramName, tag.text)
)
: undefined;
this.tags = comment.tags.map(
(tag) => new CommentTag(tag.tagName, tag.paramName, tag.text)
);
}
}
2 changes: 1 addition & 1 deletion src/lib/serialization/serializers/comments/comment.ts
Expand Up @@ -27,7 +27,7 @@ export class CommentSerializer extends SerializerComponent<Comment> {
if (comment.returns) {
obj.returns = comment.returns;
}
if (comment.tags && comment.tags.length) {
if (comment.tags.length) {
obj.tags = comment.tags.map((tag) => this.owner.toObject(tag));
}

Expand Down
14 changes: 14 additions & 0 deletions src/lib/utils/array.ts
Expand Up @@ -59,6 +59,20 @@ export function removeIfPresent<T>(arr: T[] | undefined, item: T) {
}
}

/**
* Remove items in an array which match a predicate.
* @param arr
* @param predicate
*/
export function removeIf<T>(arr: T[], predicate: (item: T) => boolean) {
const indices = filterMap(arr, (item, index) =>
predicate(item) ? index : void 0
);
for (const index of indices.reverse()) {
arr.splice(index, 1);
}
}

/**
* Filters out duplicate values from the given iterable.
* @param arr
Expand Down
9 changes: 8 additions & 1 deletion src/lib/utils/index.ts
Expand Up @@ -31,7 +31,14 @@ export type IfInternal<T, F> = InternalOnly extends true ? T : F;
export type NeverIfInternal<T> = IfInternal<never, T>;

export { Options, ParameterType, ParameterHint, BindOption } from "./options";
export { insertPrioritySorted, removeIfPresent } from "./array";
export {
insertPrioritySorted,
removeIfPresent,
removeIf,
filterMap,
unique,
uniqueByEquals,
} from "./array";
export { Component, AbstractComponent, ChildableComponent } from "./component";
export { Event, EventDispatcher } from "./events";
export {
Expand Down
10 changes: 10 additions & 0 deletions src/test/converter/function/function.ts
Expand Up @@ -188,3 +188,13 @@ export class Predicates {
static assert(x: unknown): asserts x {}
assertString(): asserts this is string {}
}

/**
* Returns true if fn returns true for every item in the iterator
*
* Returns true if the iterator is empty
*/
export const all: {
<T>(fn: (item: T) => boolean, iterator: Iterable<T>): boolean;
<T>(fn: (item: T) => boolean): (iterator: Iterable<T>) => boolean;
} = () => false as any;

0 comments on commit e15bcd6

Please sign in to comment.