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

feat: add meta.defaultOptions #17656

Draft
wants to merge 31 commits into
base: main
Choose a base branch
from

Conversation

JoshuaKGoldberg
Copy link
Contributor

@JoshuaKGoldberg JoshuaKGoldberg commented Oct 16, 2023

Prerequisites checklist

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

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

What changes did you make? (Give an overview)

Augments linting to factor in a new optional meta.defaultOptions. If a rule describes default options this way, any user-provided options are merged on top of the default options.

These defaults are applied by a new deepMergeArrays utility in:

  • ConfigValidator
  • Linter in two places:
    • For configs: by augmenting the existing getRuleOptions helper
    • For inline comments in flat config: with a new getRuleOptionsInline helper
      • Inline comments in legacy configs go through RuleValidator instead
  • RuleValidator

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

@eslint-github-bot
Copy link

Hi @JoshuaKGoldberg!, thanks for the Pull Request

The pull request title isn't properly formatted. We ask that you update the pull request title to match this format, as we use it to generate changelogs and automate releases.

  • The commit message tag wasn't recognized. Did you mean "docs", "fix", or "feat"?
  • There should be a space following the initial tag and colon, for example 'feat: Message'.
  • The first letter of the tag should be in lowercase

To Fix: You can fix this problem by clicking 'Edit' next to the pull request title at the top of this page.

Read more about contributing to ESLint here

@netlify
Copy link

netlify bot commented Oct 16, 2023

Deploy Preview for docs-eslint ready!

Name Link
🔨 Latest commit 6657263
🔍 Latest deploy log https://app.netlify.com/sites/docs-eslint/deploys/65ff19183446850008e3746f
😎 Deploy Preview https://deploy-preview-17656--docs-eslint.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@eslint-github-bot
Copy link

Hi @JoshuaKGoldberg!, thanks for the Pull Request

The pull request title isn't properly formatted. We ask that you update the pull request title to match this format, as we use it to generate changelogs and automate releases.

  • The commit message tag wasn't recognized. Did you mean "docs", "fix", or "feat"?
  • There should be a space following the initial tag and colon, for example 'feat: Message'.
  • The first letter of the tag should be in lowercase

To Fix: You can fix this problem by clicking 'Edit' next to the pull request title at the top of this page.

Read more about contributing to ESLint here

@eslint-github-bot
Copy link

Hi @JoshuaKGoldberg!, thanks for the Pull Request

The pull request title isn't properly formatted. We ask that you update the pull request title to match this format, as we use it to generate changelogs and automate releases.

  • The commit message tag wasn't recognized. Did you mean "docs", "fix", or "feat"?
  • There should be a space following the initial tag and colon, for example 'feat: Message'.
  • The first letter of the tag should be in lowercase

To Fix: You can fix this problem by clicking 'Edit' next to the pull request title at the top of this page.

Read more about contributing to ESLint here

Copy link

github-actions bot commented Nov 5, 2023

Hi everyone, it looks like we lost track of this pull request. Please review and see what the next steps are. This pull request will auto-close in 7 days without an update.

@github-actions github-actions bot added the Stale label Nov 5, 2023
@github-actions github-actions bot removed the Stale label Nov 6, 2023
Copy link

Hi everyone, it looks like we lost track of this pull request. Please review and see what the next steps are. This pull request will auto-close in 7 days without an update.

@github-actions github-actions bot added the Stale label Nov 16, 2023
@Rec0iL99 Rec0iL99 removed the Stale label Nov 17, 2023
@Rec0iL99
Copy link
Member

Not stale. @JoshuaKGoldberg there's a review comment for you to look into. Thanks.

@eslint-github-bot
Copy link

Hi @JoshuaKGoldberg!, thanks for the Pull Request

The pull request title isn't properly formatted. We ask that you update the pull request title to match this format, as we use it to generate changelogs and automate releases.

  • The commit message tag wasn't recognized. Did you mean "docs", "fix", or "feat"?
  • There should be a space following the initial tag and colon, for example 'feat: Message'.
  • The first letter of the tag should be in lowercase

To Fix: You can fix this problem by clicking 'Edit' next to the pull request title at the top of this page.

Read more about contributing to ESLint here

@eslint-github-bot
Copy link

Hi @JoshuaKGoldberg!, thanks for the Pull Request

