From 96978768c93ceeb1433885d8acb3fba373820bff Mon Sep 17 00:00:00 2001 From: Rich Trott Date: Tue, 19 Apr 2016 15:47:12 -0700 Subject: [PATCH] tools: enforce deepStrictEqual over deepEqual Introduce a lint rule that enforces use of `assert.deepStrictEqual()` over `assert.deepEqual()`. PR-URL: https://github.com/nodejs/node/pull/6213 Reviewed-By: James M Snell Reviewed-By: Colin Ihrig --- .eslintrc | 3 ++- tools/eslint-rules/no-deepEqual.js | 32 ++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 tools/eslint-rules/no-deepEqual.js diff --git a/.eslintrc b/.eslintrc index af0e6ddc15..b48f2277c8 100644 --- a/.eslintrc +++ b/.eslintrc @@ -85,9 +85,10 @@ rules: prefer-const: 2 # Custom rules in tools/eslint-rules + align-multiline-assignment: 2 assert-fail-single-argument: 2 new-with-error: [2, "Error", "RangeError", "TypeError", "SyntaxError", "ReferenceError"] - align-multiline-assignment: 2 + no-deepEqual: 2 # Global scoped method and vars globals: diff --git a/tools/eslint-rules/no-deepEqual.js b/tools/eslint-rules/no-deepEqual.js new file mode 100644 index 0000000000..46341d7d92 --- /dev/null +++ b/tools/eslint-rules/no-deepEqual.js @@ -0,0 +1,32 @@ +/** + * @fileoverview Prohibit use of assert.deepEqual() + * @author Rich Trott + * + * This rule is imperfect, but will find the most common forms of + * assert.deepEqual() usage. + */ +'use strict'; + +//------------------------------------------------------------------------------ +// Rule Definition +//------------------------------------------------------------------------------ + +const msg = 'assert.deepEqual() disallowed. Use assert.deepStrictEqual()'; + +function isAssert(node) { + return node.callee.object && node.callee.object.name === 'assert'; +} + +function isDeepEqual(node) { + return node.callee.property && node.callee.property.name === 'deepEqual'; +} + +module.exports = function(context) { + return { + 'CallExpression': function(node) { + if (isAssert(node) && isDeepEqual(node)) { + context.report(node, msg); + } + } + }; +};