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
Conversation
|
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.
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 |
✅ Deploy Preview for docs-eslint ready!
To edit notification comments on pull requests, go to your Netlify site configuration. |
In the documentation, can we add examples demonstrating the usage of ChainExpression and AssignmentPattern? |
lib/rules/complexity.js
Outdated
@@ -109,6 +109,8 @@ module.exports = { | |||
IfStatement: increaseComplexity, | |||
WhileStatement: increaseComplexity, | |||
DoWhileStatement: increaseComplexity, | |||
ChainExpression: increaseComplexity, |
There was a problem hiding this comment.
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 ?.
?
There was a problem hiding this comment.
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!
8fe86d6
to
adb346e
Compare
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.
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 |
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. |
adb346e
to
fb56f18
Compare
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.
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 |
Can you also decrease the length of PR's title to pass all the CI checks |
fb56f18
to
f4784ee
Compare
// optional chaining | ||
{ | ||
code: "function a(b) { b?.c; }", options: [{ max: 2 }] | ||
}, |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
f4784ee
to
32a3d9e
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thanks!
There was a problem hiding this 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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thanks!
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 encounteredAssignmentPattern
to increase the complexity by one for every default value assignment in function parameters or desturcturing expressions