Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New: no-assert-logical-expression rule (fixes #50)
- Loading branch information
1 parent
e8a7054
commit 68c68e5
Showing
5 changed files
with
432 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} | ||
|
||
}; | ||
} | ||
}; |
Oops, something went wrong.