Skip to content

fix(lines-before-block): Switch to a whitelist of punctuators #1385

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions docs/rules/lines-before-block.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,30 @@ const values = [
// "jsdoc/lines-before-block": ["error"|"warn", {"checkBlockStarts":true}]
// Message: Required 1 line(s) before JSDoc block

const values = [
value1,
/**
* Description.
*/
value2
];
// Message: Required 1 line(s) before JSDoc block

const values = [
value1,
value2
]
/**
* Description.
*/
// Message: Required 1 line(s) before JSDoc block

const value = 123
/**
* Description.
*/
// Message: Required 1 line(s) before JSDoc block

type UnionDocumentation =
/** Description. */
| { someProp: number }
Expand Down
10 changes: 5 additions & 5 deletions src/rules/linesBeforeBlock.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import iterateJsdoc from '../iterateJsdoc.js';

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

export default iterateJsdoc(({
context,
Expand All @@ -31,7 +31,7 @@ export default iterateJsdoc(({
!tokenBefore || (
tokenBefore.type === 'Punctuator' &&
!checkBlockStarts &&
!lintedPunctuators.has(tokenBefore.value)
startPunctuators.has(tokenBefore.value)
)
) {
return;
Expand Down
76 changes: 76 additions & 0 deletions test/rules/assertions/linesBeforeBlock.js
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,82 @@ export default /** @type {import('../index.js').TestCases} */ ({
];
`,
},
{
code: `
const values = [
value1,
/**
* Description.
*/
value2
];
`,
errors: [
{
line: 4,
message: 'Required 1 line(s) before JSDoc block'
}
],
output: `
const values = [
value1,

/**
* Description.
*/
value2
];
`,
},
{
code: `
const values = [
value1,
value2
]
/**
* Description.
*/
`,
errors: [
{
line: 6,
message: 'Required 1 line(s) before JSDoc block'
}
],
output: `
const values = [
value1,
value2
]

/**
* Description.
*/
`,
},
{
// This test is interesting due to the lack of semicolons.
code: `
const value = 123
/**
* Description.
*/
`,
errors: [
{
line: 3,
message: 'Required 1 line(s) before JSDoc block'
}
],
output: `
const value = 123

/**
* Description.
*/
`,
},
{
code: `
type UnionDocumentation =
Expand Down