Skip to content

Commit d7aa4e8

Browse files
ahwayakchihchiawendt
authored andcommitted
fix(check-indentation): excludeTags option, add tests, default on
Instead of rather specific `excludeExamples` option, we now have more generic `excludeTags` option. It takes an array of tag names to exclude from `check-indentation`, and defaults to `['example']`. Added test suggested by @golopot: #388 (comment)
1 parent 933b74a commit d7aa4e8

File tree

4 files changed

+99
-22
lines changed

4 files changed

+99
-22
lines changed

.README/rules/check-indentation.md

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,18 @@ Reports invalid padding inside JSDoc block.
66

77
This rule has an object option.
88

9-
##### `excludeExamples`
9+
##### `excludeTags`
1010

11-
This boolean property allows to "hide" example code from reports.
11+
Array of tags (e.g., `['example', 'description']`) whose content will be
12+
"hidden" from the `check-indentation` rule. Defaults to `['example']`.
1213

1314
By default, whole JSDoc block is checked for invalid padding.
14-
That includes example blocks too, which may get in the way of adding full,
15-
readable examples of code without ending up with multiple linting issues.
15+
That would include `@example` blocks too, which would get in the way
16+
of adding full, readable examples of code without ending up with multiple
17+
linting issues.
1618

17-
When enabled, following code will lint without any padding issue:
19+
When disabled (by passing `excludeTags: []` option), following code will
20+
lint *with* padding issue:
1821

1922
```js
2023
/**
@@ -29,6 +32,6 @@ When enabled, following code will lint without any padding issue:
2932
|---|---|
3033
|Context|everywhere|
3134
|Tags|N/A|
32-
|Options| `excludeExamples` |
35+
|Options| `excludeTags` |
3336

3437
<!-- assertions checkIndentation -->

README.md

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -840,16 +840,19 @@ Reports invalid padding inside JSDoc block.
840840

841841
This rule has an object option.
842842

843-
<a name="eslint-plugin-jsdoc-rules-check-indentation-options-1-excludeexamples"></a>
844-
##### <code>excludeExamples</code>
843+
<a name="eslint-plugin-jsdoc-rules-check-indentation-options-1-excludetags"></a>
844+
##### <code>excludeTags</code>
845845

846-
This boolean property allows to "hide" example code from reports.
846+
Array of tags (e.g., `['example', 'description']`) whose content will be
847+
"hidden" from the `check-indentation` rule. Defaults to `['example']`.
847848

848849
By default, whole JSDoc block is checked for invalid padding.
849-
That includes example blocks too, which may get in the way of adding full,
850-
readable examples of code without ending up with multiple linting issues.
850+
That would include `@example` blocks too, which would get in the way
851+
of adding full, readable examples of code without ending up with multiple
852+
linting issues.
851853

852-
When enabled, following code will lint without any padding issue:
854+
When disabled (by passing `excludeTags: []` option), following code will
855+
lint *with* padding issue:
853856

854857
```js
855858
/**
@@ -864,7 +867,7 @@ When enabled, following code will lint without any padding issue:
864867
|---|---|
865868
|Context|everywhere|
866869
|Tags|N/A|
867-
|Options| `excludeExamples` |
870+
|Options| `excludeTags` |
868871

869872
The following patterns are considered problems:
870873

@@ -903,6 +906,20 @@ class Moo {}
903906
*/
904907
function quux () {
905908

909+
}
910+
// Options: [{"excludeTags":[]}]
911+
// Message: There must be no indentation.
912+
913+
/**
914+
* foo
915+
*
916+
* @example
917+
* aaaa
918+
* @returns
919+
* eeee
920+
*/
921+
function quux () {
922+
906923
}
907924
// Message: There must be no indentation.
908925
````
@@ -936,7 +953,21 @@ function quux () {
936953
function quux () {
937954

938955
}
939-
// Options: [{"excludeExamples":true}]
956+
957+
/**
958+
* foo
959+
*
960+
* @example
961+
* anArray.filter((a) => {
962+
* return a.b;
963+
* });
964+
* @returns
965+
* eeee
966+
*/
967+
function quux () {
968+
969+
}
970+
// Options: [{"excludeTags":["example","returns"]}]
940971
````
941972

942973

src/rules/checkIndentation.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import iterateJsdoc from '../iterateJsdoc';
22

3-
const maskExamples = (str) => {
4-
const regExamples = /([ \t]+\*)[ \t]@example(?=[ \n])([\w|\W]*?\n)(?=[ \t]*\*(?:[ \t]*@|\/))/g;
3+
const maskExamples = (str, excludeTags) => {
4+
const regExamples = new RegExp(`([ \\t]+\\*)[ \\t]@(?:${excludeTags.join('|')})(?=[ \\n])([\\w|\\W]*?\\n)(?=[ \\t]*\\*(?:[ \\t]*@|\\/))`,'g');
55

66
return str.replace(regExamples, (match, margin, code) => {
77
return (new Array(code.match(/\n/g).length + 1)).join(margin + '\n');
@@ -16,11 +16,11 @@ export default iterateJsdoc(({
1616
}) => {
1717
const options = context.options[0] || {};
1818
const {
19-
excludeExamples = false,
19+
excludeTags = ['example'],
2020
} = options;
2121

2222
const reg = new RegExp(/^(?:\/?\**|[ \t]*)\*[ \t]{2}/gm);
23-
const text = excludeExamples ? maskExamples(sourceCode.getText(jsdocNode)) : sourceCode.getText(jsdocNode);
23+
const text = excludeTags.length ? maskExamples(sourceCode.getText(jsdocNode), excludeTags) : sourceCode.getText(jsdocNode);
2424

2525
if (reg.test(text)) {
2626
const lineBreaks = text.slice(0, reg.lastIndex).match(/\n/g) || [];
@@ -34,9 +34,11 @@ export default iterateJsdoc(({
3434
schema: [{
3535
additionalProperties: false,
3636
properties: {
37-
excludeExamples: {
38-
default: false,
39-
type: 'boolean',
37+
excludeTags: {
38+
items: {
39+
type: 'string',
40+
},
41+
type: 'array',
4042
},
4143
},
4244
type: 'object',

test/rules/assertions/checkIndentation.js

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,30 @@ export default {
6868
message: 'There must be no indentation.',
6969
},
7070
],
71+
options: [{
72+
excludeTags: [],
73+
}],
74+
},
75+
{
76+
code: `
77+
/**
78+
* foo
79+
*
80+
* @example
81+
* aaaa
82+
* @returns
83+
* eeee
84+
*/
85+
function quux () {
86+
87+
}
88+
`,
89+
errors: [
90+
{
91+
line: 8,
92+
message: 'There must be no indentation.',
93+
},
94+
],
7195
},
7296
],
7397
valid: [
@@ -104,10 +128,27 @@ export default {
104128
*/
105129
function quux () {
106130
131+
}
132+
`,
133+
},
134+
{
135+
code: `
136+
/**
137+
* foo
138+
*
139+
* @example
140+
* anArray.filter((a) => {
141+
* return a.b;
142+
* });
143+
* @returns
144+
* eeee
145+
*/
146+
function quux () {
147+
107148
}
108149
`,
109150
options: [{
110-
excludeExamples: true,
151+
excludeTags: ['example', 'returns'],
111152
}],
112153
},
113154
],

0 commit comments

Comments
 (0)