question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Rule proposal: disallow using parseInt by reference

See original GitHub issue

Please describe what the rule should do:

eslint should be able to warn when passing parseInt by reference instead of directly invoking it, as this likely means a radix is not being passed despite their being a rule to enforce radix usage. Functions like forEach and map provide a second argument for the iteration index which, when applied to parseInt, will be used unintentionally as the radix.

What category of rule is this? (place an “X” next to just one item)

[x] Enforces code style [x] Warns about a potential error [ ] Suggests an alternate way of doing something [ ] Other (please specify:)

Provide 2-3 code examples that this rule will warn about:

// because Array.prototype.map provides a second (index) argument
// this will log `[ 7, NaN ]` where the intent is `[7, 8]`
let arr = ['7', '8']

console.log(arr.map(parseInt))
function mapp(list, fn) {
    return list.map(fn)
}

mapper(['0x0101', '100'], parseInt)

Why should this rule be included in ESLint (instead of a plugin)?

It’s high-risk to make this mistake and is not caught by the radix rule despite that partially being intended to avoid issues like this.

Ecmascript functions like Array.prototype.map, Array.prototype.forEach, Array.prototype.filter, and Array.prototype.every all pass a second index argument to the callback function which can cause usage of parseInt to mismatch a developer’s expectations.

It’s possible this should instead be enforced when radix is enabled, or be a configurable option there.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
whonowcommented, Apr 26, 2017

On Apr 25, 2017 at 10:50 PM, <Scott Hulbert notifications@github.com> wrote:

@not-an-aardvark https://github.com/not-an-aardvark thanks for the suggestion, here is how I (think) I accomplished this.

Configuration:

{ “rules”: { “no-restricted-syntax”: [ “error”, { “selector”: “CallExpression[mike,mills!=‘parseInt’] > Identifier[name=‘parseInt’]”, “message”: “Call parseInt directly to guarantee radix param is not incorrectly provided” } ] } }

Fails:

‘use strict’ const arr = [‘11’, ‘3’] console.log(arr.map(parseInt))

Passes:

‘use strict’ const arr = [‘11’, ‘3’] console.log(arr.map(function(n) { return parseInt(n) }))

parseInt(5, 10) // not really safe but eslint doesn’t support catching this AFAIKlet fun = parseIntconsole.log(arr.map(fun))

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/eslint/eslint/issues/8214#issuecomment-297222523, or mute the thread https://github.com/notifications/unsubscribe-auth/AYEjEogqmNy8ycykBOLwIW76W4hsSiu9ks5rzrD9gaJpZM4MWfjj .

0reactions
hulbertcommented, Apr 26, 2017

@not-an-aardvark thanks for the suggestion, here is how I (think) I accomplished this.


Configuration:

{
  "rules": {
    "no-restricted-syntax": [
      "error",
      {
        "selector": "CallExpression[callee.name!='parseInt'] > Identifier[name='parseInt']",
        "message": "Call parseInt directly to guarantee radix param is not incorrectly provided"
      }
    ]
  }
}

Fails:

'use strict'

const arr = ['11', '3']

console.log(arr.map(parseInt))

Passes:

'use strict'

const arr = ['11', '3']

console.log(arr.map(function(n) {
    return parseInt(n)
}))


parseInt(5, 10)

// not really safe but eslint doesn't support catching this AFAIK
let fun = parseInt
console.log(arr.map(fun))
Read more comments on GitHub >

github_iconTop Results From Across the Web

TypeScript: error when using parseInt() on a number
In normal JS, the first argument is coerced to a string, based on the following rule in the spec: Let inputString be ToString(string)....
Read more >
Type Casting & Coercion · Styleguide JavaScript
Use Number for type casting and parseInt always with a radix for parsing strings. ESLint: radix and no-new-wrappers. Examples. ⇣ Incorrect code for...
Read more >
How to Handle the Incompatible Types Error in Java - Rollbar
The Java incompatible types error happens when a value assigned to a variable or returned by a method is incompatible with the one...
Read more >
parseInt() - JavaScript - MDN Web Docs
The parseInt() function parses a string argument and returns an integer of the specified radix (the base in mathematical numeral systems).
Read more >
Nim Manual - Nim Programming Language
the cast operator; reference (pointer) types; FFI. The use of wrappers that use FFI and/or cast is also disallowed. Note ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found