forked from gajus/eslint-plugin-jsdoc
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BREAKING CHANGE: Adds new rule to recommended
- Loading branch information
Showing
8 changed files
with
459 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# `lines-before-block` | ||
|
||
This rule enforces minimum number of newlines before JSDoc comment blocks | ||
(except at the beginning of a file). | ||
|
||
## Options | ||
|
||
### `lines` | ||
|
||
The minimum number of lines to require. Defaults to 1. | ||
|
||
||| | ||
|---|---| | ||
|Context|everywhere| | ||
|Tags|N/A| | ||
|Recommended|true| | ||
|Settings|| | ||
|Options|`lines`| | ||
|
||
## Failing examples | ||
|
||
<!-- assertions-failing linesBeforeBlock --> | ||
|
||
## Passing examples | ||
|
||
<!-- assertions-passing linesBeforeBlock --> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
<a name="user-content-lines-before-block"></a> | ||
<a name="lines-before-block"></a> | ||
# <code>lines-before-block</code> | ||
|
||
This rule enforces minimum number of newlines before JSDoc comment blocks | ||
(except at the beginning of a file). | ||
|
||
<a name="user-content-lines-before-block-options"></a> | ||
<a name="lines-before-block-options"></a> | ||
## Options | ||
|
||
<a name="user-content-lines-before-block-options-lines"></a> | ||
<a name="lines-before-block-options-lines"></a> | ||
### <code>lines</code> | ||
|
||
The minimum number of lines to require. Defaults to 1. | ||
|
||
||| | ||
|---|---| | ||
|Context|everywhere| | ||
|Tags|N/A| | ||
|Recommended|true| | ||
|Settings|| | ||
|Options|`lines`| | ||
|
||
<a name="user-content-lines-before-block-failing-examples"></a> | ||
<a name="lines-before-block-failing-examples"></a> | ||
## Failing examples | ||
|
||
The following patterns are considered problems: | ||
|
||
````js | ||
someCode; | ||
/** | ||
* | ||
*/ | ||
// Message: Required 1 line(s) before JSDoc block | ||
|
||
someCode; /** | ||
* | ||
*/ | ||
// Message: Required 1 line(s) before JSDoc block | ||
|
||
someCode; /** */ | ||
// Message: Required 1 line(s) before JSDoc block | ||
|
||
someCode; | ||
/** | ||
* | ||
*/ | ||
// "jsdoc/lines-before-block": ["error"|"warn", {"lines":2}] | ||
// Message: Required 2 line(s) before JSDoc block | ||
|
||
// Some comment | ||
/** | ||
* | ||
*/ | ||
// Message: Required 1 line(s) before JSDoc block | ||
|
||
/* Some comment */ | ||
/** | ||
* | ||
*/ | ||
// Message: Required 1 line(s) before JSDoc block | ||
|
||
/** | ||
* Some comment | ||
*/ | ||
/** | ||
* | ||
*/ | ||
// Message: Required 1 line(s) before JSDoc block | ||
```` | ||
|
||
|
||
|
||
<a name="user-content-lines-before-block-passing-examples"></a> | ||
<a name="lines-before-block-passing-examples"></a> | ||
## Passing examples | ||
|
||
The following patterns are not considered problems: | ||
|
||
````js | ||
/** | ||
* | ||
*/ | ||
|
||
someCode; | ||
|
||
/** | ||
* | ||
*/ | ||
|
||
someCode; | ||
|
||
|
||
/** | ||
* | ||
*/ | ||
// "jsdoc/lines-before-block": ["error"|"warn", {"lines":2}] | ||
|
||
// Some comment | ||
|
||
/** | ||
* | ||
*/ | ||
|
||
/* Some comment */ | ||
|
||
/** | ||
* | ||
*/ | ||
|
||
/** | ||
* Some comment | ||
*/ | ||
|
||
/** | ||
* | ||
*/ | ||
|
||
const a = { | ||
someProp: /** @type {SomeCast} */ (someVal) | ||
}; | ||
```` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import iterateJsdoc from '../iterateJsdoc.js'; | ||
|
||
export default iterateJsdoc(({ | ||
context, | ||
jsdocNode, | ||
sourceCode, | ||
report, | ||
utils, | ||
}) => { | ||
const { | ||
lines = 1, | ||
} = context.options[0] || {}; | ||
|
||
if (utils.hasATag(['type'])) { | ||
return; | ||
} | ||
|
||
const tokensBefore = sourceCode.getTokensBefore(jsdocNode, {includeComments: true}); | ||
const tokenBefore = tokensBefore.slice(-1)[0]; | ||
if (!tokenBefore) { | ||
return; | ||
} | ||
|
||
if (tokenBefore.loc?.end?.line + lines >= | ||
/** @type {number} */ | ||
(jsdocNode.loc?.start?.line) | ||
) { | ||
/** @type {import('eslint').Rule.ReportFixer} */ | ||
const fix = (fixer) => { | ||
let indent = ''; | ||
const startLine = jsdocNode.loc?.start?.line; | ||
const sameLine = tokenBefore.loc?.end?.line === startLine; | ||
if (sameLine) { | ||
const spaceDiff = /** @type {number} */ (jsdocNode.loc?.start?.column) - | ||
/** @type {number} */ (tokenBefore.loc?.end?.column); | ||
// @ts-expect-error Should be a comment | ||
indent = /** @type {import('estree').Comment} */ ( | ||
jsdocNode | ||
).value.match(/^\*\n([ \t]*) \*/)?.[1]?.slice(spaceDiff); | ||
if (!indent) { | ||
/** @type {import('eslint').AST.Token|import('estree').Comment|undefined} */ | ||
let tokenPrior = tokenBefore; | ||
let startColumn; | ||
while (tokenPrior && tokenPrior?.loc?.start?.line === startLine) { | ||
startColumn = tokenPrior.loc?.start?.column; | ||
tokenPrior = tokensBefore.pop(); | ||
} | ||
indent = ' '.repeat(/** @type {number} */ (startColumn) - spaceDiff); | ||
} | ||
} | ||
|
||
return fixer.insertTextAfter( | ||
/** @type {import('eslint').AST.Token} */ | ||
(tokenBefore), | ||
'\n'.repeat(lines) + | ||
(sameLine ? '\n' + indent : '') | ||
); | ||
}; | ||
report(`Required ${lines} line(s) before JSDoc block`, fix); | ||
} | ||
}, { | ||
iterateAllJsdocs: true, | ||
meta: { | ||
fixable: 'code', | ||
docs: { | ||
description: 'Enforces minimum number of newlines before JSDoc comment blocks', | ||
url: 'https://github.com/gajus/eslint-plugin-jsdoc/blob/main/docs/rules/lines-before-block.md#repos-sticky-header', | ||
}, | ||
schema: [ | ||
{ | ||
additionalProperties: false, | ||
properties: { | ||
lines: { | ||
type: 'integer' | ||
} | ||
}, | ||
type: 'object', | ||
}, | ||
], | ||
type: 'suggestion', | ||
}, | ||
}); |
Oops, something went wrong.