-
-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Diana Suvorova
authored and
Nicolas Fernandez
committed
Jun 15, 2017
1 parent
f8ff392
commit 81e754f
Showing
4 changed files
with
179 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# Enforce new line between declarations inside a suite | ||
|
||
Jasmine uses `describe` to begin and name a test suite. | ||
For readability purposes this rule enforces that there is a new line between declarations within a suite. | ||
|
||
## Rule details | ||
|
||
This rule triggers a **warning** (is set to **1** by default) whenever it | ||
encounters declarations not separated by a new line. | ||
|
||
### Block mode (default) | ||
|
||
The following patterns are considered warnings: | ||
|
||
```js | ||
describe("", function() { | ||
it("", function(){}); | ||
it("", function(){}); | ||
}); | ||
``` | ||
|
||
```js | ||
describe("", function() { | ||
beforeEach("", function(){}); | ||
it("", function(){}); | ||
}); | ||
``` | ||
|
||
The following patterns are not warnings: | ||
|
||
```js | ||
describe("", function() { | ||
it("", function(){}); | ||
|
||
it("", function(){}); | ||
}); | ||
``` | ||
|
||
```js | ||
describe("", function() { | ||
it("", function(){}); | ||
}); | ||
describe("", function() {}); | ||
``` |
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,54 @@ | ||
'use strict' | ||
|
||
/** | ||
* @fileoverview Enforce to have a new line between declarations inside describe | ||
* @author Diana Suvorova | ||
*/ | ||
|
||
module.exports = function (context) { | ||
return { | ||
CallExpression: function (node) { | ||
var pass = true | ||
if (node.callee.name === 'describe') { | ||
var declarations = getDescribeContent(node) | ||
pass = declarations.every((token, i) => { | ||
var next = declarations[i + 1] | ||
if (next) { | ||
return isPaddingBetweenTokens(token, next) | ||
} else { | ||
return true | ||
} | ||
}) | ||
} | ||
|
||
if (!pass) { | ||
context.report({ | ||
node, | ||
message: 'No new line between declarations' | ||
}) | ||
}; | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Returns list of declaration tokens inside describe | ||
* @param {Token} describe The first token | ||
* @returns {Token[]} list of tokens inside describe | ||
*/ | ||
function getDescribeContent (describe) { | ||
if (describe.arguments && describe.arguments[1] && describe.arguments[1].body.body) { | ||
return describe.arguments[1].body.body | ||
} | ||
return [] | ||
} | ||
|
||
/** | ||
* Checks if there is padding between two tokens | ||
* @param {Token} first The first token | ||
* @param {Token} second The second token | ||
* @returns {boolean} True if there is at least a line between the tokens | ||
*/ | ||
function isPaddingBetweenTokens (first, second) { | ||
return second.loc.start.line - first.loc.end.line >= 2 | ||
} |
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,77 @@ | ||
'use strict' | ||
|
||
var rule = require('../../lib/rules/new-line-between-declarations') | ||
var linesToCode = require('../helpers/lines_to_code') | ||
var RuleTester = require('eslint').RuleTester | ||
|
||
var eslintTester = new RuleTester() | ||
|
||
eslintTester.run('space between declarations', rule, { | ||
valid: [ | ||
linesToCode([ | ||
'describe("", function() {', | ||
'it("", function(){});', | ||
'', | ||
'it("", function(){});', | ||
'});' | ||
]), | ||
linesToCode([ | ||
'describe("", function() {', | ||
'beforeEach(function(){});', | ||
'', | ||
'it("", function(){});', | ||
'});' | ||
]), | ||
linesToCode([ | ||
'describe("", function() {', | ||
'expect(1).toBe(1);', | ||
'});' | ||
]) | ||
], | ||
invalid: [ | ||
{ | ||
code: linesToCode([ | ||
'describe("", function() {', | ||
'describe("", function(){', | ||
'it("", function(){});', | ||
'});', | ||
'it("", function(){});', | ||
'});' | ||
]), | ||
errors: [ | ||
{ | ||
message: 'No new line between declarations' | ||
} | ||
] | ||
}, | ||
{ | ||
code: linesToCode([ | ||
'describe("", function() {', | ||
'describe("", function(){', | ||
'it("", function(){});', | ||
'});', | ||
'it("", function(){});', | ||
'it("", function(){});', | ||
'});' | ||
]), | ||
errors: [ | ||
{ | ||
message: 'No new line between declarations' | ||
} | ||
] | ||
}, | ||
{ | ||
code: linesToCode([ | ||
'describe("", function() {', | ||
'beforeEach(function(){});', | ||
'it("", function(){});', | ||
'});' | ||
]), | ||
errors: [ | ||
{ | ||
message: 'No new line between declarations' | ||
} | ||
] | ||
} | ||
] | ||
}) |