From 88ef086e39cd055a1cdc6720981f50e4791fb90f Mon Sep 17 00:00:00 2001 From: Rich Trott Date: Mon, 30 Sep 2019 21:09:47 -0700 Subject: tools: update ESLint to v6.5.1 PR-URL: https://github.com/nodejs/node/pull/29785 Reviewed-By: Trivikram Kamat Reviewed-By: Colin Ihrig Reviewed-By: Yongsheng Zhang Reviewed-By: Beth Griggs Reviewed-By: Ruben Bridgewater Reviewed-By: David Carlier --- tools/node_modules/eslint/lib/rules/use-isnan.js | 73 ++++++++++++++++++++++-- 1 file changed, 67 insertions(+), 6 deletions(-) (limited to 'tools/node_modules/eslint/lib/rules/use-isnan.js') diff --git a/tools/node_modules/eslint/lib/rules/use-isnan.js b/tools/node_modules/eslint/lib/rules/use-isnan.js index 877c02754a..b2eb84b7b3 100644 --- a/tools/node_modules/eslint/lib/rules/use-isnan.js +++ b/tools/node_modules/eslint/lib/rules/use-isnan.js @@ -5,6 +5,19 @@ "use strict"; +//------------------------------------------------------------------------------ +// Helpers +//------------------------------------------------------------------------------ + +/** + * Determines if the given node is a NaN `Identifier` node. + * @param {ASTNode|null} node The node to check. + * @returns {boolean} `true` if the node is 'NaN' identifier. + */ +function isNaNIdentifier(node) { + return Boolean(node) && node.type === "Identifier" && node.name === "NaN"; +} + //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ @@ -20,21 +33,69 @@ module.exports = { url: "https://eslint.org/docs/rules/use-isnan" }, - schema: [], + schema: [ + { + type: "object", + properties: { + enforceForSwitchCase: { + type: "boolean", + default: false + } + }, + additionalProperties: false + } + ], + messages: { - useIsNaN: "Use the isNaN function to compare with NaN." + comparisonWithNaN: "Use the isNaN function to compare with NaN.", + switchNaN: "'switch(NaN)' can never match a case clause. Use Number.isNaN instead of the switch.", + caseNaN: "'case NaN' can never match. Use Number.isNaN before the switch." } }, create(context) { - return { - BinaryExpression(node) { - if (/^(?:[<>]|[!=]=)=?$/u.test(node.operator) && (node.left.name === "NaN" || node.right.name === "NaN")) { - context.report({ node, messageId: "useIsNaN" }); + const enforceForSwitchCase = context.options[0] && context.options[0].enforceForSwitchCase; + + /** + * Checks the given `BinaryExpression` node. + * @param {ASTNode} node The node to check. + * @returns {void} + */ + function checkBinaryExpression(node) { + if ( + /^(?:[<>]|[!=]=)=?$/u.test(node.operator) && + (isNaNIdentifier(node.left) || isNaNIdentifier(node.right)) + ) { + context.report({ node, messageId: "comparisonWithNaN" }); + } + } + + /** + * Checks the discriminant and all case clauses of the given `SwitchStatement` node. + * @param {ASTNode} node The node to check. + * @returns {void} + */ + function checkSwitchStatement(node) { + if (isNaNIdentifier(node.discriminant)) { + context.report({ node, messageId: "switchNaN" }); + } + + for (const switchCase of node.cases) { + if (isNaNIdentifier(switchCase.test)) { + context.report({ node: switchCase, messageId: "caseNaN" }); } } + } + + const listeners = { + BinaryExpression: checkBinaryExpression }; + if (enforceForSwitchCase) { + listeners.SwitchStatement = checkSwitchStatement; + } + + return listeners; } }; -- cgit v1.2.3