Skip to content

Commit

Permalink
[reword] reproduce current rules
Browse files Browse the repository at this point in the history
  • Loading branch information
benkimpel committed Jul 19, 2019
1 parent 4a0d298 commit 27a7c51
Show file tree
Hide file tree
Showing 8 changed files with 259 additions and 291 deletions.
4 changes: 2 additions & 2 deletions docs/rules/padding-before-describe-blocks.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# padding-describe-blocks
# padding-before-describe-blocks

## Rule Details

This rule enforces at least one line of padding before describe blocks (newline or comment).
This rule enforces a line of padding before describe blocks

Examples of **incorrect** code for this rule:

Expand Down
48 changes: 10 additions & 38 deletions docs/rules/padding-before-test-blocks.md
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
13 changes: 7 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@
* @author Dan
*/

import paddingBeforeDescribeBlocks from './rules/padding-before-describe-blocks';
import paddingBeforeTestBlocks from './rules/padding-before-test-blocks';
import { makeRule } from './utils';

//------------------------------------------------------------------------------
// Plugin Definition
//------------------------------------------------------------------------------

// import all rules in lib/rules

export const rules = {
'padding-before-describe-blocks': paddingBeforeDescribeBlocks,
'padding-before-test-blocks': paddingBeforeTestBlocks,
'padding-before-describe-blocks': makeRule([
{ blankLine: 'always', prev: '*', next: 'describe' },
]),
'padding-before-test-blocks': makeRule([
{ blankLine: 'always', prev: '*', next: ['test', 'it'] },
]),
};
48 changes: 0 additions & 48 deletions src/rules/padding-before-describe-blocks.ts

This file was deleted.

49 changes: 0 additions & 49 deletions src/rules/padding-before-test-blocks.ts

This file was deleted.

67 changes: 34 additions & 33 deletions src/utils.ts
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);
},
};
};
Loading

0 comments on commit 27a7c51

Please sign in to comment.