You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feat!: Set default schema: [], drop support for function-style rules (#17792)
* drop support for function-style rules in flat-rule-tester
* drop support for function-style rules in rule-tester
* drop support for function-style rules in config-rule (eslint-fuzzer)
* add rule object checks to rule testers
* drop support for function-style rules in flat config and Linter#defineRule
* update JSDoc
* add rule object checks to Linter
* drop support for function-style rules in eslintrc
* remove custom-rules-deprecated.md
* update flat config getRuleOptionsSchema for schema changes
* add back and update custom-rules-deprecated.md
* show ruleId in error messages for invalid schema
* throw error in Linter if rule has invalid schema
* add short description of meta.schema to type errors in flat-rule-tester
* throw error for empty object schema in flat-rule-tester
* update rule-tester for schema changes
* add integration tests for eslintrc schema changes
* add more flat-config-array unit tests for schema changes
* throw error in Linter in eslintrc mode if rule has invalid schema
* update docs for schema changes
* use eslintrc v3
Copy file name to clipboardExpand all lines: docs/src/extend/custom-rules.md
+14-5Lines changed: 14 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -10,8 +10,6 @@ eleventyNavigation:
10
10
11
11
You can create custom rules to use with ESLint. You might want to create a custom rule if the [core rules](../rules/) do not cover your use case.
12
12
13
-
**Note:** This page covers the most recent rule format for ESLint >= 3.0.0. There is also a [deprecated rule format](./custom-rules-deprecated).
14
-
15
13
Here's the basic format of a custom rule:
16
14
17
15
```js
@@ -60,7 +58,7 @@ The source file for a rule exports an object with the following properties. Both
60
58
61
59
**Important:** the `hasSuggestions` property is mandatory for rules that provide suggestions. If this property isn't set to `true`, ESLint will throw an error whenever the rule attempts to produce a suggestion. Omit the `hasSuggestions` property if the rule does not provide suggestions.
62
60
63
-
*`schema`: (`object | array`) Specifies the [options](#options-schemas) so ESLint can prevent invalid [rule configurations](../use/configure/rules).
61
+
*`schema`: (`object | array | false`) Specifies the [options](#options-schemas) so ESLint can prevent invalid [rule configurations](../use/configure/rules). Mandatory when the rule has options.
64
62
65
63
*`deprecated`: (`boolean`) Indicates whether the rule has been deprecated. You may omit the `deprecated` property if the rule has not been deprecated.
66
64
@@ -482,6 +480,13 @@ The `quotes` rule in this example has one option, `"double"` (the `error` is the
482
480
483
481
```js
484
482
module.exports= {
483
+
meta: {
484
+
schema: [
485
+
{
486
+
enum: ["single", "double", "backtick"]
487
+
}
488
+
]
489
+
},
485
490
create:function(context) {
486
491
var isDouble = (context.options[0] ==="double");
487
492
@@ -494,6 +499,8 @@ Since `context.options` is just an array, you can use it to determine how many o
494
499
495
500
When using options, make sure that your rule has some logical defaults in case the options are not provided.
496
501
502
+
Rules with options must specify a [schema](#options-schemas).
503
+
497
504
### Accessing the Source Code
498
505
499
506
The `SourceCode` object is the main object for getting more information about the source code being linted. You can retrieve the `SourceCode` object at any time by using the `context.sourceCode` property:
@@ -612,9 +619,11 @@ You can also access comments through many of `sourceCode`'s methods using the `i
612
619
613
620
### Options Schemas
614
621
615
-
Rules may specify a `schema` property, which is a [JSON Schema](https://json-schema.org/) format description of a rule's options which will be used by ESLint to validate configuration options and prevent invalid or unexpected inputs before they are passed to the rule in `context.options`.
622
+
Rules with options must specify a `meta.schema` property, which is a [JSON Schema](https://json-schema.org/) format description of a rule's options which will be used by ESLint to validate configuration options and prevent invalid or unexpected inputs before they are passed to the rule in `context.options`.
623
+
624
+
If your rule has options, it is strongly recommended that you specify a schema for options validation. However, it is possible to opt-out of options validation by setting `schema: false`, but doing so is discouraged as it increases the chance of bugs and mistakes.
616
625
617
-
Note: Prior to ESLint v9.0.0, rules without a schema are passed their options directly from the config without any validation. In ESLint v9.0.0 and later, rules without schemas will throw errors when options are passed. See the [Require schemas and object-style rules](https://github.com/eslint/rfcs/blob/main/designs/2021-schema-object-rules/README.md) RFC for further details.
626
+
For rules that don't specify a `meta.schema` property, ESLint throws errors when any options are passed. If your rule doesn't have options, do not set `schema: false`, but simply omit the schema property or use `schema: []`, both of which prevent any options from being passed.
618
627
619
628
When validating a rule's config, there are five steps:
0 commit comments