Skip to content

Commit 0a30832

Browse files
authored
fix(lines-before-block): Switch to a whitelist of punctuators (#1385)
1 parent 19fa3dc commit 0a30832

File tree

3 files changed

+105
-5
lines changed

3 files changed

+105
-5
lines changed

docs/rules/lines-before-block.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,30 @@ const values = [
131131
// "jsdoc/lines-before-block": ["error"|"warn", {"checkBlockStarts":true}]
132132
// Message: Required 1 line(s) before JSDoc block
133133

134+
const values = [
135+
value1,
136+
/**
137+
* Description.
138+
*/
139+
value2
140+
];
141+
// Message: Required 1 line(s) before JSDoc block
142+
143+
const values = [
144+
value1,
145+
value2
146+
]
147+
/**
148+
* Description.
149+
*/
150+
// Message: Required 1 line(s) before JSDoc block
151+
152+
const value = 123
153+
/**
154+
* Description.
155+
*/
156+
// Message: Required 1 line(s) before JSDoc block
157+
134158
type UnionDocumentation =
135159
/** Description. */
136160
| { someProp: number }

src/rules/linesBeforeBlock.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import iterateJsdoc from '../iterateJsdoc.js';
22

33
/**
4-
* `;` ends a previous statement, `}` ends a previous block, `|` is seen the middle of a union, and
5-
* `&` is seen in the middle of an intersection. All other punctuators are for things like arrays,
6-
* functions, type aliases, and so on that shouldn't require a line before it.
4+
* Punctuators that begin a logical group should not require a line before it skipped. Specifically
5+
* `[` starts an array, `{` starts an object or block, `(` starts a grouping, and `=` starts a
6+
* declaration (like a variable or a type alias).
77
*/
8-
const lintedPunctuators = new Set([';', '}', '|', '&']);
8+
const startPunctuators = new Set(['[', '{', '(', '=']);
99

1010
export default iterateJsdoc(({
1111
context,
@@ -31,7 +31,7 @@ export default iterateJsdoc(({
3131
!tokenBefore || (
3232
tokenBefore.type === 'Punctuator' &&
3333
!checkBlockStarts &&
34-
!lintedPunctuators.has(tokenBefore.value)
34+
startPunctuators.has(tokenBefore.value)
3535
)
3636
) {
3737
return;

test/rules/assertions/linesBeforeBlock.js

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,82 @@ export default /** @type {import('../index.js').TestCases} */ ({
267267
];
268268
`,
269269
},
270+
{
271+
code: `
272+
const values = [
273+
value1,
274+
/**
275+
* Description.
276+
*/
277+
value2
278+
];
279+
`,
280+
errors: [
281+
{
282+
line: 4,
283+
message: 'Required 1 line(s) before JSDoc block'
284+
}
285+
],
286+
output: `
287+
const values = [
288+
value1,
289+
290+
/**
291+
* Description.
292+
*/
293+
value2
294+
];
295+
`,
296+
},
297+
{
298+
code: `
299+
const values = [
300+
value1,
301+
value2
302+
]
303+
/**
304+
* Description.
305+
*/
306+
`,
307+
errors: [
308+
{
309+
line: 6,
310+
message: 'Required 1 line(s) before JSDoc block'
311+
}
312+
],
313+
output: `
314+
const values = [
315+
value1,
316+
value2
317+
]
318+
319+
/**
320+
* Description.
321+
*/
322+
`,
323+
},
324+
{
325+
// This test is interesting due to the lack of semicolons.
326+
code: `
327+
const value = 123
328+
/**
329+
* Description.
330+
*/
331+
`,
332+
errors: [
333+
{
334+
line: 3,
335+
message: 'Required 1 line(s) before JSDoc block'
336+
}
337+
],
338+
output: `
339+
const value = 123
340+
341+
/**
342+
* Description.
343+
*/
344+
`,
345+
},
270346
{
271347
code: `
272348
type UnionDocumentation =

0 commit comments

Comments
 (0)