FlutterGen / flutter_gen

The Flutter code generator for your assets, fonts, colors, … — Get rid of all String-based APIs.
https://pub.dev/packages/flutter_gen
MIT License
1.47k stars 146 forks source link

[FR]: Keyword name conflicts. Prefix the names? #420

Closed jaustin42 closed 7 months ago

jaustin42 commented 1 year ago

Is there an existing issue for this?

Describe the problem

We have some files named with region codes. India, Israel, and Dominican Republic cause an issue:

line 405, column 22 of .: 'do' can't be used as an identifier because it's a keyword.
    ╷
405 │           String get do =>  'assets/do.svg';
    │                      ^^
    ╵
line 729, column 22 of .: 'in' can't be used as an identifier because it's a keyword.
    ╷
729 │           String get in =>  'assets/in.svg';
    │                      ^^
    ╵
line 753, column 22 of .: 'is' can't be used as an identifier because it's a keyword.
    ╷
753 │           String get is =>  'assets/is.svg';

Describe the solution

It seems the easiest way around that would be to optionally prefix all the filenames when generating that code.

Additional context

No response

Code of Conduct

bramp commented 1 year ago

A proposal would be to just suffix the variable with a underscore, so do_, in_, etc. This will still make it easy to discovery (with IDE autocompletion) and is minimally invasive.

I'll take a look at fixing this for you.

As a FYI If https://github.com/dart-lang/language/issues/271 was approved, we could write `in`

bramp commented 1 year ago

Ok, I spent a little more time on this than I should have :) (and I'm not a maintainer of this project, so I hope they accept the change). My PR is #433

There are a few other issues that cite similar problems, #250, and #346, that will be fixed by my PR.

The strategy is as follows:

1) Convert the asset file name, to a valid dart identifier, that is

AlexV525 commented 7 months ago

Ok, I spent a little more time on this than I should have :) (and I'm not a maintainer of this project, so I hope they accept the change). My PR is #433

There are a few other issues that cite similar problems, #250, and #346, that will be fixed by my PR.

The strategy is as follows:

  1. Convert the asset file name, to a valid dart identifier, that is

    • Replace non ASCII chars with ASCII.
    • Ensure the name starts with a letter (not number or _).
    • Style the name per the camelCase or snakeCase rules.
  2. Use the asset name without extension. If unique and not a dart reserved word use that.
  3. Use the asset name with extension. If unique and not a dart reserved word use that.
  4. If there are any collisions, append a underscore suffix to each item until they are unique.

I like the general strategy. Let's try it then we can find edges when users start using it, it's not a big deal.