Permalink
Browse files

Update: Include debugging information when rule throws error (#9742)

When developing eslint rules (including for plugins), tracking down
errors within rules is difficult and requires manual updating of the
code to track down where the error is coming from. This adds additional
diagnostic information which can help

```
  eslint:traverser An error occurred while traversing +736ms
  eslint:traverser Filename: /Users/patrick/dev/test.js +0ms
  eslint:traverser Parser Options: { ecmaFeatures: { globalReturn: false, jsx: true },
  codeFrame: false,
  ecmaVersion: 6,
  sourceType: 'module' } +0ms
  eslint:traverser Parser Path: /Users/patrick/dev/node_modules/babel-eslint/index.js +2ms
  eslint:traverser Settings: { react: { pragma: 'React' } } +0ms
```

I opted for `debug` logging to make this minimally disruptive, but I
would also advocate for promoting this information to being always logged to
stderr. This information will be valuable to developers working on
rules, but may also be valuable to the end developer to help them track
down the source of their error
  • Loading branch information...
pfhayes authored and not-an-aardvark committed Mar 30, 2018
1 parent 9a020dc commit 462b058d04a8b4ccdee300dd2773e4d8f4125cfc
Showing with 21 additions and 12 deletions.
  1. +20 −9 lib/linter.js
  2. +1 −3 tests/lib/linter.js
View
@@ -1008,15 +1008,26 @@ module.exports = class Linter {
const configuredRules = Object.assign({}, config.rules, commentDirectives.configuredRules);
const lintingProblems = runRules(
sourceCode,
configuredRules,
ruleId => this.rules.get(ruleId),
parserOptions,
parserName,
settings,
options.filename
);
let lintingProblems;
try {
lintingProblems = runRules(
sourceCode,
configuredRules,
ruleId => this.rules.get(ruleId),
parserOptions,
parserName,
settings,
options.filename
);
} catch (err) {
debug("An error occurred while traversing");
debug("Filename:", options.filename);
debug("Parser Options:", parserOptions);
debug("Parser Path:", parserName);
debug("Settings:", settings);
throw err;
}
return applyDisableDirectives({
directives: commentDirectives.disableDirectives,
View
@@ -98,7 +98,7 @@ describe("Linter", () => {
}));
assert.throws(() => {
linter.verify(code, config, filename, true);
linter.verify(code, config, filename);
}, "Intentional error.");
});
@@ -4362,6 +4362,4 @@ describe("Linter", () => {
});
});
}
});

0 comments on commit 462b058

Please sign in to comment.