Skip to content

Commit

Permalink
tools: add number-isnan rule
Browse files Browse the repository at this point in the history
PR-URL: #17556
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
  • Loading branch information
maclover7 authored and MylesBorins committed Feb 27, 2018
1 parent e021fb7 commit 4879038
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 4 deletions.
1 change: 1 addition & 0 deletions test/.eslintrc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ rules:

# Custom rules in tools/eslint-rules
prefer-common-mustnotcall: 2
number-isnan: error

## common module is mandatory in tests
required-modules: [error, common]
Expand Down
20 changes: 20 additions & 0 deletions test/parallel/test-eslint-number-isnan.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict';

require('../common');

const RuleTester = require('../../tools/eslint').RuleTester;
const rule = require('../../tools/eslint-rules/number-isnan');

const message = 'Please use Number.isNaN instead of the global isNaN function';

new RuleTester().run('number-isnan', rule, {
valid: [
'Number.isNaN()'
],
invalid: [
{
code: 'isNaN()',
errors: [{ message }]
}
]
});
4 changes: 2 additions & 2 deletions test/parallel/test-readdouble.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ function test(clazz) {
buffer[5] = 0xff;
buffer[6] = 0x0f;
buffer[7] = 0x00;
assert.ok(isNaN(buffer.readDoubleBE(0)));
assert.ok(Number.isNaN(buffer.readDoubleBE(0)));
assert.strictEqual(2.225073858507201e-308, buffer.readDoubleLE(0));

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

buffer[0] = 0;
Expand Down
4 changes: 2 additions & 2 deletions test/parallel/test-writefloat.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ function test(clazz) {
// Darwin ia32 does the other kind of NaN.
// Compiler bug. No one really cares.
assert(0x7F === buffer[7] || 0xFF === buffer[7]);
assert.ok(isNaN(buffer.readFloatBE(0)));
assert.ok(isNaN(buffer.readFloatLE(4)));
assert.ok(Number.isNaN(buffer.readFloatBE(0)));
assert.ok(Number.isNaN(buffer.readFloatLE(4)));
}


Expand Down
14 changes: 14 additions & 0 deletions tools/eslint-rules/number-isnan.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict';

const astSelector = "CallExpression[callee.name='isNaN']";
const msg = 'Please use Number.isNaN instead of the global isNaN function';

module.exports = function(context) {
function report(node) {
context.report(node, msg);
}

return {
[astSelector]: report
};
};

0 comments on commit 4879038

Please sign in to comment.