Reproduction
Input:
/**
* @returns {number[]} [x, y]
*/
export function get() {}
Output (prettier-plugin-jsdoc 1.8.1, default options):
/**
* @returns {number[] | undefined} X, y
*/
export function get() {}
Expected: unchanged. @returns takes no name, so [x, y] is plain description text.
Cause
comment-parser's name tokenizer reads [x, y] as an optional name: { name: "x, y", optional: true, description: "" }.
@returns is in TAGS_NAMELESS, so the name is folded back into the description, but optional is left true:
https://github.com/hosseinmd/prettier-plugin-jsdoc/blob/75825bae4a9e0f74a0539aeeada777d3e54e38d2/src/parser.ts#L478-L481
assignOptionalAndDefaultToName then sees optional with an empty name and appends | undefined to the type:
https://github.com/hosseinmd/prettier-plugin-jsdoc/blob/75825bae4a9e0f74a0539aeeada777d3e54e38d2/src/parser.ts#L681
- The description formatter capitalises the first letter, giving
X, y.
Every tag in TAGS_NAMELESS (@returns, @throws, @description, @example, …) whose description begins with [ is affected.
Suggested fix
Reset the optional/default state when the name is folded into the description, in src/parser.ts#L478-L481:
if (name && TAGS_NAMELESS.includes(tag)) {
description = `${name} ${description}`;
name = "";
optional = false;
default_ = undefined;
}
Reproduction
Input:
Output (
prettier-plugin-jsdoc1.8.1, default options):Expected: unchanged.
@returnstakes no name, so[x, y]is plain description text.Cause
comment-parser's name tokenizer reads[x, y]as an optional name:{ name: "x, y", optional: true, description: "" }.@returnsis inTAGS_NAMELESS, so the name is folded back into the description, butoptionalis lefttrue:https://github.com/hosseinmd/prettier-plugin-jsdoc/blob/75825bae4a9e0f74a0539aeeada777d3e54e38d2/src/parser.ts#L478-L481
assignOptionalAndDefaultToNamethen seesoptionalwith an empty name and appends| undefinedto the type:https://github.com/hosseinmd/prettier-plugin-jsdoc/blob/75825bae4a9e0f74a0539aeeada777d3e54e38d2/src/parser.ts#L681
X, y.Every tag in
TAGS_NAMELESS(@returns,@throws,@description,@example, …) whose description begins with[is affected.Suggested fix
Reset the optional/default state when the name is folded into the description, in
src/parser.ts#L478-L481: