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

Update: support class fields in the complexity rule (refs #14857) #14957

Merged
merged 3 commits into from Sep 10, 2021

Conversation

@mdjermanovic
Copy link
Member

@mdjermanovic mdjermanovic commented Aug 21, 2021

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 autofixing to a rule
[ ] Add a CLI option
[ ] Add something to the core
[ ] Other, please explain:

refs #14857, fixes #14591 (comment)

What changes did you make? (Give an overview)

Update the complexity rule to supports class fields, in a way that the rule now treats them as functions:

  • Code in class field initializers doesn't contribute to the complexity of the enclosing code (this is a bug fix).
  • Code in class field initializers itself is checked and reported if its complexity is above the threshold (this is an enhancement).

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

  • Does everyone agree that it makes sense to report initializers?
  • Initializers are reported as just "Class field initializer", without referring to the property name. We could add a utility function similar to getFunctionNameWithKind later as an enhancement.
@mdjermanovic
Copy link
Member Author

@mdjermanovic mdjermanovic commented Aug 22, 2021

I believe this can be done better with onCodePathStart()/onCodePathEnd(), so I'll try to refactor the code after we merge #14886.

It's also quite possible that the rule could be entirely refactored to use only the code path analysis info when calculating the complexity as well, but that would require extensive testing.

@mdjermanovic mdjermanovic marked this pull request as draft Aug 22, 2021
@mdjermanovic mdjermanovic force-pushed the complexity-classfields branch from b851e2b to 7f86df8 Aug 27, 2021
@mdjermanovic mdjermanovic marked this pull request as ready for review Aug 27, 2021
@mdjermanovic
Copy link
Member Author

@mdjermanovic mdjermanovic commented Aug 27, 2021

Updated the code to use the new code path analysis, and I think this looks better now.

@nzakas
Copy link
Member

@nzakas nzakas commented Sep 10, 2021

Does everyone agree that it makes sense to report initializers?

Agree

nzakas
nzakas approved these changes Sep 10, 2021
Copy link
Member

@nzakas nzakas left a comment

LGTM. I think adding the class field name would be a great enhancement but not necessary for this first pass.

* to the enclosing function's complexity, but their own complexity should be evaluated.
*/
if (
codePath.origin !== "function" &&
Copy link
Member

@nzakas nzakas Sep 10, 2021

Choose a reason for hiding this comment

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

This came in handy! 🎉

Copy link
Member Author

@mdjermanovic mdjermanovic Sep 10, 2021

Choose a reason for hiding this comment

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

Very! This could have been done in other ways, but it wouldn't look as nice and simple - it would be always difficult to trace what's happening when the initializer is a function.

@mdjermanovic
Copy link
Member Author

@mdjermanovic mdjermanovic commented Sep 10, 2021

closing-reopening to see if that will fix the EasyCLA check.

@mdjermanovic
Copy link
Member Author

@mdjermanovic mdjermanovic commented Sep 10, 2021

All checks are green now, so merging.

@mdjermanovic mdjermanovic merged commit 88a3952 into master Sep 10, 2021
23 checks passed
@mdjermanovic mdjermanovic deleted the complexity-classfields branch Sep 10, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

2 participants