mark-nicepants / figma2flutter

Converts design token json files to flutter
Apache License 2.0
17 stars 16 forks source link

[BUG] Returning 0.0 creates silent defects #20

Open freemansoft opened 4 months ago

freemansoft commented 4 months ago

:information_source: Info

Version: 0.2.1-alpha

:speech_balloon: Description

Returning 0 for unparsable values is really a silent failure that gives you bad values in the generated code. Code like this should IMO throw a ProcessTokenException or a FormatException

double _parseNum(String value) {
  // 1px = 1.0
  if (value.endsWith('px')) {
    return double.tryParse(value.substring(0, value.length - 2)) ?? 0;
  }

  // 1rem = 16px (base font size)
  if (value.endsWith('rem')) {
    return (double.tryParse(value.substring(0, value.length - 3)) ?? 0) *
        kBaseFontSize;
  }

  // 100% = 1.0
  // 50% = 0.5
  if (value.endsWith('%')) {
    return (double.tryParse(value.substring(0, value.length - 1)) ?? 0) / 100;
  }

  return double.tryParse(value) ?? 0;
}

:scroll: tokens file you tried to import

Importing a token for something like always returns 0.0 because of the missing space between "*" and "2". It doesn't think there is a math operator so it parses them as a number and if that is null it returns zero.

"sizing-base": {
    "value": "16 *2",
    "type": "sizing"
  },