-
-
Notifications
You must be signed in to change notification settings - Fork 159
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
540 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,37 @@ | ||
# `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. | ||
|
||
### `ignoreSameLine` | ||
|
||
This option excludes cases where the JSDoc block occurs on the same line as a | ||
preceding code or comment. Defaults to `true`. | ||
|
||
### `excludedTags` | ||
|
||
An array of tags whose presence in the JSDoc block will prevent the | ||
application of the rule. Defaults to `['type']` (i.e., if `@type` is present, | ||
lines before the block will not be added). | ||
|
||
||| | ||
|---|---| | ||
|Context|everywhere| | ||
|Tags|N/A| | ||
|Recommended|true| | ||
|Settings|| | ||
|Options|`excludedTags`, `ignoreSameLine`, `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,150 @@ | ||
<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. | ||
|
||
<a name="user-content-lines-before-block-options-ignoresameline"></a> | ||
<a name="lines-before-block-options-ignoresameline"></a> | ||
### <code>ignoreSameLine</code> | ||
|
||
This option excludes cases where the JSDoc block occurs on the same line as a | ||
preceding code or comment. Defaults to `true`. | ||
|
||
<a name="user-content-lines-before-block-options-excludedtags"></a> | ||
<a name="lines-before-block-options-excludedtags"></a> | ||
### <code>excludedTags</code> | ||
|
||
An array of tags whose presence in the JSDoc block will prevent the | ||
application of the rule. Defaults to `['type']` (i.e., if `@type` is present, | ||
lines before the block will not be added). | ||
|
||
||| | ||
|---|---| | ||
|Context|everywhere| | ||
|Tags|N/A| | ||
|Recommended|true| | ||
|Settings|| | ||
|Options|`excludedTags`, `ignoreSameLine`, `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: | ||
|
||
````ts | ||
someCode; | ||
/** | ||
* | ||
*/ | ||
// Message: Required 1 line(s) before JSDoc block | ||
|
||
someCode; /** | ||
* | ||
*/ | ||
// "jsdoc/lines-before-block": ["error"|"warn", {"ignoreSameLine":false}] | ||
// Message: Required 1 line(s) before JSDoc block | ||
|
||
someCode; /** */ | ||
// "jsdoc/lines-before-block": ["error"|"warn", {"ignoreSameLine":false}] | ||
// 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: | ||
|
||
````ts | ||
/** | ||
* | ||
*/ | ||
|
||
someCode; | ||
|
||
/** | ||
* | ||
*/ | ||
|
||
someCode; | ||
|
||
|
||
/** | ||
* | ||
*/ | ||
// "jsdoc/lines-before-block": ["error"|"warn", {"lines":2}] | ||
|
||
// Some comment | ||
|
||
/** | ||
* | ||
*/ | ||
|
||
/* Some comment */ | ||
|
||
/** | ||
* | ||
*/ | ||
|
||
/** | ||
* Some comment | ||
*/ | ||
|
||
/** | ||
* | ||
*/ | ||
|
||
someCode; /** */ | ||
|
||
const a = { | ||
someProp: /** @type {SomeCast} */ (someVal) | ||
}; | ||
|
||
const a = /** @lends SomeClass */ { | ||
someProp: (someVal) | ||
}; | ||
// "jsdoc/lines-before-block": ["error"|"warn", {"excludedTags":["lends"],"ignoreSameLine":false}] | ||
```` | ||
|
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,101 @@ | ||
import iterateJsdoc from '../iterateJsdoc.js'; | ||
|
||
export default iterateJsdoc(({ | ||
context, | ||
jsdocNode, | ||
sourceCode, | ||
report, | ||
utils, | ||
}) => { | ||
const { | ||
lines = 1, | ||
ignoreSameLine = true, | ||
excludedTags = ['type'] | ||
} = context.options[0] || {}; | ||
|
||
if (utils.hasATag(excludedTags)) { | ||
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) | ||
) { | ||
const startLine = jsdocNode.loc?.start?.line; | ||
const sameLine = tokenBefore.loc?.end?.line === startLine; | ||
|
||
if (sameLine && ignoreSameLine) { | ||
return; | ||
} | ||
|
||
/** @type {import('eslint').Rule.ReportFixer} */ | ||
const fix = (fixer) => { | ||
let indent = ''; | ||
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( | ||
/* c8 ignore next */ | ||
/** @type {number} */ (startColumn ? startColumn - 1 : 0) | ||
); | ||
} | ||
} | ||
|
||
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: { | ||
excludedTags: { | ||
type: 'array', | ||
items: { | ||
type: 'string' | ||
} | ||
}, | ||
ignoreSameLine: { | ||
type: 'boolean', | ||
}, | ||
lines: { | ||
type: 'integer' | ||
} | ||
}, | ||
type: 'object', | ||
}, | ||
], | ||
type: 'suggestion', | ||
}, | ||
}); |
Oops, something went wrong.