avoid-js-rounded-ints

Same as avoid_js_rounded_ints in Dart lints

Group
Style
Path
lint/style/avoid-js-rounded-ints
Default
On (recommended)
Origin
Dart lints
Rule
80 of 512
recommended

Avoid integer literals that JavaScript would round.

Dart compiled to JavaScript stores every int as a 64-bit float, so an integer literal that a double cannot represent exactly silently changes value on the web:

int value = 9007199254740995; // BAD: becomes 9007199254740996 in JavaScript
BigInt value = BigInt.parse('9007199254740995'); // GOOD

Invalid

example.dartdart
int a = 9007199254740993;
int b = 9007199254740995;
int c = -9007199254740993;
int d = 0x20000000000001;
int e = 1234567890123456789;

void use() {
  print(9007199254740997);
  final values = [1, 18014398509481983];
  print(values);
}
Integer literal can't be represented exactly when compiled to JavaScript.
1int a = 9007199254740993;
Integer literal can't be represented exactly when compiled to JavaScript.
1int a = 9007199254740993;
2int b = 9007199254740995;
Integer literal can't be represented exactly when compiled to JavaScript.
2int b = 9007199254740995;
3int c = -9007199254740993;
Integer literal can't be represented exactly when compiled to JavaScript.
3int c = -9007199254740993;
4int d = 0x20000000000001;
Integer literal can't be represented exactly when compiled to JavaScript.
4int d = 0x20000000000001;
5int e = 1234567890123456789;
Integer literal can't be represented exactly when compiled to JavaScript.
7void use() {
8 print(9007199254740997);
Integer literal can't be represented exactly when compiled to JavaScript.
8 print(9007199254740997);
9 final values = [1, 18014398509481983];

Valid

example.dartdart
int a = 9007199254740991;
int b = 9007199254740992;
int c = -9007199254740991;
int d = 0x20000000000000;
int e = 18014398509481984;
int f = 1 << 60;
int g = 42;
double h = 9007199254740993.0;
BigInt i = BigInt.parse('9007199254740993');

Configure avoid-js-rounded-ints

Set its severity in falcon.json. See the configuration guide.

falcon.jsonjson
{
  "linter": {
    "rules": {
      "style": {
        "avoid-js-rounded-ints": "error"
      }
    }
  }
}

Or silence one occurrence with a suppression comment; the reason is required.

example.dartdart
// falcon-ignore lint/style/avoid-js-rounded-ints: <reason>