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: don't generate invalid options in config-rule #8326

Merged
merged 1 commit into from Mar 27, 2017

Conversation

not-an-aardvark
Copy link
Member

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

[x] Bug fix

Tell us about your environment

  • ESLint Version: master
  • Node Version: 7.7.4
  • npm Version: 4.1.2

What did you do? Please include the actual source code causing the issue.

const ruleConfigs = require("eslint/lib/config/config-rule").createCoreRuleConfigs();

What did you expect to happen?

I expected all the configs in ruleConfigs to be valid.

What actually happened? Please include the actual, raw output from ESLint.

Some of the configs were invalid. For example:

console.log(ruleConfigs["id-match"]);
// => [ 2, [ 2, { properties: true } ], [ 2, { properties: false } ] ]

What changes did you make? (Give an overview)

Previously, the config-rule logic for generating rule options had a bug where it could generate invalid options. For example, it could generate these options for the id-match rule:

[2, { "properties": true }]

This is invalid because the first option in id-match must be a string, and the second option must be an object. The config generator doesn't handle strings, but it was incorrectly skipping the string option and trying to fill in the config with an object after realizing that it couldn't generate a string. As a result, it placed the object where the string was supposed to go, so an invalid set of options was created. A similar issue can appear when generating configs for the max-statements rule.

Luckily, the id-match and max-statements rule are still able to run when passed an invalid config, and the invalid config never results in fewer errors being reported than the default, so the invalid options wouldn't ever get output to a config file. However, this is only the case due to implementation details in the two rules, and a new rule could be introduced in the future that crashes when given an invalid schema, so this is still worth fixing.

This commit fixes the issue by stopping early when the option generator reaches an option that it doesn't know how to fill in.

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

Nothing in particular

Previously, the `config-rule` logic for generating rule options had a bug where it could generate invalid options. For example, it could generate these options for the `id-match` rule:

```json
[2, { "properties": true }]
```

This is invalid because the first option in `id-match` must be a string, and the second option must be an object. The config generator doesn't handle strings, but it was incorrectly skipping the string option and trying to fill in the config with an object after realizing that it couldn't generate a string. As a result, it placed the object where the string was supposed to go, so an invalid set of options was created. A similar issue can appear when generating configs for the `max-statements` rule.

Luckily, the `id-match` and `max-statements` rule are still able to run when passed an invalid config, and the invalid config never results in fewer errors being reported than the default, so the invalid options wouldn't ever get output to a config file. However, this is only the case due to implementation details in the two rules, and a new rule could be introduced in the future that crashes when given an invalid schema, so this is still worth fixing.

This commit fixes the issue by stopping early when the option generator reaches an option that it doesn't know how to fill in.
@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 rule Relates to ESLint's core rules labels Mar 26, 2017
@mention-bot
Copy link

@not-an-aardvark, thanks for your PR! By analyzing the history of the files in this pull request, we identified @IanVS, @mysticatea and @gyandeeps to be potential reviewers.

@eslintbot
Copy link

LGTM

Copy link
Member

@IanVS IanVS left a comment

Choose a reason for hiding this comment

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

Beautiful. This is exactly what I had in mind, and I'm glad that someone besides myself is getting familiar with the autoconfig logic. :) Thanks for stepping in and taking care of this.

@gyandeeps gyandeeps merged commit d52173f into master Mar 27, 2017
@not-an-aardvark not-an-aardvark deleted the config-rule-valid-schemas branch March 27, 2017 19:53
@eslint-deprecated eslint-deprecated bot locked and limited conversation to collaborators Feb 6, 2018
@eslint-deprecated eslint-deprecated bot added the archived due to age This issue has been archived; please open a new issue for any further discussion label Feb 6, 2018
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 rule Relates to ESLint's core rules
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants