Skip to content

Commit 4c11c39

Browse files
committed
feat(check-examples): add paddedIndent option
1 parent cea3560 commit 4c11c39

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed

.README/rules/check-examples.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,24 @@ If neither is in use, all examples will be matched. Note also that even if
3737
`captionRequired` is not set, any initial `<caption>` will be stripped out
3838
before doing the regex matching.
3939

40+
#### `paddedIndent`
41+
42+
This integer property allows one to add a fixed amount of whitespace at the
43+
beginning of the second or later lines of the example to be stripped so as
44+
to avoid linting issues with the decorative whitespace. For example, if set
45+
to a value of `4`, the initial whitespace below will not trigger `indent`
46+
rule errors as the extra 4 spaces on each subsequent line will be stripped
47+
out before evaluation.
48+
49+
```js
50+
/**
51+
* @example
52+
* anArray.filter((a) => {
53+
* return a.b;
54+
* });
55+
*/
56+
```
57+
4058
#### `reportUnusedDisableDirectives`
4159

4260
If not set to `false`, `reportUnusedDisableDirectives` will report disabled

src/rules/checkExamples.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ export default iterateJsdoc(({
2929
noDefaultExampleRules = false,
3030
eslintrcForExamples = true,
3131
matchingFileName: filename = null,
32+
paddedIndent = 0,
3233
baseConfig = {},
3334
configFile,
3435
allowInlineConfig = true,
@@ -150,6 +151,10 @@ export default iterateJsdoc(({
150151

151152
let messages;
152153

154+
if (paddedIndent) {
155+
source = source.replace(new RegExp(`(^|\n) {${paddedIndent}}(?!$)`, 'g'), '\n');
156+
}
157+
153158
if (filename) {
154159
const config = cli.getConfigForFile(filename);
155160

@@ -258,6 +263,10 @@ export default iterateJsdoc(({
258263
default: false,
259264
type: 'boolean'
260265
},
266+
paddedIndent: {
267+
default: 0,
268+
type: 'integer'
269+
},
261270
rejectExampleCodeRegex: {
262271
type: 'string'
263272
},

test/rules/assertions/checkExamples.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,31 @@ export default {
328328
code: `
329329
/**
330330
* @example const i = 5;
331+
* quux2()
332+
*/
333+
function quux2 () {
334+
335+
}
336+
`,
337+
errors: [
338+
{
339+
message: '@example warning (id-length): Identifier name \'i\' is too short (< 2).'
340+
},
341+
{
342+
message: '@example error (semi): Missing semicolon.'
343+
}
344+
],
345+
options: [
346+
{
347+
paddedIndent: 2
348+
}
349+
]
350+
},
351+
{
352+
code: `
353+
/**
354+
* @example
355+
* const i = 5;
331356
* quux2()
332357
*/
333358
function quux2 () {

0 commit comments

Comments
 (0)