The pull request title isn't properly formatted. We ask that you update the pull request title to match this format, as we use it to generate changelogs and automate releases.

  • The commit message tag wasn't recognized. Did you mean "docs", "fix", or "feat"?
  • There should be a space following the initial tag and colon, for example 'feat: Message'.
  • The first letter of the tag should be in lowercase

To Fix: You can fix this problem by clicking 'Edit' next to the pull request title at the top of this page.

Read more about contributing to ESLint here

@eslint-github-bot
Copy link

Hi @JoshuaKGoldberg!, thanks for the Pull Request

The pull request title isn't properly formatted. We ask that you update the pull request title to match this format, as we use it to generate changelogs and automate releases.

  • The commit message tag wasn't recognized. Did you mean "docs", "fix", or "feat"?
  • There should be a space following the initial tag and colon, for example 'feat: Message'.
  • The first letter of the tag should be in lowercase

To Fix: You can fix this problem by clicking 'Edit' next to the pull request title at the top of this page.

Read more about contributing to ESLint here

@eslint-github-bot
Copy link

Hi @JoshuaKGoldberg!, thanks for the Pull Request

The pull request title isn't properly formatted. We ask that you update the pull request title to match this format, as we use it to generate changelogs and automate releases.

  • The commit message tag wasn't recognized. Did you mean "docs", "fix", or "feat"?
  • There should be a space following the initial tag and colon, for example 'feat: Message'.
  • The first letter of the tag should be in lowercase

To Fix: You can fix this problem by clicking 'Edit' next to the pull request title at the top of this page.

Read more about contributing to ESLint here

@eslint-github-bot
Copy link

Hi @JoshuaKGoldberg!, thanks for the Pull Request

The pull request title isn't properly formatted. We ask that you update the pull request title to match this format, as we use it to generate changelogs and automate releases.

  • The commit message tag wasn't recognized. Did you mean "docs", "fix", or "feat"?
  • There should be a space following the initial tag and colon, for example 'feat: Message'.
  • The first letter of the tag should be in lowercase

To Fix: You can fix this problem by clicking 'Edit' next to the pull request title at the top of this page.

Read more about contributing to ESLint here

@eslint-github-bot
Copy link

Hi @JoshuaKGoldberg!, thanks for the Pull Request

The pull request title isn't properly formatted. We ask that you update the pull request title to match this format, as we use it to generate changelogs and automate releases.

  • The commit message tag wasn't recognized. Did you mean "docs", "fix", or "feat"?
  • There should be a space following the initial tag and colon, for example 'feat: Message'.
  • The first letter of the tag should be in lowercase

To Fix: You can fix this problem by clicking 'Edit' next to the pull request title at the top of this page.

Read more about contributing to ESLint here

Copy link

github-actions bot commented Dec 1, 2023

Hi everyone, it looks like we lost track of this pull request. Please review and see what the next steps are. This pull request will auto-close in 7 days without an update.

@github-actions github-actions bot added the Stale label Dec 1, 2023
@@ -6739,6 +6762,52 @@ var a = "test2";
});
});

