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!: correct camelcase rule schema for allow option #18232

Merged
merged 1 commit into from Apr 1, 2024

Conversation

eMerzh
Copy link
Contributor

@eMerzh eMerzh commented Mar 26, 2024

Prerequisites checklist

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

The Json schema of the camelCase rule validate that the first element is a string instead of validating that it's an array of string

[ ] 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:

What changes did you make? (Give an overview)

Correct the Json Schema

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

Nope

@eMerzh eMerzh requested a review from a team as a code owner March 26, 2024 14:24
@eslint-github-bot
Copy link

Hi @eMerzh!, 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'.

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

@github-actions github-actions bot added the rule Relates to ESLint's core rules label Mar 26, 2024
Copy link

netlify bot commented Mar 26, 2024

Deploy Preview for docs-eslint ready!

Name Link
🔨 Latest commit 3b65d7c
🔍 Latest deploy log https://app.netlify.com/sites/docs-eslint/deploys/660a7745ae2f020008dac466
😎 Deploy Preview https://deploy-preview-18232--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.

@eMerzh eMerzh changed the title fix(camelcase): correct json schema to allow array of string fix: correct json schema to allow array of string Mar 26, 2024
@eslint-github-bot eslint-github-bot bot added the bug ESLint is working incorrectly label Mar 26, 2024
@mdjermanovic
Copy link
Member

Good catch!

Current schema actually allows arrays with multiple elements, as in this test case:

{
code: "ignored_foo = 0; ignored_bar = 1;",
options: [{ allow: ["ignored_foo", "ignored_bar"] }]
},

But it only validates that the first element is a string, while the others can be anything, as in this example:

/* eslint camelcase: ["error", { allow: ["a_b", 5, "c_d"] }] */ // ok configuration

let a_b; // ok
let c_d; // ok
let e_f; // error here

Playground demo

@mdjermanovic mdjermanovic changed the title fix: correct json schema to allow array of string fix!: correct camelcase rule schema for allow option Mar 26, 2024
@eslint-github-bot eslint-github-bot bot added the breaking This change is backwards-incompatible label Mar 26, 2024
@mdjermanovic
Copy link
Member

Per our semver policies, this is a breaking change (Part of the public API is removed or changed in an incompatible way > Rule schemas).

@eslint/eslint-tsc Since we've already released v9 rc, looks like we'll have to leave this for ESLint v10?

@eMerzh
Copy link
Contributor Author

eMerzh commented Mar 26, 2024

@mdjermanovic i understand your position but if i understand, this shouldn't break your CI as normally you could "only" have "one string" in the array... thus this technically allows you do to more

Also it's only in the validation side, not on the lint itself

@mdjermanovic
Copy link
Member

@mdjermanovic i understand your position but if i understand, this shouldn't break your CI as normally you could "only" have "one string" in the array... thus this technically allows you do to more

The current schema already allows multiple strings in the array (demo). items: [{ type: "string" }] means that the first element must be a string, but it doesn't disallow additional elements.

After this change, a configuration like ["error", { allow: ["a_b", "c_d", 5] }] would break CI because 5 is not a string. Currently, this configuration works (demo). That 5 is currently a no-op, but it doesn't cause validation errors.

Copy link
Contributor

@Shinigami92 Shinigami92 left a comment

Choose a reason for hiding this comment

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

This will fix eslint-types/eslint-define-config#278 semi-automatically (after the generate rule-types script is executed)

@fasttime
Copy link
Member

This looks like a bug that should be fixed. I'm noting that the current logic allows arbitrary elements in the allow array after the first string without any kind of validation. If one of the elements it's a Symbol, or an object that can't be formatted, or even an invalid regex pattern like "foo)", the rule will crash.

/*eslint camelcase: ["error", {allow: ["foo", {toString: null}]}]*/

function bar_baz() {
}

Playground link

We could look into how to improve option validation for this rule in a non-breaking way, but that wouldn't be the end of the job, so probably it's best to schedule the proper fix for v10 if it can't be done earlier.

@Tanujkanti4441
Copy link
Contributor

What i also noticed that if the allow: [""] option has an empty string then rule doesn't report the camelcase variables name - playground and also rule doesn't check whether the string has an underscore or not, should we validate the option this way?

@nzakas
Copy link
Member

nzakas commented Mar 27, 2024

@mdjermanovic If we are categorizing this as a bug fix, then I'm okay with including in the next RC. RCs (and betas) really are designed to catch bugs. As long as we're not adding anything new, I think it's appropriate to include in v9.

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.

👍 to include it in v9, but we need:

  1. add a few tests.
  2. add it in the migration guide: https://github.com/eslint/eslint/blob/main/docs/src/use/migrate-to-9.0.0.md

@mdjermanovic
Copy link
Member

@mdjermanovic If we are categorizing this as a bug fix, then I'm okay with including in the next RC. RCs (and betas) really are designed to catch bugs. As long as we're not adding anything new, I think it's appropriate to include in v9.

Sounds good to me, marking this as accepted.

@mdjermanovic mdjermanovic added the accepted There is consensus among the team that this change meets the criteria for inclusion label Mar 28, 2024
@eMerzh
Copy link
Contributor Author

eMerzh commented Mar 28, 2024

i was thinking about adding those in tests/lib/rules/camelcase.js but it is really the place? 🤔

also i was thinking of a test with 2-3 allows and 1 or 2 with numbers or objects in options, did you think about something else?

@mdjermanovic
Copy link
Member

RuleTester currently doesn't support testing invalid options. (it will when we implement https://github.com/eslint/rfcs/tree/main/designs/2023-test-rule-errors).

I think it would be fine to just add a couple more tests with valid options (allow with multiple string elements).

docs/src/use/migrate-to-9.0.0.md Outdated Show resolved Hide resolved
docs/src/use/migrate-to-9.0.0.md Outdated Show resolved Hide resolved
docs/src/use/migrate-to-9.0.0.md Outdated Show resolved Hide resolved
Comment on lines 344 to 347
{
code: "var$key = 0;",
options: [{ allow: ["__option_foo__", "\\$key"] }]
},
Copy link
Member

Choose a reason for hiding this comment

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

This code would be valid without options, maybe you intended to use some other variable name?

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! Would like another review before merging.

Copy link
Member

@mdjermanovic mdjermanovic 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! Since this is a breaking change, it requires another approval from @eslint/eslint-tsc before merging.

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. Thanks!

@nzakas nzakas merged commit b7cf3bd into eslint:main Apr 1, 2024
19 checks passed
@eMerzh eMerzh deleted the fix_schema branch April 1, 2024 18:10
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 breaking This change is backwards-incompatible bug ESLint is working incorrectly contributor pool rule Relates to ESLint's core rules
Projects
Status: Complete
Status: Done
Development

Successfully merging this pull request may close these issues.

None yet

7 participants