Skip to content

Commit

Permalink
feat: import formatting rules from eslint-plugin-jest-formatting (#…
Browse files Browse the repository at this point in the history
…1563)

* feat: naively copy and paste `eslint-plugin-jest-formatting` rules

* refactor: remove unneeded exports

* refactor: give each rule a dedicated file

* refactor: rename and convert test files to typescript

* refactor: use `import` instead of `require` and clean up other imports

* refactor: use `createRule`

* fix: pass in the correct `name` for each padding rule

* refactor: use `@typescript-eslint` types and constants

* fix: address some initial typescript errors

* refactor: use `messageId` instead of `message`

* test: use flat compat `RuleTester`

* test: specify full errors instead of counts

* test: add basic file for `padding-around-all`

* docs: add entries for new padding rules

* refactor: address remaining TypeScript issues

* refactor: address deprecated `context` method usage

* test: remove comments

* test: cleanup cases

* test: add more cases for coverage

* refactor: remove unneeded checks
  • Loading branch information
G-Rath authored Aug 7, 2024
1 parent 11ef4fc commit 74078ee
Show file tree
Hide file tree
Showing 30 changed files with 2,204 additions and 56 deletions.
118 changes: 63 additions & 55 deletions README.md

Large diffs are not rendered by default.

32 changes: 32 additions & 0 deletions docs/rules/padding-around-after-all-blocks.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Enforce padding around `afterAll` blocks (`padding-around-after-all-blocks`)

🔧 This rule is automatically fixable by the
[`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix).

<!-- end auto-generated rule header -->

## Rule Details

This rule enforces a line of padding before _and_ after 1 or more `afterAll`
statements.

Note that it doesn't add/enforce a padding line if it's the last statement in
its scope.

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

```js
const someText = 'abc';
afterAll(() => {});
describe('someText', () => {});
```

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

```js
const someText = 'abc';

afterAll(() => {});

describe('someText', () => {});
```
36 changes: 36 additions & 0 deletions docs/rules/padding-around-after-each-blocks.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Enforce padding around `afterEach` blocks (`padding-around-after-each-blocks`)

🔧 This rule is automatically fixable by the
[`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix).

<!-- end auto-generated rule header -->

## Rule Details

This rule enforces a line of padding before _and_ after 1 or more `afterEach`
statements.

Note that it doesn't add/enforce a padding line if it's the last statement in
its scope.

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

```js
const something = 123;
afterEach(() => {
// more stuff
});
describe('foo', () => {});
```

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

```js
const something = 123;

afterEach(() => {
// more stuff
});

describe('foo', () => {});
```
18 changes: 18 additions & 0 deletions docs/rules/padding-around-all.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Enforce padding around Jest functions (`padding-around-all`)

🔧 This rule is automatically fixable by the
[`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix).

<!-- end auto-generated rule header -->

## Rule Details

This is a meta rule that simply enables all of the following rules:

- [padding-around-after-all-blocks](padding-around-after-all-blocks.md)
- [padding-around-after-each-blocks](padding-around-after-each-blocks.md)
- [padding-around-before-all-blocks](padding-around-before-all-blocks.md)
- [padding-around-before-each-blocks](padding-around-before-each-blocks.md)
- [padding-around-expect-groups](padding-around-expect-groups.md)
- [padding-around-describe-blocks](padding-around-describe-blocks.md)
- [padding-around-test-blocks](padding-around-test-blocks.md)
35 changes: 35 additions & 0 deletions docs/rules/padding-around-before-all-blocks.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Enforce padding around `beforeAll` blocks (`padding-around-before-all-blocks`)

🔧 This rule is automatically fixable by the
[`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix).

<!-- end auto-generated rule header -->

## Rule Details

This rule enforces a line of padding before _and_ after `beforeAll` statements.

Note that it doesn't add/enforce a padding line if it's the last statement in
its scope.

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

```js
const something = 123;
beforeAll(() => {
// more stuff
});
describe('foo', () => {});
```

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

```js
const something = 123;

beforeAll(() => {
// more stuff
});

describe('foo', () => {});
```
36 changes: 36 additions & 0 deletions docs/rules/padding-around-before-each-blocks.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Enforce padding around `beforeEach` blocks (`padding-around-before-each-blocks`)