describe("options", () => {
it("rules should apply meta.defaultOptions on top of schema defaults", () => {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Note that this is different from main. I asked about the code intent: #17656 (comment)

Copy link

Hi everyone, it looks like we lost track of this pull request. Please review and see what the next steps are. This pull request will auto-close in 7 days without an update.

@github-actions github-actions bot added the Stale label Feb 29, 2024
@Rec0iL99
Copy link
Member

Rec0iL99 commented Mar 1, 2024

Waiting for @mdjermanovic to review here.

@JoshuaKGoldberg there's a conflict to resolve.

@Rec0iL99 Rec0iL99 removed the Stale label Mar 1, 2024
@github-actions github-actions bot added rule Relates to ESLint's core rules core Relates to ESLint's core APIs and features labels Mar 5, 2024
@JoshuaKGoldberg JoshuaKGoldberg dismissed stale reviews from bmish and nzakas via 2b8659b March 12, 2024 12:05
Copy link

Hi everyone, it looks like we lost track of this pull request. Please review and see what the next steps are. This pull request will auto-close in 7 days without an update.

@github-actions github-actions bot added the Stale label Mar 22, 2024
@JoshuaKGoldberg
Copy link
Contributor Author

I believe it's waiting on re-review from @mdjermanovic?

There were a few merge conflicts introduced since the last merge from main. I resolved them just now.

@github-actions github-actions bot removed the Stale label Mar 23, 2024
@aladdin-add aladdin-add self-requested a review March 28, 2024 13:35
@mdjermanovic
Copy link
Member

Sorry for the delay. A problem is that eslintrc will still be supported in ESLint v9, and rules should work the same regardless of which config system is used.

In flat config, default options are merged before validation. I think this generally makes more sense than validating provided options first and then merging defaults. Ajv also applies schema defaults before validating options. In eslintrc mode, however, as currently implemented, default options are merged after validation (right before running rules).

This means that the same configuration for a rule could be valid in one config system but invalid in the other.

For example, if this is a rule:

{
    meta: {
        defaultOptions: [{
            foo: 42
        }],
        schema: [{
            type: "object",
            maxProperties: 2 // allows one or two in addition to "foo"?
        }]
    },
    create() {
        return {};
    }
}

configuration ["error", { bar: 6, baz: 7 }] would be valid in .eslintrc.js file, but invalid in eslint.config.js file (Value {"foo":42,"bar":6,"baz":7} should NOT have more than 2 properties).

I think we should address this, but there are two possible ways:

  1. Change eslintrc to apply defaults before validating options.
  2. Change flat config to apply defaults after validating options.

I'd be in favor of 1), although it probably requires changes in @eslint/eslintrc package. 2) seems easier to implement at the moment, but it's unlikely that we'd ever change this behavior later, so if we think that merging defaults should be before the validation, it's probably better to do 1). Thoughts?

Comment on lines 166 to +169
if (validateRule) {

validateRule(ruleOptions.slice(1));
const slicedOptions = ruleOptions.slice(1);
const mergedOptions = deepMergeArrays(rule.meta?.defaultOptions, slicedOptions);
Copy link
Member

Choose a reason for hiding this comment

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

Defaults options wouldn't be applied if the rule has schema: false?

Comment on lines -1726 to +1759
let ruleOptions = Array.isArray(ruleValue) ? ruleValue : [ruleValue];

assertIsRuleSeverity(ruleId, ruleOptions[0]);
let ruleOptions = getRuleOptionsInline(ruleId, ruleValue, rule.meta?.defaultOptions);
Copy link
Member

Choose a reason for hiding this comment

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

Merging default options at this point would cause a bug when the configuration comment just overrides severity (https://eslint.org/docs/next/use/migrate-to-9.0.0#eslint-comment-options) because for the rest of the code below it would look like the comment had options specified.

We could let ruleValidator.validate() below merge default options, and then get the final rule config from the config that was passed to it?

Copy link

github-actions bot commented Apr 8, 2024

Hi everyone, it looks like we lost track of this pull request. Please review and see what the next steps are. This pull request will auto-close in 7 days without an update.

@github-actions github-actions bot added the Stale label Apr 8, 2024
@Tanujkanti4441
Copy link
Contributor

Hi @JoshuaKGoldberg, some reviews are there you can checkout.

@Tanujkanti4441 Tanujkanti4441 added core Relates to ESLint's core APIs and features and removed core Relates to ESLint's core APIs and features Stale labels Apr 9, 2024
@JoshuaKGoldberg
Copy link
Contributor Author

I'd be in favor of 1), although it probably requires changes in @eslint/eslintrc package.

I'm in favor of that as well. Is there some process I should follow?

@nzakas
Copy link
Member

nzakas commented Apr 11, 2024

@JoshuaKGoldberg nothing special, just go ahead and open a PR on eslint/eslintrc and mention this PR in it.

@JoshuaKGoldberg JoshuaKGoldberg marked this pull request as draft April 26, 2024 14:17
@JoshuaKGoldberg
Copy link
Contributor Author

I still plan on continuing this work, but likely won't be able to for at least the month of May. ESLint v9 & typescript-eslint v8 are much higher priority for me at the moment.

If anybody else wants to tackle the eslint/eslintrc changes in the meantime, please do go ahead! That would be lovely 🙂. But if not, I should be able to later this summer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core Relates to ESLint's core APIs and features feature This change adds a new feature to ESLint rule Relates to ESLint's core rules
Projects
Status: Implementing
Development

Successfully merging this pull request may close these issues.

None yet

7 participants