Skip to content

Commit

Permalink
feat(prettier-plugin-jsdoc): add rule to ignore consistent columns
Browse files Browse the repository at this point in the history
  • Loading branch information
homer0 committed Oct 26, 2020
1 parent 11e3d5c commit 1573db8
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/fns/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -307,10 +307,16 @@ const render = R.curry((options, column, block) => {
const data = getLengthsData(block.tags);
if (options.jsdocGroupColumnsByTag) {
const tagsData = getTagsData(data.byTag, width, options);
const atLeastOneCannot = Object.entries(tagsData).find(([tagName, info]) => (
!options.jsdocAllowDescriptionOnNewLinesForTags.includes(tagName) &&
!info.canUseColumns
));
let atLeastOneCannot;
if (options.jsdocIgnoreNewLineDescriptionsForConsistentColumns) {
atLeastOneCannot = Object.entries(tagsData).find(([tagName, info]) => (
!options.jsdocAllowDescriptionOnNewLinesForTags.includes(tagName) &&
!info.canUseColumns
));
} else {
atLeastOneCannot = Object.values(tagsData).find((info) => !info.canUseColumns);
}

if (atLeastOneCannot && options.jsdocConsistentColumns) {
lines.push(...renderTagsInlines(width, options, block.tags));
} else {
Expand Down
8 changes: 8 additions & 0 deletions src/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,14 @@ const options = {
],
}],
},
jsdocIgnoreNewLineDescriptionsForConsistentColumns: {
type: 'boolean',
category: 'jsdoc',
default: true,
description:
'If enabled, when evaluating the rule for consistent columns, tags with description ' +
'on a new line, allowed by `jsdocAllowDescriptionOnNewLinesForTags`, will be ignored.',
},
};
/**
* @type {PJPOptions}
Expand Down
3 changes: 3 additions & 0 deletions src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@
* period. Default `true`.
* @property {string[]} jsdocAllowDescriptionOnNewLinesForTags
* A list of tags that are allowed to have their description on a new line.
* @property {boolean} jsdocIgnoreNewLineDescriptionsForConsistentColumns
* If enabled, when evaluating the rule for consistent columns, tags with description on a new line,
* allowed by `jsdocAllowDescriptionOnNewLinesForTags`, will be ignored. Default `true`.
*/

/* eslint-disable max-len */
Expand Down
60 changes: 60 additions & 0 deletions test/unit/fns/render.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,12 @@ describe('render', () => {
description: 'Lorem ipsum description for the age.',
descriptionParagrah: true,
},
{
tag: 'version',
type: '',
name: '1.0.0',
description: '',
},
],
},
output: [
Expand All @@ -532,6 +538,7 @@ describe('render', () => {
'Lorem ipsum description for the name.',
'@property {number} age',
'Lorem ipsum description for the age.',
'@version 1.0.0',
],
column: 0,
options: {
Expand All @@ -543,6 +550,59 @@ describe('render', () => {
],
},
},
{
it: 'should consider inline tags for consistent columns',
input: {
description: '',
tags: [
{
tag: 'callback',
type: '',
name: 'LoremIpsumFn',
description: '',
},
{
tag: 'param',
type: 'string',
name: 'name',
description: 'Lorem ipsum description for the name.',
descriptionParagrah: true,
},
{
tag: 'param',
type: 'number',
name: 'age',
description: 'Lorem ipsum description for the age.',
descriptionParagrah: true,
},
{
tag: 'returns',
type: 'Person',
name: '',
description: 'A new person.',
},
],
},
output: [
'@callback LoremIpsumFn',
'@param {string} name',
'Lorem ipsum description for the name.',
'@param {number} age',
'Lorem ipsum description for the age.',
'@returns {Person}',
'A new person.',
],
column: 0,
options: {
...defaultOptions,
jsdocPrintWidth: 80,
jsdocIgnoreNewLineDescriptionsForConsistentColumns: false,
jsdocAllowDescriptionOnNewLinesForTags: [
...defaultOptions.jsdocAllowDescriptionOnNewLinesForTags,
'param',
],
},
},
];

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

0 comments on commit 1573db8

Please sign in to comment.