Skip to content

Commit f60537a

Browse files
ahwayakchihgolopot
authored andcommitted
fix(check-indentation): #334 - ignore content enclosed in "code blocks"
1 parent 756520a commit f60537a

File tree

4 files changed

+156
-1
lines changed

4 files changed

+156
-1
lines changed

.README/rules/check-indentation.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,20 @@
22

33
Reports invalid padding inside JSDoc block.
44

5+
Ignores parts enclosed in Markdown's "code block". For example,
6+
following description is valid:
7+
8+
```js
9+
/**
10+
* Some description:
11+
* ```html
12+
* <section>
13+
* <title>test</title>
14+
* </section>
15+
* ```
16+
*/
17+
```
18+
519
#### Options
620

721
This rule has an object option.

README.md

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -835,6 +835,20 @@ function quux () {}
835835

836836
Reports invalid padding inside JSDoc block.
837837

838+
Ignores parts enclosed in Markdown's "code block". For example,
839+
following description is valid:
840+
841+
```js
842+
/**
843+
* Some description:
844+
* ```html
845+
* <section>
846+
* <title>test</title>
847+
* </section>
848+
* ```
849+
*/
850+
```
851+
838852
<a name="eslint-plugin-jsdoc-rules-check-indentation-options-1"></a>
839853
#### Options
840854

@@ -920,6 +934,32 @@ function quux () {
920934
*/
921935
function quux () {
922936

937+
}
938+
// Message: There must be no indentation.
939+
940+
/**
941+
* foo
942+
* ```html
943+
* <section>
944+
* <title>test</title>
945+
* </section>
946+
* ```
947+
* @returns
948+
* eeee
949+
*/
950+
function quux () {
951+
952+
}
953+
// Message: There must be no indentation.
954+
955+
/**
956+
* foo
957+
* ``` aaaa```
958+
* @returns
959+
* eeee
960+
*/
961+
function quux () {
962+
923963
}
924964
// Message: There must be no indentation.
925965
````
@@ -968,6 +1008,28 @@ function quux () {
9681008

9691009
}
9701010
// Options: [{"excludeTags":["example","returns"]}]
1011+
1012+
/**
1013+
* foo
1014+
* ```html
1015+
* <section>
1016+
* <title>test</title>
1017+
* </section>
1018+
* ```
1019+
* @returns eeee
1020+
*/
1021+
function quux () {
1022+
1023+
}
1024+
1025+
/**
1026+
* foo
1027+
* ``` aaaa```
1028+
* @returns eeee
1029+
*/
1030+
function quux () {
1031+
1032+
}
9711033
````
9721034

9731035

src/rules/checkIndentation.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@ const maskExcludedContent = (str, excludeTags) => {
88
});
99
};
1010

11+
const maskCodeBlocks = (str) => {
12+
const regContent = /([ \t]+\*)[ \t]```[^\n]*?([\w|\W]*?\n)(?=[ \t]*\*(?:[ \t]*(?:```|@)|\/))/g;
13+
14+
return str.replace(regContent, (match, margin, code) => {
15+
return (new Array(code.match(/\n/g).length + 1)).join(margin + '\n');
16+
});
17+
};
18+
1119
export default iterateJsdoc(({
1220
sourceCode,
1321
jsdocNode,
@@ -20,7 +28,8 @@ export default iterateJsdoc(({
2028
} = options;
2129

2230
const reg = new RegExp(/^(?:\/?\**|[ \t]*)\*[ \t]{2}/gm);
23-
const text = excludeTags.length ? maskExcludedContent(sourceCode.getText(jsdocNode), excludeTags) : sourceCode.getText(jsdocNode);
31+
const textWithoutCodeBlocks = maskCodeBlocks(sourceCode.getText(jsdocNode));
32+
const text = excludeTags.length ? maskExcludedContent(textWithoutCodeBlocks, excludeTags) : textWithoutCodeBlocks;
2433

2534
if (reg.test(text)) {
2635
const lineBreaks = text.slice(0, reg.lastIndex).match(/\n/g) || [];

test/rules/assertions/checkIndentation.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,48 @@ export default {
9393
},
9494
],
9595
},
96+
{
97+
code: `
98+
/**
99+
* foo
100+
* \`\`\`html
101+
* <section>
102+
* <title>test</title>
103+
* </section>
104+
* \`\`\`
105+
* @returns
106+
* eeee
107+
*/
108+
function quux () {
109+
110+
}
111+
`,
112+
errors: [
113+
{
114+
line: 10,
115+
message: 'There must be no indentation.',
116+
},
117+
],
118+
},
119+
{
120+
code: `
121+
/**
122+
* foo
123+
* \`\`\` aaaa\`\`\`
124+
* @returns
125+
* eeee
126+
*/
127+
function quux () {
128+
129+
}
130+
`,
131+
errors: [
132+
{
133+
line: 6,
134+
message: 'There must be no indentation.',
135+
},
136+
],
137+
},
96138
],
97139
valid: [
98140
{
@@ -151,5 +193,33 @@ export default {
151193
excludeTags: ['example', 'returns'],
152194
}],
153195
},
196+
{
197+
code: `
198+
/**
199+
* foo
200+
* \`\`\`html
201+
* <section>
202+
* <title>test</title>
203+
* </section>
204+
* \`\`\`
205+
* @returns eeee
206+
*/
207+
function quux () {
208+
209+
}
210+
`,
211+
},
212+
{
213+
code: `
214+
/**
215+
* foo
216+
* \`\`\` aaaa\`\`\`
217+
* @returns eeee
218+
*/
219+
function quux () {
220+
221+
}
222+
`,
223+
},
154224
],
155225
};

0 commit comments

Comments
 (0)