Skip to content

Commit b0faae7

Browse files
authored
feat(lines-before-block): add default-on ignoreSingleLines option; fixes #1295 (#1408)
1 parent ae2b314 commit b0faae7

File tree

4 files changed

+111
-4
lines changed

4 files changed

+111
-4
lines changed

.README/rules/lines-before-block.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ The minimum number of lines to require. Defaults to 1.
1919
This option excludes cases where the JSDoc block occurs on the same line as a
2020
preceding code or comment. Defaults to `true`.
2121

22+
### `ignoreSingleLines`
23+
24+
This option excludes cases where the JSDoc block is only one line long.
25+
Defaults to `true`.
26+
2227
### `excludedTags`
2328

2429
An array of tags whose presence in the JSDoc block will prevent the
@@ -31,7 +36,7 @@ lines before the block will not be added).
3136
|Tags|N/A|
3237
|Recommended|false|
3338
|Settings||
34-
|Options|`checkBlockStarts`, `excludedTags`, `ignoreSameLine`, `lines`|
39+
|Options|`checkBlockStarts`, `excludedTags`, `ignoreSameLine`, `ignoreSingleLines`, `lines`|
3540

3641
## Failing examples
3742

docs/rules/lines-before-block.md

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@ The minimum number of lines to require. Defaults to 1.
2929
This option excludes cases where the JSDoc block occurs on the same line as a
3030
preceding code or comment. Defaults to `true`.
3131

32+
<a name="user-content-lines-before-block-options-ignoresinglelines"></a>
33+
<a name="lines-before-block-options-ignoresinglelines"></a>
34+
### <code>ignoreSingleLines</code>
35+
36+
This option excludes cases where the JSDoc block is only one line long.
37+
Defaults to `true`.
38+
3239
<a name="user-content-lines-before-block-options-excludedtags"></a>
3340
<a name="lines-before-block-options-excludedtags"></a>
3441
### <code>excludedTags</code>
@@ -43,7 +50,7 @@ lines before the block will not be added).
4350
|Tags|N/A|
4451
|Recommended|false|
4552
|Settings||
46-
|Options|`checkBlockStarts`, `excludedTags`, `ignoreSameLine`, `lines`|
53+
|Options|`checkBlockStarts`, `excludedTags`, `ignoreSameLine`, `ignoreSingleLines`, `lines`|
4754

4855
<a name="user-content-lines-before-block-failing-examples"></a>
4956
<a name="lines-before-block-failing-examples"></a>
@@ -65,7 +72,7 @@ someCode; /**
6572
// Message: Required 1 line(s) before JSDoc block
6673

6774
someCode; /** */
68-
// "jsdoc/lines-before-block": ["error"|"warn", {"ignoreSameLine":false}]
75+
// "jsdoc/lines-before-block": ["error"|"warn", {"ignoreSameLine":false,"ignoreSingleLines":false}]
6976
// Message: Required 1 line(s) before JSDoc block
7077

7178
someCode;
@@ -166,14 +173,27 @@ type IntersectionDocumentation =
166173
{ someProp: number } &
167174
/** Description. */
168175
{ otherProp: string }
176+
// "jsdoc/lines-before-block": ["error"|"warn", {"ignoreSingleLines":false}]
169177
// Message: Required 1 line(s) before JSDoc block
170178

171179
type IntersectionDocumentation = {
172180
someProp: number;
173181
} & /** Description. */ {
174182
otherProp: string;
175183
};
176-
// "jsdoc/lines-before-block": ["error"|"warn", {"ignoreSameLine":false}]
184+
// "jsdoc/lines-before-block": ["error"|"warn", {"ignoreSameLine":false,"ignoreSingleLines":false}]
185+
// Message: Required 1 line(s) before JSDoc block
186+
187+
/** The parameters for a request */
188+
export type RequestParams = {
189+
/** The year to retrieve. */
190+
year: `${number}`;
191+
/**
192+
* The month to retrieve.
193+
*/
194+
month: `${number}`;
195+
}
196+
// "jsdoc/lines-before-block": ["error"|"warn", {"ignoreSingleLines":true}]
177197
// Message: Required 1 line(s) before JSDoc block
178198
````
179199

