Skip to content

Commit

Permalink
feat: new line between declarations
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 f8ff392 commit 81e754f
Show file tree
Hide file tree
Showing 4 changed files with 179 additions and 2 deletions.
44 changes: 44 additions & 0 deletions docs/rules/new-line-between-declarations.md
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() {});
```
6 changes: 4 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ module.exports = {
'no-assign-spyon': require('./lib/rules/no-assign-spyon'),
'no-unsafe-spy': require('./lib/rules/no-unsafe-spy'),
'no-global-setup': require('./lib/rules/no-global-setup'),
'no-expect-in-setup-teardown': require('./lib/rules/no-expect-in-setup-teardown')
'no-expect-in-setup-teardown': require('./lib/rules/no-expect-in-setup-teardown'),
'new-line-between-declarations': require('./lib/rules/new-line-between-declarations')
},
configs: {
recommended: {
Expand All @@ -29,7 +30,8 @@ module.exports = {
'jasmine/no-assign-spyon': 0,
'jasmine/no-unsafe-spy': 1,
'jasmine/no-global-setup': 2,
'jasmine/no-expect-in-setup-teardown': 1
'jasmine/no-expect-in-setup-teardown': 1,
'jasmine/new-line-between-declarations': 1
}
}
}
Expand Down
54 changes: 54 additions & 0 deletions lib/rules/new-line-between-declarations.js
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
}
77 changes: 77 additions & 0 deletions test/rules/new-line-between-declarations.js
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'
}
]
}
]
})

0 comments on commit 81e754f

Please sign in to comment.