|
2 | 2 |
|
3 | 3 | Ensures that (JavaScript) examples within JSDoc adhere to ESLint rules.
|
4 | 4 |
|
5 |
| -Works in conjunction with the following settings: |
6 |
| - |
7 |
| -* `captionRequired` |
8 |
| -* `exampleCodeRegex` |
9 |
| -* `rejectExampleCodeRegex` |
10 |
| -* `allowInlineConfig` - Defaults to `true` |
11 |
| -* `noDefaultExampleRules` |
12 |
| -* `matchingFileName` |
13 |
| -* `configFile` |
14 |
| -* `eslintrcForExamples` - Defaults to `true` |
15 |
| -* `baseConfig` |
16 |
| -* `reportUnusedDisableDirectives` - Defaults to `true` |
| 5 | +### Options |
| 6 | + |
| 7 | +The options below all default to no-op/`false` except as noted. |
| 8 | + |
| 9 | +#### `captionRequired` |
| 10 | + |
| 11 | +JSDoc specs use of an optional `<caption>` element at the beginning of |
| 12 | +`@example`. |
| 13 | + |
| 14 | +The option `captionRequired` insists on a `<caption>` being present at |
| 15 | +the beginning of any `@example`. |
| 16 | + |
| 17 | +#### `exampleCodeRegex` and `rejectExampleCodeRegex` |
| 18 | + |
| 19 | +JSDoc does not specify a formal means for delimiting code blocks within |
| 20 | +`@example` (it uses generic syntax highlighting techniques for its own |
| 21 | +syntax highlighting). The following options determine whether a given |
| 22 | +`@example` tag will have the `check-examples` checks applied to it: |
| 23 | + |
| 24 | +* `exampleCodeRegex` - Regex which whitelists lintable |
| 25 | + examples. If a parenthetical group is used, the first one will be used, |
| 26 | + so you may wish to use `(?:...)` groups where you do not wish the |
| 27 | + first such group treated as one to include. If no parenthetical group |
| 28 | + exists or matches, the whole matching expression will be used. |
| 29 | + An example might be ````"^```(?:js|javascript)([\\s\\S]*)```$"```` |
| 30 | + to only match explicitly fenced JavaScript blocks. |
| 31 | +* `rejectExampleCodeRegex` - Regex blacklist which rejects |
| 32 | + non-lintable examples (has priority over `exampleCodeRegex`). An example |
| 33 | + might be ```"^`"``` to avoid linting fenced blocks which may indicate |
| 34 | + a non-JavaScript language. |
| 35 | + |
| 36 | +If neither is in use, all examples will be matched. Note also that even if |
| 37 | +`captionRequired` is not set, any initial `<caption>` will be stripped out |
| 38 | +before doing the regex matching. |
| 39 | + |
| 40 | +#### `reportUnusedDisableDirectives` |
| 41 | + |
| 42 | +If not set to `false`, `reportUnusedDisableDirectives` will report disabled |
| 43 | +directives which are not used (and thus not needed). Defaults to `true`. |
| 44 | +Corresponds to ESLint's [`--report-unused-disable-directives`](https://eslint.org/docs/user-guide/command-line-interface#--report-unused-disable-directives). |
17 | 45 |
|
18 | 46 | Inline ESLint config within `@example` JavaScript is allowed, though the
|
19 | 47 | disabling of ESLint directives which are not needed by the resolved rules
|
20 | 48 | will be reported as with the ESLint `--report-unused-disable-directives`
|
21 | 49 | command.
|
22 | 50 |
|
| 51 | +#### Options for Determining ESLint Rule Applicability (`allowInlineConfig`, `noDefaultExampleRules`, `matchingFileName`, `configFile`, `eslintrcForExamples`, and `baseConfig`) |
| 52 | + |
| 53 | +The following options determine which individual ESLint rules will be |
| 54 | +applied to the JavaScript found within the `@example` tags (as determined |
| 55 | +to be applicable by the above regex options). They are ordered by |
| 56 | +decreasing precedence: |
| 57 | + |
| 58 | +* `allowInlineConfig` - If not set to `false`, will allow |
| 59 | + inline config within the `@example` to override other config. Defaults |
| 60 | + to `true`. |
| 61 | +* `noDefaultExampleRules` - Setting to `true` will disable the |
| 62 | + default rules which are expected to be troublesome for most documentation |
| 63 | + use. See the section below for the specific default rules. |
| 64 | +* `matchingFileName` - Option for a file name (even non-existent) to trigger |
| 65 | + specific rules defined in one's config; usable with ESLint `.eslintrc.*` |
| 66 | + `overrides` -> `files` globs, to apply a desired subset of rules with |
| 67 | + `@example` (besides allowing for rules specific to examples, this option |
| 68 | + can be useful for enabling reuse of the same rules within `@example` as |
| 69 | + with JavaScript Markdown lintable by |
| 70 | + [other plugins](https://github.com/eslint/eslint-plugin-markdown), e.g., |
| 71 | + if one sets `matchingFileName` to `dummy.md` so that `@example` rules will |
| 72 | + follow one's Markdown rules). Note that this option may come at somewhat |
| 73 | + of a performance penalty as the file's existence is checked by eslint. |
| 74 | +* `configFile` - A config file. Corresponds to ESLint's [`-c`](https://eslint.org/docs/user-guide/command-line-interface#-c---config). |
| 75 | +* `eslintrcForExamples` - Defaults to `true` in adding rules |
| 76 | + based on an `.eslintrc.*` file. Setting to `false` corresponds to |
| 77 | + ESLint's [`--no-eslintrc`](https://eslint.org/docs/user-guide/command-line-interface#--no-eslintrc). |
| 78 | +* `baseConfig` - An object of rules with the same schema |
| 79 | + as `.eslintrc.*` for defaults |
| 80 | + |
| 81 | +##### Rules Disabled by Default Unless `noDefaultExampleRules` is Set to `true` |
| 82 | + |
| 83 | +* `eol-last` - Insisting that a newline "always" be at the end is less likely |
| 84 | + to be desired in sample code as with the code file convention |
| 85 | +* `no-console` - Unlikely to have inadvertent temporary debugging within |
| 86 | + examples |
| 87 | +* `no-undef` - Many variables in examples will be `undefined`. |
| 88 | +* `no-unused-vars` - It is common to define variables for clarity without always |
| 89 | + using them within examples. |
| 90 | +* `padded-blocks` - It can generally look nicer to pad a little even if one's |
| 91 | + code follows more stringency as far as block padding. |
| 92 | +* `import/no-unresolved` - One wouldn't generally expect example paths to |
| 93 | + resolve relative to the current JavaScript file as one would with real code. |
| 94 | +* `import/unambiguous` - Snippets in examples are likely too short to always |
| 95 | + include full import/export info |
| 96 | +* `node/no-missing-import` - See `import/no-unresolved` |
| 97 | +* `node/no-missing-require` - See `import/no-unresolved` |
| 98 | + |
23 | 99 | |||
|
24 | 100 | |---|---|
|
25 | 101 | |Context|`ArrowFunctionExpression`, `ClassDeclaration`, `FunctionDeclaration`, `FunctionExpression`|
|
26 | 102 | |Tags|`example`|
|
27 |
| -|Settings| *See above* | |
| 103 | +|Options| *See above* | |
28 | 104 |
|
29 | 105 | <!-- assertions checkExamples -->
|
0 commit comments