Skip to content

Commit

Permalink
feat(check-line-alignment): add `preserveMainDescriptionPostDelimit…
Browse files Browse the repository at this point in the history
…er` option to preserve left-hand side spacings in the main description when using the `always` option.

Co-authored-by: Renatho De Carli Rosa <renatho@automattic.com>
  • Loading branch information
brettz9 and renatho committed May 13, 2021
1 parent c6d9e7e commit 54dac34
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 3 deletions.
8 changes: 6 additions & 2 deletions src/alignTransform.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ const space = (len) => {
return ''.padStart(len, ' ');
};

const alignTransform = (tags, indent) => {
const alignTransform = (tags, indent, preserveMainDescriptionPostDelimiter) => {
let intoTags = false;
let width;

Expand Down Expand Up @@ -145,7 +145,11 @@ const alignTransform = (tags, indent) => {
/* eslint-enable */

if (!intoTags) {
tokens.postDelimiter = tokens.description === '' ? '' : ' ';
if (tokens.description === '') {
tokens.postDelimiter = '';
} else if (!preserveMainDescriptionPostDelimiter) {
tokens.postDelimiter = ' ';
}

return {
...line,
Expand Down
9 changes: 8 additions & 1 deletion src/rules/checkLineAlignment.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,12 @@ const checkAlignment = ({
indent,
jsdoc,
jsdocNode,
preserveMainDescriptionPostDelimiter,
report,
tags,
utils,
}) => {
const transform = commentFlow(alignTransform(tags, indent));
const transform = commentFlow(alignTransform(tags, indent, preserveMainDescriptionPostDelimiter));
const transformedJsdoc = transform(jsdoc);

const comment = '/*' + jsdocNode.value + '*/';
Expand All @@ -131,6 +132,7 @@ export default iterateJsdoc(({
}) => {
const {
tags: applicableTags = ['param', 'arg', 'argument', 'property', 'prop', 'returns', 'return'],
preserveMainDescriptionPostDelimiter,
} = context.options[1] || {};

if (context.options[0] === 'always') {
Expand All @@ -143,6 +145,7 @@ export default iterateJsdoc(({
indent,
jsdoc,
jsdocNode,
preserveMainDescriptionPostDelimiter,
report,
tags: applicableTags,
utils,
Expand Down Expand Up @@ -171,6 +174,10 @@ export default iterateJsdoc(({
{
additionalProperties: false,
properties: {
preserveMainDescriptionPostDelimiter: {
default: false,
type: 'boolean',
},
tags: {
items: {
type: 'string',
Expand Down
44 changes: 44 additions & 0 deletions test/rules/assertions/checkLineAlignment.js
Original file line number Diff line number Diff line change
Expand Up @@ -860,6 +860,35 @@ export default {
function quux () {}
`,
},
{
code: `
/**
* Function description
* description with post delimiter.
*
* @param {string} lorem Description.
* @param {int} sit Description multi words.
*/
const fn = ( lorem, sit ) => {}
`,
errors: [
{
message: 'Expected JSDoc block lines to be aligned.',
type: 'Block',
},
],
options: ['always'],
output: `
/**
* Function description
* description with post delimiter.
*
* @param {string} lorem Description.
* @param {int} sit Description multi words.
*/
const fn = ( lorem, sit ) => {}
`,
},
],
valid: [
{
Expand Down Expand Up @@ -1183,5 +1212,20 @@ export default {
}
`,
},
{
code: `
/**
* Function description
* description with post delimiter.
*
* @param {string} lorem Description.
* @param {int} sit Description multi words.
*/
const fn = ( lorem, sit ) => {}
`,
options: ['always', {
preserveMainDescriptionPostDelimiter: true,
}],
},
],
};

0 comments on commit 54dac34

Please sign in to comment.