Skip to content

Commit

Permalink
feat(no-blank-blocks): add new rule; fixes #1042
Browse files Browse the repository at this point in the history
  • Loading branch information
brettz9 committed Apr 24, 2023
1 parent ee2bae3 commit 152e8c2
Show file tree
Hide file tree
Showing 7 changed files with 480 additions and 188 deletions.
1 change: 1 addition & 0 deletions .README/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,7 @@ selector).
{"gitdown": "include", "file": "./rules/multiline-blocks.md"}
{"gitdown": "include", "file": "./rules/no-bad-blocks.md"}
{"gitdown": "include", "file": "./rules/no-blank-block-descriptions.md"}
{"gitdown": "include", "file": "./rules/no-blank-blocks.md"}
{"gitdown": "include", "file": "./rules/no-defaults.md"}
{"gitdown": "include", "file": "./rules/no-missing-syntax.md"}
{"gitdown": "include", "file": "./rules/no-multi-asterisks.md"}
Expand Down
19 changes: 19 additions & 0 deletions .README/rules/no-blank-blocks.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
### `no-blank-blocks`

Reports and optionally removes blocks with whitespace only.

#### Options

##### `enableFixer`

Whether or not to auto-remove the blank block. Defaults to `false`.

|||
|---|---|
|Context|everywhere|
|Tags|N/A|
|Recommended|false|
|Settings||
|Options|`enableFixer`|

<!-- assertions noBlankBlocks -->
455 changes: 267 additions & 188 deletions README.md

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import matchName from './rules/matchName';
import multilineBlocks from './rules/multilineBlocks';
import noBadBlocks from './rules/noBadBlocks';
import noBlankBlockDescriptions from './rules/noBlankBlockDescriptions';
import noBlankBlocks from './rules/noBlankBlocks';
import noDefaults from './rules/noDefaults';
import noMissingSyntax from './rules/noMissingSyntax';
import noMultiAsterisks from './rules/noMultiAsterisks';
Expand Down Expand Up @@ -72,6 +73,7 @@ const index = {
'multiline-blocks': multilineBlocks,
'no-bad-blocks': noBadBlocks,
'no-blank-block-descriptions': noBlankBlockDescriptions,
'no-blank-blocks': noBlankBlocks,
'no-defaults': noDefaults,
'no-missing-syntax': noMissingSyntax,
'no-multi-asterisks': noMultiAsterisks,
Expand Down Expand Up @@ -132,6 +134,7 @@ const createRecommendedRuleset = (warnOrError) => {
'jsdoc/multiline-blocks': warnOrError,
'jsdoc/no-bad-blocks': 'off',
'jsdoc/no-blank-block-descriptions': 'off',
'jsdoc/no-blank-blocks': 'off',
'jsdoc/no-defaults': 'off',
'jsdoc/no-missing-syntax': 'off',
'jsdoc/no-multi-asterisks': warnOrError,
Expand Down
53 changes: 53 additions & 0 deletions src/rules/noBlankBlocks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import iterateJsdoc from '../iterateJsdoc';

export default iterateJsdoc(({
context,
jsdoc,
utils,
}) => {
if (jsdoc.tags.length) {
return;
}

const {
description,
lastDescriptionLine,
} = utils.getDescription();
if (description.trim()) {
return;
}

const {
enableFixer,
} = context.options[0] || {};

utils.reportJSDoc(
'No empty blocks',
{
line: lastDescriptionLine,
},
enableFixer ? () => {
jsdoc.source.splice(0, jsdoc.source.length);
} : null,
);
}, {
iterateAllJsdocs: true,
meta: {
docs: {
description: 'Removes empty blocks with nothing but possibly line breaks',
url: 'https://github.com/gajus/eslint-plugin-jsdoc#eslint-plugin-jsdoc-rules-no-blank-blocks',
},
fixable: 'code',
schema: [
{
additionalProperties: false,
properties: {
enableFixer: {
type: 'boolean',
},
},
},
],
type: 'suggestion',
},
});
136 changes: 136 additions & 0 deletions test/rules/assertions/noBlankBlocks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
export default {
invalid: [
{
code: `
/** */
`,
errors: [
{
line: 2,
message: 'No empty blocks',
},
],
options: [
{
enableFixer: true,
},
],
output: `
`,
},
{
code: `
/**
*/
`,
errors: [
{
line: 2,
message: 'No empty blocks',
},
],
options: [
{
enableFixer: true,
},
],
output: `
`,
},
{
code: `
/**
*
*/
`,
errors: [
{
line: 3,
message: 'No empty blocks',
},
],
options: [
{
enableFixer: true,
},
],
output: `
`,
},
{
code: `
/**
*
*
*/
`,
errors: [
{
line: 4,
message: 'No empty blocks',
},
],
options: [
{
enableFixer: true,
},
],
output: `
`,
},
{
code: `
/**
*
*
*/
`,
errors: [
{
line: 4,
message: 'No empty blocks',
},
],
options: [
{
enableFixer: false,
},
],
},
{
code: `
/**
*
*
*/
`,
errors: [
{
line: 4,
message: 'No empty blocks',
},
],
},
],
valid: [
{
code: `
/** @tag */
`,
},
{
code: `
/**
* Text
*/
`,
},
{
code: `
/**
* @tag
*/
`,
},
],
};
1 change: 1 addition & 0 deletions test/rules/ruleNames.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"multiline-blocks",
"no-bad-blocks",
"no-blank-block-descriptions",
"no-blank-blocks",
"no-defaults",
"no-missing-syntax",
"no-multi-asterisks",
Expand Down

0 comments on commit 152e8c2

Please sign in to comment.