From 8200f9967d03bc9cb8152155df11d9a9d758b2e5 Mon Sep 17 00:00:00 2001 From: Kevin Partington Date: Sat, 14 May 2016 15:48:17 -0500 Subject: [PATCH] New: no-qunit-push rule (refs #20) --- README.md | 1 + docs/rules/no-qunit-push.md | 39 ++++++++++++++++++++++++++++++ index.js | 2 ++ lib/rules/no-qunit-push.js | 41 ++++++++++++++++++++++++++++++++ tests/lib/rules/no-qunit-push.js | 34 ++++++++++++++++++++++++++ 5 files changed, 117 insertions(+) create mode 100644 docs/rules/no-qunit-push.md create mode 100644 lib/rules/no-qunit-push.js create mode 100644 tests/lib/rules/no-qunit-push.js diff --git a/README.md b/README.md index cc2a6a60..5829bd8b 100644 --- a/README.md +++ b/README.md @@ -27,6 +27,7 @@ Below is the list of rules available in this plugin. * [no-negated-ok](./docs/rules/no-negated-ok.md) * [no-ok-equality](./docs/rules/no-ok-equality.md) * [no-only](./docs/rules/no-only.md) +* [no-qunit-push](./docs/rules/no-qunit-push.md) * [no-reassign-log-callbacks](./docs/rules/no-reassign-log-callbacks.md) * [no-reset](./docs/rules/no-reset.md) * [no-setup-teardown](./docs/rules/no-setup-teardown.md) diff --git a/docs/rules/no-qunit-push.md b/docs/rules/no-qunit-push.md new file mode 100644 index 00000000..ac0e7f48 --- /dev/null +++ b/docs/rules/no-qunit-push.md @@ -0,0 +1,39 @@ +# Forbid the use of QUnit.push (no-qunit-push) + +When writing custom assertions, the proper way to log an assertion result +used to be calling `QUnit.push()` with the assertion result data. However, in +order to allow for better control of test context, `QUnit.push` has been +deprecated (to be removed in 2.0) and the correct way to log the assertion +result is to call `this.pushResult()` from within the assertion function. + +## Rule Details + +The following patterns are considered warnings: + +```js + +QUnit.push(result, actual, expected, message); + +``` + +The following patterns are not warnings: + +```js + +this.pushResult({ + result: result, + actual: actual, + expected: expected, + message: message +}); + +``` + +## When Not To Use It + +This rule may be safely disabled if you are working in a legacy codebase that +will not be migrated to QUnit 2.0. + +## Further Reading + +* [QUnit.push()](http://api.qunitjs.com/QUnit.push/) diff --git a/index.js b/index.js index d1e736db..5bee0bee 100644 --- a/index.js +++ b/index.js @@ -18,6 +18,7 @@ module.exports = { "no-negated-ok": require("./lib/rules/no-negated-ok"), "no-ok-equality": require("./lib/rules/no-ok-equality"), "no-only": require("./lib/rules/no-only"), + "no-qunit-push": require("./lib/rules/no-qunit-push"), "no-reassign-log-callbacks": require("./lib/rules/no-reassign-log-callbacks"), "no-reset": require("./lib/rules/no-reset"), "no-setup-teardown": require("./lib/rules/no-setup-teardown"), @@ -41,6 +42,7 @@ module.exports = { "no-negated-ok": 0, "no-ok-equality": 1, "no-only": 1, + "no-qunit-push": 0, "no-reassign-log-callbacks": 0, "no-reset": 0, "no-setup-teardown": 0, diff --git a/lib/rules/no-qunit-push.js b/lib/rules/no-qunit-push.js new file mode 100644 index 00000000..db6d607b --- /dev/null +++ b/lib/rules/no-qunit-push.js @@ -0,0 +1,41 @@ +/** + * @fileoverview Forbid the use of QUnit.push. + * @author Kevin Partington + */ +"use strict"; + +//------------------------------------------------------------------------------ +// Rule Definition +//------------------------------------------------------------------------------ + +module.exports = function (context) { + var MESSAGE = "Do not use QUnit.push()."; + + function isQUnitPush(calleeNode) { + return calleeNode && + calleeNode.type === "MemberExpression" && + calleeNode.object && + calleeNode.object.type === "Identifier" && + calleeNode.object.name === "QUnit" && + calleeNode.property && + calleeNode.property.type === "Identifier" && + calleeNode.property.name === "push"; + } + + //-------------------------------------------------------------------------- + // Public + //-------------------------------------------------------------------------- + + return { + "CallExpression": function (node) { + if (isQUnitPush(node.callee)) { + context.report({ + node: node, + message: MESSAGE + }); + } + } + }; +}; + +module.exports.schema = []; diff --git a/tests/lib/rules/no-qunit-push.js b/tests/lib/rules/no-qunit-push.js new file mode 100644 index 00000000..441f4cc7 --- /dev/null +++ b/tests/lib/rules/no-qunit-push.js @@ -0,0 +1,34 @@ +/** + * @fileoverview Forbid the use of QUnit.push. + * @author Kevin Partington + */ +"use strict"; + +//------------------------------------------------------------------------------ +// Requirements +//------------------------------------------------------------------------------ + +var rule = require("../../../lib/rules/no-qunit-push"), + RuleTester = require("eslint").RuleTester; + + +//------------------------------------------------------------------------------ +// Tests +//------------------------------------------------------------------------------ + +var ruleTester = new RuleTester(); +ruleTester.run("no-qunit-push", rule, { + valid: [ + "this.pushResult({ result: result, actual: actual, expected: expected, message: message });" + ], + + invalid: [ + { + code: "QUnit.push(result, actual, expected, message);", + errors: [{ + message: "Do not use QUnit.push().", + type: "CallExpression" + }] + } + ] +});