eslint-plugin-jest-formatting 0.1.0 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintrc +5 -5
- package/CHANGELOG.md +28 -6
- package/README.md +29 -9
- package/docs/rules/padding-around-after-all-blocks.md +25 -0
- package/docs/rules/padding-around-after-each-blocks.md +29 -0
- package/docs/rules/padding-around-all.md +13 -0
- package/docs/rules/padding-around-before-all-blocks.md +29 -0
- package/docs/rules/padding-around-before-each-blocks.md +29 -0
- package/docs/rules/padding-around-describe-blocks.md +33 -0
- package/docs/rules/padding-around-expect-groups.md +35 -0
- package/docs/rules/padding-around-test-blocks.md +39 -0
- package/lib/ast-utils.js +40 -0
- package/lib/index.js +238 -38
- package/lib/rules/padding.js +130 -158
- package/package.json +4 -2
- package/src/ast-utils.ts +78 -0
- package/src/index.ts +282 -43
- package/src/rules/padding.ts +261 -252
- package/tests/catfood.js +39 -0
- package/tests/dogfood.spec.js +16 -1
- package/tests/lib/ast-utils.spec.ts +94 -0
- package/tests/lib/rules/padding-around-after-all-blocks.spec.js +114 -0
- package/tests/lib/rules/padding-around-after-each-blocks.spec.js +112 -0
- package/tests/lib/rules/padding-around-before-all-blocks.spec.js +114 -0
- package/tests/lib/rules/padding-around-before-each-blocks.spec.js +114 -0
- package/tests/lib/rules/padding-around-describe-blocks.spec.js +152 -0
- package/tests/lib/rules/padding-around-expect-groups.spec.js +154 -0
- package/tests/lib/rules/padding-around-test-blocks.spec.js +161 -0
- package/tests/lib/rules/padding-before-after-all-blocks.spec.js +1 -7
- package/tests/lib/rules/padding-before-after-each-blocks.spec.js +1 -7
- package/tests/lib/rules/padding-before-before-all-blocks.spec.js +1 -7
- package/tests/lib/rules/padding-before-before-each-blocks.spec.js +1 -7
- package/tests/lib/rules/padding-before-describe-blocks.spec.js +1 -7
- package/tests/lib/rules/padding-before-expect-statements.spec.js +1 -7
- package/tests/lib/rules/padding-before-test-blocks.spec.js +1 -7
- package/tsconfig.json +1 -1
- package/yarn.lock +12 -10
- package/lib/utils.js +0 -15
- package/src/utils.ts +0 -40
package/.eslintrc
CHANGED
|
@@ -4,14 +4,14 @@
|
|
|
4
4
|
"sourceType": "module"
|
|
5
5
|
},
|
|
6
6
|
"plugins": ["jest-formatting", "jest"],
|
|
7
|
-
"rules": {
|
|
8
|
-
"jest-formatting/padding-before-test-blocks": 2,
|
|
9
|
-
"jest-formatting/padding-before-describe-blocks": 2
|
|
10
|
-
},
|
|
11
7
|
"env": {
|
|
12
8
|
"jest/globals": true
|
|
13
9
|
},
|
|
14
|
-
"extends": [
|
|
10
|
+
"extends": [
|
|
11
|
+
"airbnb-base",
|
|
12
|
+
"plugin:prettier/recommended",
|
|
13
|
+
"plugin:jest-formatting/recommended"
|
|
14
|
+
],
|
|
15
15
|
"overrides": [
|
|
16
16
|
{
|
|
17
17
|
"files": ["**.*"],
|
package/CHANGELOG.md
CHANGED
|
@@ -1,20 +1,42 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## v1.1.0 - August 14, 2019
|
|
4
|
+
|
|
5
|
+
### Changed
|
|
6
|
+
|
|
7
|
+
- Removed `padding-around-expect-groups` from the "recommended" set of rules.
|
|
8
|
+
|
|
9
|
+
### Added
|
|
10
|
+
|
|
11
|
+
- Exposes "strict" rules for eslint `extends`. This preset is equivalent to version `1.0.0`'s "recommended" preset.
|
|
12
|
+
|
|
13
|
+
## v1.0.0 - August 12, 2019
|
|
14
|
+
|
|
15
|
+
### Added
|
|
16
|
+
|
|
17
|
+
- Exposes "recommended" rules for eslint `extends`
|
|
18
|
+
|
|
19
|
+
- Improve documentation
|
|
20
|
+
|
|
21
|
+
### Deprecated
|
|
22
|
+
|
|
23
|
+
- The `padding-before-all` rule has been deprecated
|
|
24
|
+
|
|
3
25
|
## v0.1.0 - July 20, 2019
|
|
4
26
|
|
|
5
27
|
### Rules Added
|
|
6
28
|
|
|
7
|
-
- padding-before-all
|
|
29
|
+
- `padding-before-all`
|
|
8
30
|
|
|
9
|
-
- padding-before-after-all-blocks
|
|
31
|
+
- `padding-before-after-all-blocks`
|
|
10
32
|
|
|
11
|
-
- padding-before-after-each-blocks
|
|
33
|
+
- `padding-before-after-each-blocks`
|
|
12
34
|
|
|
13
|
-
- padding-before-before-all-blocks
|
|
35
|
+
- `padding-before-before-all-blocks`
|
|
14
36
|
|
|
15
|
-
- padding-before-before-each-blocks
|
|
37
|
+
- `padding-before-before-each-blocks`
|
|
16
38
|
|
|
17
|
-
- padding-before-expect-statements
|
|
39
|
+
- `padding-before-expect-statements`
|
|
18
40
|
|
|
19
41
|
### Other
|
|
20
42
|
|
package/README.md
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
[](https://circleci.com/gh/dangreenisrael/eslint-plugin-jest-formatting/tree/master)
|
|
2
2
|

|
|
3
3
|
|
|
4
|
+
**_Note: The master version may not be the version deployed to npm. Please treat https://www.npmjs.com/package/eslint-plugin-jest-formatting as the cannonical source for docs._**
|
|
5
|
+
|
|
4
6
|
# eslint-plugin-jest-formatting
|
|
5
7
|
|
|
6
8
|
This project aims to provide formatting rules (auto-fixable where possible) to ensure consistency and readability in jest test suites.
|
|
@@ -12,7 +14,7 @@ Like this plugin? [Say thanks with a ⭐️](https://github.com/dangreenisrael/e
|
|
|
12
14
|
You'll first need to install [ESLint](http://eslint.org):
|
|
13
15
|
|
|
14
16
|
```
|
|
15
|
-
$ yarn add eslint
|
|
17
|
+
$ yarn add eslint --dev
|
|
16
18
|
```
|
|
17
19
|
|
|
18
20
|
Next, install `eslint-plugin-jest-formatting`:
|
|
@@ -38,30 +40,48 @@ Then configure the rules you want to use under the rules section.
|
|
|
38
40
|
```json
|
|
39
41
|
{
|
|
40
42
|
"rules": {
|
|
41
|
-
"jest-formatting/padding-
|
|
43
|
+
"jest-formatting/padding-around-describe-blocks": 2,
|
|
44
|
+
"jest-formatting/padding-around-test-blocks": 2
|
|
42
45
|
}
|
|
43
46
|
}
|
|
44
47
|
```
|
|
45
48
|
|
|
46
49
|
_or_
|
|
47
50
|
|
|
51
|
+
You can use our "recommended" settings which enables most of the rules for you
|
|
52
|
+
|
|
48
53
|
```json
|
|
49
54
|
{
|
|
50
|
-
"
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
55
|
+
"extends": ["plugin:jest-formatting/recommended"]
|
|
56
|
+
}
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
We also support a "strict" settings which enabled all of the rules for you
|
|
60
|
+
|
|
61
|
+
```json
|
|
62
|
+
{
|
|
63
|
+
"extends": ["plugin:jest-formatting/strict"]
|
|
54
64
|
}
|
|
55
65
|
```
|
|
56
66
|
|
|
57
67
|
## Rule Documentation
|
|
58
68
|
|
|
59
|
-
- [padding-
|
|
69
|
+
- [padding-around-after-all-blocks](docs/rules/padding-around-after-all-blocks.md)
|
|
70
|
+
- [padding-around-after-each-blocks](docs/rules/padding-around-after-each-blocks.md)
|
|
71
|
+
- [padding-around-before-all-blocks](docs/rules/padding-around-before-all-blocks.md)
|
|
72
|
+
- [padding-around-before-each-blocks](docs/rules/padding-around-before-each-blocks.md)
|
|
73
|
+
- [padding-around-expect-groups](docs/rules/padding-around-expect-groups.md)
|
|
74
|
+
- [padding-around-describe-blocks](docs/rules/padding-around-describe-blocks.md)
|
|
75
|
+
- [padding-around-test-blocks](docs/rules/padding-around-test-blocks.md)
|
|
76
|
+
- [padding-around-all](docs/rules/padding-around-all.md)
|
|
60
77
|
|
|
61
|
-
|
|
78
|
+
### Deprecated
|
|
79
|
+
|
|
80
|
+
- [padding-before-all](docs/rules/padding-before-all.md)
|
|
81
|
+
- [padding-before-before-each-blocks](docs/rules/padding-before-before-each-blocks.md)
|
|
62
82
|
- [padding-before-after-each-blocks](docs/rules/padding-before-after-each-blocks.md)
|
|
63
83
|
- [padding-before-before-all-blocks](docs/rules/padding-before-before-all-blocks.md)
|
|
64
|
-
- [padding-before-
|
|
84
|
+
- [padding-before-after-all-blocks](docs/rules/padding-before-after-all-blocks.md)
|
|
65
85
|
- [padding-before-describe-blocks](docs/rules/padding-before-describe-blocks.md)
|
|
66
86
|
- [padding-before-expect-statements](docs/rules/padding-before-expect-statements.md)
|
|
67
87
|
- [padding-before-test-blocks](docs/rules/padding-before-test-blocks.md)
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# padding-around-after-all-blocks
|
|
2
|
+
|
|
3
|
+
## Rule Details
|
|
4
|
+
|
|
5
|
+
This rule enforces a line of padding before _and_ after 1 or more `afterAll` statements.
|
|
6
|
+
|
|
7
|
+
Note that it doesn't add/enforce a padding line if it's the last statement in its scope.
|
|
8
|
+
|
|
9
|
+
Examples of **incorrect** code for this rule:
|
|
10
|
+
|
|
11
|
+
```js
|
|
12
|
+
const someText = 'abc';
|
|
13
|
+
afterAll(() => {});
|
|
14
|
+
describe('someText', () => {});
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
Examples of **correct** code for this rule:
|
|
18
|
+
|
|
19
|
+
```js
|
|
20
|
+
const someText = 'abc';
|
|
21
|
+
|
|
22
|
+
afterAll(() => {});
|
|
23
|
+
|
|
24
|
+
describe('someText', () => {});
|
|
25
|
+
```
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# padding-around-after-each-blocks
|
|
2
|
+
|
|
3
|
+
## Rule Details
|
|
4
|
+
|
|
5
|
+
This rule enforces a line of padding before _and_ after 1 or more `afterEach` statements.
|
|
6
|
+
|
|
7
|
+
Note that it doesn't add/enforce a padding line if it's the last statement in its scope.
|
|
8
|
+
|
|
9
|
+
Examples of **incorrect** code for this rule:
|
|
10
|
+
|
|
11
|
+
```js
|
|
12
|
+
const something = 123;
|
|
13
|
+
afterEach(() => {
|
|
14
|
+
// more stuff
|
|
15
|
+
});
|
|
16
|
+
describe('foo', () => {});
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
Examples of **correct** code for this rule:
|
|
20
|
+
|
|
21
|
+
```js
|
|
22
|
+
const something = 123;
|
|
23
|
+
|
|
24
|
+
afterEach(() => {
|
|
25
|
+
// more stuff
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
describe('foo', () => {});
|
|
29
|
+
```
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# padding-around-all
|
|
2
|
+
|
|
3
|
+
## Rule Details
|
|
4
|
+
|
|
5
|
+
This is a meta rule that simply enables all of the following rules:
|
|
6
|
+
|
|
7
|
+
- [padding-around-after-all-blocks](padding-around-after-all-blocks.md)
|
|
8
|
+
- [padding-around-after-each-blocks](padding-around-after-each-blocks.md)
|
|
9
|
+
- [padding-around-before-all-blocks](padding-around-before-all-blocks.md)
|
|
10
|
+
- [padding-around-before-each-blocks](padding-around-before-each-blocks.md)
|
|
11
|
+
- [padding-around-expect-groups](padding-around-expect-groups.md)
|
|
12
|
+
- [padding-around-describe-blocks](padding-around-describe-blocks.md)
|
|
13
|
+
- [padding-around-test-blocks](padding-around-test-blocks.md)
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# padding-around-before-all-blocks
|
|
2
|
+
|
|
3
|
+
## Rule Details
|
|
4
|
+
|
|
5
|
+
This rule enforces a line of padding before _and_ after `beforeAll` statements.
|
|
6
|
+
|
|
7
|
+
Note that it doesn't add/enforce a padding line if it's the last statement in its scope.
|
|
8
|
+
|
|
9
|
+
Examples of **incorrect** code for this rule:
|
|
10
|
+
|
|
11
|
+
```js
|
|
12
|
+
const something = 123;
|
|
13
|
+
beforeAll(() => {
|
|
14
|
+
// more stuff
|
|
15
|
+
});
|
|
16
|
+
describe('foo', () => {});
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
Examples of **correct** code for this rule:
|
|
20
|
+
|
|
21
|
+
```js
|
|
22
|
+
const something = 123;
|
|
23
|
+
|
|
24
|
+
beforeAll(() => {
|
|
25
|
+
// more stuff
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
describe('foo', () => {});
|
|
29
|
+
```
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# padding-around-before-each-blocks
|
|
2
|
+
|
|
3
|
+
## Rule Details
|
|
4
|
+
|
|
5
|
+
This rule enforces a line of padding before _and_ after 1 or more `beforeEach` statements
|
|
6
|
+
|
|
7
|
+
Note that it doesn't add/enforce a padding line if it's the last statement in its scope
|
|
8
|
+
|
|
9
|
+
Examples of **incorrect** code for this rule:
|
|
10
|
+
|
|
11
|
+
```js
|
|
12
|
+
const something = 123;
|
|
13
|
+
beforeEach(() => {
|
|
14
|
+
// more stuff
|
|
15
|
+
});
|
|
16
|
+
describe('foo', () => {});
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
Examples of **correct** code for this rule:
|
|
20
|
+
|
|
21
|
+
```js
|
|
22
|
+
const something = 123;
|
|
23
|
+
|
|
24
|
+
beforeEach(() => {
|
|
25
|
+
// more stuff
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
describe('foo', () => {});
|
|
29
|
+
```
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# padding-around-describe-blocks
|
|
2
|
+
|
|
3
|
+
## Rule Details
|
|
4
|
+
|
|
5
|
+
This rule enforces a line of padding before _and_ after 1 or more `beforeEach` statements
|
|
6
|
+
|
|
7
|
+
Note that it doesn't add/enforce a padding line if it's the last statement in its scope
|
|
8
|
+
|
|
9
|
+
Examples of **incorrect** code for this rule:
|
|
10
|
+
|
|
11
|
+
```js
|
|
12
|
+
const thing = 123;
|
|
13
|
+
describe('foo', () => {
|
|
14
|
+
// stuff
|
|
15
|
+
});
|
|
16
|
+
describe('bar', () => {
|
|
17
|
+
// more stuff
|
|
18
|
+
});
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
Examples of **correct** code for this rule:
|
|
22
|
+
|
|
23
|
+
```js
|
|
24
|
+
const thing = 123;
|
|
25
|
+
|
|
26
|
+
describe('foo', () => {
|
|
27
|
+
// stuff
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
describe('bar', () => {
|
|
31
|
+
// more stuff
|
|
32
|
+
});
|
|
33
|
+
```
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
# padding-around-expect-groups
|
|
2
|
+
|
|
3
|
+
## Rule Details
|
|
4
|
+
|
|
5
|
+
This rule enforces a line of padding before _and_ after 1 or more `expect` statements
|
|
6
|
+
|
|
7
|
+
Note that it doesn't add/enforce a padding line if it's the last statement in its scope and
|
|
8
|
+
it doesn't add/enforce padding between two or more adjacent `expect` statements.
|
|
9
|
+
|
|
10
|
+
Examples of **incorrect** code for this rule:
|
|
11
|
+
|
|
12
|
+
```js
|
|
13
|
+
test('thing one', () => {
|
|
14
|
+
let abc = 123;
|
|
15
|
+
expect(abc).toEqual(123);
|
|
16
|
+
expect(123).toEqual(abc);
|
|
17
|
+
abc = 456;
|
|
18
|
+
expect(abc).toEqual(456);
|
|
19
|
+
});
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
Examples of **correct** code for this rule:
|
|
23
|
+
|
|
24
|
+
```js
|
|
25
|
+
test('thing one', () => {
|
|
26
|
+
let abc = 123;
|
|
27
|
+
|
|
28
|
+
expect(abc).toEqual(123);
|
|
29
|
+
expect(123).toEqual(abc);
|
|
30
|
+
|
|
31
|
+
abc = 456;
|
|
32
|
+
|
|
33
|
+
expect(abc).toEqual(456);
|
|
34
|
+
});
|
|
35
|
+
```
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# padding-around-test-blocks
|
|
2
|
+
|
|
3
|
+
## Rule Details
|
|
4
|
+
|
|
5
|
+
This rule enforces a line of padding before _and_ after 1 or more `test`/`it` statements
|
|
6
|
+
|
|
7
|
+
Note that it doesn't add/enforce a padding line if it's the last statement in its scope
|
|
8
|
+
|
|
9
|
+
Examples of **incorrect** code for this rule:
|
|
10
|
+
|
|
11
|
+
```js
|
|
12
|
+
const thing = 123;
|
|
13
|
+
test('foo', () => {});
|
|
14
|
+
test('bar', () => {});
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
```js
|
|
18
|
+
const thing = 123;
|
|
19
|
+
it('foo', () => {});
|
|
20
|
+
it('bar', () => {});
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
Examples of **correct** code for this rule:
|
|
24
|
+
|
|
25
|
+
```js
|
|
26
|
+
const thing = 123;
|
|
27
|
+
|
|
28
|
+
test('foo', () => {});
|
|
29
|
+
|
|
30
|
+
test('bar', () => {});
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
```js
|
|
34
|
+
const thing = 123;
|
|
35
|
+
|
|
36
|
+
it('foo', () => {});
|
|
37
|
+
|
|
38
|
+
it('bar', () => {});
|
|
39
|
+
```
|
package/lib/ast-utils.js
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isTokenASemicolon = (token) => token.value === ';' && token.type === 'Punctuator';
|
|
4
|
+
exports.areTokensOnSameLine = (left, right) => left.loc.end.line === right.loc.start.line;
|
|
5
|
+
const STATEMENT_LIST_PARENTS = new Set([
|
|
6
|
+
'Program',
|
|
7
|
+
'BlockStatement',
|
|
8
|
+
'SwitchCase',
|
|
9
|
+
'SwitchStatement',
|
|
10
|
+
]);
|
|
11
|
+
exports.isValidParent = (parentType) => {
|
|
12
|
+
return STATEMENT_LIST_PARENTS.has(parentType);
|
|
13
|
+
};
|
|
14
|
+
exports.getActualLastToken = (sourceCode, node) => {
|
|
15
|
+
const semiToken = sourceCode.getLastToken(node);
|
|
16
|
+
const prevToken = sourceCode.getTokenBefore(semiToken);
|
|
17
|
+
const nextToken = sourceCode.getTokenAfter(semiToken);
|
|
18
|
+
const isSemicolonLessStyle = Boolean(prevToken &&
|
|
19
|
+
nextToken &&
|
|
20
|
+
prevToken.range[0] >= node.range[0] &&
|
|
21
|
+
exports.isTokenASemicolon(semiToken) &&
|
|
22
|
+
semiToken.loc.start.line !== prevToken.loc.end.line &&
|
|
23
|
+
semiToken.loc.end.line === nextToken.loc.start.line);
|
|
24
|
+
return isSemicolonLessStyle ? prevToken : semiToken;
|
|
25
|
+
};
|
|
26
|
+
exports.getPaddingLineSequences = (prevNode, nextNode, sourceCode) => {
|
|
27
|
+
const pairs = [];
|
|
28
|
+
const includeComments = true;
|
|
29
|
+
let prevToken = exports.getActualLastToken(sourceCode, prevNode);
|
|
30
|
+
if (nextNode.loc.start.line - prevToken.loc.end.line >= 2) {
|
|
31
|
+
do {
|
|
32
|
+
const token = sourceCode.getTokenAfter(prevToken, { includeComments });
|
|
33
|
+
if (token.loc.start.line - prevToken.loc.end.line >= 2) {
|
|
34
|
+
pairs.push([prevToken, token]);
|
|
35
|
+
}
|
|
36
|
+
prevToken = token;
|
|
37
|
+
} while (prevToken.range[0] < nextNode.range[0]);
|
|
38
|
+
}
|
|
39
|
+
return pairs;
|
|
40
|
+
};
|