Skip to content

Commit

Permalink
New: no-reset rule (refs #20)
Browse files Browse the repository at this point in the history
  • Loading branch information
platinumazure committed Apr 10, 2016
1 parent bc8f463 commit d624a5f
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -24,6 +24,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-reset](./docs/rules/no-reset.md)
* [no-setup-teardown](./docs/rules/no-setup-teardown.md)
* [require-expect](./docs/rules/require-expect.md)
* [resolve-async](./docs/rules/resolve-async.md)
37 changes: 37 additions & 0 deletions docs/rules/no-reset.md
@@ -0,0 +1,37 @@
# Forbids use of QUnit.reset (no-reset)

Early versions of QUnit exposed the `QUnit.reset()` function, which allowed
consumers to invoke the internal QUnit fixture reset logic. This has been
discouraged for a long time since QUnit now automatically invokes this method
between tests. In QUnit 2.0, the function will be removed from the public API.
Tests that rely on this functionality will need to be split into multiple tests.

This rule will detect and report calls to `QUnit.reset()`.

## Rule Details

The following patterns are considered warnings:

```js

QUnit.reset();

```

The following patterns are not warnings:

```js

QUnit.reset; // Only attempting to invoke the function is reported

```

## When Not To Use It

This rule can be disabled if there are tests that rely on this functionality and
there are no plans to migrate to QUnit 2.0. For the other 99.9% of use cases,
use of this rule is *highly* recommended.

## Further Reading

* [QUnit Upgrade Guide: Stop using `QUnit.reset`](http://qunitjs.com/upgrade-guide-2.x/#stop-using-qunit-reset-split-one-test-into-multiple-tests)
2 changes: 2 additions & 0 deletions index.js
Expand Up @@ -15,6 +15,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-reset": require("./lib/rules/no-reset"),
"no-setup-teardown": require("./lib/rules/no-setup-teardown"),
"resolve-async": require("./lib/rules/resolve-async"),
"require-expect": require("./lib/rules/require-expect")
Expand All @@ -33,6 +34,7 @@ module.exports = {
"no-negated-ok": 0,
"no-ok-equality": 1,
"no-only": 1,
"no-reset": 0,
"no-setup-teardown": 0,
"resolve-async": 2,
"require-expect": 0
Expand Down
42 changes: 42 additions & 0 deletions lib/rules/no-reset.js
@@ -0,0 +1,42 @@
/**
* @fileoverview Forbids use of QUnit.reset.
* @author Kevin Partington
* @copyright 2016 Kevin Partington. All rights reserved.
* See LICENSE file in root directory for full license.
*/
"use strict";

//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------

module.exports = function (context) {
var MESSAGE = "Do not use QUnit.reset().";

function isQUnitReset(calleeNode) {
return calleeNode.type === "MemberExpression" &&
calleeNode.object &&
calleeNode.object.type === "Identifier" &&
calleeNode.object.name === "QUnit" &&
calleeNode.property &&
calleeNode.property.type === "Identifier" &&
calleeNode.property.name === "reset";
}

//--------------------------------------------------------------------------
// Public
//--------------------------------------------------------------------------

return {
"CallExpression": function (node) {
if (isQUnitReset(node.callee)) {
context.report({
node: node,
message: MESSAGE
});
}
}
};
};

module.exports.schema = [];
41 changes: 41 additions & 0 deletions tests/lib/rules/no-reset.js
@@ -0,0 +1,41 @@
/**
* @fileoverview Forbids use of QUnit.reset.
* @author Kevin Partington
* @copyright 2016 Kevin Partington. All rights reserved.
* See LICENSE file in root directory for full license.
*/
"use strict";

//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------

var rule = require("../../../lib/rules/no-reset"),
RuleTester = require("eslint").RuleTester;


//------------------------------------------------------------------------------
// Tests
//------------------------------------------------------------------------------

var ruleTester = new RuleTester();
ruleTester.run("no-reset", rule, {

valid: [
// Only invocations are reported
"QUnit.reset",

// Only QUnit.reset() is reported
"QUnit.init()"
],

invalid: [
{
code: "QUnit.reset();",
errors: [{
message: "Do not use QUnit.reset().",
type: "CallExpression"
}]
}
]
});

0 comments on commit d624a5f

Please sign in to comment.