Skip to content

Commit

Permalink
fix: only checking for declaration expression statements
Browse files Browse the repository at this point in the history
  • Loading branch information
Diana Suvorova authored and Nicolas Fernandez committed Jun 15, 2017
1 parent 81e754f commit 4f318f9
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 21 deletions.
10 changes: 10 additions & 0 deletions docs/rules/new-line-between-declarations.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

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.
Declarations are `it`, `beforeEach`, `afterEach`, `beforeAll`, `afterAll`

## Rule details

Expand Down Expand Up @@ -42,3 +43,12 @@ describe("", function() {
});
describe("", function() {});
```

```js
describe("", function() {
var a = 1;
beforeEach(() => {})

it("", function(){});
});
```
19 changes: 13 additions & 6 deletions lib/rules/new-line-between-declarations.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module.exports = function (context) {
CallExpression: function (node) {
var pass = true
if (node.callee.name === 'describe') {
var declarations = getDescribeContent(node)
var declarations = getDescribeDeclarationsContent(node)
pass = declarations.every((token, i) => {
var next = declarations[i + 1]
if (next) {
Expand All @@ -32,15 +32,22 @@ module.exports = function (context) {
}

/**
* Returns list of declaration tokens inside describe
* Returns list of declaration tokens (it, before,after/each,all) inside describe
* @param {Token} describe The first token
* @returns {Token[]} list of tokens inside describe
* @returns {Token[]} list of declaration tokens inside describe
*/
function getDescribeContent (describe) {
function getDescribeDeclarationsContent (describe) {
var declartionsRegexp = /^((before|after)(Each|All))|it$/
var declarations = []
if (describe.arguments && describe.arguments[1] && describe.arguments[1].body.body) {
return describe.arguments[1].body.body
var content = describe.arguments[1].body.body
content.forEach(node => {
if (node.type === 'ExpressionStatement' && node.expression.callee && declartionsRegexp.test(node.expression.callee.name)) {
declarations.push(node)
}
})
}
return []
return declarations
}

/**
Expand Down
24 changes: 9 additions & 15 deletions test/rules/new-line-between-declarations.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ eslintTester.run('space between declarations', rule, {
]),
linesToCode([
'describe("", function() {',
'var a=1;',
'beforeEach(function(){});',
'',
'it("", function(){});',
Expand All @@ -26,24 +27,17 @@ eslintTester.run('space between declarations', rule, {
'describe("", function() {',
'expect(1).toBe(1);',
'});'
]),
linesToCode([
'describe("", function() {',
'describe("", function(){',
'it("", function(){});',
'});',
'it("", function(){});',
'});'
])
],
invalid: [
{
code: linesToCode([
'describe("", function() {',
'describe("", function(){',
'it("", function(){});',
'});',
'it("", function(){});',
'});'
]),
errors: [
{
message: 'No new line between declarations'
}
]
},
{
code: linesToCode([
'describe("", function() {',
Expand Down

0 comments on commit 4f318f9

Please sign in to comment.