Skip to content

Commit 41b240f

Browse files
brettz9gajus
authored andcommitted
feat: check-examples rule (#119)
* - feat: Add `check-examples` rule to lint JavaScript `@example` (fixes #101); with settings to: 1. require JSDoc-spec'd `<caption>` at beginning of `@example` 2. regex settings to whitelist and blacklist lintable examples 3. setting for a dummy file name to trigger specific rules defined in one's config; usable with ESLint `overrides`->`files` globs, to apply a desired subset of rules with `@example` (besides allowing for rules specific to examples, can be useful for using same rules within `@example` as with JavaScript Markdown lintable by other plugins) 4. Other settings for specifying config (base config object or an `.eslintrc` config file; checks normal `.eslintrc` by default unless `eslintrcForExamples` setting is set to `false`) 5. Provides some defaults for suppressing reporting of rules which are likely to be troublesome in example files unless `noDefaultExampleRules` setting is `true` - Report column (for checkExamples and any that report it) * docs: generate docs
1 parent b32e6ba commit 41b240f

File tree

8 files changed

+937
-5
lines changed

8 files changed

+937
-5
lines changed

.README/README.md

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ This table maps the rules between `eslint-plugin-jsdoc` and `jscs-jsdoc`.
1414

1515
| `eslint-plugin-jsdoc` | `jscs-jsdoc` |
1616
| --- | --- |
17+
| [`check-examples`](https://github.com/gajus/eslint-plugin-jsdoc#eslint-plugin-jsdoc-rules-check-examples) | N/A |
1718
| [`check-param-names`](https://github.com/gajus/eslint-plugin-jsdoc#eslint-plugin-jsdoc-rules-check-param-names) | [`checkParamNames`](https://github.com/jscs-dev/jscs-jsdoc#checkparamnames) |
1819
| [`check-tag-names`](https://github.com/gajus/eslint-plugin-jsdoc#eslint-plugin-jsdoc-rules-check-tag-names) | N/A ~ [`checkAnnotations`](https://github.com/jscs-dev/jscs-jsdoc#checkannotations) |
1920
| [`check-types`](https://github.com/gajus/eslint-plugin-jsdoc#eslint-plugin-jsdoc-rules-check-types) | [`checkTypes`](https://github.com/jscs-dev/jscs-jsdoc#checktypes) |
@@ -68,6 +69,7 @@ Finally, enable all of the rules that you would like to use.
6869
```json
6970
{
7071
"rules": {
72+
"jsdoc/check-examples": 1,
7173
"jsdoc/check-param-names": 1,
7274
"jsdoc/check-tag-names": 1,
7375
"jsdoc/check-types": 1,
@@ -108,7 +110,6 @@ Use `settings.jsdoc.tagNamePreference` to configure a preferred alias name for a
108110
}
109111
```
110112

111-
112113
### Additional Tag Names
113114

114115
Use `settings.jsdoc.additionalTagNames` to configure additional, allowed JSDoc tags. The format of the configuration is as follows:
@@ -154,8 +155,83 @@ The format of the configuration is as follows:
154155
}
155156
```
156157

158+
### Settings to Configure `check-examples`
159+
160+
The settings below all impact the `check-examples` rule and default to
161+
no-op/false except for `eslintrcForExamples` which defaults to `true`.
162+
163+
JSDoc specs use of an optional `<caption>` element at the beginning of
164+
`@example`. The following setting requires its use.
165+
166+
* `settings.jsdoc.captionRequired` - Require `<caption>` at beginning
167+
of any `@example`
168+
169+
JSDoc does not specify a formal means for delimiting code blocks within
170+
`@example` (it uses generic syntax highlighting techniques for its own
171+
syntax highlighting). The following settings determine whether a given
172+
`@example` tag will have the `check-examples` checks applied to it:
173+
174+
* `settings.jsdoc.exampleCodeRegex` - Regex which whitelists lintable
175+
examples. If a parenthetical group is used, the first one will be used,
176+
so you may wish to use `(?:...)` groups where you do not wish the
177+
first such group treated as one to include. If no parenthetical group
178+
exists or matches, the whole matching expression will be used.
179+
An example might be ````"^```(?:js|javascript)([\\s\\S]*)```$"````
180+
to only match explicitly fenced JavaScript blocks.
181+
* `settings.jsdoc.rejectExampleCodeRegex` - Regex blacklist which rejects
182+
non-lintable examples (has priority over `exampleCodeRegex`). An example
183+
might be ```"^`"``` to avoid linting fenced blocks which may indicate
184+
a non-JavaScript language.
185+
186+
If neither is in use, all examples will be matched. Note also that even if
187+
`settings.jsdoc.captionRequired` is not set, any initial `<caption>`
188+
will be stripped out before doing the regex matching.
189+
190+
The following settings determine which individual ESLint rules will be
191+
applied to the JavaScript found within the `@example` tags (as determined
192+
to be applicable by the above regex settings). They are ordered by
193+
decreasing precedence:
194+
195+
* `settings.jsdoc.noDefaultExampleRules` - Setting to `true` will disable the
196+
default rules which are expected to be troublesome for most documentation
197+
use. See the section below for the specific default rules.
198+
* `settings.jsdoc.matchingFileName` - Setting for a dummy file name to trigger
199+
specific rules defined in one's config; usable with ESLint `.eslintrc.*`
200+
`overrides` -> `files` globs, to apply a desired subset of rules with
201+
`@example` (besides allowing for rules specific to examples, this setting
202+
can be useful for enabling reuse of the same rules within `@example` as
203+
with JavaScript Markdown lintable by
204+
[other plugins](https://github.com/eslint/eslint-plugin-markdown), e.g.,
205+
if one sets `matchingFileName` to `dummy.md` so that `@example` rules will
206+
follow one's Markdown rules).
207+
* `settings.jsdoc.configFile` - A config file. Corresponds to `-c`.
208+
* `settings.jsdoc.eslintrcForExamples` - Defaults to `true` in adding rules
209+
based on an `.eslintrc.*` file. Setting to `false` corresponds to
210+
`--no-eslintrc`.
211+
* `settings.jsdoc.baseConfig` - An object of rules with the same schema
212+
as `.eslintrc.*` for defaults
213+
214+
#### Rules Disabled by Default Unless `noDefaultExampleRules` is Set to `true`
215+
216+
* `eol-last` - Insisting that a newline "always" be at the end is less likely
217+
to be desired in sample code as with the code file convention
218+
* `no-console` - Unlikely to have inadvertent temporary debugging within
219+
examples
220+
* `no-undef` - Many variables in examples will be `undefined`.
221+
* `no-unused-vars` - It is common to define variables for clarity without always
222+
using them within examples.
223+
* `padded-blocks` - It can generally look nicer to pad a little even if one's
224+
code follows more stringency as far as block padding.
225+
* `import/no-unresolved` - One wouldn't generally expect example paths to
226+
resolve relative to the current JavaScript file as one would with real code.
227+
* `import/unambiguous` - Snippets in examples are likely too short to always
228+
include full import/export info
229+
* `node/no-missing-import` - See `import/no-unresolved`
230+
* `node/no-missing-require` - See `import/no-unresolved`
231+
157232
## Rules
158233

234+
{"gitdown": "include", "file": "./rules/check-examples.md"}
159235
{"gitdown": "include", "file": "./rules/check-param-names.md"}
160236
{"gitdown": "include", "file": "./rules/check-tag-names.md"}
161237
{"gitdown": "include", "file": "./rules/check-types.md"}

.README/rules/check-examples.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
### `check-examples`
2+
3+
Ensures that (JavaScript) examples within JSDoc adhere to ESLint rules.
4+
5+
Works in conjunction with the following settings:
6+
7+
* `captionRequired`
8+
* `exampleCodeRegex`
9+
* `rejectExampleCodeRegex`
10+
* `noDefaultExampleRules`
11+
* `matchingFileName`
12+
* `configFile`
13+
* `eslintrcForExamples` - Defaults to `true`
14+
* `baseConfig`
15+
16+
Inline ESLint config within `@example` JavaScript is allowed, though the
17+
disabling of ESLint directives which are not needed by the resolved rules
18+
will be reported as with the ESLint `--report-unused-disable-directives`
19+
command.
20+
21+
|||
22+
|---|---|
23+
|Context|`ArrowFunctionExpression`, `FunctionDeclaration`, `FunctionExpression`|
24+
|Tags|`param`|
25+
26+
<!-- assertions checkExamples -->

0 commit comments

Comments
 (0)