-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add consistent-spacing-between-blocks
- Loading branch information
Showing
4 changed files
with
169 additions
and
28 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
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,66 @@ | ||
# Require consistent spacing between blocks (`mocha/consistent-spacing-between-blocks`) | ||
|
||
💼 This rule is enabled in the ✅ `recommended` [config](https://github.com/lo1tuma/eslint-plugin-mocha#configs). | ||
|
||
🔧 This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix). | ||
|
||
<!-- end auto-generated rule header --> | ||
|
||
Mocha testing framework provides a structured way of writing tests using functions like `describe`, `it`, `before`, `after`, `beforeEach`, and `afterEach`. As a convention, it is very common to add some spacing between these calls. It's unfortunately also quite common that this spacing is applied inconsistently. | ||
|
||
Example: | ||
|
||
```js | ||
describe("MyComponent", function () { | ||
beforeEach(function () { | ||
// setup code | ||
}); | ||
it("should behave correctly", function () { | ||
// test code | ||
}); | ||
afterEach(function () { | ||
// teardown code | ||
}); | ||
}); | ||
``` | ||
|
||
In this example, there are no line breaks between Mocha function calls, making the code harder to read. | ||
|
||
## Rule Details | ||
|
||
This rule enforces a line break between calls to Mocha functions (before, after, describe, it, beforeEach, afterEach) within describe blocks. | ||
|
||
The following patterns are considered errors: | ||
|
||
```javascript | ||
describe("MyComponent", function () { | ||
beforeEach(function () { | ||
// setup code | ||
}); | ||
it("should behave correctly", function () { | ||
// test code | ||
}); | ||
}); | ||
``` | ||
|
||
These patterns would not be considered errors: | ||
|
||
```javascript | ||
describe("MyComponent", function () { | ||
beforeEach(function () { | ||
// setup code | ||
}); | ||
|
||
it("should behave correctly", function () { | ||
// test code | ||
}); | ||
|
||
afterEach(function () { | ||
// teardown code | ||
}); | ||
}); | ||
``` | ||
|
||
## When Not To Use It | ||
|
||
If you don't prefer this convention. |
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,71 @@ | ||
'use strict'; | ||
|
||
/* eslint "complexity": [ "error", 6 ] */ | ||
|
||
exports.meta = { | ||
type: 'suggestion', | ||
fixable: 'whitespace', | ||
schema: [], | ||
docs: { | ||
description: 'Require consistent spacing between blocks', | ||
url: | ||
'https://github.com/lo1tuma/eslint-plugin-mocha/blob/master/docs/rules/' + | ||
'consistent-spacing-between-blocks.md' | ||
} | ||
}; | ||
|
||
// List of Mocha functions that should have a line break before them. | ||
const MOCHA_FUNCTIONS = [ | ||
'before', | ||
'after', | ||
'describe', | ||
'it', | ||
'beforeEach', | ||
'afterEach' | ||
]; | ||
|
||
// Avoids enforcing line breaks at the beginning of a block. | ||
function isFirstStatementInScope(node) { | ||
return node.parent.parent.body[0] === node.parent; | ||
} | ||
|
||
// Ensure that the rule is applied only within the context of Mocha describe blocks. | ||
function isInsideDescribeBlock(node) { | ||
return ( | ||
node.parent.type === 'ExpressionStatement' && | ||
node.parent.parent.type === 'BlockStatement' && | ||
(node.parent.parent.parent.type === 'ArrowFunctionExpression' || | ||
node.parent.parent.parent.type === 'FunctionExpression') && | ||
node.parent.parent.parent.parent.type === 'CallExpression' && | ||
node.parent.parent.parent.parent.callee.name === 'describe' | ||
); | ||
} | ||
|
||
exports.create = function (context) { | ||
return { | ||
CallExpression(node) { | ||
if ( | ||
!MOCHA_FUNCTIONS.includes(node.callee.name) || | ||
!isInsideDescribeBlock(node) || | ||
isFirstStatementInScope(node) | ||
) { | ||
return; | ||
} | ||
|
||
// Retrieves the token before the current node, skipping comments. | ||
const beforeToken = context.getSourceCode().getTokenBefore(node); | ||
|
||
// And then count the number of lines between the two. | ||
const linesBetween = node.loc.start.line - beforeToken.loc.end.line; | ||
if (linesBetween < 2) { | ||
context.report({ | ||
node, | ||
message: 'Expected line break before this statement.', | ||
fix(fixer) { | ||
return fixer.insertTextAfter(beforeToken, '\n'); | ||
} | ||
}); | ||
} | ||
} | ||
}; | ||
}; |