-
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.
Merge pull request #340 from bjornua/master
Add `consistent-spacing-between-blocks` rule
- Loading branch information
Showing
5 changed files
with
296 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,74 @@ | ||
'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, | ||
linesBetween === 0 ? '\n\n' : '\n' | ||
); | ||
} | ||
}); | ||
} | ||
} | ||
}; | ||
}; |
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,124 @@ | ||
'use strict'; | ||
|
||
const { RuleTester } = require('eslint'); | ||
|
||
const rule = require('../../lib/rules/consistent-spacing-between-blocks.js'); | ||
|
||
const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 2020 } }); | ||
|
||
ruleTester.run('require-spacing-between-mocha-calls', rule, { | ||
valid: [ | ||
// Basic describe block | ||
`describe('My Test', () => { | ||
it('does something', () => {}); | ||
});`, | ||
|
||
// Proper line break before each block within describe | ||
`describe('My Test', () => { | ||
it('performs action one', () => {}); | ||
it('performs action two', () => {}); | ||
});`, | ||
|
||
// Nested describe blocks with proper spacing | ||
`describe('Outer block', () => { | ||
describe('Inner block', () => { | ||
it('performs an action', () => {}); | ||
}); | ||
afterEach(() => {}); | ||
});`, | ||
|
||
// Describe block with comments | ||
`describe('My Test With Comments', () => { | ||
it('does something', () => {}); | ||
// Some comment | ||
afterEach(() => {}); | ||
});`, | ||
|
||
// Mocha functions outside of a describe block | ||
`it('does something outside a describe block', () => {}); | ||
afterEach(() => {});` | ||
], | ||
|
||
invalid: [ | ||
// Missing line break between it and afterEach | ||
{ | ||
code: `describe('My Test', function () { | ||
it('does something', () => {}); | ||
afterEach(() => {}); | ||
});`, | ||
output: `describe('My Test', function () { | ||
it('does something', () => {}); | ||
afterEach(() => {}); | ||
});`, | ||
errors: [ | ||
{ | ||
message: 'Expected line break before this statement.', | ||
type: 'CallExpression' | ||
} | ||
] | ||
}, | ||
|
||
// Missing line break between beforeEach and it | ||
{ | ||
code: `describe('My Test', () => { | ||
beforeEach(() => {}); | ||
it('does something', () => {}); | ||
});`, | ||
output: `describe('My Test', () => { | ||
beforeEach(() => {}); | ||
it('does something', () => {}); | ||
});`, | ||
errors: [ | ||
{ | ||
message: 'Expected line break before this statement.', | ||
type: 'CallExpression' | ||
} | ||
] | ||
}, | ||
|
||
// Missing line break after a variable declaration | ||
{ | ||
code: `describe('Variable declaration', () => { | ||
const a = 1; | ||
it('uses a variable', () => {}); | ||
});`, | ||
output: `describe('Variable declaration', () => { | ||
const a = 1; | ||
it('uses a variable', () => {}); | ||
});`, | ||
errors: [ | ||
{ | ||
message: 'Expected line break before this statement.', | ||
type: 'CallExpression' | ||
} | ||
] | ||
}, | ||
|
||
// Blocks on the same line | ||
{ | ||
code: | ||
'describe(\'Same line blocks\', () => {' + | ||
'it(\'block one\', () => {});' + | ||
'it(\'block two\', () => {});' + | ||
'});', | ||
output: | ||
'describe(\'Same line blocks\', () => {' + | ||
'it(\'block one\', () => {});' + | ||
'\n\n' + | ||
'it(\'block two\', () => {});' + | ||
'});', | ||
errors: [ | ||
{ | ||
message: 'Expected line break before this statement.', | ||
type: 'CallExpression' | ||
} | ||
] | ||
} | ||
] | ||
}); |