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
* [Reference] feat: add meta.defaultOptions
* Removed optionsRaw
* computed-property-spacing: defaultOptions
* fix: handle object type mismatches in merging
* Validate arrays in flat-config-array
* Fix rule defaultOptions typos
* Put back getRuleOptions as before
* Apply deep merging in config-validator and rule-validator
* Converted remaining rules. Note: inline comments still need to have defaults applied.
* Fixes around inline comments
* Extract to a getRuleOptionsInline
* nit: new extra line
* Test fix: meta.defaultOptions in a comment
* Refactor-level review feedback
* Used a recommended rule in linter.js test
* Added custom-rules.md docs
* Update docs/src/extend/custom-rules.md
Co-authored-by: Nicholas C. Zakas <[email protected]>
* Clarified undefined point
* Adjusted for edge cases per review
* Refactored per review
* Removed lint disable in source
* Added Linter test for meta.defaultOptions
* Documented useDefaults from Ajv
* Set up meta+schema merging unit tests for flat (passing) and legacy (failing)
* Potential solution: boolean applyDefaultOptions param for runRules
* chore: node:assert
* Update lib/shared/deep-merge-arrays.js
Co-authored-by: Milos Djermanovic <[email protected]>
* Made tests more explicit on defaulting behavior
* Handled defaultOptions and option-less inline comments
* Added explicit tests for mismatched comment options and comment options with schema: false
* Try out configToValidate approach
* Add in unit tests
Co-authored-by: Milos Djermanovic <[email protected]>
* Always apply defaultOptions, even with meta.schema: false
* Filled in some falsy values
* Fix a few lint complaints
* That's right, Infinity is not allowed
* Update lib/config/rule-validator.js
Co-authored-by: Milos Djermanovic <[email protected]>
* Update docs/src/extend/custom-rules.md
Co-authored-by: Milos Djermanovic <[email protected]>
* Update docs/src/extend/custom-rules.md
Co-authored-by: Milos Djermanovic <[email protected]>
* Revert deprecated rules
* Bring in eslintrc#factor-in-default-options
* Add index.d.ts types
* Update lib/linter/linter.js
Co-authored-by: Milos Djermanovic <[email protected]>
* Apply suggestions from code review
Co-authored-by: Milos Djermanovic <[email protected]>
* Moved config changing outside of validation
* git checkout main -- lib/config/rule-validator.js
* linter.js touchups and revert
* Update lib/config/config.js
Co-authored-by: Milos Djermanovic <[email protected]>
* Update package.json
---------
Co-authored-by: Nicholas C. Zakas <[email protected]>
Co-authored-by: Milos Djermanovic <[email protected]>
Copy file name to clipboardExpand all lines: docs/src/extend/custom-rules.md
+47Lines changed: 47 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -60,6 +60,8 @@ The source file for a rule exports an object with the following properties. Both
60
60
61
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.
62
62
63
+
*`defaultOptions`: (`array`) Specifies [default options](#option-defaults) for the rule. If present, any user-provided options in their config will be merged on top of them recursively.
64
+
63
65
*`deprecated`: (`boolean`) Indicates whether the rule has been deprecated. You may omit the `deprecated` property if the rule has not been deprecated.
64
66
65
67
*`replacedBy`: (`array`) In the case of a deprecated rule, specify replacement rule(s).
@@ -800,6 +802,51 @@ module.exports = {
800
802
801
803
To learn more about JSON Schema, we recommend looking at some examples on the [JSON Schema website](https://json-schema.org/learn/miscellaneous-examples), or reading the free [Understanding JSON Schema](https://json-schema.org/understanding-json-schema/) ebook.
802
804
805
+
### Option Defaults
806
+
807
+
Rules may specify a `meta.defaultOptions` array of default values for any options.
808
+
When the rule is enabled in a user configuration, ESLint will recursively merge any user-provided option elements on top of the default elements.
809
+
810
+
For example, given the following defaults:
811
+
812
+
```js
813
+
exportdefault {
814
+
meta: {
815
+
defaultOptions: [{
816
+
alias:"basic",
817
+
}],
818
+
schema: [{
819
+
type:"object",
820
+
properties: {
821
+
alias: {
822
+
type:"string"
823
+
}
824
+
},
825
+
additionalProperties:false
826
+
}]
827
+
},
828
+
create(context) {
829
+
const [{ alias }] =context.options;
830
+
831
+
return { /* ... */ };
832
+
}
833
+
}
834
+
```
835
+
836
+
The rule would have a runtime `alias` value of `"basic"` unless the user configuration specifies a different value, such as with `["error", { alias: "complex" }]`.
837
+
838
+
Each element of the options array is merged according to the following rules:
839
+
840
+
* Any missing value or explicit user-provided `undefined` will fall back to a default option
841
+
* User-provided arrays and primitive values other than `undefined` override a default option
842
+
* User-provided objects will merge into a default option object and replace a non-object default otherwise
843
+
844
+
Option defaults will also be validated against the rule's `meta.schema`.
845
+
846
+
**Note:** ESLint internally uses [Ajv](https://ajv.js.org) for schema validation with its [`useDefaults` option](https://ajv.js.org/guide/modifying-data.html#assigning-defaults) enabled.
847
+
Both user-provided and `meta.defaultOptions` options will override any defaults specified in a rule's schema.
848
+
ESLint may disable Ajv's `useDefaults` in a future major version.
849
+
803
850
### Accessing Shebangs
804
851
805
852
[Shebangs (#!)](https://en.wikipedia.org/wiki/Shebang_(Unix)) are represented by the unique tokens of type `"Shebang"`. They are treated as comments and can be accessed by the methods outlined in the [Accessing Comments](#accessing-comments) section, such as `sourceCode.getAllComments()`.
0 commit comments