Skip to content
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

Add linter rule to disallow string literals in Widget classes #261

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

bdmendes
Copy link

@bdmendes bdmendes commented Mar 22, 2024

Closes #260
This linter rule makes code review easier for internationalized apps, by preventing the usage of string literals in widgets.

This may be annoying, since we need string literals for things other than UI inside a widget's class, and it's not possible (at least from what I've seen from the analyzer API) to guarantee with a 100% certainty that they are being used in that context. As such, I added 2 simple exceptions:

  • When handling an exception: so that we can log it/send to a server
  • When throwing an exception: so that we can define a proper message

Furthermore, there are some things we should discuss:

  • Should we make these ancestor exceptions configurable?
  • Should the exceptions be related instead to e.g. the name of the function that is being invoked?

@shilangyu
Copy link
Contributor

Did you try to enable it in some project to see what kind of false positives you got?

Generally speaking, I think I would for approaching this more conservatively. As I said, we'd rather miss some violations than annoy the user with false-positives. I was thinking that maybe we should just complain if a string literal is used directly in a widget constructor, as I think this is actually 90% of the cases (source: I made it up).

Then, it could be configurable what kind of constructor parameters should not be flagged (for instance TextField.obscuringCharacter seems like something that shouldn't be flagged). Also, maybe the empty string literal shouldn't be ever flagged, its probably used as some UI workaround.

What do you think?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add custom linter rule to prevent usage of string literals in widgets
2 participants