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? |
incendial
left a comment
There was a problem hiding this comment.
Overall looks amazing! 🔥 Left a few small comments
| ], | ||
| }).check(unit); | ||
|
|
||
| RuleTestHelper.verifyIssues( |
There was a problem hiding this comment.
Please use verifyNoIssues instead.
| ], | ||
| }).check(unit); | ||
|
|
||
| RuleTestHelper.verifyIssues( |
| 'ignore-private': true, | ||
| }).check(unit); | ||
|
|
||
| RuleTestHelper.verifyIssues( |
| final unit = await RuleTestHelper.resolveFromFile(_correctExamplePath); | ||
| final issues = PreferStaticClassRule().check(unit); | ||
|
|
||
| RuleTestHelper.verifyIssues( |
| if (node.parent is CompilationUnit && | ||
| !_hasIgnoredAnnotation(node) && | ||
| !_shouldIgnoreName(nodeName) && | ||
| nodeName != 'main') { |
There was a problem hiding this comment.
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.
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.getAreaandgetAreaorgetCircleArea.Cons:
Use
ignore-privateconfiguration to ignore private global declarations.Use
ignore-namesto ignore names matching regular expressions (for example, Riverpod providers, flutter hooks, etc).Use
ignore-annotationsconfiguration to override default ignored annotation list.⚙️ Config example {#config-example}
Example {#example}
❌ Bad:
✅ Good: