Skip to content

Commit

Permalink
fix(check-examples): when matchingFileName used, properly copy pars…
Browse files Browse the repository at this point in the history
…er 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)
  • Loading branch information
brettz9 committed Jun 26, 2019
1 parent 5ca5db7 commit 383a557
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 3 deletions.
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -684,7 +684,18 @@ function quux () {}
// Message: @example error (semi): Missing semicolon.

/**
* @example quux2()
* @example const i = 5;
* quux2()
*/
function quux2 () {

}
// Settings: {"jsdoc":{"matchingFileName":"test/jsdocUtils.js"}}
// Message: @example warning (id-length): Identifier name 'i' is too short (< 2).

/**
* @example const i = 5;
* quux2()
*/
function quux2 () {

Expand Down
28 changes: 27 additions & 1 deletion src/rules/checkExamples.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,16 +149,42 @@ export default iterateJsdoc(({

if (filename) {
const config = cli.getConfigForFile(filename);

// We need a new instance to ensure that the rules that may only
// be available to `filename` (if it has its own `.eslintrc`),
// will be defined.
const cliFile = new CLIEngine({
allowInlineConfig,
baseConfig: config,
configFile,
reportUnusedDisableDirectives,
rulePaths,
rules,
useEslintrc: eslintrcForExamples
});

const linter = new Linter();

const linterRules = [...cli.getRules().entries()].reduce((obj, [key, val]) => {
// Force external rules to become available on `cli`
try {
cliFile.executeOnText('');
} catch (error) {
// Ignore
}

const linterRules = [...cliFile.getRules().entries()].reduce((obj, [key, val]) => {
obj[key] = val;

return obj;
}, {});

linter.defineRules(linterRules);

if (config.parser) {
// eslint-disable-next-line global-require, import/no-dynamic-require
linter.defineParser(config.parser, require(config.parser));
}

// Could also support `disableFixes` and `allowInlineConfig`
messages = linter.verify(source, config, {
filename,
Expand Down
27 changes: 26 additions & 1 deletion test/rules/assertions/checkExamples.js
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,32 @@ export default {
{
code: `
/**
* @example quux2()
* @example const i = 5;
* quux2()
*/
function quux2 () {
}
`,
errors: [
{
message: '@example warning (id-length): Identifier name \'i\' is too short (< 2).'
},
{
message: '@example error (semi): Missing semicolon.'
}
],
settings: {
jsdoc: {
matchingFileName: 'test/jsdocUtils.js'
}
}
},
{
code: `
/**
* @example const i = 5;
* quux2()
*/
function quux2 () {
Expand Down
3 changes: 3 additions & 0 deletions test/rules/data/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"root": true,
"parserOptions": {
"ecmaVersion": 8
},
"rules": {
"semi": ["error", "always"]
}
Expand Down

0 comments on commit 383a557

Please sign in to comment.