Comments (7)
Makes sense to count these. I'm used to test coverage tools counting them as separate branches.
from eslint.
The null coalescing operator ??
should also be counted in my opinion because it also creates conditional branches which are increasing complexity:
- value
is null
or undefined
- value
is not null
or undefined
Forget about it. It seems that this is already the case š
from eslint.
I don't think optional chaining and parameters will increase the cyclomatic complexity of the code
function a(x) {
return x?.a?.b?.c?.d?.e?.f;
}
This code doesn't have control flow statements such as loops or conditionals. It's a simple expression that uses the optional chaining operator (?.) to access nested properties in the object x. Therefore, the control flow graph is straightforward (ie 1) and doesn't introduce additional complexity.
/*eslint complexity: ["error", 2]*/
function a(x) {
return x?.a?.b?.c?.d?.e?.f;
}
function b(x) {
const { a = '', b = 0 } = x;
}
function c(a = '', b = 0) {
}
According to JSHint, the complexity of the above functions is 1.
from eslint.
This code doesn't have control flow statements such as loops or conditionals. It's a simple expression that uses the optional chaining operator (?.)
How would you implement the code without optional chaining? Iām pretty sure that you would need a conditional. IMHO optional chaining is only syntax sugar for an if statement or an ternary. Every access to an optional property via optional chaining creates two possible code branches. Which is AFAIK the definition of cyclomatic complexity. In the example x?.b
there are two possible branches in which the code can execute.
Also coverage tools like c8
report untested optional chaining expressions as an uncovered branch.
According to JSHint, the complexity of the above functions is 1.
Iām not sure if this is relevant. Iām not following the development of JSHint anymore, but I would be surprised if they implement rule-specific logic for modern JS language features.
from eslint.
I agree with @lo1tuma -- both optional chaining and default parameters represent branches along a code path, which is the definition of complexity.
I'm š for this change but would like some other opinions from @eslint/eslint-team.
from eslint.
Agreement from me on account of what nzakas and bmish have already said.
from eslint.
Looks like there is consensus, so marking as accepted. @lo1tuma if you want to submit a PR, we can review it.
from eslint.
Related Issues (20)
- Docs: Some dark mode text styles missing when JavaScript is disabled HOT 1
- Bug: false positive in `lines-around-comment` after invalid tagged template HOT 2
- Change Request: Flat Config does not support FileEnumerator HOT 31
- Clarify the config location responsible for the error "You have used a rule which requires parserServices to be generated. You must therefore provide a value for the "parserOptions.project" property for @typescript-eslint/parser." HOT 1
- š Tracking: Flat Config rollout HOT 8
- Bug: Cannot read properties of undefined (reading 'ignores') HOT 6
- Change Request: Standardized export of flat configs to enable programmatic detection HOT 10
- Bug: The rule 'no-unused-vars' works incorrectly with PUG template in Vue 3 (script+setup) HOT 4
- Bug: Types used in JSDoc blocks of @eslint/js are missing HOT 12
- Change Request: `Linter.getRules()` equivalent support in Flat ESLint HOT 9
- Rule Change: no-restricted-imports should honor a package's declared exports HOT 6
- Bug: object-property-newline does not work with imports and exports HOT 3
- Scheduled release for February 23rd, 2024 HOT 4
- Docs: flat config `files` / `ignores` function HOT 14
- Repo: Remove unused tools/update-rule-types.js HOT 2
- Rule Change: [multiline-comment-style] Allow //- slash hyphen blocks HOT 7
- Bug: `no-restricted-imports` doesn't distinguish between package names that have a substring as a name HOT 3
- Bug: Resolve local module in eslint.config.js for flat config will not work HOT 7
- Change Request: Disallow multiple configuration comments for the same rule HOT 1
- Rule Change: id-denylist HOT 10
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
š Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. ššš
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ā¤ļø Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from eslint.