useNumericLiterals
Ce contenu n’est pas encore disponible dans votre langue.
Summary
Section titled “Summary”- Rule available since: v1.0.0
- Diagnostic Category: lint/complexity/useNumericLiterals
- This rule is recommended, which means is enabled by default.
- This rule has a safe fix.
- The default severity of this rule is warning.
- Sources:
- Same as prefer-numeric-literals
 
- Same as 
How to configure
Section titled “How to configure”{  "linter": {    "rules": {      "complexity": {        "useNumericLiterals": "error"      }    }  }}Description
Section titled “Description”Disallow parseInt() and Number.parseInt() in favor of binary, octal, and hexadecimal literals
JavaScript provides literal forms for binary, octal, and hexadecimal numbers.
For example: 0b11, 0o77, and 0xff.
Using the literal forms enable static code analysis and avoid unnecessary computations.
Examples
Section titled “Examples”Invalid
Section titled “Invalid”parseInt("111110111", 2);code-block.js:1:1 lint/complexity/useNumericLiterals  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
  ⚠ This call to parseInt() can be replaced by a binary literal.
  
  > 1 │ parseInt(“111110111”, 2);
      │ ^^^^^^^^^^^^^^^^^^^^^^^^
    2 │ 
  
  ℹ Using a literal avoids unnecessary computations.
  
  ℹ Safe fix: Use the computed binary literal instead.
  
    1   │ - parseInt(“111110111”,·2);
      1 │ + 0b111110111;
    2 2 │   
  
Number.parseInt("767", 8);code-block.js:1:1 lint/complexity/useNumericLiterals  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
  ⚠ This call to Number.parseInt() can be replaced by an octal literal.
  
  > 1 │ Number.parseInt(“767”, 8);
      │ ^^^^^^^^^^^^^^^^^^^^^^^^^
    2 │ 
  
  ℹ Using a literal avoids unnecessary computations.
  
  ℹ Safe fix: Use the computed octal literal instead.
  
    1   │ - Number.parseInt(“767”,·8);
      1 │ + 0o767;
    2 2 │   
  
Number.parseInt("-1f7", 16);code-block.js:1:1 lint/complexity/useNumericLiterals  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
  ⚠ This call to Number.parseInt() can be replaced by a hexadecimal literal.
  
  > 1 │ Number.parseInt(“-1f7”, 16);
      │ ^^^^^^^^^^^^^^^^^^^^^^^^^^^
    2 │ 
  
  ℹ Using a literal avoids unnecessary computations.
  
  ℹ Safe fix: Use the computed hexadecimal literal instead.
  
    1   │ - Number.parseInt(“-1f7”,·16);
      1 │ + -0x1f7;
    2 2 │   
  
parseInt(1);parseInt(1, 3);Number.parseInt(1);Number.parseInt(1, 3);
0b111110111 === 503;0o767 === 503;0x1F7 === 503;
a[parseInt](1,2);
parseInt(foo);parseInt(foo, 2);Number.parseInt(foo);Number.parseInt(foo, 2);Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.