diff --git a/docs/rules/new-line-between-declarations.md b/docs/rules/new-line-between-declarations.md new file mode 100644 index 0000000..5a95415 --- /dev/null +++ b/docs/rules/new-line-between-declarations.md @@ -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() {}); +``` diff --git a/index.js b/index.js index ee5c385..ab0019f 100644 --- a/index.js +++ b/index.js @@ -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: { @@ -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 } } } diff --git a/lib/rules/new-line-between-declarations.js b/lib/rules/new-line-between-declarations.js new file mode 100644 index 0000000..1d31f80 --- /dev/null +++ b/lib/rules/new-line-between-declarations.js @@ -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 +} diff --git a/test/rules/new-line-between-declarations.js b/test/rules/new-line-between-declarations.js new file mode 100644 index 0000000..bb11390 --- /dev/null +++ b/test/rules/new-line-between-declarations.js @@ -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' + } + ] + } + ] +})