Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Infer the name of a destructured parameter #1704

Merged
merged 2 commits into from Sep 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 14 additions & 1 deletion src/lib/converter/plugins/CommentPlugin.ts
Expand Up @@ -349,8 +349,21 @@ export class CommentPlugin extends ConverterComponent {
childComment.tags ||= [...comment.tags];
}

signature.parameters?.forEach((parameter) => {
signature.parameters?.forEach((parameter, index) => {
let tag: CommentTag | undefined;
if (childComment && parameter.name === "__namedParameters") {
const commentParams = childComment?.tags.filter(
(tag) =>
tag.tagName === "param" &&
!tag.paramName.includes(".")
);
if (
signature.parameters?.length === commentParams.length &&
commentParams[index].paramName
) {
parameter.name = commentParams[index].paramName;
}
}
if (childComment) {
moveNestedParamTags(childComment, parameter);
tag = childComment.getTag("param", parameter.name);
Expand Down
96 changes: 96 additions & 0 deletions src/test/converter/function/function.ts
Expand Up @@ -79,6 +79,102 @@ export function functionWithRest(...rest: string[]): string {
return rest.join(", ");
}

/**
* This is a function with a destructured parameter.
*
* @param destructuredParam - This is the parameter that is destructured.
* @param destructuredParam.paramZ - This is a string parameter.
* @param destructuredParam.paramG - This is a parameter flagged with any.
* This sentence is placed in the next line.
*
* @param destructuredParam.paramA
* This is a **parameter** pointing to an interface.
*
* ~~~
* const value:BaseClass = new BaseClass('test');
* functionWithArguments('arg', 0, value);
* ~~~
*
* @returns This is the return value of the function.
*/
export function functionWithADestructuredParameter({
paramZ,
paramG,
paramA,
}: {
paramZ: string;
paramG: any;
paramA: Object;
}): number {
return 0;
}

/**
* This is a function with a destructured parameter and additional undocumented parameters.
* The `@param` directives are ignored because we cannot be certain which parameter they refer to.
*
* @param destructuredParam - This is the parameter that is destructured.
* @param destructuredParam.paramZ - This is a string parameter.
* @param destructuredParam.paramG - This is a parameter flagged with any.
* This sentence is placed in the next line.
*
* @param destructuredParam.paramA
* This is a **parameter** pointing to an interface.
*
* ~~~
* const value:BaseClass = new BaseClass('test');
* functionWithArguments('arg', 0, value);
* ~~~
*
* @returns This is the return value of the function.
*/
export function functionWithADestructuredParameterAndExtraParameters(
{
paramZ,
paramG,
paramA,
}: {
paramZ: string;
paramG: any;
paramA: Object;
},
extraParameter: string
): number {
return 0;
}

/**
* This is a function with a destructured parameter and an extra `@param` directive with no corresponding parameter.
* The `@param` directives are ignored because we cannot be certain which corresponds to the real parameter.
*
* @param fakeParameter - This directive does not have a corresponding parameter.
* @param destructuredParam - This is the parameter that is destructured.
* @param destructuredParam.paramZ - This is a string parameter.
* @param destructuredParam.paramG - This is a parameter flagged with any.
* This sentence is placed in the next line.
*
* @param destructuredParam.paramA
* This is a **parameter** pointing to an interface.
*
* ~~~
* const value:BaseClass = new BaseClass('test');
* functionWithArguments('arg', 0, value);
* ~~~
*
* @returns This is the return value of the function.
*/
export function functionWithADestructuredParameterAndAnExtraParamDirective({
paramZ,
paramG,
paramA,
}: {
paramZ: string;
paramG: any;
paramA: Object;
}): number {
return 0;
}

/**
* This is the first signature of a function with multiple signatures.
*
Expand Down