From e7a99b3c6f4720492c9329b92900ad6d3821cc8d Mon Sep 17 00:00:00 2001 From: Jon Moss Date: Mon, 11 Dec 2017 17:20:39 -0500 Subject: [PATCH] tools: add number-isnan rule PR-URL: https://github.com/nodejs/node/pull/17556 Reviewed-By: Rich Trott Reviewed-By: Colin Ihrig --- test/.eslintrc.yaml | 1 + test/parallel/test-eslint-number-isnan.js | 20 ++++++++++++++++++++ tools/eslint-rules/number-isnan.js | 14 ++++++++++++++ 3 files changed, 35 insertions(+) create mode 100644 test/parallel/test-eslint-number-isnan.js create mode 100644 tools/eslint-rules/number-isnan.js diff --git a/test/.eslintrc.yaml b/test/.eslintrc.yaml index 7a5d002e1b51e8..dbcb8ea6e0aa87 100644 --- a/test/.eslintrc.yaml +++ b/test/.eslintrc.yaml @@ -14,6 +14,7 @@ rules: prefer-common-mustnotcall: error crypto-check: error inspector-check: error + number-isnan: error ## common module is mandatory in tests required-modules: [error, common] diff --git a/test/parallel/test-eslint-number-isnan.js b/test/parallel/test-eslint-number-isnan.js new file mode 100644 index 00000000000000..deeac48bcccb68 --- /dev/null +++ b/test/parallel/test-eslint-number-isnan.js @@ -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 }] + } + ] +}); diff --git a/tools/eslint-rules/number-isnan.js b/tools/eslint-rules/number-isnan.js new file mode 100644 index 00000000000000..885c38be8b2384 --- /dev/null +++ b/tools/eslint-rules/number-isnan.js @@ -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 + }; +};