Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New: Unsafe integer rule #12889

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
40 changes: 40 additions & 0 deletions docs/rules/no-unsafe-integer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# disallow unsafe integers (no-unsafe-integer)

Due to IEEE-754 double precision, integers with a value higher or equal to 2^53 cannot properly be represented in JavaScript and are therefore considered unsafe.
Use the [`BigInt`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt) object or the `10n` literal instead.

See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isSafeInteger.

## Rule Details

This rule disallows usage of unsafe integer literals. It also checks `parseInt()` and `Number.parseInt()` function calls with unsafe integers.

Examples of **incorrect** code for this rule:

```js
/*eslint no-unsafe-integer: "error"*/

var num1 = 9223372036854775807; // INT64_MAX_NUMBER
var num2 = 0x7FFFFFFFFFFFFFFF; // INT64_MAX_NUMBER in hex
var num3 = 9007199254740992; // MAX_SAFE_INTEGER + 1
foo(9007199254740992);
parseInt("9007199254740992");
parseInt("9007199254740992", 10);

```

Examples of **correct** code for this rule:

```js
/*eslint no-empty: "error"*/

var num1 = 0;
var num2 = 1;
var num3 = 123456789;
var num4 = 9007199254740991; // MAX_SAFE_INTEGER
var num5 = 0x1FFFFFFFFFFFFF; // MAX_SAFE_INTEGER in hex
foo(9007199254740991);
parseInt("9007199254740991");
parseInt("9007199254740991", 10);
parseInt("1234567891011121315", 8); // 342391
```
88 changes: 88 additions & 0 deletions lib/rules/no-unsafe-integer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/**
* @fileoverview Rule to flag use of a probable unsafe integer
* @author Joshua Westerheide
*/
"use strict";

//------------------------------------------------------------------------------
// Helpers
//------------------------------------------------------------------------------

/**
* Checks to see if a CallExpression's callee node is `parseInt` or
* `Number.parseInt`.
* @param {ASTNode} calleeNode The callee node to evaluate.
* @returns {boolean} True if the callee is `parseInt` or `Number.parseInt`,
* false otherwise.
*/
function isParseInt(calleeNode) {
switch (calleeNode.type) {
case "Identifier":
return calleeNode.name === "parseInt";
case "MemberExpression":
return calleeNode.object.type === "Identifier" &&
calleeNode.object.name === "Number" &&
calleeNode.property.type === "Identifier" &&
calleeNode.property.name === "parseInt";

// no default
}

return false;
}

//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------

module.exports = {
meta: {
type: "problem",

docs: {
description: "disallow unsafe integers",
category: "Possible Errors",
recommended: true,
url: "https://eslint.org/docs/rules/no-unsafe-integer"
},

schema: [],

messages: {
unsafe: "Possible unsafe integer {{value}}.",
"unsafe-parsing": "Possible unsafe integer {{value}} parsing."
}
},

create(context) {
return {
Literal(node) {
if (typeof node.value === "number" && !Number.isSafeInteger(node.value)) {
context.report({ node, messageId: "unsafe", data: { value: node.raw } });
}
},

"CallExpression[arguments.length>=1]"(node) {
const strNode = node.arguments[0];

if (
strNode.type === "Literal" &&
typeof strNode.value === "string" &&
isParseInt(node.callee)
) {
let radix = 10;

if (node.arguments.length === 2) {
radix = node.arguments[1].value;
}

const num = Number.parseInt(strNode.value, radix);

if (!Number.isSafeInteger(num)) {
context.report({ node, messageId: "unsafe-parsing", data: { value: strNode.value } });
}
}
}
};
}
};
40 changes: 40 additions & 0 deletions tests/lib/rules/no-unsafe-integer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* @fileoverview Test for no-unsafe-integer rule
* @author Joshua Westerheide
*/
"use strict";

//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------

const rule = require("../../../lib/rules/no-unsafe-integer"),
{ RuleTester } = require("../../../lib/rule-tester");

//------------------------------------------------------------------------------
// Tests
//------------------------------------------------------------------------------

const ruleTester = new RuleTester();

ruleTester.run("no-unsafe-integer", rule, {
valid: [
"var num1 = 0",
"var num2 = 1",
"var num3 = 123456789",
"var num4 = 9007199254740991",
"var num5 = 0x1FFFFFFFFFFFFF",
"parseInt(\"9007199254740991\")",
"parseInt(\"9007199254740991\", 10)",
"parseInt(\"1234567891011121315\", 8)",
"foo(9007199254740991)"
],
invalid: [
{ code: "var num1 = 9223372036854775807", errors: [{ messageId: "unsafe", data: { value: "9223372036854775807" }, type: "Literal" }] },
{ code: "var num2 = 0x7FFFFFFFFFFFFFFF", errors: [{ messageId: "unsafe", data: { value: "0x7FFFFFFFFFFFFFFF" }, type: "Literal" }] },
{ code: "var num3 = 9007199254740992", errors: [{ messageId: "unsafe", data: { value: "9007199254740992" }, type: "Literal" }] },
{ code: "parseInt(\"9007199254740992\")", errors: [{ messageId: "unsafe-parsing", data: { value: "9007199254740992" }, type: "CallExpression" }] },
{ code: "parseInt(\"9007199254740992\", 10)", errors: [{ messageId: "unsafe-parsing", data: { value: "9007199254740992" }, type: "CallExpression" }] },
{ code: "foo(9007199254740992)", errors: [{ messageId: "unsafe", data: { value: "9007199254740992" }, type: "Literal" }] }
]
});