Registry
Mmohebifar avatar

prefer-numeric-literals-over-parseint-radix

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

mohebifar0 executions

  • javascript
  • typescript
  • parseint
  • numeric-literals
  • eslint
Connect a repo
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
parseInt("1010", 2)
// becomes
0b1010

Number.parseInt("755", 8)
// becomes
0o755

parseInt("   -ff   ", 16)
// becomes
(-0xff)

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
pnpm dlx codemod@latest workflow run -w . -t /path/to/project

Development

bash
pnpm test
pnpm run validate
pnpm run check-types

License

MIT

Ready to evolve your codebase?

Build tailored, compiler-aware skills to assess, plan, and automate complex codebase changes.