From 0e37054c9620d85bab94e7db98a48c1accd0ffed 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 aa320996aa4b16..23d2184860b346 100644 --- a/test/.eslintrc.yaml +++ b/test/.eslintrc.yaml @@ -13,6 +13,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 + }; +};