Skip to content

Commit

Permalink
feat(parser): do not remove default values
Browse files Browse the repository at this point in the history
  • Loading branch information
RunDevelopment authored and hosseinmd committed Feb 1, 2021
1 parent 0db143b commit 36f500d
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 3 deletions.
11 changes: 10 additions & 1 deletion src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,19 @@ export const getParser = (parser: Parser["parse"]) =>
// Optional tag name
if (optional) {
// Figure out if tag type have default value
_default = _default?.trim();
if (_default) {
description = description
.trim()
.replace(/[ \t]*Default is `.*`\.?$/, "");
if (description && !/[.\r\n]$/.test(description)) {
description += ".";
}
description += ` Default is \`${_default}\``;
name = `[${name}=${_default}]`;
} else {
name = `[${name}]`;
}
name = `[${name}]`;
}

if (isVerticallyAlignAbleTags)
Expand Down
26 changes: 24 additions & 2 deletions tests/__snapshots__/main.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,28 @@ exports[`Should convert to single line if necessary 3`] = `
"
`;

exports[`Should format jsDoc default values 1`] = `
"/**
* @param {String} [arg1=\\"defaultTest\\"] Foo. Default is \`\\"defaultTest\\"\`
* @param {number} [arg2=123] The width of the rectangle. Default is \`123\`
* @param {number} [arg3=123] Default is \`123\`
* @param {number} [arg4=Foo.bar.baz] Default is \`Foo.bar.baz\`
* @param {number | string} [arg5=123] Something. Default is \`123\`
*/
"
`;

exports[`Should format jsDoc default values 2`] = `
"/**
* @param {String} [arg1=\\"defaultTest\\"] Foo. Default is \`\\"defaultTest\\"\`
* @param {number} [arg2=123] The width of the rectangle. Default is \`123\`
* @param {number} [arg3=123] Default is \`123\`
* @param {number} [arg4=Foo.bar.baz] Default is \`Foo.bar.baz\`
* @param {number | string} [arg5=123] Something. Default is \`123\`
*/
"
`;

exports[`Should format regular jsDoc 1`] = `
"/**
* Function example description that was wrapped by hand so it have more then
Expand All @@ -155,7 +177,7 @@ exports[`Should format regular jsDoc 1`] = `
*
* @param {String | Number} text - Some text description that is very long and
* needs to be wrapped
* @param {String} [defaultValue] TODO Default is \`\\"defaultTest\\"\`
* @param {String} [defaultValue=\\"defaultTest\\"] TODO. Default is \`\\"defaultTest\\"\`
* @param {Number | Null} [optionalNumber]
* @undefiendTag
* @undefiendTag {number} name des
Expand Down Expand Up @@ -185,7 +207,7 @@ exports[`Should format regular jsDoc 2`] = `
*
* @param {String | Number} text - Some text description that is very long and
* needs to be wrapped
* @param {String} [defaultValue] TODO Default is \`\\"defaultTest\\"\`
* @param {String} [defaultValue=\\"defaultTest\\"] TODO. Default is \`\\"defaultTest\\"\`
* @param {Number | Null} [optionalNumber]
* @undefiendTag
* @undefiendTag {number} name des
Expand Down
15 changes: 15 additions & 0 deletions tests/main.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,21 @@ const testFunction = (text, defaultValue, optionalNumber) => true
expect(subject(result)).toMatchSnapshot();
});

test("Should format jsDoc default values", () => {
const result = subject(`
/**
* @param {String} [arg1="defaultTest"] foo
* @param {number} [arg2=123] the width of the rectangle
* @param {number} [arg3= 123 ]
* @param {number} [arg4= Foo.bar.baz ]
* @param {number|string} [arg5=123] Something. Default is \`"wrong"\`
*/
`);

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

test("Should convert to single line if necessary", () => {
const Result1 = subject(`/** single line description*/`);
const Result2 = subject(`/**
Expand Down

0 comments on commit 36f500d

Please sign in to comment.