Skip to content

Commit

Permalink
New: no-global-stop-start rule (refs #20)
Browse files Browse the repository at this point in the history
  • Loading branch information
platinumazure committed Apr 11, 2016
1 parent 8af3dbf commit fb9ef12
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Below is the list of rules available in this plugin.
* [no-global-assertions](./docs/rules/no-global-assertions.md)
* [no-global-expect](./docs/rules/no-global-expect.md)
* [no-global-module-test](./docs/rules/no-global-module-test.md)
* [no-global-stop-start](./docs/rules/no-global-stop-start.md)
* [no-init](./docs/rules/no-init.md)
* [no-negated-ok](./docs/rules/no-negated-ok.md)
* [no-ok-equality](./docs/rules/no-ok-equality.md)
Expand Down
38 changes: 38 additions & 0 deletions docs/rules/no-global-stop-start.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Forbid use of global stop()/start() (no-global-stop-start)

QUnit 2.0 is deprecating and removing all of its global exports, including
`stop()` and `start()`.

## Rule Details

The following patterns are considered warnings:

```js

stop();

start();

```

The following patterns are not warnings:

```js

QUnit.stop();

QUnit.start();

var done = assert.async();
done();

```

## When Not To Use It

If you are working in a codebase that will not use QUnit 2.0, this rule can be
safely disabled.

## Further Reading

* [QUnit 2.0 Migration Guide: Stop using stop/start](http://qunitjs.com/upgrade-guide-2.x/#replace-stop-and-start-with-assert-async)
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ module.exports = {
"no-global-assertions": require("./lib/rules/no-global-assertions"),
"no-global-expect": require("./lib/rules/no-global-expect"),
"no-global-module-test": require("./lib/rules/no-global-module-test"),
"no-global-stop-start": require("./lib/rules/no-global-stop-start"),
"no-init": require("./lib/rules/no-init"),
"no-negated-ok": require("./lib/rules/no-negated-ok"),
"no-ok-equality": require("./lib/rules/no-ok-equality"),
Expand All @@ -32,6 +33,7 @@ module.exports = {
"no-global-assertions": 0,
"no-global-expect": 0,
"no-global-module-test": 0,
"no-global-stop-start": 0,
"no-init": 0,
"no-negated-ok": 0,
"no-ok-equality": 1,
Expand Down
45 changes: 45 additions & 0 deletions lib/rules/no-global-stop-start.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* @fileoverview Forbid use of global stop()/start().
* @author Kevin Partington
* @copyright 2016 Kevin Partington. All rights reserved.
* See LICENSE file in root directory for full license.
*/
"use strict";

var utils = require("../utils");

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

module.exports = function (context) {
var MESSAGE = "Unexpected global {{callee}}() call.";

function isGlobalStop(calleeNode) {
return calleeNode.type === "Identifier" && utils.isStop(calleeNode);
}

function isGlobalStart(calleeNode) {
return calleeNode.type === "Identifier" && utils.isStart(calleeNode);
}

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

return {
"CallExpression": function (node) {
if (isGlobalStop(node.callee) || isGlobalStart(node.callee)) {
context.report({
node: node,
message: MESSAGE,
data: {
callee: node.callee.name
}
});
}
}
};
};

module.exports.schema = [];
45 changes: 45 additions & 0 deletions tests/lib/rules/no-global-stop-start.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* @fileoverview Forbid use of global stop()/start().
* @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-global-stop-start"),
RuleTester = require("eslint").RuleTester;


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

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

valid: [
"QUnit.stop();",
"QUnit.start();"
],

invalid: [
{
code: "stop();",
errors: [{
message: "Unexpected global stop() call.",
type: "CallExpression"
}]
},
{
code: "start();",
errors: [{
message: "Unexpected global start() call.",
type: "CallExpression"
}]
}
]
});

0 comments on commit fb9ef12

Please sign in to comment.