Skip to content

Commit

Permalink
Merge branch 'main' into drop-eslint-6
Browse files Browse the repository at this point in the history
* main:
  docs: modernize rule doc for `no-identical-tests` (eslint-community#265)
  docs: modernize rule doc for `require-meta-schema` (eslint-community#266)
  docs: modernize rule doc for `prefer-object-rule` (eslint-community#264)
  docs: modernize rule doc for `consistent-output` (eslint-community#263)
  chore: run version-specific CI jobs on Node 18 (eslint-community#260)
  • Loading branch information
bmish committed Jul 8, 2022
2 parents c3f0fdf + d6d79aa commit 4d0d0c8
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 54 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ jobs:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: "16.x"
node-version: "18.x"
- run: npm install
- run: npm run lint

Expand All @@ -43,7 +43,7 @@ jobs:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: "16.x"
node-version: "18.x"
- run: npm install
- run: npm install --save-dev eslint@7
- run: npm test
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ Name | ✔️ | 🛠 | 💡 | Description
[no-unused-placeholders](https://github.com/not-an-aardvark/eslint-plugin-eslint-plugin/blob/master/docs/rules/no-unused-placeholders.md) | ✔️ | | | disallow unused placeholders in rule report messages
[no-useless-token-range](https://github.com/not-an-aardvark/eslint-plugin-eslint-plugin/blob/master/docs/rules/no-useless-token-range.md) | ✔️ | 🛠 | | disallow unnecessary calls to `sourceCode.getFirstToken()` and `sourceCode.getLastToken()`
[prefer-message-ids](https://github.com/not-an-aardvark/eslint-plugin-eslint-plugin/blob/master/docs/rules/prefer-message-ids.md) | | | | require using `messageId` instead of `message` to report rule violations
[prefer-object-rule](https://github.com/not-an-aardvark/eslint-plugin-eslint-plugin/blob/master/docs/rules/prefer-object-rule.md) | ✔️ | 🛠 | | disallow rule exports where the export is a function
[prefer-object-rule](https://github.com/not-an-aardvark/eslint-plugin-eslint-plugin/blob/master/docs/rules/prefer-object-rule.md) | ✔️ | 🛠 | | disallow function-style rules
[prefer-output-null](https://github.com/not-an-aardvark/eslint-plugin-eslint-plugin/blob/master/docs/rules/prefer-output-null.md) | | 🛠 | | disallow invalid RuleTester test cases where the `output` matches the `code`
[prefer-placeholders](https://github.com/not-an-aardvark/eslint-plugin-eslint-plugin/blob/master/docs/rules/prefer-placeholders.md) | | | | require using placeholders for dynamic report messages
[prefer-replace-text](https://github.com/not-an-aardvark/eslint-plugin-eslint-plugin/blob/master/docs/rules/prefer-replace-text.md) | | | | require using `replaceText()` instead of `replaceTextRange()`
Expand Down
6 changes: 4 additions & 2 deletions docs/rules/consistent-output.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

✔️ The `"extends": "plugin:eslint-plugin/recommended"` property in a configuration file enables this rule.

When writing tests for fixable rules, it's a best practice to use the `output` property on each test case to assert what autofixed code is produced, or to assert that no autofix is produced using `output: null`.
When writing tests for fixable rules, the `output` property on each test case can be used to assert what autofixed code is produced, or to assert that no autofix is produced using `output: null`.

Prior to ESLint 7, it was easy to forget to assert the autofix output of a particular test case, resulting in incomplete test coverage and a greater chance of unexpected behavior / bugs.

[As of ESLint 7](https://eslint.org/docs/user-guide/migrating-to-7.0.0#additional-validation-added-to-the-ruletester-class), test cases that trigger an autofix are required to provide the `output` property.
[As of ESLint 7](https://eslint.org/docs/user-guide/migrating-to-7.0.0#additional-validation-added-to-the-ruletester-class), test cases that trigger an autofix are required by ESLint to provide the `output` property. Thus, it's now acceptable and more concise to omit this property when there's no autofix.

## Rule Details

Expand Down Expand Up @@ -71,6 +71,8 @@ This rule takes an optional string enum option with one of the following values:

If you're not writing fixable rules, or you want to write test cases without output assertions, do not enable this rule.

As mentioned in the introduction, the need for this rule is reduced as of ESLint v7.

## Further Reading

* [`RuleTester` documentation](http://eslint.org/docs/developer-guide/working-with-plugins#testing)
35 changes: 26 additions & 9 deletions docs/rules/no-identical-tests.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,34 @@

⚒️ The `--fix` option on the [command line](https://eslint.org/docs/user-guide/command-line-interface#--fix) can automatically fix some of the problems reported by this rule.

When a rule has a lot of tests, it's sometimes difficult to tell if any tests are duplicates. This rule would warn if any test cases have the same properties.
Duplicate test cases can cause confusion, can be hard to detect manually in a long file, and serve no purpose.

As of [ESLint v9](https://github.com/eslint/rfcs/tree/main/designs/2021-stricter-rule-test-validation#disallow-identical-test-cases), ESLint attempts to detect and disallow duplicate tests.

## Rule Details

This rule detects duplicate test cases.

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

```js
/* eslint eslint-plugin/no-identical-tests: error */

new RuleTester().run('foo', bar, {
valid: [{ code: 'foo' }, { code: 'foo' }],
invalid: [],
valid: [
'foo',
'foo', // duplicate of previous
],
invalid: [
{
code: 'bar',
errors: [{ messageId: 'my-message', type: 'CallExpression' }],
},
{
code: 'bar',
errors: [{ messageId: 'my-message', type: 'CallExpression' }],
}, // duplicate of previous
],
});
```

Expand All @@ -25,11 +41,12 @@ Examples of **correct** code for this rule:
/* eslint eslint-plugin/no-identical-tests: error */

new RuleTester().run('foo', bar, {
valid: [{ code: 'foo' }, { code: 'bar' }],
invalid: [],
valid: ['foo', 'bar'],
invalid: [
{
code: 'baz',
errors: [{ messageId: 'my-message', type: 'CallExpression' }],
},
],
});
```

## When Not To Use It

If you want to allow identical tests, do not enable this rule.
45 changes: 7 additions & 38 deletions docs/rules/prefer-object-rule.md
Original file line number Diff line number Diff line change
@@ -1,41 +1,29 @@
# Disallow rule exports where the export is a function (prefer-object-rule)
# Disallow function-style rules (prefer-object-rule)

✔️ The `"extends": "plugin:eslint-plugin/recommended"` property in a configuration file enables this rule.

⚒️ The `--fix` option on the [command line](https://eslint.org/docs/user-guide/command-line-interface#--fix) can automatically fix some of the problems reported by this rule.

Prior to ESLint v9, ESLint supported both [function-style](https://eslint.org/docs/developer-guide/working-with-rules-deprecated) and [object-style](https://eslint.org/docs/developer-guide/working-with-rules) rules. However, function-style rules have been deprecated since 2016, and do not support newer features like autofixing and suggestions.

As of [ESLint v9](https://github.com/eslint/rfcs/tree/main/designs/2021-schema-object-rules#motivation-for-requiring-object-style-rules), ESLint supports only object-style rules.

## Rule Details

The rule reports an error if it encounters a rule that's defined using the [deprecated style](https://eslint.org/docs/developer-guide/working-with-rules-deprecated) of just a `create` function instead of the newer [object style](https://eslint.org/docs/developer-guide/working-with-rules).
The rule reports an error if it encounters a rule that's defined using the deprecated function-style format.

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

```js
/* eslint eslint-plugin/prefer-object-rule: error */

module.exports = function (context) {
return {
Program() {
context.report();
},
};
};

module.exports = function create(context) {
return {
Program() {
context.report();
},
};
};

module.exports = (context) => {
return {
Program() {
context.report();
},
};
};
```

Examples of **correct** code for this rule:
Expand All @@ -44,6 +32,7 @@ Examples of **correct** code for this rule:
/* eslint eslint-plugin/prefer-object-rule: error */

module.exports = {
meta: { /* ... */ },
create(context) {
return {
Program() {
Expand All @@ -52,24 +41,4 @@ module.exports = {
};
},
};

module.exports = {
create(context) {
return {
Program() {
context.report();
},
};
},
};

module.exports = {
create: (context) => {
return {
Program() {
context.report();
},
};
},
};
```
8 changes: 7 additions & 1 deletion docs/rules/require-meta-schema.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

💡 Some problems reported by this rule are manually fixable by editor [suggestions](https://eslint.org/docs/developer-guide/working-with-rules#providing-suggestions).

Defining a schema for each rule allows eslint to validate that configuration options are passed correctly. Even when there are no options for a rule, a schema should still be defined (as an empty array) so that eslint can validate that no data is mistakenly passed to the rule.
Defining a schema for each rule allows eslint to validate that configuration options are passed correctly. Even when there are no options for a rule, a schema can still be defined as an empty array to validate that no data is mistakenly passed to the rule.

As of [ESLint v9](https://github.com/eslint/rfcs/tree/main/designs/2021-schema-object-rules#motivation-for-requiring-schemas), ESLint will validate that options are not provided to a rule when a schema is omitted.

## Rule Details

Expand Down Expand Up @@ -75,6 +77,10 @@ This rule takes an optional object containing:

* `boolean``requireSchemaPropertyWhenOptionless` — Whether the rule should require the `meta.schema` property to be specified (with `schema: []`) for rules that have no options. Defaults to `true`.

## When Not To Use It

As mentioned in the introduction, the need for this rule is reduced as of ESLint v9.

## Further Reading

* [working-with-rules#options-schemas](https://eslint.org/docs/developer-guide/working-with-rules#options-schemas)
2 changes: 1 addition & 1 deletion lib/rules/prefer-object-rule.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ module.exports = {
meta: {
type: 'suggestion',
docs: {
description: 'disallow rule exports where the export is a function',
description: 'disallow function-style rules',
category: 'Rules',
recommended: true,
url: 'https://github.com/not-an-aardvark/eslint-plugin-eslint-plugin/tree/HEAD/docs/rules/prefer-object-rule.md',
Expand Down

0 comments on commit 4d0d0c8

Please sign in to comment.