-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Add a new lint to require types on variables but not collections #58015
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
The linter includes some rules that are explicitly against the effective Dart style guide. In particular, |
Yes I get that. I also prefer to always specify types. But specifying the type is only needed for the But the linter shouldn't make me do: That's what I was pointing out. If there is another rule that makes me specify the type but not in the literal itself, please point me to it, I couldn't find one. |
That goes against Effective Dart. https://dart.dev/guides/language/effective-dart/design#avoid-type-annotating-initialized-local-variables So what you are asking for is neither Effective Dart style, nor Flutter SDK style.
There isn't one. I guess we could reopen this as a request for a new lint like In any case I worry that there might not be enough value in a lint that is neither Dart nor Flutter SDK style... |
Yeah, I know. But the reason I still do it is beacuse I don't like including type arguments on literals (basically what this issue is about), so the type is not at all obvious. Besides, it lets me understand code by looking at it quicker. So my code would be: And since I don't like putting generics on literals: I just want |
As above, this would require a new lint which follows neither Dart nor Flutter SDK style. The current lint is behaving as requested by the Flutter team and we can't change its behavior without their permission. Specifically it looks like @Hixie drove the request in #57240 |
I don't see anything about generics on literals in that issue, nor any issue linked there, but I had a look through some Flutter source code and found that there are type args on literals, so I guess that's something they do. I see your point about this not really being needed. Would you be able to point me to any resources (probably involving the |
Specifically it was here: #57240
This package is really the only place we have to write lints, and they are centralized. We don't have good infrastructure for plugging in distributed lints. Perhaps @bwilkerson knows of some existing feature requests for that type of thing. |
As an aside, you might consider trying to work for a while without all the redundant type annotations. We see a general trend where authors who are new to Dart are initially resistant to letting inference fill in the gaps, but then grow to prefer it when they give it a chance. Usually the reluctance comes from being comfortable seeing redundant types because it's what we're used to. Given the change to become accustomed to |
That's actually pretty funny seeing as the language I was most involved in before Dart was Python, and I originally hated all the types! So yeah, like you said, I do feel a lot better being able to see the types (and for But I get your point. Most times when I sit down to code I ask myself when I'll finally stop typing everything. And it's things like |
Hang on, I was looking through the code in this package (I didn't realize @override
void registerNodeProcessors(
NodeLintRegistry registry, LinterContext context) {
final visitor = _Visitor(this);
registry.addDeclaredIdentifier(this, visitor);
registry.addListLiteral(this, visitor);
registry.addSetOrMapLiteral(this, visitor);
registry.addSimpleFormalParameter(this, visitor);
registry.addTypeName(this, visitor);
registry.addVariableDeclarationList(this, visitor);
}
} Which I presume relates to this: @override
void visitDeclaredIdentifier(DeclaredIdentifier node) {
if (node.type == null) {
rule.reportLintForToken(node.keyword);
}
} this: @override
void checkLiteral(TypedLiteral literal) {
if (literal.typeArguments == null) {
rule.reportLintForToken(literal.beginToken);
}
} and this: @override
void visitSetOrMapLiteral(SetOrMapLiteral literal) {
checkLiteral(literal);
} If I customize the logic in these two methods to only react if the main identifier doesn't already have a type, would that achieve the effect I'm looking for? |
And I just love that Dart-parsing code is written in Dart which makes this process of finding fixes so straightforward. |
It might. We'd need to get buy-in before we can make such a change and I think we're unlikely to get agreement.
If you want to try it out you'll need to be able to build a local SDK, so not quite as straightforward as editing Dart code in your own projects... See https://github.com/dart-lang/sdk/wiki/Building |
The
There is a mechanism that exists that would allow you to add new custom lint rules, but unfortunately it has some serious drawbacks. As a result, we discourage users from trying to use it. We had hoped to make it a feature in our product, but we have not yet been able to put that on our roadmap. First, it's fairly heavy-weight in that you have to create and publish a package that includes the new lints, duplicate most of the linter support, and register the package as a plugin you want to use in every package that wants to use it. Second, we have not been able to do any performance profiling for it, so we can't guarantee that using this mechanism won't negatively impact the performance of the analyzer enough to make it unusable. Third, it's only supported in the analysis server, so you can't use it from the command line or in a continuous build environment. |
Yes, I know, I was just using that to justify my interest in
So I had a look around, and I found a couple of issues of interest, namely: #57588, #57238, #57240 (closed), and #58015 (also closed). I see that this will probably take a while (understandably). I personally would like to explore @natebosch's idea of rebuilding the SDK with my own logic in place of the old, but obviously this is not ideal and just a temporary solution if at all. |
Per Effective Dart:
But with the following code:
I get the following:
INFO|LINT|... Specify type annotations.
This comes from the rule
always_specify_types
.The text was updated successfully, but these errors were encountered: