Skip to content

Commit

Permalink
Merge pull request #1704 from Gudahtt/infer-destructured-param-name
Browse files Browse the repository at this point in the history
Infer the name of a destructured parameter
  • Loading branch information
Gerrit0 committed Sep 23, 2021
2 parents 9494412 + 4118426 commit 01d9ac1
Show file tree
Hide file tree
Showing 3 changed files with 559 additions and 156 deletions.
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

0 comments on commit 01d9ac1

Please sign in to comment.