Skip to content

Commit 9c9429e

Browse files
authored
Merge pull request #318 from brettz9/check-examples-settings-to-options
`check-examples`: change settings to options
2 parents f44b25e + c4f221d commit 9c9429e

File tree

7 files changed

+526
-454
lines changed

7 files changed

+526
-454
lines changed

.README/README.md

Lines changed: 0 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -315,91 +315,6 @@ how the keys of `preferredTypes` may have `<>` or `.<>` (or just `.`)
315315
appended and its bearing on whether types are checked as parents/children
316316
only (e.g., to match `Array` if the type is `Array` vs. `Array.<string>`).
317317

318-
### Settings to Configure `check-examples`
319-
320-
The settings below all impact the `check-examples` rule and default to
321-
no-op/false except as noted.
322-
323-
JSDoc specs use of an optional `<caption>` element at the beginning of
324-
`@example`. The following setting requires its use.
325-
326-
* `settings.jsdoc.captionRequired` - Require `<caption>` at beginning
327-
of any `@example`
328-
329-
JSDoc does not specify a formal means for delimiting code blocks within
330-
`@example` (it uses generic syntax highlighting techniques for its own
331-
syntax highlighting). The following settings determine whether a given
332-
`@example` tag will have the `check-examples` checks applied to it:
333-
334-
* `settings.jsdoc.exampleCodeRegex` - Regex which whitelists lintable
335-
examples. If a parenthetical group is used, the first one will be used,
336-
so you may wish to use `(?:...)` groups where you do not wish the
337-
first such group treated as one to include. If no parenthetical group
338-
exists or matches, the whole matching expression will be used.
339-
An example might be ````"^```(?:js|javascript)([\\s\\S]*)```$"````
340-
to only match explicitly fenced JavaScript blocks.
341-
* `settings.jsdoc.rejectExampleCodeRegex` - Regex blacklist which rejects
342-
non-lintable examples (has priority over `exampleCodeRegex`). An example
343-
might be ```"^`"``` to avoid linting fenced blocks which may indicate
344-
a non-JavaScript language.
345-
346-
If neither is in use, all examples will be matched. Note also that even if
347-
`settings.jsdoc.captionRequired` is not set, any initial `<caption>`
348-
will be stripped out before doing the regex matching.
349-
350-
The following settings determine which individual ESLint rules will be
351-
applied to the JavaScript found within the `@example` tags (as determined
352-
to be applicable by the above regex settings). They are ordered by
353-
decreasing precedence:
354-
355-
* `settings.jsdoc.allowInlineConfig` - If not set to `false`, will allow
356-
inline config within the `@example` to override other config. Defaults
357-
to `true`.
358-
* `settings.jsdoc.noDefaultExampleRules` - Setting to `true` will disable the
359-
default rules which are expected to be troublesome for most documentation
360-
use. See the section below for the specific default rules.
361-
* `settings.jsdoc.matchingFileName` - Setting for a dummy file name to trigger
362-
specific rules defined in one's config; usable with ESLint `.eslintrc.*`
363-
`overrides` -> `files` globs, to apply a desired subset of rules with
364-
`@example` (besides allowing for rules specific to examples, this setting
365-
can be useful for enabling reuse of the same rules within `@example` as
366-
with JavaScript Markdown lintable by
367-
[other plugins](https://github.com/eslint/eslint-plugin-markdown), e.g.,
368-
if one sets `matchingFileName` to `dummy.md` so that `@example` rules will
369-
follow one's Markdown rules). Note that this option may come at somewhat
370-
of a performance penalty as the file's existence is checked by eslint.
371-
* `settings.jsdoc.configFile` - A config file. Corresponds to ESLint's `-c`.
372-
* `settings.jsdoc.eslintrcForExamples` - Defaults to `true` in adding rules
373-
based on an `.eslintrc.*` file. Setting to `false` corresponds to
374-
ESLint's `--no-eslintrc`.
375-
* `settings.jsdoc.baseConfig` - An object of rules with the same schema
376-
as `.eslintrc.*` for defaults
377-
378-
Finally, the following rule pertains to inline disable directives:
379-
380-
- `settings.jsdoc.reportUnusedDisableDirectives` - If not set to `false`,
381-
this will report disabled directives which are not used (and thus not
382-
needed). Defaults to `true`. Corresponds to ESLint's
383-
`--report-unused-disable-directives`.
384-
385-
#### Rules Disabled by Default Unless `noDefaultExampleRules` is Set to `true`
386-
387-
* `eol-last` - Insisting that a newline "always" be at the end is less likely
388-
to be desired in sample code as with the code file convention
389-
* `no-console` - Unlikely to have inadvertent temporary debugging within
390-
examples
391-
* `no-undef` - Many variables in examples will be `undefined`.
392-
* `no-unused-vars` - It is common to define variables for clarity without always
393-
using them within examples.
394-
* `padded-blocks` - It can generally look nicer to pad a little even if one's
395-
code follows more stringency as far as block padding.
396-
* `import/no-unresolved` - One wouldn't generally expect example paths to
397-
resolve relative to the current JavaScript file as one would with real code.
398-
* `import/unambiguous` - Snippets in examples are likely too short to always
399-
include full import/export info
400-
* `node/no-missing-import` - See `import/no-unresolved`
401-
* `node/no-missing-require` - See `import/no-unresolved`
402-
403318
## Rules
404319

405320
{"gitdown": "include", "file": "./rules/check-alignment.md"}

.README/rules/check-examples.md

Lines changed: 89 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,104 @@
22

33
Ensures that (JavaScript) examples within JSDoc adhere to ESLint rules.
44

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).
1745

1846
Inline ESLint config within `@example` JavaScript is allowed, though the
1947
disabling of ESLint directives which are not needed by the resolved rules
2048
will be reported as with the ESLint `--report-unused-disable-directives`
2149
command.
2250

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+
2399
|||
24100
|---|---|
25101
|Context|`ArrowFunctionExpression`, `ClassDeclaration`, `FunctionDeclaration`, `FunctionExpression`|
26102
|Tags|`example`|
27-
|Settings| *See above* |
103+
|Options| *See above* |
28104

29105
<!-- assertions checkExamples -->

0 commit comments

Comments
 (0)