Mmohebifar

prefer-numeric-literals-over-parseint-radix

Rewrite safe parseInt and Number.parseInt calls on static binary, octal, and hexadecimal strings to equivalent numeric literals.

javascripttypescriptparseintnumeric-literalseslint
Public
0 executions

Run locally

npx codemod prefer-numeric-literals-over-parseint-radix

prefer-numeric-literals-over-parseint-radix

Replaces parseInt() and Number.parseInt() calls that parse static base-2, base-8, or base-16 strings with equivalent 0b, 0o, and 0x numeric literals when the result is semantically safe.

What It Rewrites

js

Safety Rules

  • Only rewrites calls with exactly 2 arguments.
  • Only rewrites string literals or template literals with no expressions.
  • Only rewrites radix literals that evaluate to 2, 8, or 16.
  • Requires the trimmed payload to be entirely valid digits for that radix.
  • Preserves hex digit casing from the source string.
  • Skips shadowed parseInt bindings and shadowed Number.parseInt receivers.
  • Skips partial parses, prefixed payloads like "0xFF", and negative-zero inputs such as "-0".

Target Files

  • *.js, *.cjs, *.mjs
  • *.ts, *.cts, *.mts
  • *.jsx, *.tsx

Usage

bash

Development

bash

License

MIT

Ready to contribute?

Build your own codemod and share it with the community.