You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
changed the title [-]consider a `prefer_enums` lint[/-][+]consider a `prefer_enums` lint to encourage migration from enum-like classes to enhanced enums[/+]on Jan 24, 2022
Like other language feature migration lints, this is a lint that I would want to have ship with a quickfix to make it easy for users to learn how to do the migration.
As a starting point we can probably use the existing definition of an enum-like class to define which classes to lint, but we might need to make some adjustments. For reference, the documentation currently states:
An enum-like class must meet the following requirements:
is concrete,
has no public constructors,
has no factory constructors,
has two or more static const fields with the same type as the class,
has no subclasses declared in the defining library.
We probably need to add at least one additional criteria:
is a subclass of Object (because enum's can't extend other classes).
If enums aren't allowed to have mixins then we'll also need to check that the class being linted doesn't have a with clause.
My first cut implementation settled on a slight variation on the list above. (See PR.)
TL;DR:
Candidates for enums are classes that:
* are concrete,
* are private or have only private generative constructors,
* have two or more static const fields with the same type as the class,
* have generative constructors that are only invoked at the top-level of the
initialization expression of these static fields,
* do not define `hashCode`, `==`, `values` or `index`,
* do not extend any class other than Object, and
* have no subclasses declared in the defining library.
As mentioned in the WIP PR, the index restriction isn't strictly necessary and I'm open to expanding the check to handle cases where classes use an index in an enum-compatible way.
changed the title [-]consider a `prefer_enums` lint to encourage migration from enum-like classes to enhanced enums[/-][+]consider a `use_enums` lint to encourage migration from enum-like classes to enhanced enums[/+]on Mar 1, 2022
Activity
[-]consider a `prefer_enums` lint[/-][+]consider a `prefer_enums` lint to encourage migration from enum-like classes to enhanced enums[/+]jacob314 commentedon Jan 24, 2022
Like other language feature migration lints, this is a lint that I would want to have ship with a quickfix to make it easy for users to learn how to do the migration.
bwilkerson commentedon Jan 26, 2022
As a starting point we can probably use the existing definition of an enum-like class to define which classes to lint, but we might need to make some adjustments. For reference, the documentation currently states:
We probably need to add at least one additional criteria:
Object
(because enum's can't extend other classes).If enums aren't allowed to have mixins then we'll also need to check that the class being linted doesn't have a
with
clause.Are there other criteria we need to check for?
pq commentedon Feb 26, 2022
My first cut implementation settled on a slight variation on the list above. (See PR.)
TL;DR:
As mentioned in the WIP PR, the
index
restriction isn't strictly necessary and I'm open to expanding the check to handle cases where classes use anindex
in an enum-compatible way.[-]consider a `prefer_enums` lint to encourage migration from enum-like classes to enhanced enums[/-][+]consider a `use_enums` lint to encourage migration from enum-like classes to enhanced enums[/+]