Skip to content

Commit

Permalink
fix(new-line-before-expect): ok if another expect on the prev line
Browse files Browse the repository at this point in the history
Fixes #141
  • Loading branch information
surdu authored and DianaSuvorova committed Aug 10, 2017
1 parent a0d1347 commit ea41ce9
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
13 changes: 12 additions & 1 deletion docs/rules/new-line-before-expect.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ If `expect` is the first statement inside a test then rule is not enforced.

## Rule details

This rule triggers a **warning** (is set to **1** by default) whenever there is no new line before expect.
This rule triggers a **warning** (is set to **1** by default) whenever there is no new line or another except before expect.

The following pattern is considered a warning:

Expand Down Expand Up @@ -48,3 +48,14 @@ describe("", function() {
});
});
```

```js
describe("", function() {
it("", function() {
var a = 1;

expect(a).toBe(1);
expect(a).not.toBe(0);
});
});
```
11 changes: 11 additions & 0 deletions lib/rules/new-line-before-expect.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,18 @@ var blockRegexp = /^((f|x)?(it|describe))$/

module.exports = function (context) {
var suiteDepth = 0
var lastExpectNode
return {
CallExpression: function (node) {
if (blockRegexp.test(node.callee.name)) {
lastExpectNode = null
suiteDepth++
} else if (node.callee.name === 'expect' && suiteDepth > 0) {
if (lastExpectNode && linesDelta(node, lastExpectNode) === 1) {
lastExpectNode = node
return
}
lastExpectNode = node
const prevToken = context.getSourceCode().getTokenBefore(node)
if (prevToken) {
if (prevToken.type === 'Punctuator' && prevToken.value === '{') {
Expand All @@ -40,3 +47,7 @@ module.exports = function (context) {
}
}
}

function linesDelta (node1, node2) {
return Math.abs(node1.loc.start.line - node2.loc.start.line)
}
8 changes: 8 additions & 0 deletions test/rules/new-line-before-expect.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ eslintTester.run('new line before expect', rule, {
' });',
'});'
]),
linesToCode([
'it("", helper(function() {',
' var a = 1',
'',
' expect(a).toBe(1);',
' expect(a).not.toBe(0);',
'}));'
]),
linesToCode([
'notJasmineTestSuite()',
'expect(a)'
Expand Down

0 comments on commit ea41ce9

Please sign in to comment.