Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: avoid crashing on malformed configuration comments (fixes #9373) #9819

Merged
merged 2 commits into from Jan 10, 2018

Conversation

not-an-aardvark
Copy link
Member

What is the purpose of this pull request? (put an "X" next to item)

[x] Bug fix (#9373)

What changes did you make? (Give an overview)

This updates Linter to treat malformed configuration comments as linting errors rather than fatal issues that crash the process. Previously, this could cause surprising behavior because it was the only known instance where a problem in the source code (given a valid configuration) could cause an error to be thrown.

For example, this also fixes eslint/archive-website#413.

Is there anything you'd like reviewers to focus on?

Nothing in particular

This updates `Linter` to treat malformed configuration comments as linting errors rather than fatal issues that crash the process. Previously, this could cause surprising behavior because it was the only known instance where a problem in the source code (given a valid configuration) could cause an error to be thrown.

For example, this also fixes eslint/archive-website#413.
@not-an-aardvark not-an-aardvark added accepted There is consensus among the team that this change meets the criteria for inclusion bug ESLint is working incorrectly core Relates to ESLint's core APIs and features labels Jan 7, 2018
source: null,
message: err.message,
line: comment.loc.start.line,
column: comment.loc.start.column + 1,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think good if it defines endLine and endColumn with comment.loc.end.

Copy link
Member

@platinumazure platinumazure left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This generally looks good to me, but I wanted to leave some thoughts inline and see what you think.

@@ -112,7 +113,13 @@ function validateRuleOptions(rule, ruleId, options, source) {
validateRuleSchema(rule, Array.isArray(options) ? options.slice(1) : []);
}
} catch (err) {
throw new Error(`${source}:\n\tConfiguration for rule "${ruleId}" is invalid:\n${err.message}`);
const enhancedMessage = `Configuration for rule "${ruleId}" is invalid:\n${err.message}`;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wondering if this should say "Inline configuration" (or equivalent), in order to make it clear that it's from a configuration comment rather than from a user's configuration files?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I went with the current behavior on the assumption that it would be clear because the reported problem is at the comment's location.

I'd be fine with changing it if you feel strongly about it, although I wonder if we would want to refactor this function to make the error messages more flexible.

validator.validateRuleOptions(ruleMapper(name), name, ruleValue, `${filename} line ${comment.loc.start.line}`);
try {
validator.validateRuleOptions(ruleMapper(name), name, ruleValue);
} catch (err) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we decorate the thrown error object with some property to indicate it is specifically the validation error we want to report? That way, if we check for that property here, an unexpected exception (from something not directly due to schema validation) would propagate as a crash.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems reasonable, although I think we already use thrown errors to communicate schema issues in other places.

Maybe it would be better to switch to a result type instead, where validator.validateRuleOptions would return something like { success: true } or { success: false, error: err }. Then a thrown error would always indicate that something went wrong.

@platinumazure
Copy link
Member

platinumazure commented Jan 7, 2018 via email

@platinumazure
Copy link
Member

@not-an-aardvark Instead of calculating line/column/endLine/endColumn manually, can we just pass the comment token to context.report? Or does that not support comments, only nodes/tokens?

@not-an-aardvark
Copy link
Member Author

not-an-aardvark commented Jan 8, 2018

I can look into doing that. I think it would be nontrivial because a context only exists when a rule has been created, but it would be nice to reuse the report translator logic to avoid needing to manually remember all of the properties of a processed report.

@platinumazure
Copy link
Member

I think it would be nontrivial because a context only exists when a rule has been created

Ack, for some reason I thought we were calling context.report, but you're quite right. I don't think we should pull out the report translator here. I'm fine with this going in as is. Maybe we could look into that functionality later... Thanks!

Copy link
Member

@mysticatea mysticatea left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thank you!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
accepted There is consensus among the team that this change meets the criteria for inclusion archived due to age This issue has been archived; please open a new issue for any further discussion bug ESLint is working incorrectly core Relates to ESLint's core APIs and features
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Invalid rule configuration only gives feedback on console and does not update UI at all
4 participants