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: update complexity rule for optional chaining & default values #18152

Merged
merged 1 commit into from Mar 1, 2024

Conversation

lo1tuma
Copy link
Member

@lo1tuma lo1tuma commented Feb 27, 2024

Fixes: #18060

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)
[x] 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)

In addition to the existing patterns that increase complexity I’ve added the following:

  • ChainExpression to increase the complexity by one for every optional chain expression that is encountered
  • AssignmentPattern to increase the complexity by one for every default value assignment in function parameters or desturcturing expressions

@lo1tuma lo1tuma requested a review from a team as a code owner February 27, 2024 09:12
Copy link

linux-foundation-easycla bot commented Feb 27, 2024

CLA Signed

The committers listed above are authorized under a signed CLA.

  • ✅ login: lo1tuma / name: Mathias Schreck (32a3d9e)

@eslint-github-bot
Copy link

Hi @lo1tuma!, 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 length of the commit message must be less than or equal to 72

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 Feb 27, 2024
Copy link

netlify bot commented Feb 27, 2024

Deploy Preview for docs-eslint ready!

Name Link
🔨 Latest commit 32a3d9e
🔍 Latest deploy log https://app.netlify.com/sites/docs-eslint/deploys/65df3086e884290007d9a931
😎 Deploy Preview https://deploy-preview-18152--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.

@amareshsm amareshsm added the accepted There is consensus among the team that this change meets the criteria for inclusion label Feb 27, 2024
@amareshsm
Copy link
Member

In the documentation, can we add examples demonstrating the usage of ChainExpression and AssignmentPattern?
https://eslint.org/docs/latest/rules/complexity

@mdjermanovic mdjermanovic added the feature This change adds a new feature to ESLint label Feb 27, 2024
@@ -109,6 +109,8 @@ module.exports = {
IfStatement: increaseComplexity,
WhileStatement: increaseComplexity,
DoWhileStatement: increaseComplexity,
ChainExpression: increaseComplexity,
Copy link
Member

Choose a reason for hiding this comment

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

This doesn't report the first example from #18060:

/*eslint complexity: ["error", 2]*/

// still valid (calculated complexity = 2)
function a(x) {
    return x?.a?.b?.c?.d?.e?.f;
}

Because x?.a?.b?.c?.d?.e?.f is one ChainExpression. Perhaps we should count each ?.?

Copy link
Member Author

Choose a reason for hiding this comment

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

You are right. I’ve misunderstood how ChainExpression works. I’ve added a test-case and updated the implementation. Thank you!

@eslint-github-bot
Copy link

Hi @lo1tuma!, 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 length of the commit message must be less than or equal to 72

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

@lo1tuma
Copy link
Member Author

lo1tuma commented Feb 27, 2024

In the documentation, can we add examples demonstrating the usage of ChainExpression and AssignmentPattern? https://eslint.org/docs/latest/rules/complexity

I’ve thought about this as well and figured out that the current documentation doesn’t cover several cases that the rule already handles as for example nullish coalescing. I’ve have added two examples now, but I’m not sure if we should overhaul the whole documentation about the rule to explain exactly what increases branching.

@eslint-github-bot
Copy link

Hi @lo1tuma!, 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 length of the commit message must be less than or equal to 72

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

@lo1tuma lo1tuma changed the title feat: update complexity rule to count optional chaining and default values feat: update complexity rule for optional chaining & default values Feb 27, 2024
@Tanujkanti4441
Copy link
Contributor

Can you also decrease the length of PR's title to pass all the CI checks

Comment on lines +129 to +132
// optional chaining
{
code: "function a(b) { b?.c; }", options: [{ max: 2 }]
},
Copy link
Member

Choose a reason for hiding this comment

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

What about optional call expressions like function a(b) { b?.(); }? Those should also increase the complexity I think.

Copy link
Member Author

Choose a reason for hiding this comment

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

Good catch! I’ve added test cases and implemented handling of optional call expression.

Both, optional chaining expressions and default values (in function
parameters or descructuring expressions) increase the branching of the
code and thus the complexity is increased.

Fixes: eslint#18060
Copy link
Member

@fasttime fasttime 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!

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. Looks like @mdjermanovic was specifically requested to review this, so waiting for that 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!

@mdjermanovic mdjermanovic merged commit c49ed63 into eslint:main Mar 1, 2024
19 checks passed
@lo1tuma lo1tuma deleted the complexity-chaining-defaults branch March 1, 2024 11:59
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 contributor pool feature This change adds a new feature to ESLint rule Relates to ESLint's core rules
Projects
Status: Complete
Development

Successfully merging this pull request may close these issues.

Rule Change: extend complexity rule to count optional chaining and default parameters/values
6 participants