UndocumentedPublicProperty and UndocumentedPublicFunction should include objects#3940
UndocumentedPublicProperty and UndocumentedPublicFunction should include objects#3940schalkms merged 6 commits intodetekt:mainfrom anthonycr:undocumented-public-functions-in-objects
Conversation
The class returned was null for functions inside an object, resulting in a false negative, so we instead use containingClassOrObject to determine if the containing object is public or not.
…etermining eligibility for UndocumentedPublicProperty check containingClass() returns null when checking an object not nested in another class, making it ineligible to check. Switching to containingClassOrObject allows properties in an obect to be checked.
|
Thanks for the fix 🙏 That's really appreciated. |
Codecov Report
@@ Coverage Diff @@
## main #3940 +/- ##
=========================================
Coverage 83.44% 83.44%
Complexity 3149 3149
=========================================
Files 456 456
Lines 9014 9014
Branches 1754 1754
=========================================
Hits 7522 7522
Misses 565 565
Partials 927 927
Continue to review full report at Codecov.
|
schalkms
left a comment
There was a problem hiding this comment.
Does the rule also flag interfaces with this implementation?
|
@schalkms yes, interfaces were being flagged before with |
Summary
Given an
objectwith an undocumented public function or property, I would expect theUndocumentedPublicFunctionrule orUndocumentedPublicPropertyrule to fail the build. However, the behavior of the rules is inconsistent in regards toobjects.The following code is failed when the rules are enabled and an object is nested in a class, as expected:
However, if the object is promoted to a top level object, then the rules will not fail the function or property.
Changes
I discovered that the
containingClass()function used in both rules to determine if the function or property is part of a public class returns null if the function or property is in anobject. We can change this behavior to check if theobjectis public or not by replacing the usage ofcontainingClass()withcontainingClassOrObject. In this way, we can treat an undocumented function or property in anobjectthe same as one in a regularclass.I changed both
UndocumentedPublicFunctionandUndocumentedPublicPropertyto utilizecontainingClassOrObjectinstead ofcontainingClass(). I also added test cases for public and privateobjectcases as well as a nestedobjecttest case. For the test cases I added to theUndocumentedPublicPropertySpec, I renamed two of the existing test cases with ambiguous names to avoid confusion with the test cases I added.