-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Ban public top-level members in a test #57824
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
Comments
Like #57724, right? |
I think it would be mostly covered by #57724 but it's still worth tracking separately.
|
We should also be careful, though. Make sure the lib in question is importing Most folks use |
The folks who don't shouldn't enable this lint... |
As a data point: Recently did some clean-up in the framework with the help of this tool: https://pub.dev/packages/dart_code_metrics. We had accumulated quite a bit of dead code in tests: flutter/flutter#104550. This proposal (or #57580 or #58292) probably could have prevented that build-up of dead code. |
Thanks for the concrete example @goderbauer! |
In dart-archive/linter#3453 I implemented such a lint but not specifically for tests. The lint checks every executable libraries (ie. a library containing a On Flutter codebase there are only few cases (among tens of thousands) that are triggered out of tests. Most of them can easily be refactored to prevent the lint (and make the code structured in a better way) |
Any opinion about limiting the application of the lint to tests or to extend it to executable libraries (ie. a library containing a main top level function)? The current PR (that is ready to land) applies on executable libraries but I can change it if we think it will be a too broadened application. |
Nobody? |
That limitation sounds good to me
…On Wed, Jun 29, 2022, 02:23 Alexandre Ardhuin ***@***.***> wrote:
Any opinion about limiting the application of the lint to tests or to
extend it to *executable libraries* (ie. a library containing a main top
level function)?
Nobody?
—
Reply to this email directly, view it on GitHub
<#57824>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAAEFCRMTFN43R4LBH5K3PLVRQIZFANCNFSM4GCL74VQ>
.
You are receiving this because you commented.Message ID:
***@***.***>
|
Extending the lint to executable libraries could be a way to cover/workaround Report unused public elements in an entrypoint library. User can always limits the application of the lint to My point is that this lint could have a lot of value also outside of the test directory. Should we have 2 lints (one for every executable library and one limited to test)? |
Re-reading the issue description the lint request doesn't seem limited to tests:
I'm concern about provinding 2 lints |
I think we want to get some data about common usage patterns before we make a decision. How many top-level members (other than I'm not familiar with test code outside the analyzer packages, but I do know of several libraries / packages that make fairly significant use of top-level members, so it's possible that this is a not-uncommon coding style. |
I think it's fairly common to have top-level helper functions in test files, and these would be the one's we'd want to ensure are used. I hypothesize that top-level constants like Strings and ints are cheap and not horrible if left unused, but top-level helpers can reference imported elements, so that it is more likely that an unused top-level function can represent actual bloat. All of that being said, I'm much more in favor, these days, of just writing public top-level members, and improving our tooling to find unused elements like these. I used to write private top-level consts, helper classes, etc. in tests, but I think it does get pretty hard to read anything like that. |
I would be in favor of that. We don't have to close until one is implemented 🤣 because I think there is very good discussion. |
The same question about the pertinence to have 2 lints doing the same thing (only on tests vs. on executable libs) remains. Any opinion on that? |
Again, I think the data would be useful. If it's common to define public top-level functions outside |
I don't feel strongly about this issue any more. I'm happy with dart-archive/linter#3513 and think it is fairly non-idiomatic to declare private helpers and consts in test libraries. Anyone who feels strongly can re-open. |
Proposal
Given a library with URI with pattern
/test/.*_test.dart
, report any public top-level members exceptmain
. "Top-level members in a test should be private (or relocated to withinmain
), to prevent unused members."Secondary proposal
Given a library which does not live in it's declaring package's public API (i.e. files in
bin/
,test/
, andtool/
), report any public top-level members exceptmain
.Justification
This lint helps prevent code bloat. Without full-program analysis, we cannot tell if a public member is used anywhere, so it is not reported by the analyzer as unused_variable or something similar. For top-level members, only private ones can be rigorously checked for usage. But in tests (and
bin/
andtool/
scripts), which are typically their own entry points and never imported by another library, we want to be able to report unused top-level members.This includes top-level variables, constants, functions, typedefs, enums, mixins, and classes.
This is a counter-request to #57580. I think this is better choice for a few reasons. #57580 proposes either a change to the analyzer's unused analysis to take into account the library URI ("does it end with _test.dart?"), leading to possible false positives. I don't think that's going to go well. The alternative is to implement a new unused analysis in the linter. Unused analysis is expensive though, and adding a second one in linter (duplicating a ton of code) will make it more expensive.
Downsides
One downside to recommending private top-level members is that it might be counter to what users typically do today. I think a lot of people just write regular, public, top-level consts and helper functions. Personally, I always write private top-level members, so that I know when something becomes unused. Perhaps a study should be done on what most people do. #57580 does not have this downside.
The text was updated successfully, but these errors were encountered: