-
Notifications
You must be signed in to change notification settings - Fork 271
feat: add static code diagnostic prefer-static-class #1086
feat: add static code diagnostic prefer-static-class #1086
Conversation
Codecov Report
@@ Coverage Diff @@
## master #1086 +/- ##
==========================================
+ Coverage 84.85% 84.89% +0.04%
==========================================
Files 330 333 +3
Lines 7275 7331 +56
==========================================
+ Hits 6173 6224 +51
- Misses 1102 1107 +5
📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more |
I just have finished rule implementation. I also tested this rule on our code base and not found any false positives. I got ~90 true positives which we really should fix. @dkrutskikh , @incendial - can you please take a look? |
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.
Overall looks amazing! 🔥 Left a few small comments
], | ||
}).check(unit); | ||
|
||
RuleTestHelper.verifyIssues( |
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.
Please use verifyNoIssues
instead.
], | ||
}).check(unit); | ||
|
||
RuleTestHelper.verifyIssues( |
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.
Same for NoIssues
'ignore-private': true, | ||
}).check(unit); | ||
|
||
RuleTestHelper.verifyIssues( |
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.
And here
final unit = await RuleTestHelper.resolveFromFile(_correctExamplePath); | ||
final issues = PreferStaticClassRule().check(unit); | ||
|
||
RuleTestHelper.verifyIssues( |
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.
And here 🙂
if (node.parent is CompilationUnit && | ||
!_hasIgnoredAnnotation(node) && | ||
!_shouldIgnoreName(nodeName) && | ||
nodeName != 'main') { |
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.
I believe isEnitrypoint
helper function should be used here. It covers not only main
, but also other possible entry points.
} | ||
|
||
bool _shouldIgnoreName(String name) => | ||
(_ignorePrivate && name.startsWith('_')) || |
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.
Please use Identifier.isPrivateName
instead of name.startsWith('_')
Kudos, SonarCloud Quality Gate passed!
|
Suggests to use static class member instead of global constants, variables and functions.
Pros:
import 'file.dart' as 'file'
to resolve name conflicts.Circle.getArea
andgetArea
orgetCircleArea
.Cons:
Use
ignore-private
configuration to ignore private global declarations.Use
ignore-names
to ignore names matching regular expressions (for example, Riverpod providers, flutter hooks, etc).Use
ignore-annotations
configuration to override default ignored annotation list.⚙️ Config example {#config-example}
Example {#example}
❌ Bad:
✅ Good: