Skip to content

Commit 85a1ea1

Browse files
committed
feat(sort-tags): add tagExceptions to allow additional lines per tag; fixes #1594
1 parent 2653fe1 commit 85a1ea1

File tree

5 files changed

+116
-3
lines changed

5 files changed

+116
-3
lines changed

.README/rules/sort-tags.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ adding line breaks between tag groups.
3636
|Tags|any|
3737
|Recommended|false|
3838
|Settings||
39-
|Options|`alphabetizeExtras`, `linesBetween`, `reportIntraTagGroupSpacing`, `reportTagGroupSpacing`, `tagSequence`|
39+
|Options|`alphabetizeExtras`, `linesBetween`, `reportIntraTagGroupSpacing`, `reportTagGroupSpacing`, `tagExceptions`, `tagSequence`|
4040

4141
## Failing examples
4242

docs/rules/sort-tags.md

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
* [`linesBetween`](#user-content-sort-tags-options-linesbetween)
99
* [`reportIntraTagGroupSpacing`](#user-content-sort-tags-options-reportintrataggroupspacing)
1010
* [`reportTagGroupSpacing`](#user-content-sort-tags-options-reporttaggroupspacing)
11+
* [`tagExceptions`](#user-content-sort-tags-options-tagexceptions)
1112
* [`tagSequence`](#user-content-sort-tags-options-tagsequence)
1213
* [Context and settings](#user-content-sort-tags-context-and-settings)
1314
* [Failing examples](#user-content-sort-tags-failing-examples)
@@ -80,6 +81,12 @@ as set by `linesBetween`. Defaults to `true`. Note that the very last tag
8081
will not have spacing applied regardless. For adding line breaks there, you
8182
may wish to use the `endLines` option of the `tag-lines` rule.
8283

84+
<a name="user-content-sort-tags-options-tagexceptions"></a>
85+
<a name="sort-tags-options-tagexceptions"></a>
86+
### <code>tagExceptions</code>
87+
88+
Allows specification by tag of a specific higher maximum number of lines. Keys are tags and values are the maximum number of lines allowed for such tags. Overrides `linesBetween`. Defaults to no special exceptions per tag.
89+
8390
<a name="user-content-sort-tags-options-tagsequence"></a>
8491
<a name="sort-tags-options-tagsequence"></a>
8592
### <code>tagSequence</code>
@@ -285,7 +292,7 @@ See description on `tagSequence`.
285292
|Tags|any|
286293
|Recommended|false|
287294
|Settings||
288-
|Options|`alphabetizeExtras`, `linesBetween`, `reportIntraTagGroupSpacing`, `reportTagGroupSpacing`, `tagSequence`|
295+
|Options|`alphabetizeExtras`, `linesBetween`, `reportIntraTagGroupSpacing`, `reportTagGroupSpacing`, `tagExceptions`, `tagSequence`|
289296

290297
<a name="user-content-sort-tags-failing-examples"></a>
291298
<a name="sort-tags-failing-examples"></a>
@@ -531,6 +538,19 @@ function quux () {}
531538
*/
532539
// "jsdoc/sort-tags": ["error"|"warn", {"tagSequence":[{"tags":["internal"]},{"tags":["template","param"]},{"tags":["returns"]},{"tags":["throws"]},{"tags":["see"]},{"tags":["example"]},{"tags":["since"]},{"tags":["deprecated"]}]}]
533540
// Message: Tag groups do not have the expected whitespace
541+
542+
/**
543+
* @param b
544+
* @param a
545+
* @returns {string}
546+
* @example abc
547+
*
548+
*
549+
* @example def
550+
*/
551+
function quux () {}
552+
// "jsdoc/sort-tags": ["error"|"warn", {"linesBetween":0,"tagExceptions":{"example":1}}]
553+
// Message: Intra-group tags have unexpected whitespace
534554
````
535555

536556

@@ -651,5 +671,16 @@ function quux () {}
651671
*/
652672
function quux () {}
653673
// "jsdoc/sort-tags": ["error"|"warn", {"linesBetween":2,"reportTagGroupSpacing":false,"tagSequence":[{"tags":["qrs"]},{"tags":["def","xyz"]},{"tags":["abc"]}]}]
674+
675+
/**
676+
* @param b
677+
* @param a
678+
* @returns {string}
679+
* @example abc
680+
*
681+
* @example def
682+
*/
683+
function quux () {}
684+
// "jsdoc/sort-tags": ["error"|"warn", {"linesBetween":0,"tagExceptions":{"example":1}}]
654685
````
655686

src/rules.d.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2660,6 +2660,16 @@ export interface Rules {
26602660
* may wish to use the `endLines` option of the `tag-lines` rule.
26612661
*/
26622662
reportTagGroupSpacing?: boolean;
2663+
/**
2664+
* Allows specification by tag of a specific higher maximum number of lines. Keys are tags and values are the maximum number of lines allowed for such tags. Overrides `linesBetween`. Defaults to no special exceptions per tag.
2665+
*/
2666+
tagExceptions?: {
2667+
/**
2668+
* This interface was referenced by `undefined`'s JSON-Schema definition
2669+
* via the `patternProperty` ".*".
2670+
*/
2671+
[k: string]: number;
2672+
};
26632673
/**
26642674
* An array of tag group objects indicating the preferred sequence for sorting tags.
26652675
*

src/rules/sortTags.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@ export default iterateJsdoc(({
55
context,
66
jsdoc,
77
utils,
8+
// eslint-disable-next-line complexity -- Temporary
89
}) => {
910
const
1011
/**
1112
* @type {{
1213
* linesBetween: import('../iterateJsdoc.js').Integer,
14+
* tagExceptions: Record<string, number>,
1315
* tagSequence: {
1416
* tags: string[]
1517
* }[],
@@ -22,6 +24,7 @@ export default iterateJsdoc(({
2224
linesBetween = 1,
2325
reportIntraTagGroupSpacing = true,
2426
reportTagGroupSpacing = true,
27+
tagExceptions = {},
2528
tagSequence = defaultTagOrder,
2629
} = context.options[0] || {};
2730

@@ -328,7 +331,7 @@ export default iterateJsdoc(({
328331
}
329332

330333
const ct = countTagEmptyLines(tag);
331-
if (ct) {
334+
if (ct && (!tagExceptions[tag.tag] || tagExceptions[tag.tag] < ct)) {
332335
const fixer = () => {
333336
let foundFirstTag = false;
334337

@@ -548,6 +551,15 @@ will not have spacing applied regardless. For adding line breaks there, you
548551
may wish to use the \`endLines\` option of the \`tag-lines\` rule.`,
549552
type: 'boolean',
550553
},
554+
tagExceptions: {
555+
description: 'Allows specification by tag of a specific higher maximum number of lines. Keys are tags and values are the maximum number of lines allowed for such tags. Overrides `linesBetween`. Defaults to no special exceptions per tag.',
556+
patternProperties: {
557+
'.*': {
558+
type: 'number',
559+
},
560+
},
561+
type: 'object',
562+
},
551563
tagSequence: {
552564
description: `An array of tag group objects indicating the preferred sequence for sorting tags.
553565

test/rules/assertions/sortTags.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1461,6 +1461,45 @@ export default /** @type {import('../index.js').TestCases} */ ({
14611461
*/
14621462
`,
14631463
},
1464+
{
1465+
code: `
1466+
/**
1467+
* @param b
1468+
* @param a
1469+
* @returns {string}
1470+
* @example abc
1471+
*
1472+
*
1473+
* @example def
1474+
*/
1475+
function quux () {}
1476+
`,
1477+
errors: [
1478+
{
1479+
line: 6,
1480+
message: 'Intra-group tags have unexpected whitespace',
1481+
},
1482+
],
1483+
options: [
1484+
{
1485+
linesBetween: 0,
1486+
tagExceptions: {
1487+
example: 1,
1488+
},
1489+
},
1490+
],
1491+
output: `
1492+
/**
1493+
* @param b
1494+
* @param a
1495+
* @returns {string}
1496+
* @example abc
1497+
*
1498+
* @example def
1499+
*/
1500+
function quux () {}
1501+
`,
1502+
},
14641503
],
14651504
valid: [
14661505
{
@@ -1890,5 +1929,26 @@ export default /** @type {import('../index.js').TestCases} */ ({
18901929
},
18911930
],
18921931
},
1932+
{
1933+
code: `
1934+
/**
1935+
* @param b
1936+
* @param a
1937+
* @returns {string}
1938+
* @example abc
1939+
*
1940+
* @example def
1941+
*/
1942+
function quux () {}
1943+
`,
1944+
options: [
1945+
{
1946+
linesBetween: 0,
1947+
tagExceptions: {
1948+
example: 1,
1949+
},
1950+
},
1951+
],
1952+
},
18931953
],
18941954
});

0 commit comments

Comments
 (0)