Skip to content

Commit 4879038

Browse files
maclover7MylesBorins
authored andcommitted
tools: add number-isnan rule
PR-URL: #17556 Reviewed-By: Rich Trott <[email protected]> Reviewed-By: Colin Ihrig <[email protected]>
1 parent e021fb7 commit 4879038

File tree

5 files changed

+39
-4
lines changed

5 files changed

+39
-4
lines changed

test/.eslintrc.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ rules:
88

99
# Custom rules in tools/eslint-rules
1010
prefer-common-mustnotcall: 2
11+
number-isnan: error
1112

1213
## common module is mandatory in tests
1314
required-modules: [error, common]
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'use strict';
2+
3+
require('../common');
4+
5+
const RuleTester = require('../../tools/eslint').RuleTester;
6+
const rule = require('../../tools/eslint-rules/number-isnan');
7+
8+
const message = 'Please use Number.isNaN instead of the global isNaN function';
9+
10+
new RuleTester().run('number-isnan', rule, {
11+
valid: [
12+
'Number.isNaN()'
13+
],
14+
invalid: [
15+
{
16+
code: 'isNaN()',
17+
errors: [{ message }]
18+
}
19+
]
20+
});

test/parallel/test-readdouble.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,12 @@ function test(clazz) {
5151
buffer[5] = 0xff;
5252
buffer[6] = 0x0f;
5353
buffer[7] = 0x00;
54-
assert.ok(isNaN(buffer.readDoubleBE(0)));
54+
assert.ok(Number.isNaN(buffer.readDoubleBE(0)));
5555
assert.strictEqual(2.225073858507201e-308, buffer.readDoubleLE(0));
5656

5757
buffer[6] = 0xef;
5858
buffer[7] = 0x7f;
59-
assert.ok(isNaN(buffer.readDoubleBE(0)));
59+
assert.ok(Number.isNaN(buffer.readDoubleBE(0)));
6060
assert.strictEqual(1.7976931348623157e+308, buffer.readDoubleLE(0));
6161

6262
buffer[0] = 0;

test/parallel/test-writefloat.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,8 @@ function test(clazz) {
106106
// Darwin ia32 does the other kind of NaN.
107107
// Compiler bug. No one really cares.
108108
assert(0x7F === buffer[7] || 0xFF === buffer[7]);
109-
assert.ok(isNaN(buffer.readFloatBE(0)));
110-
assert.ok(isNaN(buffer.readFloatLE(4)));
109+
assert.ok(Number.isNaN(buffer.readFloatBE(0)));
110+
assert.ok(Number.isNaN(buffer.readFloatLE(4)));
111111
}
112112

113113

tools/eslint-rules/number-isnan.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
'use strict';
2+
3+
const astSelector = "CallExpression[callee.name='isNaN']";
4+
const msg = 'Please use Number.isNaN instead of the global isNaN function';
5+
6+
module.exports = function(context) {
7+
function report(node) {
8+
context.report(node, msg);
9+
}
10+
11+
return {
12+
[astSelector]: report
13+
};
14+
};

0 commit comments

Comments
 (0)