Skip to content

Commit

Permalink
fix(check-indentation): #334 - ignore content enclosed in "code blo…
Browse files Browse the repository at this point in the history
…cks"
  • Loading branch information
ahwayakchih authored and golopot committed Oct 2, 2019
1 parent 756520a commit f60537a
Show file tree
Hide file tree
Showing 4 changed files with 156 additions and 1 deletion.
14 changes: 14 additions & 0 deletions .README/rules/check-indentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,20 @@

Reports invalid padding inside JSDoc block.

Ignores parts enclosed in Markdown's "code block". For example,
following description is valid:

```js
/**
* Some description:
* ```html
* <section>
* <title>test</title>
* </section>
* ```
*/
```

#### Options

This rule has an object option.
Expand Down
62 changes: 62 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -835,6 +835,20 @@ function quux () {}

Reports invalid padding inside JSDoc block.

Ignores parts enclosed in Markdown's "code block". For example,
following description is valid:

```js
/**
* Some description:
* ```html
* <section>
* <title>test</title>
* </section>
* ```
*/
```

<a name="eslint-plugin-jsdoc-rules-check-indentation-options-1"></a>
#### Options

Expand Down Expand Up @@ -920,6 +934,32 @@ function quux () {
*/
function quux () {

}
// Message: There must be no indentation.

/**
* foo
* ```html
* <section>
* <title>test</title>
* </section>
* ```
* @returns
* eeee
*/
function quux () {

}
// Message: There must be no indentation.

/**
* foo
* ``` aaaa```
* @returns
* eeee
*/
function quux () {

}
// Message: There must be no indentation.
````
Expand Down Expand Up @@ -968,6 +1008,28 @@ function quux () {

}
// Options: [{"excludeTags":["example","returns"]}]

/**
* foo
* ```html
* <section>
* <title>test</title>
* </section>
* ```
* @returns eeee
*/
function quux () {

}

/**
* foo
* ``` aaaa```
* @returns eeee
*/
function quux () {

}
````


Expand Down
11 changes: 10 additions & 1 deletion src/rules/checkIndentation.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ const maskExcludedContent = (str, excludeTags) => {
});
};

const maskCodeBlocks = (str) => {
const regContent = /([ \t]+\*)[ \t]```[^\n]*?([\w|\W]*?\n)(?=[ \t]*\*(?:[ \t]*(?:```|@)|\/))/g;

return str.replace(regContent, (match, margin, code) => {
return (new Array(code.match(/\n/g).length + 1)).join(margin + '\n');
});
};

export default iterateJsdoc(({
sourceCode,
jsdocNode,
Expand All @@ -20,7 +28,8 @@ export default iterateJsdoc(({
} = options;

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

if (reg.test(text)) {
const lineBreaks = text.slice(0, reg.lastIndex).match(/\n/g) || [];
Expand Down
70 changes: 70 additions & 0 deletions test/rules/assertions/checkIndentation.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,48 @@ export default {
},
],
},
{
code: `
/**
* foo
* \`\`\`html
* <section>
* <title>test</title>
* </section>
* \`\`\`
* @returns
* eeee
*/
function quux () {
}
`,
errors: [
{
line: 10,
message: 'There must be no indentation.',
},
],
},
{
code: `
/**
* foo
* \`\`\` aaaa\`\`\`
* @returns
* eeee
*/
function quux () {
}
`,
errors: [
{
line: 6,
message: 'There must be no indentation.',
},
],
},
],
valid: [
{
Expand Down Expand Up @@ -151,5 +193,33 @@ export default {
excludeTags: ['example', 'returns'],
}],
},
{
code: `
/**
* foo
* \`\`\`html
* <section>
* <title>test</title>
* </section>
* \`\`\`
* @returns eeee
*/
function quux () {
}
`,
},
{
code: `
/**
* foo
* \`\`\` aaaa\`\`\`
* @returns eeee
*/
function quux () {
}
`,
},
],
};

0 comments on commit f60537a

Please sign in to comment.