Skip to content

Commit 152e8c2

Browse files
committed
feat(no-blank-blocks): add new rule; fixes #1042
1 parent ee2bae3 commit 152e8c2

File tree

7 files changed

+480
-188
lines changed

7 files changed

+480
-188
lines changed

.README/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -608,6 +608,7 @@ selector).
608608
{"gitdown": "include", "file": "./rules/multiline-blocks.md"}
609609
{"gitdown": "include", "file": "./rules/no-bad-blocks.md"}
610610
{"gitdown": "include", "file": "./rules/no-blank-block-descriptions.md"}
611+
{"gitdown": "include", "file": "./rules/no-blank-blocks.md"}
611612
{"gitdown": "include", "file": "./rules/no-defaults.md"}
612613
{"gitdown": "include", "file": "./rules/no-missing-syntax.md"}
613614
{"gitdown": "include", "file": "./rules/no-multi-asterisks.md"}

.README/rules/no-blank-blocks.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
### `no-blank-blocks`
2+
3+
Reports and optionally removes blocks with whitespace only.
4+
5+
#### Options
6+
7+
##### `enableFixer`
8+
9+
Whether or not to auto-remove the blank block. Defaults to `false`.
10+
11+
|||
12+
|---|---|
13+
|Context|everywhere|
14+
|Tags|N/A|
15+
|Recommended|false|
16+
|Settings||
17+
|Options|`enableFixer`|
18+
19+
<!-- assertions noBlankBlocks -->

README.md

Lines changed: 267 additions & 188 deletions
Large diffs are not rendered by default.

src/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import matchName from './rules/matchName';
1717
import multilineBlocks from './rules/multilineBlocks';
1818
import noBadBlocks from './rules/noBadBlocks';
1919
import noBlankBlockDescriptions from './rules/noBlankBlockDescriptions';
20+
import noBlankBlocks from './rules/noBlankBlocks';
2021
import noDefaults from './rules/noDefaults';
2122
import noMissingSyntax from './rules/noMissingSyntax';
2223
import noMultiAsterisks from './rules/noMultiAsterisks';
@@ -72,6 +73,7 @@ const index = {
7273
'multiline-blocks': multilineBlocks,
7374
'no-bad-blocks': noBadBlocks,
7475
'no-blank-block-descriptions': noBlankBlockDescriptions,
76+
'no-blank-blocks': noBlankBlocks,
7577
'no-defaults': noDefaults,
7678
'no-missing-syntax': noMissingSyntax,
7779
'no-multi-asterisks': noMultiAsterisks,
@@ -132,6 +134,7 @@ const createRecommendedRuleset = (warnOrError) => {
132134
'jsdoc/multiline-blocks': warnOrError,
133135
'jsdoc/no-bad-blocks': 'off',
134136
'jsdoc/no-blank-block-descriptions': 'off',
137+
'jsdoc/no-blank-blocks': 'off',
135138
'jsdoc/no-defaults': 'off',
136139
'jsdoc/no-missing-syntax': 'off',
137140
'jsdoc/no-multi-asterisks': warnOrError,

src/rules/noBlankBlocks.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import iterateJsdoc from '../iterateJsdoc';
2+
3+
export default iterateJsdoc(({
4+
context,
5+
jsdoc,
6+
utils,
7+
}) => {
8+
if (jsdoc.tags.length) {
9+
return;
10+
}
11+
12+
const {
13+
description,
14+
lastDescriptionLine,
15+
} = utils.getDescription();
16+
if (description.trim()) {
17+
return;
18+
}
19+
20+
const {
21+
enableFixer,
22+
} = context.options[0] || {};
23+
24+
utils.reportJSDoc(
25+
'No empty blocks',
26+
{
27+
line: lastDescriptionLine,
28+
},
29+
enableFixer ? () => {
30+
jsdoc.source.splice(0, jsdoc.source.length);
31+
} : null,
32+
);
33+
}, {
34+
iterateAllJsdocs: true,
35+
meta: {
36+
docs: {
37+
description: 'Removes empty blocks with nothing but possibly line breaks',
38+
url: 'https://github.com/gajus/eslint-plugin-jsdoc#eslint-plugin-jsdoc-rules-no-blank-blocks',
39+
},
40+
fixable: 'code',
41+
schema: [
42+
{
43+
additionalProperties: false,
44+
properties: {
45+
enableFixer: {
46+
type: 'boolean',
47+
},
48+
},
49+
},
50+
],
51+
type: 'suggestion',
52+
},
53+
});
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
export default {
2+
invalid: [
3+
{
4+
code: `
5+
/** */
6+
`,
7+
errors: [
8+
{
9+
line: 2,
10+
message: 'No empty blocks',
11+
},
12+
],
13+
options: [
14+
{
15+
enableFixer: true,
16+
},
17+
],
18+
output: `
19+
`,
20+
},
21+
{
22+
code: `
23+
/**
24+
*/
25+
`,
26+
errors: [
27+
{
28+
line: 2,
29+
message: 'No empty blocks',
30+
},
31+
],
32+
options: [
33+
{
34+
enableFixer: true,
35+
},
36+
],
37+
output: `
38+
`,
39+
},
40+
{
41+
code: `
42+
/**
43+
*
44+
*/
45+
`,
46+
errors: [
47+
{
48+
line: 3,
49+
message: 'No empty blocks',
50+
},
51+
],
52+
options: [
53+
{
54+
enableFixer: true,
55+
},
56+
],
57+
output: `
58+
`,
59+
},
60+
{
61+
code: `
62+
/**
63+
*
64+
*
65+
*/
66+
`,
67+
errors: [
68+
{
69+
line: 4,
70+
message: 'No empty blocks',
71+
},
72+
],
73+
options: [
74+
{
75+
enableFixer: true,
76+
},
77+
],
78+
output: `
79+
`,
80+
},
81+
{
82+
code: `
83+
/**
84+
*
85+
*
86+
*/
87+
`,
88+
errors: [
89+
{
90+
line: 4,
91+
message: 'No empty blocks',
92+
},
93+
],
94+
options: [
95+
{
96+
enableFixer: false,
97+
},
98+
],
99+
},
100+
{
101+
code: `
102+
/**
103+
*
104+
*
105+
*/
106+
`,
107+
errors: [
108+
{
109+
line: 4,
110+
message: 'No empty blocks',
111+
},
112+
],
113+
},
114+
],
115+
valid: [
116+
{
117+
code: `
118+
/** @tag */
119+
`,
120+
},
121+
{
122+
code: `
123+
/**
124+
* Text
125+
*/
126+
`,
127+
},
128+
{
129+
code: `
130+
/**
131+
* @tag
132+
*/
133+
`,
134+
},
135+
],
136+
};

test/rules/ruleNames.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"multiline-blocks",
1919
"no-bad-blocks",
2020
"no-blank-block-descriptions",
21+
"no-blank-blocks",
2122
"no-defaults",
2223
"no-missing-syntax",
2324
"no-multi-asterisks",

0 commit comments

Comments
 (0)