eslint-plugin-jest-formatting 3.0.0 → 4.0.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/AGENTS.md +110 -0
- package/CHANGELOG.md +33 -0
- package/README.md +44 -1
- package/docs/rules/padding-around-after-all-blocks.md +73 -11
- package/docs/rules/padding-around-after-each-blocks.md +67 -11
- package/docs/rules/padding-around-all.md +102 -9
- package/docs/rules/padding-around-before-all-blocks.md +67 -13
- package/docs/rules/padding-around-before-each-blocks.md +65 -11
- package/docs/rules/padding-around-describe-blocks.md +64 -5
- package/docs/rules/padding-around-expect-groups.md +64 -6
- package/docs/rules/padding-around-test-blocks.md +76 -13
- package/lib/ast-utils.js +15 -8
- package/lib/index.js +84 -54
- package/lib/rules/padding.js +47 -21
- package/llms.txt +29 -0
- package/package.json +28 -21
- package/.circleci/config.yml +0 -67
- package/.eslintignore +0 -3
- package/.eslintrc +0 -34
- package/.github/ISSUE_TEMPLATE/bug_report.md +0 -31
- package/.github/ISSUE_TEMPLATE/rule-request.md +0 -21
- package/.prettierignore +0 -4
- package/.prettierrc +0 -4
- package/docs/rules/padding-before-after-all-blocks.md +0 -24
- package/docs/rules/padding-before-after-each-blocks.md +0 -24
- package/docs/rules/padding-before-all.md +0 -15
- package/docs/rules/padding-before-before-all-blocks.md +0 -24
- package/docs/rules/padding-before-before-each-blocks.md +0 -24
- package/docs/rules/padding-before-describe-blocks.md +0 -28
- package/docs/rules/padding-before-expect-statements.md +0 -22
- package/docs/rules/padding-before-test-blocks.md +0 -31
- package/jest.config.js +0 -4
- package/renovate.json +0 -3
- package/src/ast-utils.ts +0 -80
- package/src/index.ts +0 -182
- package/src/rules/padding.ts +0 -357
- package/tests/catfood.js +0 -41
- package/tests/dogfood.spec.js +0 -32
- package/tests/lib/ast-utils.spec.ts +0 -94
- package/tests/lib/rules/padding-around-after-all-blocks.spec.js +0 -115
- package/tests/lib/rules/padding-around-after-each-blocks.spec.js +0 -113
- package/tests/lib/rules/padding-around-before-all-blocks.spec.js +0 -115
- package/tests/lib/rules/padding-around-before-each-blocks.spec.js +0 -115
- package/tests/lib/rules/padding-around-describe-blocks.spec.js +0 -153
- package/tests/lib/rules/padding-around-expect-groups.spec.js +0 -217
- package/tests/lib/rules/padding-around-test-blocks.spec.js +0 -162
- package/tsconfig.json +0 -12
|
@@ -1,29 +1,83 @@
|
|
|
1
1
|
# padding-around-before-each-blocks
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
- **Rule ID:** `jest-formatting/padding-around-before-each-blocks`
|
|
4
|
+
- **Presets:** `recommended`; covered by `padding-around-all` in `strict`
|
|
5
|
+
- **Fixable:** yes, `eslint --fix` inserts the missing blank lines
|
|
6
|
+
- **Options:** none
|
|
7
|
+
- **Message IDs:** `missingPaddingBefore`, `missingPaddingAfter`
|
|
4
8
|
|
|
5
|
-
|
|
9
|
+
## Rule summary
|
|
6
10
|
|
|
7
|
-
|
|
11
|
+
Require a blank line before and after every `beforeEach` call.
|
|
8
12
|
|
|
9
|
-
|
|
13
|
+
- Matches every statement that starts with the identifier `beforeEach`.
|
|
14
|
+
- Only neighbouring statements in the same block are compared: the top level of the file, a `{ ... }` block such as a callback body, or a `switch` case. The first statement in a block needs no blank line before it, and the last needs none after it.
|
|
15
|
+
- One blank line is enough; extra blank lines are never reported.
|
|
16
|
+
- Two consecutive `beforeEach` calls also need a blank line between them.
|
|
17
|
+
- A comment on its own line is not a blank line, but a blank line on either side of a comment satisfies the rule.
|
|
18
|
+
|
|
19
|
+
## Why
|
|
20
|
+
|
|
21
|
+
`beforeEach` resets state before every test in its scope. A blank line on each side marks it as shared per-test setup rather than part of a specific test, which is the first thing a reader needs when working out why a test sees the state it does.
|
|
22
|
+
|
|
23
|
+
## Incorrect examples
|
|
10
24
|
|
|
11
25
|
```js
|
|
12
|
-
|
|
26
|
+
/* eslint jest-formatting/padding-around-before-each-blocks: "error" */
|
|
27
|
+
let counter;
|
|
13
28
|
beforeEach(() => {
|
|
14
|
-
|
|
29
|
+
counter = 0;
|
|
30
|
+
});
|
|
31
|
+
test('starts at zero', () => {
|
|
32
|
+
expect(counter).toBe(0);
|
|
15
33
|
});
|
|
16
|
-
describe('foo', () => {});
|
|
17
34
|
```
|
|
18
35
|
|
|
19
|
-
|
|
36
|
+
ESLint reports:
|
|
37
|
+
|
|
38
|
+
```text
|
|
39
|
+
3:1 error Missing blank line before `beforeEach`. Expected at least one blank line between the previous statement and this `beforeEach` call. Fix: insert an empty line between the statements, before any comments leading the next statement (autofixable with --fix). jest-formatting/padding-around-before-each-blocks
|
|
40
|
+
6:1 error Missing blank line after `beforeEach`. Expected at least one blank line between the `beforeEach` call above and this statement. Fix: insert an empty line between the statements, before any comments leading the next statement (autofixable with --fix). jest-formatting/padding-around-before-each-blocks
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## Correct examples
|
|
20
44
|
|
|
21
45
|
```js
|
|
22
|
-
|
|
46
|
+
/* eslint jest-formatting/padding-around-before-each-blocks: "error" */
|
|
47
|
+
let counter;
|
|
23
48
|
|
|
24
49
|
beforeEach(() => {
|
|
25
|
-
|
|
50
|
+
counter = 0;
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
test('starts at zero', () => {
|
|
54
|
+
expect(counter).toBe(0);
|
|
26
55
|
});
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
## Options
|
|
59
|
+
|
|
60
|
+
This rule has no options; configure only the severity. Passing options makes ESLint reject the configuration, because the rule's schema is empty.
|
|
61
|
+
|
|
62
|
+
```js
|
|
63
|
+
// eslint.config.js (flat); the same `rules` entry works in a legacy .eslintrc
|
|
64
|
+
import jestFormatting from 'eslint-plugin-jest-formatting';
|
|
27
65
|
|
|
28
|
-
|
|
66
|
+
export default [
|
|
67
|
+
{
|
|
68
|
+
plugins: { 'jest-formatting': jestFormatting },
|
|
69
|
+
rules: {
|
|
70
|
+
'jest-formatting/padding-around-before-each-blocks': 'error',
|
|
71
|
+
},
|
|
72
|
+
},
|
|
73
|
+
];
|
|
29
74
|
```
|
|
75
|
+
|
|
76
|
+
## Fixability
|
|
77
|
+
|
|
78
|
+
Fixable (`meta.fixable` is `"whitespace"`). `eslint --fix` resolves every report from this rule by inserting a newline after the last token of the statement before the gap, after any comment that trails on the same line, so that a blank line separates the two statements. The fix only adds whitespace; it never removes, moves, or reformats code.
|
|
79
|
+
|
|
80
|
+
Both messages are reported on the statement that should have a blank line above it. `{{token}}` is the identifier that matched, for example `beforeEach`.
|
|
81
|
+
|
|
82
|
+
- `missingPaddingBefore`, reported on the `beforeEach` call: Missing blank line before `{{token}}`. Expected at least one blank line between the previous statement and this `{{token}}` call. Fix: insert an empty line between the statements, before any comments leading the next statement (autofixable with --fix).
|
|
83
|
+
- `missingPaddingAfter`, reported on the statement after the `beforeEach` call: Missing blank line after `{{token}}`. Expected at least one blank line between the `{{token}}` call above and this statement. Fix: insert an empty line between the statements, before any comments leading the next statement (autofixable with --fix).
|
|
@@ -1,14 +1,29 @@
|
|
|
1
1
|
# padding-around-describe-blocks
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
- **Rule ID:** `jest-formatting/padding-around-describe-blocks`
|
|
4
|
+
- **Presets:** `recommended`; covered by `padding-around-all` in `strict`
|
|
5
|
+
- **Fixable:** yes, `eslint --fix` inserts the missing blank lines
|
|
6
|
+
- **Options:** none
|
|
7
|
+
- **Message IDs:** `missingPaddingBefore`, `missingPaddingAfter`
|
|
4
8
|
|
|
5
|
-
|
|
9
|
+
## Rule summary
|
|
6
10
|
|
|
7
|
-
|
|
11
|
+
Require a blank line before and after every `describe` block, including `fdescribe` and `xdescribe`.
|
|
8
12
|
|
|
9
|
-
|
|
13
|
+
- Matches every statement that starts with `describe`, `fdescribe`, or `xdescribe`, including `describe.each(...)`, `describe.only(...)`, and `describe.skip(...)`.
|
|
14
|
+
- Only neighbouring statements in the same block are compared: the top level of the file, a `{ ... }` block such as a callback body, or a `switch` case. The first statement in a block needs no blank line before it, and the last needs none after it.
|
|
15
|
+
- One blank line is enough; extra blank lines are never reported.
|
|
16
|
+
- Consecutive `describe` blocks need a blank line between them.
|
|
17
|
+
- A comment on its own line is not a blank line, but a blank line on either side of a comment satisfies the rule.
|
|
18
|
+
|
|
19
|
+
## Why
|
|
20
|
+
|
|
21
|
+
`describe` blocks are the outline of a test file. Blank lines around them, and between consecutive blocks, make that outline visible while scrolling, so the boundaries between groups of tests are clear without reading closing braces or indentation.
|
|
22
|
+
|
|
23
|
+
## Incorrect examples
|
|
10
24
|
|
|
11
25
|
```js
|
|
26
|
+
/* eslint jest-formatting/padding-around-describe-blocks: "error" */
|
|
12
27
|
const thing = 123;
|
|
13
28
|
describe('foo', () => {
|
|
14
29
|
// stuff
|
|
@@ -18,9 +33,17 @@ describe('bar', () => {
|
|
|
18
33
|
});
|
|
19
34
|
```
|
|
20
35
|
|
|
21
|
-
|
|
36
|
+
ESLint reports:
|
|
37
|
+
|
|
38
|
+
```text
|
|
39
|
+
3:1 error Missing blank line before `describe`. Expected at least one blank line between the previous statement and this `describe` call. Fix: insert an empty line between the statements, before any comments leading the next statement (autofixable with --fix). jest-formatting/padding-around-describe-blocks
|
|
40
|
+
6:1 error Missing blank line after `describe`. Expected at least one blank line between the `describe` call above and this statement. Fix: insert an empty line between the statements, before any comments leading the next statement (autofixable with --fix). jest-formatting/padding-around-describe-blocks
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## Correct examples
|
|
22
44
|
|
|
23
45
|
```js
|
|
46
|
+
/* eslint jest-formatting/padding-around-describe-blocks: "error" */
|
|
24
47
|
const thing = 123;
|
|
25
48
|
|
|
26
49
|
describe('foo', () => {
|
|
@@ -31,3 +54,39 @@ describe('bar', () => {
|
|
|
31
54
|
// more stuff
|
|
32
55
|
});
|
|
33
56
|
```
|
|
57
|
+
|
|
58
|
+
```js
|
|
59
|
+
/* eslint jest-formatting/padding-around-describe-blocks: "error" */
|
|
60
|
+
describe('outer', () => {
|
|
61
|
+
describe('first', () => {});
|
|
62
|
+
|
|
63
|
+
describe.each([1, 2])('case %i', () => {});
|
|
64
|
+
});
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
## Options
|
|
68
|
+
|
|
69
|
+
This rule has no options; configure only the severity. Passing options makes ESLint reject the configuration, because the rule's schema is empty.
|
|
70
|
+
|
|
71
|
+
```js
|
|
72
|
+
// eslint.config.js (flat); the same `rules` entry works in a legacy .eslintrc
|
|
73
|
+
import jestFormatting from 'eslint-plugin-jest-formatting';
|
|
74
|
+
|
|
75
|
+
export default [
|
|
76
|
+
{
|
|
77
|
+
plugins: { 'jest-formatting': jestFormatting },
|
|
78
|
+
rules: {
|
|
79
|
+
'jest-formatting/padding-around-describe-blocks': 'error',
|
|
80
|
+
},
|
|
81
|
+
},
|
|
82
|
+
];
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
## Fixability
|
|
86
|
+
|
|
87
|
+
Fixable (`meta.fixable` is `"whitespace"`). `eslint --fix` resolves every report from this rule by inserting a newline after the last token of the statement before the gap, after any comment that trails on the same line, so that a blank line separates the two statements. The fix only adds whitespace; it never removes, moves, or reformats code.
|
|
88
|
+
|
|
89
|
+
Both messages are reported on the statement that should have a blank line above it. `{{token}}` is the identifier that matched, for example `xdescribe`.
|
|
90
|
+
|
|
91
|
+
- `missingPaddingBefore`, reported on the `describe` call: Missing blank line before `{{token}}`. Expected at least one blank line between the previous statement and this `{{token}}` call. Fix: insert an empty line between the statements, before any comments leading the next statement (autofixable with --fix).
|
|
92
|
+
- `missingPaddingAfter`, reported on the statement after the `describe` call: Missing blank line after `{{token}}`. Expected at least one blank line between the `{{token}}` call above and this statement. Fix: insert an empty line between the statements, before any comments leading the next statement (autofixable with --fix).
|
|
@@ -1,15 +1,29 @@
|
|
|
1
1
|
# padding-around-expect-groups
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
- **Rule ID:** `jest-formatting/padding-around-expect-groups`
|
|
4
|
+
- **Presets:** not in `recommended`; covered by `padding-around-all` in `strict`
|
|
5
|
+
- **Fixable:** yes, `eslint --fix` inserts the missing blank lines
|
|
6
|
+
- **Options:** none
|
|
7
|
+
- **Message IDs:** `missingPaddingBefore`, `missingPaddingAfter`
|
|
4
8
|
|
|
5
|
-
|
|
9
|
+
## Rule summary
|
|
6
10
|
|
|
7
|
-
|
|
8
|
-
it doesn't add/enforce padding between two or more adjacent `expect` statements.
|
|
11
|
+
Require a blank line before and after every group of consecutive `expect` statements, with no blank line required inside the group.
|
|
9
12
|
|
|
10
|
-
|
|
13
|
+
- Matches every statement that starts with `expect`, including `await expect(...)`, `expect.assertions(...)`, and `expect.hasAssertions()`. A `return expect(...)` is a return statement and is not matched.
|
|
14
|
+
- Only neighbouring statements in the same block are compared: the top level of the file, a `{ ... }` block such as a callback body, or a `switch` case. The first statement in a block needs no blank line before it, and the last needs none after it.
|
|
15
|
+
- One blank line is enough; extra blank lines are never reported.
|
|
16
|
+
- Consecutive `expect` statements form one group and need no blank line between them; the group as a whole needs a blank line before it and after it.
|
|
17
|
+
- A comment on its own line is not a blank line, but a blank line on either side of a comment satisfies the rule.
|
|
18
|
+
|
|
19
|
+
## Why
|
|
20
|
+
|
|
21
|
+
Tests read best in arrange, act, assert order. Separating each run of assertions from the code that set up and produced the values makes it obvious which `expect` calls belong to which action, while keeping assertions about the same result together as one group.
|
|
22
|
+
|
|
23
|
+
## Incorrect examples
|
|
11
24
|
|
|
12
25
|
```js
|
|
26
|
+
/* eslint jest-formatting/padding-around-expect-groups: "error" */
|
|
13
27
|
test('thing one', () => {
|
|
14
28
|
let abc = 123;
|
|
15
29
|
expect(abc).toEqual(123);
|
|
@@ -19,9 +33,18 @@ test('thing one', () => {
|
|
|
19
33
|
});
|
|
20
34
|
```
|
|
21
35
|
|
|
22
|
-
|
|
36
|
+
ESLint reports:
|
|
37
|
+
|
|
38
|
+
```text
|
|
39
|
+
4:3 error Missing blank line before `expect`. Expected at least one blank line between the previous statement and this `expect` call. Fix: insert an empty line between the statements, before any comments leading the next statement (autofixable with --fix). jest-formatting/padding-around-expect-groups
|
|
40
|
+
6:3 error Missing blank line after `expect`. Expected at least one blank line between the `expect` call above and this statement. Fix: insert an empty line between the statements, before any comments leading the next statement (autofixable with --fix). jest-formatting/padding-around-expect-groups
|
|
41
|
+
7:3 error Missing blank line before `expect`. Expected at least one blank line between the previous statement and this `expect` call. Fix: insert an empty line between the statements, before any comments leading the next statement (autofixable with --fix). jest-formatting/padding-around-expect-groups
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
## Correct examples
|
|
23
45
|
|
|
24
46
|
```js
|
|
47
|
+
/* eslint jest-formatting/padding-around-expect-groups: "error" */
|
|
25
48
|
test('thing one', () => {
|
|
26
49
|
let abc = 123;
|
|
27
50
|
|
|
@@ -33,3 +56,38 @@ test('thing one', () => {
|
|
|
33
56
|
expect(abc).toEqual(456);
|
|
34
57
|
});
|
|
35
58
|
```
|
|
59
|
+
|
|
60
|
+
```js
|
|
61
|
+
/* eslint jest-formatting/padding-around-expect-groups: "error" */
|
|
62
|
+
test('resolves', async () => {
|
|
63
|
+
await expect(fetchUser(1)).resolves.toEqual({ id: 1 });
|
|
64
|
+
expect(cache.size).toBe(1);
|
|
65
|
+
});
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
## Options
|
|
69
|
+
|
|
70
|
+
This rule has no options; configure only the severity. Passing options makes ESLint reject the configuration, because the rule's schema is empty.
|
|
71
|
+
|
|
72
|
+
```js
|
|
73
|
+
// eslint.config.js (flat); the same `rules` entry works in a legacy .eslintrc
|
|
74
|
+
import jestFormatting from 'eslint-plugin-jest-formatting';
|
|
75
|
+
|
|
76
|
+
export default [
|
|
77
|
+
{
|
|
78
|
+
plugins: { 'jest-formatting': jestFormatting },
|
|
79
|
+
rules: {
|
|
80
|
+
'jest-formatting/padding-around-expect-groups': 'error',
|
|
81
|
+
},
|
|
82
|
+
},
|
|
83
|
+
];
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
## Fixability
|
|
87
|
+
|
|
88
|
+
Fixable (`meta.fixable` is `"whitespace"`). `eslint --fix` resolves every report from this rule by inserting a newline after the last token of the statement before the gap, after any comment that trails on the same line, so that a blank line separates the two statements. The fix only adds whitespace; it never removes, moves, or reformats code.
|
|
89
|
+
|
|
90
|
+
Both messages are reported on the statement that should have a blank line above it. `{{token}}` is the identifier that matched, for example `expect`.
|
|
91
|
+
|
|
92
|
+
- `missingPaddingBefore`, reported on the `expect` call: Missing blank line before `{{token}}`. Expected at least one blank line between the previous statement and this `{{token}}` call. Fix: insert an empty line between the statements, before any comments leading the next statement (autofixable with --fix).
|
|
93
|
+
- `missingPaddingAfter`, reported on the statement after the `expect` call: Missing blank line after `{{token}}`. Expected at least one blank line between the `{{token}}` call above and this statement. Fix: insert an empty line between the statements, before any comments leading the next statement (autofixable with --fix).
|
|
@@ -1,39 +1,102 @@
|
|
|
1
1
|
# padding-around-test-blocks
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
- **Rule ID:** `jest-formatting/padding-around-test-blocks`
|
|
4
|
+
- **Presets:** `recommended`; covered by `padding-around-all` in `strict`
|
|
5
|
+
- **Fixable:** yes, `eslint --fix` inserts the missing blank lines
|
|
6
|
+
- **Options:** none
|
|
7
|
+
- **Message IDs:** `missingPaddingBefore`, `missingPaddingAfter`
|
|
4
8
|
|
|
5
|
-
|
|
9
|
+
## Rule summary
|
|
6
10
|
|
|
7
|
-
|
|
11
|
+
Require a blank line before and after every `test` or `it` block, including `fit`, `xit`, and `xtest`.
|
|
8
12
|
|
|
9
|
-
|
|
13
|
+
- Matches every statement that starts with `test`, `it`, `fit`, `xit`, or `xtest`, including `test.each(...)`, `it.only(...)`, `test.skip(...)`, and `test.todo(...)`.
|
|
14
|
+
- Only neighbouring statements in the same block are compared: the top level of the file, a `{ ... }` block such as a callback body, or a `switch` case. The first statement in a block needs no blank line before it, and the last needs none after it.
|
|
15
|
+
- One blank line is enough; extra blank lines are never reported.
|
|
16
|
+
- Consecutive tests need a blank line between them.
|
|
17
|
+
- A comment on its own line is not a blank line, but a blank line on either side of a comment satisfies the rule.
|
|
18
|
+
|
|
19
|
+
## Why
|
|
20
|
+
|
|
21
|
+
Each `test` or `it` block is one self-contained case. A blank line between cases, and between a case and the surrounding setup, makes individual tests easy to find, count, and move, and keeps one test's body from running visually into the next.
|
|
22
|
+
|
|
23
|
+
## Incorrect examples
|
|
10
24
|
|
|
11
25
|
```js
|
|
26
|
+
/* eslint jest-formatting/padding-around-test-blocks: "error" */
|
|
12
27
|
const thing = 123;
|
|
13
28
|
test('foo', () => {});
|
|
14
|
-
test('bar', () => {});
|
|
29
|
+
test.each([1, 2])('bar %i', () => {});
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
ESLint reports:
|
|
33
|
+
|
|
34
|
+
```text
|
|
35
|
+
3:1 error Missing blank line before `test`. Expected at least one blank line between the previous statement and this `test` call. Fix: insert an empty line between the statements, before any comments leading the next statement (autofixable with --fix). jest-formatting/padding-around-test-blocks
|
|
36
|
+
4:1 error Missing blank line after `test`. Expected at least one blank line between the `test` call above and this statement. Fix: insert an empty line between the statements, before any comments leading the next statement (autofixable with --fix). jest-formatting/padding-around-test-blocks
|
|
15
37
|
```
|
|
16
38
|
|
|
17
39
|
```js
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
40
|
+
/* eslint jest-formatting/padding-around-test-blocks: "error" */
|
|
41
|
+
describe('suite', () => {
|
|
42
|
+
const thing = 123;
|
|
43
|
+
it('foo', () => {});
|
|
44
|
+
xit('bar', () => {});
|
|
45
|
+
});
|
|
21
46
|
```
|
|
22
47
|
|
|
23
|
-
|
|
48
|
+
ESLint reports:
|
|
49
|
+
|
|
50
|
+
```text
|
|
51
|
+
4:3 error Missing blank line before `it`. Expected at least one blank line between the previous statement and this `it` call. Fix: insert an empty line between the statements, before any comments leading the next statement (autofixable with --fix). jest-formatting/padding-around-test-blocks
|
|
52
|
+
5:3 error Missing blank line after `it`. Expected at least one blank line between the `it` call above and this statement. Fix: insert an empty line between the statements, before any comments leading the next statement (autofixable with --fix). jest-formatting/padding-around-test-blocks
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
## Correct examples
|
|
24
56
|
|
|
25
57
|
```js
|
|
58
|
+
/* eslint jest-formatting/padding-around-test-blocks: "error" */
|
|
26
59
|
const thing = 123;
|
|
27
60
|
|
|
28
61
|
test('foo', () => {});
|
|
29
62
|
|
|
30
|
-
test('bar', () => {});
|
|
63
|
+
test.each([1, 2])('bar %i', () => {});
|
|
31
64
|
```
|
|
32
65
|
|
|
33
66
|
```js
|
|
34
|
-
|
|
67
|
+
/* eslint jest-formatting/padding-around-test-blocks: "error" */
|
|
68
|
+
describe('suite', () => {
|
|
69
|
+
const thing = 123;
|
|
35
70
|
|
|
36
|
-
it('foo', () => {});
|
|
71
|
+
it('foo', () => {});
|
|
37
72
|
|
|
38
|
-
|
|
73
|
+
xit('bar', () => {});
|
|
74
|
+
});
|
|
39
75
|
```
|
|
76
|
+
|
|
77
|
+
## Options
|
|
78
|
+
|
|
79
|
+
This rule has no options; configure only the severity. Passing options makes ESLint reject the configuration, because the rule's schema is empty.
|
|
80
|
+
|
|
81
|
+
```js
|
|
82
|
+
// eslint.config.js (flat); the same `rules` entry works in a legacy .eslintrc
|
|
83
|
+
import jestFormatting from 'eslint-plugin-jest-formatting';
|
|
84
|
+
|
|
85
|
+
export default [
|
|
86
|
+
{
|
|
87
|
+
plugins: { 'jest-formatting': jestFormatting },
|
|
88
|
+
rules: {
|
|
89
|
+
'jest-formatting/padding-around-test-blocks': 'error',
|
|
90
|
+
},
|
|
91
|
+
},
|
|
92
|
+
];
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
## Fixability
|
|
96
|
+
|
|
97
|
+
Fixable (`meta.fixable` is `"whitespace"`). `eslint --fix` resolves every report from this rule by inserting a newline after the last token of the statement before the gap, after any comment that trails on the same line, so that a blank line separates the two statements. The fix only adds whitespace; it never removes, moves, or reformats code.
|
|
98
|
+
|
|
99
|
+
Both messages are reported on the statement that should have a blank line above it. `{{token}}` is the identifier that matched, for example `xit`.
|
|
100
|
+
|
|
101
|
+
- `missingPaddingBefore`, reported on the `test` or `it` call: Missing blank line before `{{token}}`. Expected at least one blank line between the previous statement and this `{{token}}` call. Fix: insert an empty line between the statements, before any comments leading the next statement (autofixable with --fix).
|
|
102
|
+
- `missingPaddingAfter`, reported on the statement after the `test` or `it` call: Missing blank line after `{{token}}`. Expected at least one blank line between the `{{token}}` call above and this statement. Fix: insert an empty line between the statements, before any comments leading the next statement (autofixable with --fix).
|
package/lib/ast-utils.js
CHANGED
|
@@ -1,36 +1,42 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.getPaddingLineSequences = exports.getActualLastToken = exports.isValidParent = exports.areTokensOnSameLine = exports.isTokenASemicolon = void 0;
|
|
4
|
-
|
|
5
|
-
exports.
|
|
4
|
+
const isTokenASemicolon = (token) => token.value === ';' && token.type === 'Punctuator';
|
|
5
|
+
exports.isTokenASemicolon = isTokenASemicolon;
|
|
6
|
+
const areTokensOnSameLine = (left, right) => left.loc.end.line === right.loc.start.line;
|
|
7
|
+
exports.areTokensOnSameLine = areTokensOnSameLine;
|
|
6
8
|
const STATEMENT_LIST_PARENTS = new Set([
|
|
7
9
|
'Program',
|
|
8
10
|
'BlockStatement',
|
|
9
11
|
'SwitchCase',
|
|
10
12
|
'SwitchStatement',
|
|
11
13
|
]);
|
|
12
|
-
|
|
14
|
+
const isValidParent = (parentType) => {
|
|
13
15
|
return STATEMENT_LIST_PARENTS.has(parentType);
|
|
14
16
|
};
|
|
15
|
-
exports.
|
|
17
|
+
exports.isValidParent = isValidParent;
|
|
18
|
+
const getActualLastToken = (sourceCode, node) => {
|
|
16
19
|
const semiToken = sourceCode.getLastToken(node);
|
|
17
20
|
const prevToken = sourceCode.getTokenBefore(semiToken);
|
|
18
21
|
const nextToken = sourceCode.getTokenAfter(semiToken);
|
|
19
22
|
const isSemicolonLessStyle = Boolean(prevToken &&
|
|
20
23
|
nextToken &&
|
|
21
24
|
prevToken.range[0] >= node.range[0] &&
|
|
22
|
-
exports.isTokenASemicolon(semiToken) &&
|
|
25
|
+
(0, exports.isTokenASemicolon)(semiToken) &&
|
|
23
26
|
semiToken.loc.start.line !== prevToken.loc.end.line &&
|
|
24
27
|
semiToken.loc.end.line === nextToken.loc.start.line);
|
|
25
28
|
return isSemicolonLessStyle ? prevToken : semiToken;
|
|
26
29
|
};
|
|
27
|
-
exports.
|
|
30
|
+
exports.getActualLastToken = getActualLastToken;
|
|
31
|
+
const getPaddingLineSequences = (prevNode, nextNode, sourceCode) => {
|
|
28
32
|
const pairs = [];
|
|
29
33
|
const includeComments = true;
|
|
30
|
-
let prevToken = exports.getActualLastToken(sourceCode, prevNode);
|
|
34
|
+
let prevToken = (0, exports.getActualLastToken)(sourceCode, prevNode);
|
|
31
35
|
if (nextNode.loc.start.line - prevToken.loc.end.line >= 2) {
|
|
32
36
|
do {
|
|
33
|
-
const token = sourceCode.getTokenAfter(prevToken, {
|
|
37
|
+
const token = sourceCode.getTokenAfter(prevToken, {
|
|
38
|
+
includeComments,
|
|
39
|
+
});
|
|
34
40
|
if (token.loc.start.line - prevToken.loc.end.line >= 2) {
|
|
35
41
|
pairs.push([prevToken, token]);
|
|
36
42
|
}
|
|
@@ -39,3 +45,4 @@ exports.getPaddingLineSequences = (prevNode, nextNode, sourceCode) => {
|
|
|
39
45
|
}
|
|
40
46
|
return pairs;
|
|
41
47
|
};
|
|
48
|
+
exports.getPaddingLineSequences = getPaddingLineSequences;
|
package/lib/index.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.configs = exports.rules = void 0;
|
|
4
2
|
const padding_1 = require("./rules/padding");
|
|
3
|
+
const packageJson = require('../package.json');
|
|
4
|
+
const packageName = packageJson.name;
|
|
5
|
+
const packageVersion = packageJson.version;
|
|
5
6
|
const paddingConfigs = {
|
|
6
7
|
afterAll: [
|
|
7
8
|
{
|
|
@@ -113,58 +114,87 @@ const paddingConfigs = {
|
|
|
113
114
|
},
|
|
114
115
|
],
|
|
115
116
|
};
|
|
116
|
-
|
|
117
|
-
'padding-around-after-all-blocks':
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
'padding-around-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
'jest-formatting/padding-around-before-each-blocks': 2,
|
|
145
|
-
'jest-formatting/padding-around-describe-blocks': 2,
|
|
146
|
-
'jest-formatting/padding-around-test-blocks': 2,
|
|
147
|
-
},
|
|
148
|
-
},
|
|
149
|
-
],
|
|
117
|
+
const ruleDefinitions = {
|
|
118
|
+
'padding-around-after-all-blocks': {
|
|
119
|
+
description: 'Enforce padding around `afterAll` blocks',
|
|
120
|
+
configs: paddingConfigs.afterAll,
|
|
121
|
+
},
|
|
122
|
+
'padding-around-after-each-blocks': {
|
|
123
|
+
description: 'Enforce padding around `afterEach` blocks',
|
|
124
|
+
configs: paddingConfigs.afterEach,
|
|
125
|
+
},
|
|
126
|
+
'padding-around-before-all-blocks': {
|
|
127
|
+
description: 'Enforce padding around `beforeAll` blocks',
|
|
128
|
+
configs: paddingConfigs.beforeAll,
|
|
129
|
+
},
|
|
130
|
+
'padding-around-before-each-blocks': {
|
|
131
|
+
description: 'Enforce padding around `beforeEach` blocks',
|
|
132
|
+
configs: paddingConfigs.beforeEach,
|
|
133
|
+
},
|
|
134
|
+
'padding-around-describe-blocks': {
|
|
135
|
+
description: 'Enforce padding around `describe` blocks',
|
|
136
|
+
configs: paddingConfigs.describe,
|
|
137
|
+
},
|
|
138
|
+
'padding-around-expect-groups': {
|
|
139
|
+
description: 'Enforce padding around groups of `expect` statements',
|
|
140
|
+
configs: paddingConfigs.expect,
|
|
141
|
+
},
|
|
142
|
+
'padding-around-test-blocks': {
|
|
143
|
+
description: 'Enforce padding around `test` and `it` blocks',
|
|
144
|
+
configs: paddingConfigs.test,
|
|
150
145
|
},
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
{
|
|
155
|
-
files: [
|
|
156
|
-
'**/*.test.*',
|
|
157
|
-
'**/*_test.*',
|
|
158
|
-
'**/*Test.*',
|
|
159
|
-
'**/*.spec.*',
|
|
160
|
-
'**/*_spec.*',
|
|
161
|
-
'**/*Spec.*',
|
|
162
|
-
'**/__tests__/*',
|
|
163
|
-
],
|
|
164
|
-
rules: {
|
|
165
|
-
'jest-formatting/padding-around-all': 2,
|
|
166
|
-
},
|
|
167
|
-
},
|
|
168
|
-
],
|
|
146
|
+
'padding-around-all': {
|
|
147
|
+
description: 'Enforce padding around all Jest blocks covered by the other `padding-around-*` rules',
|
|
148
|
+
configs: [].concat(...Object.keys(paddingConfigs).map((k) => paddingConfigs[k])),
|
|
169
149
|
},
|
|
170
150
|
};
|
|
151
|
+
const rules = {};
|
|
152
|
+
Object.keys(ruleDefinitions).forEach((name) => {
|
|
153
|
+
rules[name] = (0, padding_1.createRule)(Object.assign({ name }, ruleDefinitions[name]));
|
|
154
|
+
});
|
|
155
|
+
const testFileGlobs = [
|
|
156
|
+
'**/*.test.*',
|
|
157
|
+
'**/*_test.*',
|
|
158
|
+
'**/*Test.*',
|
|
159
|
+
'**/*.spec.*',
|
|
160
|
+
'**/*_spec.*',
|
|
161
|
+
'**/*Spec.*',
|
|
162
|
+
'**/__tests__/*',
|
|
163
|
+
];
|
|
164
|
+
const recommendedRules = {
|
|
165
|
+
'jest-formatting/padding-around-after-all-blocks': 2,
|
|
166
|
+
'jest-formatting/padding-around-after-each-blocks': 2,
|
|
167
|
+
'jest-formatting/padding-around-before-all-blocks': 2,
|
|
168
|
+
'jest-formatting/padding-around-before-each-blocks': 2,
|
|
169
|
+
'jest-formatting/padding-around-describe-blocks': 2,
|
|
170
|
+
'jest-formatting/padding-around-test-blocks': 2,
|
|
171
|
+
};
|
|
172
|
+
const strictRules = {
|
|
173
|
+
'jest-formatting/padding-around-all': 2,
|
|
174
|
+
};
|
|
175
|
+
const plugin = {
|
|
176
|
+
meta: { name: packageName, version: packageVersion },
|
|
177
|
+
rules,
|
|
178
|
+
configs: {},
|
|
179
|
+
};
|
|
180
|
+
const createLegacyConfig = (configRules) => ({
|
|
181
|
+
plugins: ['jest-formatting'],
|
|
182
|
+
overrides: [
|
|
183
|
+
{
|
|
184
|
+
files: [...testFileGlobs],
|
|
185
|
+
rules: configRules,
|
|
186
|
+
},
|
|
187
|
+
],
|
|
188
|
+
});
|
|
189
|
+
const createFlatConfig = (configName, configRules) => ({
|
|
190
|
+
name: `jest-formatting/${configName}`,
|
|
191
|
+
plugins: { 'jest-formatting': plugin },
|
|
192
|
+
rules: configRules,
|
|
193
|
+
});
|
|
194
|
+
plugin.configs = {
|
|
195
|
+
recommended: createLegacyConfig(recommendedRules),
|
|
196
|
+
strict: createLegacyConfig(strictRules),
|
|
197
|
+
'flat/recommended': createFlatConfig('recommended', recommendedRules),
|
|
198
|
+
'flat/strict': createFlatConfig('strict', strictRules),
|
|
199
|
+
};
|
|
200
|
+
module.exports = plugin;
|