diff options
author | Rich Trott <rtrott@gmail.com> | 2019-09-30 21:09:47 -0700 |
---|---|---|
committer | Rich Trott <rtrott@gmail.com> | 2019-10-02 21:05:28 -0700 |
commit | 88ef086e39cd055a1cdc6720981f50e4791fb90f (patch) | |
tree | e6d92724ca0c2fba668394f6d0c34c85a610c5ca /tools/node_modules/eslint/lib/rules/use-isnan.js | |
parent | a67b73b9aee64d74dba52029b57c7d2b97b9e149 (diff) | |
download | android-node-v8-88ef086e39cd055a1cdc6720981f50e4791fb90f.tar.gz android-node-v8-88ef086e39cd055a1cdc6720981f50e4791fb90f.tar.bz2 android-node-v8-88ef086e39cd055a1cdc6720981f50e4791fb90f.zip |
tools: update ESLint to v6.5.1
PR-URL: https://github.com/nodejs/node/pull/29785
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Yongsheng Zhang <zyszys98@gmail.com>
Reviewed-By: Beth Griggs <Bethany.Griggs@uk.ibm.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: David Carlier <devnexen@gmail.com>
Diffstat (limited to 'tools/node_modules/eslint/lib/rules/use-isnan.js')
-rw-r--r-- | tools/node_modules/eslint/lib/rules/use-isnan.js | 73 |
1 files changed, 67 insertions, 6 deletions
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 @@ -6,6 +6,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; } }; |