diff --git a/docs/rules/new-line-between-declarations.md b/docs/rules/new-line-between-declarations.md index 5a95415..4539976 100644 --- a/docs/rules/new-line-between-declarations.md +++ b/docs/rules/new-line-between-declarations.md @@ -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 @@ -42,3 +43,12 @@ describe("", function() { }); describe("", function() {}); ``` + +```js +describe("", function() { + var a = 1; + beforeEach(() => {}) + + it("", function(){}); +}); +``` diff --git a/lib/rules/new-line-between-declarations.js b/lib/rules/new-line-between-declarations.js index 1d31f80..c2485e3 100644 --- a/lib/rules/new-line-between-declarations.js +++ b/lib/rules/new-line-between-declarations.js @@ -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) { @@ -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 } /** diff --git a/test/rules/new-line-between-declarations.js b/test/rules/new-line-between-declarations.js index bb11390..50bfb6f 100644 --- a/test/rules/new-line-between-declarations.js +++ b/test/rules/new-line-between-declarations.js @@ -17,6 +17,7 @@ eslintTester.run('space between declarations', rule, { ]), linesToCode([ 'describe("", function() {', + 'var a=1;', 'beforeEach(function(){});', '', 'it("", function(){});', @@ -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() {',