Skip to content

Commit

Permalink
fix: optional type without name
Browse files Browse the repository at this point in the history
issue: #153
  • Loading branch information
hosseinmd committed Mar 24, 2022
1 parent bd31495 commit b14397c
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 13 deletions.
43 changes: 30 additions & 13 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ export const getParser = (originalParse: Parser["parse"], parserName: string) =>
parsersOrOptions: Parameters<Parser["parse"]>[1],
maybeOptions?: AllOptions,
): AST {
const parsers = parsersOrOptions;
let options = (maybeOptions ?? parsersOrOptions) as AllOptions;
const parsers = parsersOrOptions;
let options = (maybeOptions ?? parsersOrOptions) as AllOptions;
const prettierParse =
findPluginByParser(parserName, options)?.parse || originalParse;

Expand Down Expand Up @@ -107,10 +107,6 @@ export const getParser = (originalParse: Parser["parse"], parserName: string) =>
});

type = convertToModernType(type);
type = formatType(type, {
...options,
printWidth: commentContentPrintWidth,
});
}

return {
Expand Down Expand Up @@ -138,6 +134,19 @@ export const getParser = (originalParse: Parser["parse"], parserName: string) =>

tags = tags
.map(assignOptionalAndDefaultToName)
.map(({ type, ...rest }) => {
if (type) {
type = formatType(type, {
...options,
printWidth: commentContentPrintWidth,
});
}

return {
...rest,
type,
} as Spec;
})
.map(({ type, name, description, tag, ...rest }) => {
const isVerticallyAlignAbleTags =
TAGS_VERTICALLY_ALIGN_ABLE.includes(tag);
Expand Down Expand Up @@ -527,21 +536,29 @@ function assignOptionalAndDefaultToName({
name,
optional,
default: default_,
tag,
type,
...rest
}: Spec): Spec {
if (name && optional) {
// Figure out if tag type have default value
if (default_) {
name = `[${name}=${default_}]`;
if (optional) {
if (name) {
// Figure out if tag type have default value
if (default_) {
name = `[${name}=${default_}]`;
} else {
name = `[${name}]`;
}
} else {
name = `[${name}]`;
type = `${type} | undefined`;
}
}

return {
...rest,
name: name,
optional: optional,
tag,
name,
optional,
type,
default: default_,
};
}
9 changes: 9 additions & 0 deletions tests/__snapshots__/main.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,15 @@ exports[`Optional parameters 1`] = `
"
`;
exports[`Optional params 1`] = `
"/**
* @param {string} [p2] - An optional param (Google Closure syntax)
* @param {string} [p3] - Another optional param (JSDoc syntax).
* @returns {string | undefined}
*/
"
`;
exports[`Should align vertically param|property|returns|yields|throws if option set to true, and amount of spaces is different than default 1`] = `
"/**
* @property {Object} unalginedProp Unaligned property descriptin
Expand Down
12 changes: 12 additions & 0 deletions tests/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -671,3 +671,15 @@ test("example with tab intention", () => {
),
).toMatchSnapshot();
});

test("Optional params", () => {
const result = subject(`
/**
* @param {string=} p2 - An optional param (Google Closure syntax)
* @param {string} [p3] - Another optional param (JSDoc syntax).
* @returns {string=}
*/
`);

expect(result).toMatchSnapshot();
});

0 comments on commit b14397c

Please sign in to comment.