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: validate options when comment with just severity enables rule #18133

Merged
merged 1 commit into from Feb 23, 2024

Conversation

mdjermanovic
Copy link
Member

Prerequisites checklist

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

[ ] Documentation update
[x] Bug fix (template)
[ ] New rule (template)
[ ] Changes an existing rule (template)
[ ] Add autofix to a rule
[ ] Add a CLI option
[ ] Add something to the core
[ ] Other, please explain:

Tell us about your environment (npx eslint --env-info):

  • Node version: 20.9.0
  • npm version: 10.2.0
  • Local ESLint version: 9.0.0-beta.0
  • Global ESLint version: no
  • Operating System: windows

What parser are you using (place an "X" next to just one item)?

[x] Default (Espree)
[ ] @typescript-eslint/parser
[ ] @babel/eslint-parser
[ ] vue-eslint-parser
[ ] @angular-eslint/template-parser
[ ] Other

Please show your full configuration:

Configuration
// eslint.config.js
module.exports = [{
    rules: {
        "accessor-pairs": [0, "foo"]
    }
}];

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

/* eslint accessor-pairs: "error" */

What did you expect to happen?

A lint error about invalid configuration for rule accessor-pairs, because the final configuration for this rule is ["error", "foo"] (severity from the inline config comment, options from the config file), which isn't valid.

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

No errors.

This happens because config arrays (both eslintrc and flat) don't validate options when rule is set to "off" in the configuration, which is intended behavior, but when that rule is later enabled by a comment and inherits options from the configurations, those options should be validated.

What changes did you make? (Give an overview)

This scenario wasn't covered in #17945. Now I moved the logic that calculates options for a configuration comment to an earlier point so that the validation gets the final options.

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

@mdjermanovic mdjermanovic added bug ESLint is working incorrectly core Relates to ESLint's core APIs and features accepted There is consensus among the team that this change meets the criteria for inclusion labels Feb 20, 2024
@mdjermanovic mdjermanovic requested a review from a team as a code owner February 20, 2024 13:47
Copy link

netlify bot commented Feb 20, 2024

Deploy Preview for docs-eslint canceled.

Name Link
🔨 Latest commit c014195
🔍 Latest deploy log https://app.netlify.com/sites/docs-eslint/deploys/65d4ad78ccbec100083b0ef5

Comment on lines -1716 to -1718
const ruleOptions = Array.isArray(ruleValue) ? ruleValue : [ruleValue];
let ruleOptions = Array.isArray(ruleValue) ? ruleValue : [ruleValue];

assertIsRuleOptions(ruleId, ruleValue);
Copy link
Member Author

Choose a reason for hiding this comment

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

Note: I removed this because at this point ruleValue is always an array, so this wasn't actually checking anything.

function assertIsRuleOptions(ruleId, value) {
if (typeof value !== "string" && typeof value !== "number" && !Array.isArray(value)) {
throw new InvalidRuleOptionsError(ruleId, value);
}
}

Copy link
Member

@nzakas nzakas left a comment

Choose a reason for hiding this comment

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

LGTM. Leaving open for a couple of days to allow others a chance to review.

Copy link
Member

@aladdin-add aladdin-add left a comment

Choose a reason for hiding this comment

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

LGTM, thanks!

@mdjermanovic mdjermanovic merged commit 0cb4914 into main Feb 23, 2024
18 checks passed
@mdjermanovic mdjermanovic deleted the fix-disable-enable-rule branch February 23, 2024 15:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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
Projects
Status: Complete
Development

Successfully merging this pull request may close these issues.

None yet

3 participants