Skip to content

Commit 1a9775d

Browse files
renathobrettz9
authored andcommitted
Add preserveMainDescriptionPostDelimiter option (gajus#731)
feat(`check-line-alignment`): add `preserveMainDescriptionPostDelimiter` option to preserve left-hand side spacings in the main description when using the `always` option. Co-authored-by: Renatho De Carli Rosa <[email protected]>
1 parent 99cd455 commit 1a9775d

File tree

3 files changed

+58
-3
lines changed

3 files changed

+58
-3
lines changed

src/alignTransform.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ const space = (len) => {
5858
return ''.padStart(len, ' ');
5959
};
6060

61-
const alignTransform = (tags, indent) => {
61+
const alignTransform = (tags, indent, preserveMainDescriptionPostDelimiter) => {
6262
let intoTags = false;
6363
let width;
6464

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

147147
if (!intoTags) {
148-
tokens.postDelimiter = tokens.description === '' ? '' : ' ';
148+
if (tokens.description === '') {
149+
tokens.postDelimiter = '';
150+
} else if (!preserveMainDescriptionPostDelimiter) {
151+
tokens.postDelimiter = ' ';
152+
}
149153

150154
return {
151155
...line,

src/rules/checkLineAlignment.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,12 @@ const checkAlignment = ({
100100
indent,
101101
jsdoc,
102102
jsdocNode,
103+
preserveMainDescriptionPostDelimiter,
103104
report,
104105
tags,
105106
utils,
106107
}) => {
107-
const transform = commentFlow(alignTransform(tags, indent));
108+
const transform = commentFlow(alignTransform(tags, indent, preserveMainDescriptionPostDelimiter));
108109
const transformedJsdoc = transform(jsdoc);
109110

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

136138
if (context.options[0] === 'always') {
@@ -143,6 +145,7 @@ export default iterateJsdoc(({
143145
indent,
144146
jsdoc,
145147
jsdocNode,
148+
preserveMainDescriptionPostDelimiter,
146149
report,
147150
tags: applicableTags,
148151
utils,
@@ -171,6 +174,10 @@ export default iterateJsdoc(({
171174
{
172175
additionalProperties: false,
173176
properties: {
177+
preserveMainDescriptionPostDelimiter: {
178+
default: false,
179+
type: 'boolean',
180+
},
174181
tags: {
175182
items: {
176183
type: 'string',

test/rules/assertions/checkLineAlignment.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -860,6 +860,35 @@ export default {
860860
function quux () {}
861861
`,
862862
},
863+
{
864+
code: `
865+
/**
866+
* Function description
867+
* description with post delimiter.
868+
*
869+
* @param {string} lorem Description.
870+
* @param {int} sit Description multi words.
871+
*/
872+
const fn = ( lorem, sit ) => {}
873+
`,
874+
errors: [
875+
{
876+
message: 'Expected JSDoc block lines to be aligned.',
877+
type: 'Block',
878+
},
879+
],
880+
options: ['always'],
881+
output: `
882+
/**
883+
* Function description
884+
* description with post delimiter.
885+
*
886+
* @param {string} lorem Description.
887+
* @param {int} sit Description multi words.
888+
*/
889+
const fn = ( lorem, sit ) => {}
890+
`,
891+
},
863892
],
864893
valid: [
865894
{
@@ -1183,5 +1212,20 @@ export default {
11831212
}
11841213
`,
11851214
},
1215+
{
1216+
code: `
1217+
/**
1218+
* Function description
1219+
* description with post delimiter.
1220+
*
1221+
* @param {string} lorem Description.
1222+
* @param {int} sit Description multi words.
1223+
*/
1224+
const fn = ( lorem, sit ) => {}
1225+
`,
1226+
options: ['always', {
1227+
preserveMainDescriptionPostDelimiter: true,
1228+
}],
1229+
},
11861230
],
11871231
};

0 commit comments

Comments
 (0)