-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
259 additions
and
291 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 |
---|---|---|
@@ -1,59 +1,31 @@ | ||
# padding-test-blocks | ||
# padding-before-test-blocks | ||
|
||
## Rule Details | ||
|
||
This rule enforces at least one line of padding before test blocks with a describe (newling or comment). | ||
This rule enforces a line of padding before test/it blocks | ||
|
||
Examples of **incorrect** code for this rule: | ||
|
||
```js | ||
describe('foo', () => { | ||
test('foo', () => {}); | ||
test('bar', () => {}); | ||
}); | ||
test('foo', () => {}); | ||
test('bar', () => {}); | ||
``` | ||
|
||
```js | ||
describe('foo', () => { | ||
it('foo', () => {}); | ||
it('bar', () => {}); | ||
}); | ||
``` | ||
|
||
```js | ||
describe('foo', () => { | ||
it('foo', () => {}); | ||
|
||
it('bar', () => {}); | ||
}); | ||
it('foo', () => {}); | ||
it('bar', () => {}); | ||
``` | ||
|
||
Examples of **correct** code for this rule: | ||
|
||
```js | ||
describe('foo', () => { | ||
test('foo', () => {}); | ||
|
||
test('bar', () => {}); | ||
}); | ||
``` | ||
|
||
```js | ||
describe('foo', () => { | ||
it('foo', () => {}); | ||
test('foo', () => {}); | ||
|
||
it('bar', () => {}); | ||
}); | ||
test('bar', () => {}); | ||
``` | ||
|
||
```js | ||
describe('foo', () => { | ||
it('foo', () => {}); | ||
it('foo', () => {}); | ||
|
||
it('bar', () => {}); | ||
}); | ||
it('bar', () => {}); | ||
``` | ||
|
||
## When Not To Use It | ||
|
||
If you don't care about spacing between test/it blocks |
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,39 +1,40 @@ | ||
const setPaddingBetweenNodes = ({ | ||
context, | ||
problemNode, | ||
firstNode, | ||
message, | ||
}) => { | ||
context.report({ | ||
node: problemNode, | ||
message, | ||
fix: function(fixer) { | ||
return fixer.insertTextAfter(firstNode, '\n'); | ||
}, | ||
}); | ||
}; | ||
import { Rule } from 'eslint'; | ||
import padding from './rules/padding'; | ||
|
||
const getLeftSibling = node => { | ||
const siblings = node.parent.body; | ||
const nodePosition = siblings.indexOf(node); | ||
return siblings[nodePosition - 1]; | ||
}; | ||
type TokenIdentifier = | ||
| '*' | ||
| 'afterAll' | ||
| 'afterEach' | ||
| 'beforeAll' | ||
| 'beforeEach' | ||
| 'describe' | ||
| 'expect' | ||
| 'it' | ||
| 'test'; | ||
|
||
const getStartLine = node => node && node.loc.start.line; | ||
interface RuleOption { | ||
blankLine: 'always' | 'any' | 'never'; | ||
prev: TokenIdentifier | TokenIdentifier[]; | ||
next: TokenIdentifier | TokenIdentifier[]; | ||
} | ||
|
||
const getEndLine = node => node && node.loc.end.line; | ||
// Returns a rule that wraps a call to padding.create and adds the given options | ||
// to the RuleContext so users don't have to. | ||
export const makeRule = (options: RuleOption[]): Rule.RuleModule => { | ||
return { | ||
meta: { | ||
fixable: 'whitespace', | ||
}, | ||
create(context: Rule.RuleContext) { | ||
// Copy the RuleContext and overwrite options; it's frozen and | ||
// we can't set them directly. | ||
const ctx = Object.create(context, { options: { value: options } }); | ||
|
||
const shouldFixGap = (bottomNode, topNode) => | ||
getStartLine(topNode) - getEndLine(bottomNode) < 2; | ||
// Freeze it again | ||
Object.freeze(ctx); | ||
|
||
export const padBefore = ({ context, node, beforeMessage }) => { | ||
const leftSibling = getLeftSibling(node); | ||
if (leftSibling && shouldFixGap(leftSibling, node)) { | ||
setPaddingBetweenNodes({ | ||
context, | ||
problemNode: node, | ||
firstNode: leftSibling, | ||
message: beforeMessage, | ||
}); | ||
} | ||
// Call the original create method with new context | ||
return padding.create(ctx); | ||
}, | ||
}; | ||
}; |
Oops, something went wrong.