Skip to content

Commit

Permalink
New: no-assert-logical-expression rule (fixes #50)
Browse files Browse the repository at this point in the history
  • Loading branch information
platinumazure committed Aug 28, 2016
1 parent e8a7054 commit 68c68e5
Show file tree
Hide file tree
Showing 5 changed files with 432 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -15,6 +15,7 @@ Below is the list of rules available in this plugin.
* [literal-compare-order](./docs/rules/literal-compare-order.md)
* [no-arrow-tests](./docs/rules/no-arrow-tests.md)
* [no-assert-equal](./docs/rules/no-assert-equal.md)
* [no-assert-logical-expression](./docs/rules/no-assert-logical-expression.md)
* [no-async-in-loops](./docs/rules/no-async-in-loops.md)
* [no-async-test](./docs/rules/no-async-test.md)
* [no-commented-tests](./docs/rules/no-commented-tests.md)
Expand Down
32 changes: 32 additions & 0 deletions docs/rules/no-assert-logical-expression.md
@@ -0,0 +1,32 @@
# Forbid binary logical expressions in assert arguments (no-assert-logical-expression)

Generally, it is not a good idea to use logical expressions as assertion arguments. Logical-and assertions can be broken down into multiple assertions, while logical-or assertions may be indicative of uncertainty or nondeterminism in a test.

## Rule Details

The following patterns are considered warnings:

```js

assert.ok(foo && bar);

assert.ok(foo || bar);

```

The following patterns are not warnings:

```js

// Multiple assertions for logical-and cases
assert.ok(foo);
assert.ok(bar);

// More deterministic test expectation: maybe only foo should be true here
assert.ok(foo);

```

## When Not To Use It

This rule can be safely disabled if you do not care about the quality of test assertions and the determinism of tests.
1 change: 1 addition & 0 deletions index.js
Expand Up @@ -6,6 +6,7 @@ module.exports = {
"literal-compare-order": require("./lib/rules/literal-compare-order"),
"no-arrow-tests": require("./lib/rules/no-arrow-tests"),
"no-assert-equal": require("./lib/rules/no-assert-equal"),
"no-assert-logical-expression": require("./lib/rules/no-assert-logical-expression"),
"no-async-in-loops": require("./lib/rules/no-async-in-loops"),
"no-async-test": require("./lib/rules/no-async-test"),
"no-commented-tests": require("./lib/rules/no-commented-tests"),
Expand Down
84 changes: 84 additions & 0 deletions lib/rules/no-assert-logical-expression.js
@@ -0,0 +1,84 @@
/**
* @fileoverview forbid binary logical expressions in assert arguments
* @author Kevin Partington
*/
"use strict";

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

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

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

module.exports = {
meta: {
docs: {
description: "forbid binary logical expressions in assert arguments",
category: "Best Practices",
recommended: false
},
fixable: null,
schema: []
},

create: function (context) {
var testStack = [],
MESSAGE = "Do not use '{{operator}}' in assertion arguments.";

//----------------------------------------------------------------------
// Helpers
//----------------------------------------------------------------------

function checkAndReport(argNodes) {
argNodes.forEach(function (arg) {
if (arg.type === "LogicalExpression") {
context.report({
node: arg,
message: MESSAGE,
data: {
operator: arg.operator
}
});
}
});
}

function getAssertVar() {
var result = null;

if (testStack.length) {
result = testStack[testStack.length - 1].assertContextVar;
}

return result;
}

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

return {

"CallExpression": function (node) {
if (utils.isTest(node.callee)) {
testStack.push({
assertContextVar: utils.getAssertContextNameForTest(node.arguments)
});
} else if (utils.isAssertion(node.callee, getAssertVar())) {
checkAndReport(node.arguments);
}
},

"CallExpression:exit": function (node) {
if (utils.isTest(node.callee)) {
testStack.pop();
}
}

};
}
};

0 comments on commit 68c68e5

Please sign in to comment.