Skip to content

Commit

Permalink
Detect default values on destructured parameters
Browse files Browse the repository at this point in the history
Resolves #2430
  • Loading branch information
Gerrit0 committed Dec 29, 2023
1 parent 6370490 commit 6150075
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
- TypeDoc now inherits `typedocOptions` fields from extended tsconfig files, #2334.
- Methods which return function types no longer have duplicated comments, #2336.
- Comments on function-like type aliases will now show up under the type alias, rather than nested within the type declaration, #2372.
- Improved detection of default values for parameters with destructured values, #2430.
- Fix crash when converting some complicated union/intersection types, #2451.
- Navigation triangle markers should no longer display on a separate line with some font settings, #2457.
- `@group` and `@category` organization is now applied later to allow inherited comments to create groups/categories, #2459.
Expand Down
22 changes: 22 additions & 0 deletions src/lib/converter/factories/signature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ function convertParameters(
}

paramRefl.setFlag(ReflectionFlag.Rest, isRest);
checkForDestructuredParameterDefaults(paramRefl, parameterNodes?.[i]);
return paramRefl;
});
}
Expand Down Expand Up @@ -261,10 +262,31 @@ export function convertParameterNodes(
: !!param.typeExpression &&
ts.isJSDocVariadicType(param.typeExpression.type),
);
checkForDestructuredParameterDefaults(paramRefl, param);
return paramRefl;
});
}

function checkForDestructuredParameterDefaults(
param: ParameterReflection,
decl: ts.ParameterDeclaration | ts.JSDocParameterTag | undefined,
) {
if (!decl || !ts.isParameter(decl)) return;
if (param.name !== "__namedParameters") return;
if (!ts.isObjectBindingPattern(decl.name)) return;
if (param.type?.type !== "reflection") return;

for (const child of param.type.declaration.children || []) {
const tsChild = decl.name.elements.find(
(el) => (el.propertyName || el.name).getText() === child.name,
);

if (tsChild) {
child.defaultValue = convertDefaultValue(tsChild);
}
}
}

function convertTypeParameters(
context: Context,
parent: Reflection,
Expand Down
9 changes: 6 additions & 3 deletions src/test/converter/variables/specs.json
Original file line number Diff line number Diff line change
Expand Up @@ -687,7 +687,8 @@
"name": "boolean"
}
]
}
},
"defaultValue": "false"
},
{
"id": 21,
Expand Down Expand Up @@ -716,7 +717,8 @@
]
}
]
}
},
"defaultValue": "..."
},
{
"id": 20,
Expand All @@ -736,7 +738,8 @@
"name": "string"
}
]
}
},
"defaultValue": "\"\""
}
],
"groups": [
Expand Down
9 changes: 6 additions & 3 deletions src/test/converter/variables/specs.nodoc.json
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,8 @@
"name": "boolean"
}
]
}
},
"defaultValue": "false"
},
{
"id": 21,
Expand Down Expand Up @@ -356,7 +357,8 @@
]
}
]
}
},
"defaultValue": "..."
},
{
"id": 20,
Expand All @@ -376,7 +378,8 @@
"name": "string"
}
]
}
},
"defaultValue": "\"\""
}
],
"groups": [
Expand Down
11 changes: 11 additions & 0 deletions src/test/converter2/issues/gh2430.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* @param {object} props
* @param {any} props.value Value
* @param {any} props.trueValue True default
* @param {any} props.falseValue False default
*/
export const Checkbox = ({
value,
trueValue = true,
falseValue: renamed = false,
}) => {};
19 changes: 19 additions & 0 deletions src/test/issues.c2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1276,6 +1276,25 @@ describe("Issue Tests", () => {
);
});

it("Handles destructured object parameter defaults, #2430", () => {
const project = convert();
const Checkbox = querySig(project, "Checkbox");
equal(Checkbox.parameters?.length, 1);
equal(Checkbox.parameters[0].name, "props");
const type = Checkbox.parameters[0].type;
equal(type?.type, "reflection");
equal(type.declaration.children?.map((c) => c.name), [
"falseValue",
"trueValue",
"value",
]);
equal(type.declaration.children?.map((c) => c.defaultValue), [
"false",
"true",
undefined,
]);
});

it("Handles function-namespaces created with Object.assign #2436", () => {
const project = convert();
equal(query(project, "bug").kind, ReflectionKind.Function);
Expand Down

0 comments on commit 6150075

Please sign in to comment.