@@ -304,5 +324,14 @@ type IntersectionDocumentation = {
304324
} & /** Description. */ {
305325
otherProp: string;
306326
};
327+
328+
/** The parameters for a request */
329+
export type RequestParams = {
330+
/** The year to retrieve. */
331+
year: `${number}`;
332+
/** The month to retrieve. */
333+
month: `${number}`;
334+
}
335+
// "jsdoc/lines-before-block": ["error"|"warn", {"ignoreSingleLines":true}]
307336
````
308337

src/rules/linesBeforeBlock.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export default iterateJsdoc(({
2222
'type',
2323
],
2424
ignoreSameLine = true,
25+
ignoreSingleLines = true,
2526
lines = 1,
2627
} = context.options[0] || {};
2728

@@ -54,6 +55,10 @@ export default iterateJsdoc(({
5455
return;
5556
}
5657

58+
if (ignoreSingleLines && jsdocNode.loc?.start.line === jsdocNode.loc?.end.line) {
59+
return;
60+
}
61+
5762
/** @type {import('eslint').Rule.ReportFixer} */
5863
const fix = (fixer) => {
5964
let indent = '';
@@ -114,6 +119,9 @@ export default iterateJsdoc(({
114119
ignoreSameLine: {
115120
type: 'boolean',
116121
},
122+
ignoreSingleLines: {
123+
type: 'boolean',
124+
},
117125
lines: {
118126
type: 'integer',
119127
},

test/rules/assertions/linesBeforeBlock.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ export default /** @type {import('../index.js').TestCases} */ ({
6363
options: [
6464
{
6565
ignoreSameLine: false,
66+
ignoreSingleLines: false,
6667
},
6768
],
6869
output: `
@@ -388,6 +389,11 @@ export default /** @type {import('../index.js').TestCases} */ ({
388389
languageOptions: {
389390
parser: typescriptEslintParser,
390391
},
392+
options: [
393+
{
394+
ignoreSingleLines: false,
395+
},
396+
],
391397
output: `
392398
type UnionDocumentation =
393399
/** Description. */
@@ -426,6 +432,7 @@ export default /** @type {import('../index.js').TestCases} */ ({
426432
options: [
427433
{
428434
ignoreSameLine: false,
435+
ignoreSingleLines: false,
429436
},
430437
],
431438
output: `
@@ -438,6 +445,45 @@ export default /** @type {import('../index.js').TestCases} */ ({
438445
};
439446
`,
440447
},
448+
{
449+
code: `
450+
/** The parameters for a request */
451+
export type RequestParams = {
452+
/** The year to retrieve. */
453+
year: \`\${number}\`;
454+
/**
455+
* The month to retrieve.
456+
*/
457+
month: \`\${number}\`;
458+
}
459+
`,
460+
errors: [
461+
{
462+
line: 6,
463+
message: 'Required 1 line(s) before JSDoc block',
464+
},
465+
],
466+
languageOptions: {
467+
parser: typescriptEslintParser,
468+
},
469+
options: [
470+
{
471+
ignoreSingleLines: true,
472+
},
473+
],
474+
output: `
475+
/** The parameters for a request */
476+
export type RequestParams = {
477+
/** The year to retrieve. */
478+
year: \`\${number}\`;
479+
480+
/**
481+
* The month to retrieve.
482+
*/
483+
month: \`\${number}\`;
484+
}
485+
`,
486+
},
441487
],
442488
valid: [
443489
{
@@ -621,5 +667,24 @@ export default /** @type {import('../index.js').TestCases} */ ({
621667
parser: typescriptEslintParser,
622668
},
623669
},
670+
{
671+
code: `
672+
/** The parameters for a request */
673+
export type RequestParams = {
674+
/** The year to retrieve. */
675+
year: \`\${number}\`;
676+
/** The month to retrieve. */
677+
month: \`\${number}\`;
678+
}
679+
`,
680+
languageOptions: {
681+
parser: typescriptEslintParser,
682+
},
683+
options: [
684+
{
685+
ignoreSingleLines: true,
686+
},
687+
],
688+
},
624689
],
625690
});

0 commit comments

Comments
 (0)