Skip to content

Commit 383a557

Browse files
committed
fix(check-examples): when matchingFileName used, properly copy parser and load external rules for eslint 6
To test this fix, added a line to an existing test so as to differentiate it from another (and more complex) `.eslintrc.*` (our own project's)
1 parent 5ca5db7 commit 383a557

File tree

4 files changed

+68
-3
lines changed

4 files changed

+68
-3
lines changed

README.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -684,7 +684,18 @@ function quux () {}
684684
// Message: @example error (semi): Missing semicolon.
685685

686686
/**
687-
* @example quux2()
687+
* @example const i = 5;
688+
* quux2()
689+
*/
690+
function quux2 () {
691+
692+
}
693+
// Settings: {"jsdoc":{"matchingFileName":"test/jsdocUtils.js"}}
694+
// Message: @example warning (id-length): Identifier name 'i' is too short (< 2).
695+
696+
/**
697+
* @example const i = 5;
698+
* quux2()
688699
*/
689700
function quux2 () {
690701

src/rules/checkExamples.js

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,16 +149,42 @@ export default iterateJsdoc(({
149149

150150
if (filename) {
151151
const config = cli.getConfigForFile(filename);
152+
153+
// We need a new instance to ensure that the rules that may only
154+
// be available to `filename` (if it has its own `.eslintrc`),
155+
// will be defined.
156+
const cliFile = new CLIEngine({
157+
allowInlineConfig,
158+
baseConfig: config,
159+
configFile,
160+
reportUnusedDisableDirectives,
161+
rulePaths,
162+
rules,
163+
useEslintrc: eslintrcForExamples
164+
});
165+
152166
const linter = new Linter();
153167

154-
const linterRules = [...cli.getRules().entries()].reduce((obj, [key, val]) => {
168+
// Force external rules to become available on `cli`
169+
try {
170+
cliFile.executeOnText('');
171+
} catch (error) {
172+
// Ignore
173+
}
174+
175+
const linterRules = [...cliFile.getRules().entries()].reduce((obj, [key, val]) => {
155176
obj[key] = val;
156177

157178
return obj;
158179
}, {});
159180

160181
linter.defineRules(linterRules);
161182

183+
if (config.parser) {
184+
// eslint-disable-next-line global-require, import/no-dynamic-require
185+
linter.defineParser(config.parser, require(config.parser));
186+
}
187+
162188
// Could also support `disableFixes` and `allowInlineConfig`
163189
messages = linter.verify(source, config, {
164190
filename,

test/rules/assertions/checkExamples.js

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,32 @@ export default {
301301
{
302302
code: `
303303
/**
304-
* @example quux2()
304+
* @example const i = 5;
305+
* quux2()
306+
*/
307+
function quux2 () {
308+
309+
}
310+
`,
311+
errors: [
312+
{
313+
message: '@example warning (id-length): Identifier name \'i\' is too short (< 2).'
314+
},
315+
{
316+
message: '@example error (semi): Missing semicolon.'
317+
}
318+
],
319+
settings: {
320+
jsdoc: {
321+
matchingFileName: 'test/jsdocUtils.js'
322+
}
323+
}
324+
},
325+
{
326+
code: `
327+
/**
328+
* @example const i = 5;
329+
* quux2()
305330
*/
306331
function quux2 () {
307332

test/rules/data/.eslintrc.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
{
22
"root": true,
3+
"parserOptions": {
4+
"ecmaVersion": 8
5+
},
36
"rules": {
47
"semi": ["error", "always"]
58
}

0 commit comments

Comments
 (0)