Skip to content

Commit 702b226

Browse files
committed
fix(tag-lines): avoid reformatting post-delimiter spacing; fixes #1593
1 parent 39a5b0a commit 702b226

File tree

4 files changed

+121
-10
lines changed

4 files changed

+121
-10
lines changed

docs/rules/tag-lines.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,31 @@ The following patterns are considered problems:
397397
*/
398398
// "jsdoc/tag-lines": ["error"|"warn", "any",{"maxBlockLines":2,"startLines":5}]
399399
// Message: If set to a number, `maxBlockLines` must be greater than or equal to `startLines`.
400+
401+
/**
402+
* My Test Function, with some example code.
403+
*
404+
* ```js
405+
* new Foo();
406+
* ```
407+
*
408+
*
409+
* @param {string} bar
410+
*/
411+
function myTestFunction(bar) {
412+
413+
}
414+
// "jsdoc/tag-lines": ["error"|"warn", "any",{"startLines":1}]
415+
// Message: Expected only 1 line after block description
416+
417+
/**
418+
*
419+
* Second Test Case
420+
* @param {number} baz
421+
*
422+
*/
423+
// "jsdoc/tag-lines": ["error"|"warn", "any",{"startLines":1}]
424+
// Message: Expected 1 lines after block description
400425
````
401426

402427

src/iterateJsdoc.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,8 @@ import esquery from 'esquery';
170170
* seedTokens: (
171171
* tokens?: Partial<import('comment-parser').Tokens>
172172
* ) => import('comment-parser').Tokens,
173-
* descLines: string[]
173+
* descLines: string[],
174+
* postDelims: string[]
174175
* ) => import('comment-parser').Line[]} setter
175176
* @returns {void}
176177
*/
@@ -928,6 +929,8 @@ const getUtils = (
928929
utils.setBlockDescription = (setter) => {
929930
/** @type {string[]} */
930931
const descLines = [];
932+
/** @type {string[]} */
933+
const postDelims = [];
931934
/**
932935
* @type {undefined|Integer}
933936
*/
@@ -974,6 +977,7 @@ const getUtils = (
974977
return true;
975978
}
976979

980+
postDelims.push(postDelimiter);
977981
descLines.push(description);
978982
return false;
979983
});
@@ -994,6 +998,7 @@ const getUtils = (
994998
(info),
995999
seedTokens,
9961000
descLines,
1001+
postDelims,
9971002
),
9981003
);
9991004
}

src/rules/tagLines.js

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,22 @@ const checkMaxBlockLines = ({
3737
line: excessIndexLine,
3838
},
3939
() => {
40-
utils.setBlockDescription((info, seedTokens, descLines) => {
40+
utils.setBlockDescription((info, seedTokens, descLines, postDelims) => {
41+
const newPostDelims = [
42+
...postDelims.slice(0, excessIndexLine),
43+
...postDelims.slice(excessIndexLine + excessBlockLines - 1 - maxBlockLines),
44+
];
4145
return [
4246
...descLines.slice(0, excessIndexLine),
4347
...descLines.slice(excessIndexLine + excessBlockLines - 1 - maxBlockLines),
44-
].map((desc) => {
48+
].map((desc, idx) => {
4549
return {
4650
number: 0,
4751
source: '',
4852
tokens: seedTokens({
4953
...info,
5054
description: desc,
51-
postDelimiter: desc.trim() ? ' ' : '',
55+
postDelimiter: newPostDelims[idx],
5256
}),
5357
};
5458
});
@@ -310,15 +314,15 @@ export default iterateJsdoc(({
310314
line: lastDescriptionLine - trailingDiff,
311315
},
312316
() => {
313-
utils.setBlockDescription((info, seedTokens, descLines) => {
314-
return descLines.slice(0, -trailingDiff).map((desc) => {
317+
utils.setBlockDescription((info, seedTokens, descLines, postDelims) => {
318+
return descLines.slice(0, -trailingDiff).map((desc, idx) => {
315319
return {
316320
number: 0,
317321
source: '',
318322
tokens: seedTokens({
319323
...info,
320324
description: desc,
321-
postDelimiter: desc.trim() ? info.postDelimiter : '',
325+
postDelimiter: postDelims[idx],
322326
}),
323327
};
324328
});
@@ -332,22 +336,22 @@ export default iterateJsdoc(({
332336
line: lastDescriptionLine,
333337
},
334338
() => {
335-
utils.setBlockDescription((info, seedTokens, descLines) => {
339+
utils.setBlockDescription((info, seedTokens, descLines, postDelims) => {
336340
return [
337341
...descLines,
338342
...Array.from({
339343
length: -trailingDiff,
340344
}, () => {
341345
return '';
342346
}),
343-
].map((desc) => {
347+
].map((desc, idx) => {
344348
return {
345349
number: 0,
346350
source: '',
347351
tokens: seedTokens({
348352
...info,
349353
description: desc,
350-
postDelimiter: desc.trim() ? info.postDelimiter : '',
354+
postDelimiter: desc.trim() ? postDelims[idx] : '',
351355
}),
352356
};
353357
});

test/rules/assertions/tagLines.js

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -853,6 +853,83 @@ export default /** @type {import('../index.js').TestCases} */ ({
853853
},
854854
],
855855
},
856+
{
857+
code: `
858+
/**
859+
* My Test Function, with some example code.
860+
*
861+
* \`\`\`js
862+
* new Foo();
863+
* \`\`\`
864+
*
865+
*
866+
* @param {string} bar
867+
*/
868+
function myTestFunction(bar) {
869+
870+
}
871+
`,
872+
errors: [
873+
{
874+
line: 8,
875+
message: 'Expected only 1 line after block description',
876+
},
877+
],
878+
options: [
879+
'any',
880+
{
881+
startLines: 1,
882+
},
883+
],
884+
output: `
885+
/**
886+
* My Test Function, with some example code.
887+
*
888+
* \`\`\`js
889+
* new Foo();
890+
* \`\`\`
891+
*
892+
* @param {string} bar
893+
*/
894+
function myTestFunction(bar) {
895+
896+
}
897+
`,
898+
},
899+
{
900+
code: `
901+
/**
902+
*
903+
* Second Test Case
904+
* @param {number} baz
905+
*
906+
*/
907+
`,
908+
errors: [
909+
{
910+
line: 4,
911+
message: 'Expected 1 lines after block description',
912+
},
913+
{
914+
line: 6,
915+
message: 'Expected 0 trailing lines',
916+
},
917+
],
918+
options: [
919+
'any',
920+
{
921+
startLines: 1,
922+
},
923+
],
924+
output: `
925+
/**
926+
*
927+
* Second Test Case
928+
*
929+
* @param {number} baz
930+
*/
931+
`,
932+
},
856933
],
857934
valid: [
858935
{

0 commit comments

Comments
 (0)