From 4c11c39c1b641a27f4d83292a5baa48a629df09d Mon Sep 17 00:00:00 2001 From: Brett Zamir Date: Fri, 12 Jul 2019 17:44:40 -0700 Subject: [PATCH] feat(check-examples): add `paddedIndent` option --- .README/rules/check-examples.md | 18 ++++++++++++++++++ src/rules/checkExamples.js | 9 +++++++++ test/rules/assertions/checkExamples.js | 25 +++++++++++++++++++++++++ 3 files changed, 52 insertions(+) diff --git a/.README/rules/check-examples.md b/.README/rules/check-examples.md index 49d4818bf..c420759e9 100644 --- a/.README/rules/check-examples.md +++ b/.README/rules/check-examples.md @@ -37,6 +37,24 @@ If neither is in use, all examples will be matched. Note also that even if `captionRequired` is not set, any initial `` will be stripped out before doing the regex matching. +#### `paddedIndent` + +This integer property allows one to add a fixed amount of whitespace at the +beginning of the second or later lines of the example to be stripped so as +to avoid linting issues with the decorative whitespace. For example, if set +to a value of `4`, the initial whitespace below will not trigger `indent` +rule errors as the extra 4 spaces on each subsequent line will be stripped +out before evaluation. + +```js +/** + * @example + * anArray.filter((a) => { + * return a.b; + * }); + */ +``` + #### `reportUnusedDisableDirectives` If not set to `false`, `reportUnusedDisableDirectives` will report disabled diff --git a/src/rules/checkExamples.js b/src/rules/checkExamples.js index be8d6396a..87ab72de7 100644 --- a/src/rules/checkExamples.js +++ b/src/rules/checkExamples.js @@ -29,6 +29,7 @@ export default iterateJsdoc(({ noDefaultExampleRules = false, eslintrcForExamples = true, matchingFileName: filename = null, + paddedIndent = 0, baseConfig = {}, configFile, allowInlineConfig = true, @@ -150,6 +151,10 @@ export default iterateJsdoc(({ let messages; + if (paddedIndent) { + source = source.replace(new RegExp(`(^|\n) {${paddedIndent}}(?!$)`, 'g'), '\n'); + } + if (filename) { const config = cli.getConfigForFile(filename); @@ -258,6 +263,10 @@ export default iterateJsdoc(({ default: false, type: 'boolean' }, + paddedIndent: { + default: 0, + type: 'integer' + }, rejectExampleCodeRegex: { type: 'string' }, diff --git a/test/rules/assertions/checkExamples.js b/test/rules/assertions/checkExamples.js index 3b498e2ed..2cc6fc69a 100644 --- a/test/rules/assertions/checkExamples.js +++ b/test/rules/assertions/checkExamples.js @@ -328,6 +328,31 @@ export default { code: ` /** * @example const i = 5; + * quux2() + */ + function quux2 () { + + } + `, + errors: [ + { + message: '@example warning (id-length): Identifier name \'i\' is too short (< 2).' + }, + { + message: '@example error (semi): Missing semicolon.' + } + ], + options: [ + { + paddedIndent: 2 + } + ] + }, + { + code: ` + /** + * @example + * const i = 5; * quux2() */ function quux2 () {