🔧 This rule is automatically fixable by the
[`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix).

<!-- end auto-generated rule header -->

## Rule Details

This rule enforces a line of padding before _and_ after 1 or more `beforeEach`
statements

Note that it doesn't add/enforce a padding line if it's the last statement in
its scope

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

```js
const something = 123;
beforeEach(() => {
// more stuff
});
describe('foo', () => {});
```

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

```js
const something = 123;

beforeEach(() => {
// more stuff
});

describe('foo', () => {});
```
40 changes: 40 additions & 0 deletions docs/rules/padding-around-describe-blocks.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Enforce padding around `describe` blocks (`padding-around-describe-blocks`)

🔧 This rule is automatically fixable by the
[`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix).

<!-- end auto-generated rule header -->

## Rule Details

This rule enforces a line of padding before _and_ after 1 or more `describe`
statements

Note that it doesn't add/enforce a padding line if it's the last statement in
its scope

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

```js
const thing = 123;
describe('foo', () => {
// stuff
});
describe('bar', () => {
// more stuff
});
```

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

```js
const thing = 123;

describe('foo', () => {
// stuff
});

describe('bar', () => {
// more stuff
});
```
42 changes: 42 additions & 0 deletions docs/rules/padding-around-expect-groups.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Enforce padding around `expect` groups (`padding-around-expect-groups`)

🔧 This rule is automatically fixable by the
[`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix).

<!-- end auto-generated rule header -->

## Rule Details

This rule enforces a line of padding before _and_ after 1 or more `expect`
statements

Note that it doesn't add/enforce a padding line if it's the last statement in
its scope and it doesn't add/enforce padding between two or more adjacent
`expect` statements.

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

```js
test('thing one', () => {
let abc = 123;
expect(abc).toEqual(123);
expect(123).toEqual(abc);
abc = 456;
expect(abc).toEqual(456);
});
```

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

```js
test('thing one', () => {
let abc = 123;

expect(abc).toEqual(123);
expect(123).toEqual(abc);

abc = 456;

expect(abc).toEqual(456);
});
```
46 changes: 46 additions & 0 deletions docs/rules/padding-around-test-blocks.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Enforce padding around afterAll blocks (`padding-around-test-blocks`)

🔧 This rule is automatically fixable by the
[`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix).

<!-- end auto-generated rule header -->

## Rule Details

This rule enforces a line of padding before _and_ after 1 or more `test`/`it`
statements

Note that it doesn't add/enforce a padding line if it's the last statement in
its scope

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

```js
const thing = 123;
test('foo', () => {});
test('bar', () => {});
```

```js
const thing = 123;
it('foo', () => {});
it('bar', () => {});
```

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

```js
const thing = 123;

test('foo', () => {});

test('bar', () => {});
```

```js
const thing = 123;

it('foo', () => {});

it('bar', () => {});
```
16 changes: 16 additions & 0 deletions src/__tests__/__snapshots__/rules.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ exports[`rules should export configs that refer to actual rules 1`] = `
"jest/no-test-prefixes": "error",
"jest/no-test-return-statement": "error",
"jest/no-untyped-mock-factory": "error",
"jest/padding-around-after-all-blocks": "error",
"jest/padding-around-after-each-blocks": "error",
"jest/padding-around-all": "error",
"jest/padding-around-before-all-blocks": "error",
"jest/padding-around-before-each-blocks": "error",
"jest/padding-around-describe-blocks": "error",
"jest/padding-around-expect-groups": "error",
"jest/padding-around-test-blocks": "error",
"jest/prefer-called-with": "error",
"jest/prefer-comparison-matcher": "error",
"jest/prefer-each": "error",
Expand Down Expand Up @@ -120,6 +128,14 @@ exports[`rules should export configs that refer to actual rules 1`] = `
"jest/no-test-prefixes": "error",
"jest/no-test-return-statement": "error",
"jest/no-untyped-mock-factory": "error",
"jest/padding-around-after-all-blocks": "error",
"jest/padding-around-after-each-blocks": "error",
"jest/padding-around-all": "error",
"jest/padding-around-before-all-blocks": "error",
"jest/padding-around-before-each-blocks": "error",
"jest/padding-around-describe-blocks": "error",
"jest/padding-around-expect-groups": "error",
"jest/padding-around-test-blocks": "error",
"jest/prefer-called-with": "error",
"jest/prefer-comparison-matcher": "error",
"jest/prefer-each": "error",
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/rules.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { existsSync } from 'fs';
import { resolve } from 'path';
import plugin from '../';

const numberOfRules = 54;
const numberOfRules = 62;
const ruleNames = Object.keys(plugin.rules);
const deprecatedRules = Object.entries(plugin.rules)
.filter(([, rule]) => rule.meta.deprecated)
Expand Down
Loading

0 comments on commit 74078ee

Please sign in to comment.