Skip to content

Commit

Permalink
fix(prettier-plugin-jsdoc): support multiline types
Browse files Browse the repository at this point in the history
  • Loading branch information
homer0 committed Oct 22, 2020
1 parent 99ac641 commit 2efd010
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 5 deletions.
26 changes: 21 additions & 5 deletions src/fns/renderTagInLine.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,29 @@ const { splitText } = require('./splitText');
* @type {RenderTagInLineFn}
*/
const renderTagInLine = R.curry((width, typePadding, namePadding, tag) => {
const tagHeader = `@${tag.tag}`;
const useNamePadding = ' '.repeat(namePadding);
const headerRest = tag.type ?
`${' '.repeat(typePadding)}{${tag.type}}${useNamePadding}${tag.name}` :
`${useNamePadding}${tag.name}`;
const header = `@${tag.tag}${headerRest}`;
let result;
if (tag.type) {
const useTypePadding = ' '.repeat(typePadding);
if (tag.type.includes('\n')) {
const typeLines = tag.type.split('\n');
const typeFirstLine = typeLines.shift();
const typeLastLine = typeLines.pop();
const header = `${tagHeader}${useTypePadding}`;
result = [
`${header}{${typeFirstLine}`,
...typeLines,
`${typeLastLine}}${useNamePadding}${tag.name}`,
];
} else {
const rest = `${useTypePadding}{${tag.type}}${useNamePadding}${tag.name}`.trimRight();
result = [`${tagHeader}${rest}`];
}
} else {
result = [`${tagHeader}${useNamePadding}${tag.name}`.trimRight()];
}

const result = [header.trimRight()];
if (tag.description) {
result.push(...splitText(tag.description, width));
}
Expand Down
19 changes: 19 additions & 0 deletions test/unit/fns/renderTagInLine.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,25 @@ describe('renderTagInLine', () => {
namePadding: 2,
},
},
{
it: 'should render a tag with a multiline type',
input: {
tag: 'type',
type: '{\n prop: boolean;\n}',
name: 'MyType',
description: '',
},
output: [
'@type {{',
' prop: boolean;',
'}} MyType',
],
options: {
width: 50,
typePadding: 1,
namePadding: 1,
},
},
];

it.each(cases)('should correctly format the case %#', (caseInfo) => {
Expand Down

0 comments on commit 2efd010

Please sign in to comment.