Skip to content

Commit 1b69b04

Browse files
brettz9gajus
authored andcommitted
feat: add allowInlineConfig and reportUnusedDisableDirectives (#126)
1 parent 442285d commit 1b69b04

File tree

6 files changed

+109
-12
lines changed

6 files changed

+109
-12
lines changed

.README/README.md

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ The format of the configuration is as follows:
158158
### Settings to Configure `check-examples`
159159

160160
The settings below all impact the `check-examples` rule and default to
161-
no-op/false except for `eslintrcForExamples` which defaults to `true`.
161+
no-op/false except as noted.
162162

163163
JSDoc specs use of an optional `<caption>` element at the beginning of
164164
`@example`. The following setting requires its use.
@@ -192,6 +192,9 @@ applied to the JavaScript found within the `@example` tags (as determined
192192
to be applicable by the above regex settings). They are ordered by
193193
decreasing precedence:
194194

195+
* `settings.jsdoc.allowInlineConfig` - If not set to `false`, will allow
196+
inline config within the `@example` to override other config. Defaults
197+
to `true`.
195198
* `settings.jsdoc.noDefaultExampleRules` - Setting to `true` will disable the
196199
default rules which are expected to be troublesome for most documentation
197200
use. See the section below for the specific default rules.
@@ -204,13 +207,20 @@ decreasing precedence:
204207
[other plugins](https://github.com/eslint/eslint-plugin-markdown), e.g.,
205208
if one sets `matchingFileName` to `dummy.md` so that `@example` rules will
206209
follow one's Markdown rules).
207-
* `settings.jsdoc.configFile` - A config file. Corresponds to `-c`.
210+
* `settings.jsdoc.configFile` - A config file. Corresponds to ESLint's `-c`.
208211
* `settings.jsdoc.eslintrcForExamples` - Defaults to `true` in adding rules
209212
based on an `.eslintrc.*` file. Setting to `false` corresponds to
210-
`--no-eslintrc`.
213+
ESLint's `--no-eslintrc`.
211214
* `settings.jsdoc.baseConfig` - An object of rules with the same schema
212215
as `.eslintrc.*` for defaults
213216

217+
Finally, the following rule pertains to inline disable directives:
218+
219+
- `settings.jsdoc.reportUnusedDisableDirectives` - If not set to `false`,
220+
this will report disabled directives which are not used (and thus not
221+
needed). Defaults to `true`. Corresponds to ESLint's
222+
`--report-unused-disable-directives`.
223+
214224
#### Rules Disabled by Default Unless `noDefaultExampleRules` is Set to `true`
215225

216226
* `eol-last` - Insisting that a newline "always" be at the end is less likely

.README/rules/check-examples.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@ Works in conjunction with the following settings:
77
* `captionRequired`
88
* `exampleCodeRegex`
99
* `rejectExampleCodeRegex`
10+
* `allowInlineConfig` - Defaults to `true`
1011
* `noDefaultExampleRules`
1112
* `matchingFileName`
1213
* `configFile`
1314
* `eslintrcForExamples` - Defaults to `true`
1415
* `baseConfig`
16+
* `reportUnusedDisableDirectives` - Defaults to `true`
1517

1618
Inline ESLint config within `@example` JavaScript is allowed, though the
1719
disabling of ESLint directives which are not needed by the resolved rules

README.md

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -782,7 +782,7 @@ function quux (foo) {
782782
}
783783
// Settings: {"jsdoc":{"additionalTagNames":{"customTags":["baz","bar"]}}}
784784

785-
/**
785+
/**
786786
* @abstract
787787
* @access
788788
* @alias
@@ -870,9 +870,9 @@ RegExp
870870
<a name="eslint-plugin-jsdoc-rules-check-types-why-not-capital-case-everything"></a>
871871
#### Why not capital case everything?
872872

873-
Why are `boolean`, `number` and `string` exempt from starting with a capital letter? Let's take `string` as an example. In Javascript, everything is an object. The string Object has prototypes for string functions such as `.toUpperCase()`.
873+
Why are `boolean`, `number` and `string` exempt from starting with a capital letter? Let's take `string` as an example. In Javascript, everything is an object. The string Object has prototypes for string functions such as `.toUpperCase()`.
874874

875-
Fortunately we don't have to write `new String()` everywhere in our code. Javascript will automatically wrap string primitives into string Objects when we're applying a string function to a string primitive. This way the memory footprint is a tiny little bit smaller, and the [GC](https://en.wikipedia.org/wiki/Garbage_collection_(computer_science)) has less work to do.
875+
Fortunately we don't have to write `new String()` everywhere in our code. Javascript will automatically wrap string primitives into string Objects when we're applying a string function to a string primitive. This way the memory footprint is a tiny little bit smaller, and the [GC](https://en.wikipedia.org/wiki/Garbage_collection_(computer_science)) has less work to do.
876876

877877
So in a sense, there two types of strings in Javascript; `{string}` literals, also called primitives and `{String}` Objects. We use the primitives because it's easier to write and uses less memory. `{String}` and `{string}` are technically both valid, but they are not the same.
878878

@@ -2190,5 +2190,3 @@ function quux() {
21902190

21912191
}
21922192
```
2193-
2194-

src/iterateJsdoc.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ const curryUtils = (
3333
captionRequired,
3434
matchingFileName,
3535
eslintrcForExamples,
36+
allowInlineConfig,
37+
reportUnusedDisableDirectives,
3638
noDefaultExampleRules,
3739
allowOverrideWithoutParam,
3840
allowImplementsWithoutParam,
@@ -82,6 +84,14 @@ const curryUtils = (
8284
return eslintrcForExamples;
8385
};
8486

87+
utils.allowInlineConfig = () => {
88+
return allowInlineConfig;
89+
};
90+
91+
utils.reportUnusedDisableDirectives = () => {
92+
return reportUnusedDisableDirectives;
93+
};
94+
8595
utils.hasNoDefaultExampleRules = () => {
8696
return noDefaultExampleRules;
8797
};
@@ -148,6 +158,8 @@ export default (iterator) => {
148158
const baseConfig = _.get(context, 'settings.jsdoc.baseConfig') || {};
149159
const configFile = _.get(context, 'settings.jsdoc.configFile');
150160
const eslintrcForExamples = _.get(context, 'settings.jsdoc.eslintrcForExamples') !== false;
161+
const allowInlineConfig = _.get(context, 'settings.jsdoc.allowInlineConfig') !== false;
162+
const reportUnusedDisableDirectives = _.get(context, 'settings.jsdoc.reportUnusedDisableDirectives') !== false;
151163
const captionRequired = Boolean(_.get(context, 'settings.jsdoc.captionRequired'));
152164
const noDefaultExampleRules = Boolean(_.get(context, 'settings.jsdoc.noDefaultExampleRules'));
153165
const allowOverrideWithoutParam = Boolean(_.get(context, 'settings.jsdoc.allowOverrideWithoutParam'));
@@ -212,6 +224,8 @@ export default (iterator) => {
212224
captionRequired,
213225
matchingFileName,
214226
eslintrcForExamples,
227+
allowInlineConfig,
228+
reportUnusedDisableDirectives,
215229
noDefaultExampleRules,
216230
allowOverrideWithoutParam,
217231
allowImplementsWithoutParam,

src/rules/checkExamples.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ export default iterateJsdoc(({
2626
const filename = utils.getMatchingFileName();
2727
const baseConfig = utils.getBaseConfig();
2828
const configFile = utils.getConfigFile();
29+
const allowInlineConfig = utils.allowInlineConfig();
30+
const reportUnusedDisableDirectives = utils.reportUnusedDisableDirectives();
2931

30-
// Make these configurable?
31-
const reportUnusedDisableDirectives = true;
32-
const allowInlineConfig = true;
32+
// Make this configurable?
3333
const rulePaths = [];
3434

3535
const rules = noDefaultExampleRules ? undefined : {

test/rules/assertions/checkExamples.js

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,6 @@ export default {
137137
}
138138
}
139139
},
140-
141140
{
142141
code: `
143142
/**
@@ -209,6 +208,44 @@ export default {
209208
noDefaultExampleRules: false
210209
}
211210
}
211+
},
212+
{
213+
code: `
214+
/**
215+
* @example test() // eslint-disable-line semi
216+
*/
217+
function quux () {}
218+
`,
219+
errors: ['@example error: Unused eslint-disable directive (no problems were reported from \'semi\').'],
220+
settings: {
221+
jsdoc: {
222+
eslintrcForExamples: false,
223+
noDefaultExampleRules: true,
224+
reportUnusedDisableDirectives: true
225+
}
226+
}
227+
},
228+
{
229+
code: `
230+
/**
231+
* @example
232+
test() // eslint-disable-line semi
233+
*/
234+
function quux () {}
235+
`,
236+
errors: ['@example error (semi): Missing semicolon.'],
237+
settings: {
238+
jsdoc: {
239+
allowInlineConfig: false,
240+
baseConfig: {
241+
rules: {
242+
semi: ['error', 'always']
243+
}
244+
},
245+
eslintrcForExamples: false,
246+
noDefaultExampleRules: true
247+
}
248+
}
212249
}
213250
],
214251
valid: [
@@ -313,6 +350,42 @@ export default {
313350
eslintrcForExamples: false
314351
}
315352
}
353+
},
354+
{
355+
code: `
356+
/**
357+
* @example test() // eslint-disable-line semi
358+
*/
359+
function quux () {}
360+
`,
361+
settings: {
362+
jsdoc: {
363+
eslintrcForExamples: false,
364+
noDefaultExampleRules: true,
365+
reportUnusedDisableDirectives: false
366+
}
367+
}
368+
},
369+
{
370+
code: `
371+
/**
372+
* @example
373+
test() // eslint-disable-line semi
374+
*/
375+
function quux () {}
376+
`,
377+
settings: {
378+
jsdoc: {
379+
allowInlineConfig: true,
380+
baseConfig: {
381+
rules: {
382+
semi: ['error', 'always']
383+
}
384+
},
385+
eslintrcForExamples: false,
386+
noDefaultExampleRules: true
387+
}
388+
}
316389
}
317390
]
318391
};

0 commit comments

Comments
 (0)