eslint-plugin-jest 28.7.0 → 28.8.1
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/README.md +63 -55
- package/docs/rules/padding-around-after-all-blocks.md +32 -0
- package/docs/rules/padding-around-after-each-blocks.md +36 -0
- package/docs/rules/padding-around-all.md +18 -0
- package/docs/rules/padding-around-before-all-blocks.md +35 -0
- package/docs/rules/padding-around-before-each-blocks.md +36 -0
- package/docs/rules/padding-around-describe-blocks.md +40 -0
- package/docs/rules/padding-around-expect-groups.md +42 -0
- package/docs/rules/padding-around-test-blocks.md +46 -0
- package/lib/rules/padding-around-after-all-blocks.js +17 -0
- package/lib/rules/padding-around-after-each-blocks.js +17 -0
- package/lib/rules/padding-around-all.js +15 -0
- package/lib/rules/padding-around-before-all-blocks.js +17 -0
- package/lib/rules/padding-around-before-each-blocks.js +17 -0
- package/lib/rules/padding-around-describe-blocks.js +17 -0
- package/lib/rules/padding-around-expect-groups.js +21 -0
- package/lib/rules/padding-around-test-blocks.js +17 -0
- package/lib/rules/prefer-importing-jest-globals.js +1 -1
- package/lib/rules/utils/ast-utils.js +59 -0
- package/lib/rules/utils/padding.js +308 -0
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -317,61 +317,69 @@ Automatically fixable by the
|
|
|
317
317
|
Manually fixable by
|
|
318
318
|
[editor suggestions](https://eslint.org/docs/latest/use/core-concepts#rule-suggestions).
|
|
319
319
|
|
|
320
|
-
| Name
|
|
321
|
-
|
|
|
322
|
-
| [consistent-test-it](docs/rules/consistent-test-it.md)
|
|
323
|
-
| [expect-expect](docs/rules/expect-expect.md)
|
|
324
|
-
| [max-expects](docs/rules/max-expects.md)
|
|
325
|
-
| [max-nested-describe](docs/rules/max-nested-describe.md)
|
|
326
|
-
| [no-alias-methods](docs/rules/no-alias-methods.md)
|
|
327
|
-
| [no-commented-out-tests](docs/rules/no-commented-out-tests.md)
|
|
328
|
-
| [no-conditional-expect](docs/rules/no-conditional-expect.md)
|
|
329
|
-
| [no-conditional-in-test](docs/rules/no-conditional-in-test.md)
|
|
330
|
-
| [no-confusing-set-timeout](docs/rules/no-confusing-set-timeout.md)
|
|
331
|
-
| [no-deprecated-functions](docs/rules/no-deprecated-functions.md)
|
|
332
|
-
| [no-disabled-tests](docs/rules/no-disabled-tests.md)
|
|
333
|
-
| [no-done-callback](docs/rules/no-done-callback.md)
|
|
334
|
-
| [no-duplicate-hooks](docs/rules/no-duplicate-hooks.md)
|
|
335
|
-
| [no-export](docs/rules/no-export.md)
|
|
336
|
-
| [no-focused-tests](docs/rules/no-focused-tests.md)
|
|
337
|
-
| [no-hooks](docs/rules/no-hooks.md)
|
|
338
|
-
| [no-identical-title](docs/rules/no-identical-title.md)
|
|
339
|
-
| [no-interpolation-in-snapshots](docs/rules/no-interpolation-in-snapshots.md)
|
|
340
|
-
| [no-jasmine-globals](docs/rules/no-jasmine-globals.md)
|
|
341
|
-
| [no-large-snapshots](docs/rules/no-large-snapshots.md)
|
|
342
|
-
| [no-mocks-import](docs/rules/no-mocks-import.md)
|
|
343
|
-
| [no-restricted-jest-methods](docs/rules/no-restricted-jest-methods.md)
|
|
344
|
-
| [no-restricted-matchers](docs/rules/no-restricted-matchers.md)
|
|
345
|
-
| [no-standalone-expect](docs/rules/no-standalone-expect.md)
|
|
346
|
-
| [no-test-prefixes](docs/rules/no-test-prefixes.md)
|
|
347
|
-
| [no-test-return-statement](docs/rules/no-test-return-statement.md)
|
|
348
|
-
| [no-untyped-mock-factory](docs/rules/no-untyped-mock-factory.md)
|
|
349
|
-
| [
|
|
350
|
-
| [
|
|
351
|
-
| [
|
|
352
|
-
| [
|
|
353
|
-
| [
|
|
354
|
-
| [
|
|
355
|
-
| [
|
|
356
|
-
| [
|
|
357
|
-
| [prefer-
|
|
358
|
-
| [prefer-
|
|
359
|
-
| [prefer-
|
|
360
|
-
| [prefer-
|
|
361
|
-
| [prefer-
|
|
362
|
-
| [prefer-
|
|
363
|
-
| [prefer-
|
|
364
|
-
| [prefer-
|
|
365
|
-
| [prefer-
|
|
366
|
-
| [prefer-
|
|
367
|
-
| [prefer-
|
|
368
|
-
| [
|
|
369
|
-
| [
|
|
370
|
-
| [
|
|
371
|
-
| [
|
|
372
|
-
| [
|
|
373
|
-
| [
|
|
374
|
-
| [
|
|
320
|
+
| Name | Description | 💼 | ⚠️ | 🔧 | 💡 |
|
|
321
|
+
| :----------------------------------------------------------------------------------- | :------------------------------------------------------------------------ | :-- | :-- | :-- | :-- |
|
|
322
|
+
| [consistent-test-it](docs/rules/consistent-test-it.md) | Enforce `test` and `it` usage conventions | | | 🔧 | |
|
|
323
|
+
| [expect-expect](docs/rules/expect-expect.md) | Enforce assertion to be made in a test body | | ✅ | | |
|
|
324
|
+
| [max-expects](docs/rules/max-expects.md) | Enforces a maximum number assertion calls in a test body | | | | |
|
|
325
|
+
| [max-nested-describe](docs/rules/max-nested-describe.md) | Enforces a maximum depth to nested describe calls | | | | |
|
|
326
|
+
| [no-alias-methods](docs/rules/no-alias-methods.md) | Disallow alias methods | ✅ | 🎨 | 🔧 | |
|
|
327
|
+
| [no-commented-out-tests](docs/rules/no-commented-out-tests.md) | Disallow commented out tests | | ✅ | | |
|
|
328
|
+
| [no-conditional-expect](docs/rules/no-conditional-expect.md) | Disallow calling `expect` conditionally | ✅ | | | |
|
|
329
|
+
| [no-conditional-in-test](docs/rules/no-conditional-in-test.md) | Disallow conditional logic in tests | | | | |
|
|
330
|
+
| [no-confusing-set-timeout](docs/rules/no-confusing-set-timeout.md) | Disallow confusing usages of jest.setTimeout | | | | |
|
|
331
|
+
| [no-deprecated-functions](docs/rules/no-deprecated-functions.md) | Disallow use of deprecated functions | ✅ | | 🔧 | |
|
|
332
|
+
| [no-disabled-tests](docs/rules/no-disabled-tests.md) | Disallow disabled tests | | ✅ | | |
|
|
333
|
+
| [no-done-callback](docs/rules/no-done-callback.md) | Disallow using a callback in asynchronous tests and hooks | ✅ | | | 💡 |
|
|
334
|
+
| [no-duplicate-hooks](docs/rules/no-duplicate-hooks.md) | Disallow duplicate setup and teardown hooks | | | | |
|
|
335
|
+
| [no-export](docs/rules/no-export.md) | Disallow using `exports` in files containing tests | ✅ | | | |
|
|
336
|
+
| [no-focused-tests](docs/rules/no-focused-tests.md) | Disallow focused tests | ✅ | | | 💡 |
|
|
337
|
+
| [no-hooks](docs/rules/no-hooks.md) | Disallow setup and teardown hooks | | | | |
|
|
338
|
+
| [no-identical-title](docs/rules/no-identical-title.md) | Disallow identical titles | ✅ | | | |
|
|
339
|
+
| [no-interpolation-in-snapshots](docs/rules/no-interpolation-in-snapshots.md) | Disallow string interpolation inside snapshots | ✅ | | | |
|
|
340
|
+
| [no-jasmine-globals](docs/rules/no-jasmine-globals.md) | Disallow Jasmine globals | ✅ | | 🔧 | |
|
|
341
|
+
| [no-large-snapshots](docs/rules/no-large-snapshots.md) | Disallow large snapshots | | | | |
|
|
342
|
+
| [no-mocks-import](docs/rules/no-mocks-import.md) | Disallow manually importing from `__mocks__` | ✅ | | | |
|
|
343
|
+
| [no-restricted-jest-methods](docs/rules/no-restricted-jest-methods.md) | Disallow specific `jest.` methods | | | | |
|
|
344
|
+
| [no-restricted-matchers](docs/rules/no-restricted-matchers.md) | Disallow specific matchers & modifiers | | | | |
|
|
345
|
+
| [no-standalone-expect](docs/rules/no-standalone-expect.md) | Disallow using `expect` outside of `it` or `test` blocks | ✅ | | | |
|
|
346
|
+
| [no-test-prefixes](docs/rules/no-test-prefixes.md) | Require using `.only` and `.skip` over `f` and `x` | ✅ | | 🔧 | |
|
|
347
|
+
| [no-test-return-statement](docs/rules/no-test-return-statement.md) | Disallow explicitly returning from tests | | | | |
|
|
348
|
+
| [no-untyped-mock-factory](docs/rules/no-untyped-mock-factory.md) | Disallow using `jest.mock()` factories without an explicit type parameter | | | 🔧 | |
|
|
349
|
+
| [padding-around-after-all-blocks](docs/rules/padding-around-after-all-blocks.md) | Enforce padding around `afterAll` blocks | | | 🔧 | |
|
|
350
|
+
| [padding-around-after-each-blocks](docs/rules/padding-around-after-each-blocks.md) | Enforce padding around `afterEach` blocks | | | 🔧 | |
|
|
351
|
+
| [padding-around-all](docs/rules/padding-around-all.md) | Enforce padding around Jest functions | | | 🔧 | |
|
|
352
|
+
| [padding-around-before-all-blocks](docs/rules/padding-around-before-all-blocks.md) | Enforce padding around `beforeAll` blocks | | | 🔧 | |
|
|
353
|
+
| [padding-around-before-each-blocks](docs/rules/padding-around-before-each-blocks.md) | Enforce padding around `beforeEach` blocks | | | 🔧 | |
|
|
354
|
+
| [padding-around-describe-blocks](docs/rules/padding-around-describe-blocks.md) | Enforce padding around `describe` blocks | | | 🔧 | |
|
|
355
|
+
| [padding-around-expect-groups](docs/rules/padding-around-expect-groups.md) | Enforce padding around `expect` groups | | | 🔧 | |
|
|
356
|
+
| [padding-around-test-blocks](docs/rules/padding-around-test-blocks.md) | Enforce padding around afterAll blocks | | | 🔧 | |
|
|
357
|
+
| [prefer-called-with](docs/rules/prefer-called-with.md) | Suggest using `toBeCalledWith()` or `toHaveBeenCalledWith()` | | | | |
|
|
358
|
+
| [prefer-comparison-matcher](docs/rules/prefer-comparison-matcher.md) | Suggest using the built-in comparison matchers | | | 🔧 | |
|
|
359
|
+
| [prefer-each](docs/rules/prefer-each.md) | Prefer using `.each` rather than manual loops | | | | |
|
|
360
|
+
| [prefer-equality-matcher](docs/rules/prefer-equality-matcher.md) | Suggest using the built-in equality matchers | | | | 💡 |
|
|
361
|
+
| [prefer-expect-assertions](docs/rules/prefer-expect-assertions.md) | Suggest using `expect.assertions()` OR `expect.hasAssertions()` | | | | 💡 |
|
|
362
|
+
| [prefer-expect-resolves](docs/rules/prefer-expect-resolves.md) | Prefer `await expect(...).resolves` over `expect(await ...)` syntax | | | 🔧 | |
|
|
363
|
+
| [prefer-hooks-in-order](docs/rules/prefer-hooks-in-order.md) | Prefer having hooks in a consistent order | | | | |
|
|
364
|
+
| [prefer-hooks-on-top](docs/rules/prefer-hooks-on-top.md) | Suggest having hooks before any test cases | | | | |
|
|
365
|
+
| [prefer-importing-jest-globals](docs/rules/prefer-importing-jest-globals.md) | Prefer importing Jest globals | | | 🔧 | |
|
|
366
|
+
| [prefer-jest-mocked](docs/rules/prefer-jest-mocked.md) | Prefer `jest.mocked()` over `fn as jest.Mock` | | | 🔧 | |
|
|
367
|
+
| [prefer-lowercase-title](docs/rules/prefer-lowercase-title.md) | Enforce lowercase test names | | | 🔧 | |
|
|
368
|
+
| [prefer-mock-promise-shorthand](docs/rules/prefer-mock-promise-shorthand.md) | Prefer mock resolved/rejected shorthands for promises | | | 🔧 | |
|
|
369
|
+
| [prefer-snapshot-hint](docs/rules/prefer-snapshot-hint.md) | Prefer including a hint with external snapshots | | | | |
|
|
370
|
+
| [prefer-spy-on](docs/rules/prefer-spy-on.md) | Suggest using `jest.spyOn()` | | | 🔧 | |
|
|
371
|
+
| [prefer-strict-equal](docs/rules/prefer-strict-equal.md) | Suggest using `toStrictEqual()` | | | | 💡 |
|
|
372
|
+
| [prefer-to-be](docs/rules/prefer-to-be.md) | Suggest using `toBe()` for primitive literals | 🎨 | | 🔧 | |
|
|
373
|
+
| [prefer-to-contain](docs/rules/prefer-to-contain.md) | Suggest using `toContain()` | 🎨 | | 🔧 | |
|
|
374
|
+
| [prefer-to-have-length](docs/rules/prefer-to-have-length.md) | Suggest using `toHaveLength()` | 🎨 | | 🔧 | |
|
|
375
|
+
| [prefer-todo](docs/rules/prefer-todo.md) | Suggest using `test.todo` | | | 🔧 | |
|
|
376
|
+
| [require-hook](docs/rules/require-hook.md) | Require setup and teardown code to be within a hook | | | | |
|
|
377
|
+
| [require-to-throw-message](docs/rules/require-to-throw-message.md) | Require a message for `toThrow()` | | | | |
|
|
378
|
+
| [require-top-level-describe](docs/rules/require-top-level-describe.md) | Require test cases and hooks to be inside a `describe` block | | | | |
|
|
379
|
+
| [valid-describe-callback](docs/rules/valid-describe-callback.md) | Enforce valid `describe()` callback | ✅ | | | |
|
|
380
|
+
| [valid-expect](docs/rules/valid-expect.md) | Enforce valid `expect()` usage | ✅ | | 🔧 | |
|
|
381
|
+
| [valid-expect-in-promise](docs/rules/valid-expect-in-promise.md) | Require promises that have expectations in their chain to be valid | ✅ | | | |
|
|
382
|
+
| [valid-title](docs/rules/valid-title.md) | Enforce valid titles | ✅ | | 🔧 | |
|
|
375
383
|
|
|
376
384
|
### Requires Type Checking
|
|
377
385
|
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
# Enforce padding around `afterAll` blocks (`padding-around-after-all-blocks`)
|
|
2
|
+
|
|
3
|
+
🔧 This rule is automatically fixable by the
|
|
4
|
+
[`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix).
|
|
5
|
+
|
|
6
|
+
<!-- end auto-generated rule header -->
|
|
7
|
+
|
|
8
|
+
## Rule Details
|
|
9
|
+
|
|
10
|
+
This rule enforces a line of padding before _and_ after 1 or more `afterAll`
|
|
11
|
+
statements.
|
|
12
|
+
|
|
13
|
+
Note that it doesn't add/enforce a padding line if it's the last statement in
|
|
14
|
+
its scope.
|
|
15
|
+
|
|
16
|
+
Examples of **incorrect** code for this rule:
|
|
17
|
+
|
|
18
|
+
```js
|
|
19
|
+
const someText = 'abc';
|
|
20
|
+
afterAll(() => {});
|
|
21
|
+
describe('someText', () => {});
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
Examples of **correct** code for this rule:
|
|
25
|
+
|
|
26
|
+
```js
|
|
27
|
+
const someText = 'abc';
|
|
28
|
+
|
|
29
|
+
afterAll(() => {});
|
|
30
|
+
|
|
31
|
+
describe('someText', () => {});
|
|
32
|
+
```
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
# Enforce padding around `afterEach` blocks (`padding-around-after-each-blocks`)
|
|
2
|
+
|
|
3
|
+
🔧 This rule is automatically fixable by the
|
|
4
|
+
[`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix).
|
|
5
|
+
|
|
6
|
+
<!-- end auto-generated rule header -->
|
|
7
|
+
|
|
8
|
+
## Rule Details
|
|
9
|
+
|
|
10
|
+
This rule enforces a line of padding before _and_ after 1 or more `afterEach`
|
|
11
|
+
statements.
|
|
12
|
+
|
|
13
|
+
Note that it doesn't add/enforce a padding line if it's the last statement in
|
|
14
|
+
its scope.
|
|
15
|
+
|
|
16
|
+
Examples of **incorrect** code for this rule:
|
|
17
|
+
|
|
18
|
+
```js
|
|
19
|
+
const something = 123;
|
|
20
|
+
afterEach(() => {
|
|
21
|
+
// more stuff
|
|
22
|
+
});
|
|
23
|
+
describe('foo', () => {});
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
Examples of **correct** code for this rule:
|
|
27
|
+
|
|
28
|
+
```js
|
|
29
|
+
const something = 123;
|
|
30
|
+
|
|
31
|
+
afterEach(() => {
|
|
32
|
+
// more stuff
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
describe('foo', () => {});
|
|
36
|
+
```
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# Enforce padding around Jest functions (`padding-around-all`)
|
|
2
|
+
|
|
3
|
+
🔧 This rule is automatically fixable by the
|
|
4
|
+
[`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix).
|
|
5
|
+
|
|
6
|
+
<!-- end auto-generated rule header -->
|
|
7
|
+
|
|
8
|
+
## Rule Details
|
|
9
|
+
|
|
10
|
+
This is a meta rule that simply enables all of the following rules:
|
|
11
|
+
|
|
12
|
+
- [padding-around-after-all-blocks](padding-around-after-all-blocks.md)
|
|
13
|
+
- [padding-around-after-each-blocks](padding-around-after-each-blocks.md)
|
|
14
|
+
- [padding-around-before-all-blocks](padding-around-before-all-blocks.md)
|
|
15
|
+
- [padding-around-before-each-blocks](padding-around-before-each-blocks.md)
|
|
16
|
+
- [padding-around-expect-groups](padding-around-expect-groups.md)
|
|
17
|
+
- [padding-around-describe-blocks](padding-around-describe-blocks.md)
|
|
18
|
+
- [padding-around-test-blocks](padding-around-test-blocks.md)
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
# Enforce padding around `beforeAll` blocks (`padding-around-before-all-blocks`)
|
|
2
|
+
|
|
3
|
+
🔧 This rule is automatically fixable by the
|
|
4
|
+
[`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix).
|
|
5
|
+
|
|
6
|
+
<!-- end auto-generated rule header -->
|
|
7
|
+
|
|
8
|
+
## Rule Details
|
|
9
|
+
|
|
10
|
+
This rule enforces a line of padding before _and_ after `beforeAll` statements.
|
|
11
|
+
|
|
12
|
+
Note that it doesn't add/enforce a padding line if it's the last statement in
|
|
13
|
+
its scope.
|
|
14
|
+
|
|
15
|
+
Examples of **incorrect** code for this rule:
|
|
16
|
+
|
|
17
|
+
```js
|
|
18
|
+
const something = 123;
|
|
19
|
+
beforeAll(() => {
|
|
20
|
+
// more stuff
|
|
21
|
+
});
|
|
22
|
+
describe('foo', () => {});
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
Examples of **correct** code for this rule:
|
|
26
|
+
|
|
27
|
+
```js
|
|
28
|
+
const something = 123;
|
|
29
|
+
|
|
30
|
+
beforeAll(() => {
|
|
31
|
+
// more stuff
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
describe('foo', () => {});
|
|
35
|
+
```
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
# Enforce padding around `beforeEach` blocks (`padding-around-before-each-blocks`)
|
|
2
|
+
|
|
3
|
+
🔧 This rule is automatically fixable by the
|
|
4
|
+
[`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix).
|
|
5
|
+
|
|
6
|
+
<!-- end auto-generated rule header -->
|
|
7
|
+
|
|
8
|
+
## Rule Details
|
|
9
|
+
|
|
10
|
+
This rule enforces a line of padding before _and_ after 1 or more `beforeEach`
|
|
11
|
+
statements
|
|
12
|
+
|
|
13
|
+
Note that it doesn't add/enforce a padding line if it's the last statement in
|
|
14
|
+
its scope
|
|
15
|
+
|
|
16
|
+
Examples of **incorrect** code for this rule:
|
|
17
|
+
|
|
18
|
+
```js
|
|
19
|
+
const something = 123;
|
|
20
|
+
beforeEach(() => {
|
|
21
|
+
// more stuff
|
|
22
|
+
});
|
|
23
|
+
describe('foo', () => {});
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
Examples of **correct** code for this rule:
|
|
27
|
+
|
|
28
|
+
```js
|
|
29
|
+
const something = 123;
|
|
30
|
+
|
|
31
|
+
beforeEach(() => {
|
|
32
|
+
// more stuff
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
describe('foo', () => {});
|
|
36
|
+
```
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
# Enforce padding around `describe` blocks (`padding-around-describe-blocks`)
|
|
2
|
+
|
|
3
|
+
🔧 This rule is automatically fixable by the
|
|
4
|
+
[`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix).
|
|
5
|
+
|
|
6
|
+
<!-- end auto-generated rule header -->
|
|
7
|
+
|
|
8
|
+
## Rule Details
|
|
9
|
+
|
|
10
|
+
This rule enforces a line of padding before _and_ after 1 or more `describe`
|
|
11
|
+
statements
|
|
12
|
+
|
|
13
|
+
Note that it doesn't add/enforce a padding line if it's the last statement in
|
|
14
|
+
its scope
|
|
15
|
+
|
|
16
|
+
Examples of **incorrect** code for this rule:
|
|
17
|
+
|
|
18
|
+
```js
|
|
19
|
+
const thing = 123;
|
|
20
|
+
describe('foo', () => {
|
|
21
|
+
// stuff
|
|
22
|
+
});
|
|
23
|
+
describe('bar', () => {
|
|
24
|
+
// more stuff
|
|
25
|
+
});
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
Examples of **correct** code for this rule:
|
|
29
|
+
|
|
30
|
+
```js
|
|
31
|
+
const thing = 123;
|
|
32
|
+
|
|
33
|
+
describe('foo', () => {
|
|
34
|
+
// stuff
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
describe('bar', () => {
|
|
38
|
+
// more stuff
|
|
39
|
+
});
|
|
40
|
+
```
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
# Enforce padding around `expect` groups (`padding-around-expect-groups`)
|
|
2
|
+
|
|
3
|
+
🔧 This rule is automatically fixable by the
|
|
4
|
+
[`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix).
|
|
5
|
+
|
|
6
|
+
<!-- end auto-generated rule header -->
|
|
7
|
+
|
|
8
|
+
## Rule Details
|
|
9
|
+
|
|
10
|
+
This rule enforces a line of padding before _and_ after 1 or more `expect`
|
|
11
|
+
statements
|
|
12
|
+
|
|
13
|
+
Note that it doesn't add/enforce a padding line if it's the last statement in
|
|
14
|
+
its scope and it doesn't add/enforce padding between two or more adjacent
|
|
15
|
+
`expect` statements.
|
|
16
|
+
|
|
17
|
+
Examples of **incorrect** code for this rule:
|
|
18
|
+
|
|
19
|
+
```js
|
|
20
|
+
test('thing one', () => {
|
|
21
|
+
let abc = 123;
|
|
22
|
+
expect(abc).toEqual(123);
|
|
23
|
+
expect(123).toEqual(abc);
|
|
24
|
+
abc = 456;
|
|
25
|
+
expect(abc).toEqual(456);
|
|
26
|
+
});
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
Examples of **correct** code for this rule:
|
|
30
|
+
|
|
31
|
+
```js
|
|
32
|
+
test('thing one', () => {
|
|
33
|
+
let abc = 123;
|
|
34
|
+
|
|
35
|
+
expect(abc).toEqual(123);
|
|
36
|
+
expect(123).toEqual(abc);
|
|
37
|
+
|
|
38
|
+
abc = 456;
|
|
39
|
+
|
|
40
|
+
expect(abc).toEqual(456);
|
|
41
|
+
});
|
|
42
|
+
```
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
# Enforce padding around afterAll blocks (`padding-around-test-blocks`)
|
|
2
|
+
|
|
3
|
+
🔧 This rule is automatically fixable by the
|
|
4
|
+
[`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix).
|
|
5
|
+
|
|
6
|
+
<!-- end auto-generated rule header -->
|
|
7
|
+
|
|
8
|
+
## Rule Details
|
|
9
|
+
|
|
10
|
+
This rule enforces a line of padding before _and_ after 1 or more `test`/`it`
|
|
11
|
+
statements
|
|
12
|
+
|
|
13
|
+
Note that it doesn't add/enforce a padding line if it's the last statement in
|
|
14
|
+
its scope
|
|
15
|
+
|
|
16
|
+
Examples of **incorrect** code for this rule:
|
|
17
|
+
|
|
18
|
+
```js
|
|
19
|
+
const thing = 123;
|
|
20
|
+
test('foo', () => {});
|
|
21
|
+
test('bar', () => {});
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
```js
|
|
25
|
+
const thing = 123;
|
|
26
|
+
it('foo', () => {});
|
|
27
|
+
it('bar', () => {});
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
Examples of **correct** code for this rule:
|
|
31
|
+
|
|
32
|
+
```js
|
|
33
|
+
const thing = 123;
|
|
34
|
+
|
|
35
|
+
test('foo', () => {});
|
|
36
|
+
|
|
37
|
+
test('bar', () => {});
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
```js
|
|
41
|
+
const thing = 123;
|
|
42
|
+
|
|
43
|
+
it('foo', () => {});
|
|
44
|
+
|
|
45
|
+
it('bar', () => {});
|
|
46
|
+
```
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = exports.config = void 0;
|
|
7
|
+
var _padding = require("./utils/padding");
|
|
8
|
+
const config = exports.config = [{
|
|
9
|
+
paddingType: _padding.PaddingType.Always,
|
|
10
|
+
prevStatementType: _padding.StatementType.Any,
|
|
11
|
+
nextStatementType: _padding.StatementType.AfterAllToken
|
|
12
|
+
}, {
|
|
13
|
+
paddingType: _padding.PaddingType.Always,
|
|
14
|
+
prevStatementType: _padding.StatementType.AfterAllToken,
|
|
15
|
+
nextStatementType: _padding.StatementType.Any
|
|
16
|
+
}];
|
|
17
|
+
var _default = exports.default = (0, _padding.createPaddingRule)(__filename, 'Enforce padding around `afterAll` blocks', config);
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = exports.config = void 0;
|
|
7
|
+
var _padding = require("./utils/padding");
|
|
8
|
+
const config = exports.config = [{
|
|
9
|
+
paddingType: _padding.PaddingType.Always,
|
|
10
|
+
prevStatementType: _padding.StatementType.Any,
|
|
11
|
+
nextStatementType: _padding.StatementType.AfterEachToken
|
|
12
|
+
}, {
|
|
13
|
+
paddingType: _padding.PaddingType.Always,
|
|
14
|
+
prevStatementType: _padding.StatementType.AfterEachToken,
|
|
15
|
+
nextStatementType: _padding.StatementType.Any
|
|
16
|
+
}];
|
|
17
|
+
var _default = exports.default = (0, _padding.createPaddingRule)(__filename, 'Enforce padding around `afterEach` blocks', config);
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
var _paddingAroundAfterAllBlocks = require("./padding-around-after-all-blocks");
|
|
8
|
+
var _paddingAroundAfterEachBlocks = require("./padding-around-after-each-blocks");
|
|
9
|
+
var _paddingAroundBeforeAllBlocks = require("./padding-around-before-all-blocks");
|
|
10
|
+
var _paddingAroundBeforeEachBlocks = require("./padding-around-before-each-blocks");
|
|
11
|
+
var _paddingAroundDescribeBlocks = require("./padding-around-describe-blocks");
|
|
12
|
+
var _paddingAroundExpectGroups = require("./padding-around-expect-groups");
|
|
13
|
+
var _paddingAroundTestBlocks = require("./padding-around-test-blocks");
|
|
14
|
+
var _padding = require("./utils/padding");
|
|
15
|
+
var _default = exports.default = (0, _padding.createPaddingRule)(__filename, 'Enforce padding around Jest functions', [..._paddingAroundAfterAllBlocks.config, ..._paddingAroundAfterEachBlocks.config, ..._paddingAroundBeforeAllBlocks.config, ..._paddingAroundBeforeEachBlocks.config, ..._paddingAroundDescribeBlocks.config, ..._paddingAroundExpectGroups.config, ..._paddingAroundTestBlocks.config]);
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = exports.config = void 0;
|
|
7
|
+
var _padding = require("./utils/padding");
|
|
8
|
+
const config = exports.config = [{
|
|
9
|
+
paddingType: _padding.PaddingType.Always,
|
|
10
|
+
prevStatementType: _padding.StatementType.Any,
|
|
11
|
+
nextStatementType: _padding.StatementType.BeforeAllToken
|
|
12
|
+
}, {
|
|
13
|
+
paddingType: _padding.PaddingType.Always,
|
|
14
|
+
prevStatementType: _padding.StatementType.BeforeAllToken,
|
|
15
|
+
nextStatementType: _padding.StatementType.Any
|
|
16
|
+
}];
|
|
17
|
+
var _default = exports.default = (0, _padding.createPaddingRule)(__filename, 'Enforce padding around `beforeAll` blocks', config);
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = exports.config = void 0;
|
|
7
|
+
var _padding = require("./utils/padding");
|
|
8
|
+
const config = exports.config = [{
|
|
9
|
+
paddingType: _padding.PaddingType.Always,
|
|
10
|
+
prevStatementType: _padding.StatementType.Any,
|
|
11
|
+
nextStatementType: _padding.StatementType.BeforeEachToken
|
|
12
|
+
}, {
|
|
13
|
+
paddingType: _padding.PaddingType.Always,
|
|
14
|
+
prevStatementType: _padding.StatementType.BeforeEachToken,
|
|
15
|
+
nextStatementType: _padding.StatementType.Any
|
|
16
|
+
}];
|
|
17
|
+
var _default = exports.default = (0, _padding.createPaddingRule)(__filename, 'Enforce padding around `beforeEach` blocks', config);
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = exports.config = void 0;
|
|
7
|
+
var _padding = require("./utils/padding");
|
|
8
|
+
const config = exports.config = [{
|
|
9
|
+
paddingType: _padding.PaddingType.Always,
|
|
10
|
+
prevStatementType: _padding.StatementType.Any,
|
|
11
|
+
nextStatementType: [_padding.StatementType.DescribeToken, _padding.StatementType.FdescribeToken, _padding.StatementType.XdescribeToken]
|
|
12
|
+
}, {
|
|
13
|
+
paddingType: _padding.PaddingType.Always,
|
|
14
|
+
prevStatementType: [_padding.StatementType.DescribeToken, _padding.StatementType.FdescribeToken, _padding.StatementType.XdescribeToken],
|
|
15
|
+
nextStatementType: _padding.StatementType.Any
|
|
16
|
+
}];
|
|
17
|
+
var _default = exports.default = (0, _padding.createPaddingRule)(__filename, 'Enforce padding around `describe` blocks', config);
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = exports.config = void 0;
|
|
7
|
+
var _padding = require("./utils/padding");
|
|
8
|
+
const config = exports.config = [{
|
|
9
|
+
paddingType: _padding.PaddingType.Always,
|
|
10
|
+
prevStatementType: _padding.StatementType.Any,
|
|
11
|
+
nextStatementType: _padding.StatementType.ExpectToken
|
|
12
|
+
}, {
|
|
13
|
+
paddingType: _padding.PaddingType.Always,
|
|
14
|
+
prevStatementType: _padding.StatementType.ExpectToken,
|
|
15
|
+
nextStatementType: _padding.StatementType.Any
|
|
16
|
+
}, {
|
|
17
|
+
paddingType: _padding.PaddingType.Any,
|
|
18
|
+
prevStatementType: _padding.StatementType.ExpectToken,
|
|
19
|
+
nextStatementType: _padding.StatementType.ExpectToken
|
|
20
|
+
}];
|
|
21
|
+
var _default = exports.default = (0, _padding.createPaddingRule)(__filename, 'Enforce padding around `expect` groups', config);
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = exports.config = void 0;
|
|
7
|
+
var _padding = require("./utils/padding");
|
|
8
|
+
const config = exports.config = [{
|
|
9
|
+
paddingType: _padding.PaddingType.Always,
|
|
10
|
+
prevStatementType: _padding.StatementType.Any,
|
|
11
|
+
nextStatementType: [_padding.StatementType.TestToken, _padding.StatementType.ItToken, _padding.StatementType.FitToken, _padding.StatementType.XitToken, _padding.StatementType.XtestToken]
|
|
12
|
+
}, {
|
|
13
|
+
paddingType: _padding.PaddingType.Always,
|
|
14
|
+
prevStatementType: [_padding.StatementType.TestToken, _padding.StatementType.ItToken, _padding.StatementType.FitToken, _padding.StatementType.XitToken, _padding.StatementType.XtestToken],
|
|
15
|
+
nextStatementType: _padding.StatementType.Any
|
|
16
|
+
}];
|
|
17
|
+
var _default = exports.default = (0, _padding.createPaddingRule)(__filename, 'Enforce padding around afterAll blocks', config);
|
|
@@ -69,7 +69,7 @@ var _default = exports.default = (0, _utils2.createRule)({
|
|
|
69
69
|
if (!reportingNode) {
|
|
70
70
|
return;
|
|
71
71
|
}
|
|
72
|
-
const isModule = context.parserOptions.sourceType === 'module';
|
|
72
|
+
const isModule = context.parserOptions.sourceType === 'module' || context.languageOptions?.sourceType === 'module';
|
|
73
73
|
context.report({
|
|
74
74
|
node: reportingNode,
|
|
75
75
|
messageId: 'preferImportingJestGlobal',
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.isValidParent = exports.isTokenASemicolon = exports.getPaddingLineSequences = exports.getActualLastToken = exports.areTokensOnSameLine = void 0;
|
|
7
|
+
var _utils = require("@typescript-eslint/utils");
|
|
8
|
+
const isTokenASemicolon = token => token.value === ';' && token.type === _utils.AST_TOKEN_TYPES.Punctuator;
|
|
9
|
+
exports.isTokenASemicolon = isTokenASemicolon;
|
|
10
|
+
const areTokensOnSameLine = (left, right) => left.loc.end.line === right.loc.start.line;
|
|
11
|
+
|
|
12
|
+
// We'll only verify nodes with these parent types
|
|
13
|
+
exports.areTokensOnSameLine = areTokensOnSameLine;
|
|
14
|
+
const STATEMENT_LIST_PARENTS = new Set([_utils.AST_NODE_TYPES.Program, _utils.AST_NODE_TYPES.BlockStatement, _utils.AST_NODE_TYPES.SwitchCase, _utils.AST_NODE_TYPES.SwitchStatement]);
|
|
15
|
+
const isValidParent = parentType => {
|
|
16
|
+
return STATEMENT_LIST_PARENTS.has(parentType);
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Gets the actual last token.
|
|
21
|
+
*
|
|
22
|
+
* If a semicolon is semicolon-less style's semicolon, this ignores it.
|
|
23
|
+
* For example:
|
|
24
|
+
*
|
|
25
|
+
* foo()
|
|
26
|
+
* ;[1, 2, 3].forEach(bar)
|
|
27
|
+
*/
|
|
28
|
+
exports.isValidParent = isValidParent;
|
|
29
|
+
const getActualLastToken = (sourceCode, node) => {
|
|
30
|
+
const semiToken = sourceCode.getLastToken(node);
|
|
31
|
+
const prevToken = sourceCode.getTokenBefore(semiToken);
|
|
32
|
+
const nextToken = sourceCode.getTokenAfter(semiToken);
|
|
33
|
+
const isSemicolonLessStyle = Boolean(prevToken && nextToken && prevToken.range[0] >= node.range[0] && isTokenASemicolon(semiToken) && semiToken.loc.start.line !== prevToken.loc.end.line && semiToken.loc.end.line === nextToken.loc.start.line);
|
|
34
|
+
return isSemicolonLessStyle ? prevToken : semiToken;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Gets padding line sequences between the given 2 statements.
|
|
39
|
+
* Comments are separators of the padding line sequences.
|
|
40
|
+
*/
|
|
41
|
+
exports.getActualLastToken = getActualLastToken;
|
|
42
|
+
const getPaddingLineSequences = (prevNode, nextNode, sourceCode) => {
|
|
43
|
+
const pairs = [];
|
|
44
|
+
const includeComments = true;
|
|
45
|
+
let prevToken = getActualLastToken(sourceCode, prevNode);
|
|
46
|
+
if (nextNode.loc.start.line - prevToken.loc.end.line >= 2) {
|
|
47
|
+
do {
|
|
48
|
+
const token = sourceCode.getTokenAfter(prevToken, {
|
|
49
|
+
includeComments
|
|
50
|
+
});
|
|
51
|
+
if (token.loc.start.line - prevToken.loc.end.line >= 2) {
|
|
52
|
+
pairs.push([prevToken, token]);
|
|
53
|
+
}
|
|
54
|
+
prevToken = token;
|
|
55
|
+
} while (prevToken.range[0] < nextNode.range[0]);
|
|
56
|
+
}
|
|
57
|
+
return pairs;
|
|
58
|
+
};
|
|
59
|
+
exports.getPaddingLineSequences = getPaddingLineSequences;
|
|
@@ -0,0 +1,308 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.createPaddingRule = exports.StatementType = exports.PaddingType = void 0;
|
|
7
|
+
var _utils = require("@typescript-eslint/utils");
|
|
8
|
+
var astUtils = _interopRequireWildcard(require("./ast-utils"));
|
|
9
|
+
var _misc = require("./misc");
|
|
10
|
+
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
|
|
11
|
+
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
|
|
12
|
+
/**
|
|
13
|
+
* Require/fix newlines around jest functions
|
|
14
|
+
*
|
|
15
|
+
* Based on eslint/padding-line-between-statements by Toru Nagashima
|
|
16
|
+
* See: https://github.com/eslint/eslint/blob/master/lib/rules/padding-line-between-statements.js
|
|
17
|
+
*
|
|
18
|
+
* Some helpers borrowed from eslint ast-utils by Gyandeep Singh
|
|
19
|
+
* See: https://github.com/eslint/eslint/blob/master/lib/rules/utils/ast-utils.js
|
|
20
|
+
*/
|
|
21
|
+
// Statement types we'll respond to
|
|
22
|
+
let StatementType = exports.StatementType = /*#__PURE__*/function (StatementType) {
|
|
23
|
+
StatementType[StatementType["Any"] = 0] = "Any";
|
|
24
|
+
StatementType[StatementType["AfterAllToken"] = 1] = "AfterAllToken";
|
|
25
|
+
StatementType[StatementType["AfterEachToken"] = 2] = "AfterEachToken";
|
|
26
|
+
StatementType[StatementType["BeforeAllToken"] = 3] = "BeforeAllToken";
|
|
27
|
+
StatementType[StatementType["BeforeEachToken"] = 4] = "BeforeEachToken";
|
|
28
|
+
StatementType[StatementType["DescribeToken"] = 5] = "DescribeToken";
|
|
29
|
+
StatementType[StatementType["ExpectToken"] = 6] = "ExpectToken";
|
|
30
|
+
StatementType[StatementType["FdescribeToken"] = 7] = "FdescribeToken";
|
|
31
|
+
StatementType[StatementType["FitToken"] = 8] = "FitToken";
|
|
32
|
+
StatementType[StatementType["ItToken"] = 9] = "ItToken";
|
|
33
|
+
StatementType[StatementType["TestToken"] = 10] = "TestToken";
|
|
34
|
+
StatementType[StatementType["XdescribeToken"] = 11] = "XdescribeToken";
|
|
35
|
+
StatementType[StatementType["XitToken"] = 12] = "XitToken";
|
|
36
|
+
StatementType[StatementType["XtestToken"] = 13] = "XtestToken";
|
|
37
|
+
return StatementType;
|
|
38
|
+
}({});
|
|
39
|
+
// Padding type to apply between statements
|
|
40
|
+
let PaddingType = exports.PaddingType = /*#__PURE__*/function (PaddingType) {
|
|
41
|
+
PaddingType[PaddingType["Any"] = 0] = "Any";
|
|
42
|
+
PaddingType[PaddingType["Always"] = 1] = "Always";
|
|
43
|
+
return PaddingType;
|
|
44
|
+
}({}); // A configuration object for padding type and the two statement types
|
|
45
|
+
// Tracks position in scope and prevNode. Used to compare current and prev node
|
|
46
|
+
// and then to walk back up to the parent scope or down into the next one.
|
|
47
|
+
// And so on...
|
|
48
|
+
// Creates a StatementTester to test an ExpressionStatement's first token name
|
|
49
|
+
const createTokenTester = tokenName => {
|
|
50
|
+
return (node, sourceCode) => {
|
|
51
|
+
let activeNode = node;
|
|
52
|
+
if (activeNode.type === _utils.AST_NODE_TYPES.ExpressionStatement) {
|
|
53
|
+
// In the case of `await`, we actually care about its argument
|
|
54
|
+
if (activeNode.expression.type === _utils.AST_NODE_TYPES.AwaitExpression) {
|
|
55
|
+
activeNode = activeNode.expression.argument;
|
|
56
|
+
}
|
|
57
|
+
const token = sourceCode.getFirstToken(activeNode);
|
|
58
|
+
return token?.type === _utils.AST_TOKEN_TYPES.Identifier && token.value === tokenName;
|
|
59
|
+
}
|
|
60
|
+
return false;
|
|
61
|
+
};
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
// A mapping of StatementType to StatementTester for... testing statements
|
|
65
|
+
const statementTesters = {
|
|
66
|
+
[StatementType.Any]: () => true,
|
|
67
|
+
[StatementType.AfterAllToken]: createTokenTester('afterAll'),
|
|
68
|
+
[StatementType.AfterEachToken]: createTokenTester('afterEach'),
|
|
69
|
+
[StatementType.BeforeAllToken]: createTokenTester('beforeAll'),
|
|
70
|
+
[StatementType.BeforeEachToken]: createTokenTester('beforeEach'),
|
|
71
|
+
[StatementType.DescribeToken]: createTokenTester('describe'),
|
|
72
|
+
[StatementType.ExpectToken]: createTokenTester('expect'),
|
|
73
|
+
[StatementType.FdescribeToken]: createTokenTester('fdescribe'),
|
|
74
|
+
[StatementType.FitToken]: createTokenTester('fit'),
|
|
75
|
+
[StatementType.ItToken]: createTokenTester('it'),
|
|
76
|
+
[StatementType.TestToken]: createTokenTester('test'),
|
|
77
|
+
[StatementType.XdescribeToken]: createTokenTester('xdescribe'),
|
|
78
|
+
[StatementType.XitToken]: createTokenTester('xit'),
|
|
79
|
+
[StatementType.XtestToken]: createTokenTester('xtest')
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Check and report statements for `PaddingType.Always` configuration.
|
|
84
|
+
* This autofix inserts a blank line between the given 2 statements.
|
|
85
|
+
* If the `prevNode` has trailing comments, it inserts a blank line after the
|
|
86
|
+
* trailing comments.
|
|
87
|
+
*/
|
|
88
|
+
const paddingAlwaysTester = (prevNode, nextNode, paddingContext) => {
|
|
89
|
+
const {
|
|
90
|
+
sourceCode,
|
|
91
|
+
ruleContext
|
|
92
|
+
} = paddingContext;
|
|
93
|
+
const paddingLines = astUtils.getPaddingLineSequences(prevNode, nextNode, sourceCode);
|
|
94
|
+
|
|
95
|
+
// We've got some padding lines. Great.
|
|
96
|
+
if (paddingLines.length > 0) {
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// Missing padding line
|
|
101
|
+
ruleContext.report({
|
|
102
|
+
node: nextNode,
|
|
103
|
+
messageId: 'missingPadding',
|
|
104
|
+
fix(fixer) {
|
|
105
|
+
let prevToken = astUtils.getActualLastToken(sourceCode, prevNode);
|
|
106
|
+
const nextToken = sourceCode.getFirstTokenBetween(prevToken, nextNode, {
|
|
107
|
+
includeComments: true,
|
|
108
|
+
/**
|
|
109
|
+
* Skip the trailing comments of the previous node.
|
|
110
|
+
* This inserts a blank line after the last trailing comment.
|
|
111
|
+
*
|
|
112
|
+
* For example:
|
|
113
|
+
*
|
|
114
|
+
* foo(); // trailing comment.
|
|
115
|
+
* // comment.
|
|
116
|
+
* bar();
|
|
117
|
+
*
|
|
118
|
+
* Get fixed to:
|
|
119
|
+
*
|
|
120
|
+
* foo(); // trailing comment.
|
|
121
|
+
*
|
|
122
|
+
* // comment.
|
|
123
|
+
* bar();
|
|
124
|
+
*/
|
|
125
|
+
filter(token) {
|
|
126
|
+
if (astUtils.areTokensOnSameLine(prevToken, token)) {
|
|
127
|
+
prevToken = token;
|
|
128
|
+
return false;
|
|
129
|
+
}
|
|
130
|
+
return true;
|
|
131
|
+
}
|
|
132
|
+
}) || nextNode;
|
|
133
|
+
const insertText = astUtils.areTokensOnSameLine(prevToken, nextToken) ? '\n\n' : '\n';
|
|
134
|
+
return fixer.insertTextAfter(prevToken, insertText);
|
|
135
|
+
}
|
|
136
|
+
});
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
// A mapping of PaddingType to PaddingTester
|
|
140
|
+
const paddingTesters = {
|
|
141
|
+
[PaddingType.Any]: () => true,
|
|
142
|
+
[PaddingType.Always]: paddingAlwaysTester
|
|
143
|
+
};
|
|
144
|
+
const createScopeInfo = () => {
|
|
145
|
+
let scope = null;
|
|
146
|
+
|
|
147
|
+
// todo: explore seeing if we can refactor to a more TypeScript friendly structure
|
|
148
|
+
return {
|
|
149
|
+
get prevNode() {
|
|
150
|
+
return scope.prevNode;
|
|
151
|
+
},
|
|
152
|
+
set prevNode(node) {
|
|
153
|
+
scope.prevNode = node;
|
|
154
|
+
},
|
|
155
|
+
enter() {
|
|
156
|
+
scope = {
|
|
157
|
+
upper: scope,
|
|
158
|
+
prevNode: null
|
|
159
|
+
};
|
|
160
|
+
},
|
|
161
|
+
exit() {
|
|
162
|
+
scope = scope.upper;
|
|
163
|
+
}
|
|
164
|
+
};
|
|
165
|
+
};
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Check whether the given node matches the statement type
|
|
169
|
+
*/
|
|
170
|
+
const nodeMatchesType = (node, statementType, paddingContext) => {
|
|
171
|
+
let innerStatementNode = node;
|
|
172
|
+
const {
|
|
173
|
+
sourceCode
|
|
174
|
+
} = paddingContext;
|
|
175
|
+
|
|
176
|
+
// Dig into LabeledStatement body until it's not that anymore
|
|
177
|
+
while (innerStatementNode.type === _utils.AST_NODE_TYPES.LabeledStatement) {
|
|
178
|
+
innerStatementNode = innerStatementNode.body;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
// If it's an array recursively check if any of the statement types match
|
|
182
|
+
// the node
|
|
183
|
+
if (Array.isArray(statementType)) {
|
|
184
|
+
return statementType.some(type => nodeMatchesType(innerStatementNode, type, paddingContext));
|
|
185
|
+
}
|
|
186
|
+
return statementTesters[statementType](innerStatementNode, sourceCode);
|
|
187
|
+
};
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* Executes matching padding tester for last matched padding config for given
|
|
191
|
+
* nodes
|
|
192
|
+
*/
|
|
193
|
+
const testPadding = (prevNode, nextNode, paddingContext) => {
|
|
194
|
+
const {
|
|
195
|
+
configs
|
|
196
|
+
} = paddingContext;
|
|
197
|
+
const testType = type => paddingTesters[type](prevNode, nextNode, paddingContext);
|
|
198
|
+
for (let i = configs.length - 1; i >= 0; --i) {
|
|
199
|
+
const {
|
|
200
|
+
prevStatementType: prevType,
|
|
201
|
+
nextStatementType: nextType,
|
|
202
|
+
paddingType
|
|
203
|
+
} = configs[i];
|
|
204
|
+
if (nodeMatchesType(prevNode, prevType, paddingContext) && nodeMatchesType(nextNode, nextType, paddingContext)) {
|
|
205
|
+
return testType(paddingType);
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
// There were no matching padding rules for the prevNode, nextNode,
|
|
210
|
+
// paddingType combination... so we'll use PaddingType.Any which is always ok
|
|
211
|
+
return testType(PaddingType.Any);
|
|
212
|
+
};
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* Verify padding lines between the given node and the previous node.
|
|
216
|
+
*/
|
|
217
|
+
const verifyNode = (node, paddingContext) => {
|
|
218
|
+
const {
|
|
219
|
+
scopeInfo
|
|
220
|
+
} = paddingContext;
|
|
221
|
+
|
|
222
|
+
// NOTE: ESLint types use ESTree which provides a Node type, however
|
|
223
|
+
// ESTree.Node doesn't support the parent property which is added by
|
|
224
|
+
// ESLint during traversal. Our best bet is to ignore the property access
|
|
225
|
+
// here as it's the only place that it's checked.
|
|
226
|
+
|
|
227
|
+
if (!astUtils.isValidParent(node.parent.type)) {
|
|
228
|
+
return;
|
|
229
|
+
}
|
|
230
|
+
if (scopeInfo.prevNode) {
|
|
231
|
+
testPadding(scopeInfo.prevNode, node, paddingContext);
|
|
232
|
+
}
|
|
233
|
+
scopeInfo.prevNode = node;
|
|
234
|
+
};
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* Creates an ESLint rule for a given set of padding Config objects.
|
|
238
|
+
*
|
|
239
|
+
* The algorithm is approximately this:
|
|
240
|
+
*
|
|
241
|
+
* For each 'scope' in the program
|
|
242
|
+
* - Enter the scope (store the parent scope and previous node)
|
|
243
|
+
* - For each statement in the scope
|
|
244
|
+
* - Check the current node and previous node against the Config objects
|
|
245
|
+
* - If the current node and previous node match a Config, check the padding.
|
|
246
|
+
* Otherwise, ignore it.
|
|
247
|
+
* - If the padding is missing (and required), report and fix
|
|
248
|
+
* - Store the current node as the previous
|
|
249
|
+
* - Repeat
|
|
250
|
+
* - Exit scope (return to parent scope and clear previous node)
|
|
251
|
+
*
|
|
252
|
+
* The items we're looking for with this rule are ExpressionStatement nodes
|
|
253
|
+
* where the first token is an Identifier with a name matching one of the Jest
|
|
254
|
+
* functions. It's not foolproof, of course, but it's probably good enough for
|
|
255
|
+
* almost all cases.
|
|
256
|
+
*
|
|
257
|
+
* The Config objects specify a padding type, a previous statement type, and a
|
|
258
|
+
* next statement type. Wildcard statement types and padding types are
|
|
259
|
+
* supported. The current node and previous node are checked against the
|
|
260
|
+
* statement types. If they match then the specified padding type is
|
|
261
|
+
* tested/enforced.
|
|
262
|
+
*
|
|
263
|
+
* See src/index.ts for examples of Config usage.
|
|
264
|
+
*/
|
|
265
|
+
const createPaddingRule = (name, description, configs, deprecated = false) => {
|
|
266
|
+
return (0, _misc.createRule)({
|
|
267
|
+
name,
|
|
268
|
+
meta: {
|
|
269
|
+
docs: {
|
|
270
|
+
description
|
|
271
|
+
},
|
|
272
|
+
fixable: 'whitespace',
|
|
273
|
+
deprecated,
|
|
274
|
+
messages: {
|
|
275
|
+
missingPadding: 'Expected blank line before this statement.'
|
|
276
|
+
},
|
|
277
|
+
schema: [],
|
|
278
|
+
type: 'suggestion'
|
|
279
|
+
},
|
|
280
|
+
defaultOptions: [],
|
|
281
|
+
create(context) {
|
|
282
|
+
const paddingContext = {
|
|
283
|
+
ruleContext: context,
|
|
284
|
+
sourceCode: (0, _misc.getSourceCode)(context),
|
|
285
|
+
scopeInfo: createScopeInfo(),
|
|
286
|
+
configs
|
|
287
|
+
};
|
|
288
|
+
const {
|
|
289
|
+
scopeInfo
|
|
290
|
+
} = paddingContext;
|
|
291
|
+
return {
|
|
292
|
+
Program: scopeInfo.enter,
|
|
293
|
+
'Program:exit': scopeInfo.enter,
|
|
294
|
+
BlockStatement: scopeInfo.enter,
|
|
295
|
+
'BlockStatement:exit': scopeInfo.exit,
|
|
296
|
+
SwitchStatement: scopeInfo.enter,
|
|
297
|
+
'SwitchStatement:exit': scopeInfo.exit,
|
|
298
|
+
':statement': node => verifyNode(node, paddingContext),
|
|
299
|
+
SwitchCase(node) {
|
|
300
|
+
verifyNode(node, paddingContext);
|
|
301
|
+
scopeInfo.enter();
|
|
302
|
+
},
|
|
303
|
+
'SwitchCase:exit': scopeInfo.exit
|
|
304
|
+
};
|
|
305
|
+
}
|
|
306
|
+
});
|
|
307
|
+
};
|
|
308
|
+
exports.createPaddingRule = createPaddingRule;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "eslint-plugin-jest",
|
|
3
|
-
"version": "28.
|
|
3
|
+
"version": "28.8.1",
|
|
4
4
|
"description": "ESLint rules for Jest",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"eslint",
|
|
@@ -118,7 +118,7 @@
|
|
|
118
118
|
"optional": true
|
|
119
119
|
}
|
|
120
120
|
},
|
|
121
|
-
"packageManager": "yarn@3.8.
|
|
121
|
+
"packageManager": "yarn@3.8.4",
|
|
122
122
|
"engines": {
|
|
123
123
|
"node": "^16.10.0 || ^18.12.0 || >=20.0.0"
|
|
124
124
|
},
|