-
-
Notifications
You must be signed in to change notification settings - Fork 30
Add new require-meta-default-options
rule
#502
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
aladdin-add
merged 5 commits into
eslint-community:main
from
FloEdelmann:require-meta-default-options
Dec 18, 2024
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e6a2f42
Add new `require-meta-default-options` rule
FloEdelmann ca712cd
Add `defaultOptions` to all rules
FloEdelmann 011f8b2
Improve rule description
FloEdelmann 235bb00
Allow array root schemas to have empty `defaultOptions`
FloEdelmann 60eecd2
Merge branch 'main' into require-meta-default-options
FloEdelmann File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
# Require only rules with options to implement a `meta.defaultOptions` property (`eslint-plugin/require-meta-default-options`) | ||
|
||
🔧 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 --> | ||
|
||
Defining default options declaratively in a rule's `meta.defaultOptions` property enables ESLint v9.15.0+ to merge any user-provided options with the default options, simplifying the rule's implementation. It can also be useful for other tools like [eslint-doc-generator](https://github.com/bmish/eslint-doc-generator) to generate documentation for the rule's options. | ||
|
||
## Rule Details | ||
|
||
This rule requires ESLint rules to have a valid `meta.defaultOptions` property if and only if the rule has options defined in its `meta.schema` property. | ||
|
||
Examples of **incorrect** code for this rule: | ||
|
||
```js | ||
/* eslint eslint-plugin/require-meta-default-options: error */ | ||
|
||
module.exports = { | ||
meta: { | ||
schema: [ | ||
{ | ||
type: 'object', | ||
/* ... */ | ||
}, | ||
], | ||
// defaultOptions is missing | ||
}, | ||
create(context) { | ||
/* ... */ | ||
}, | ||
}; | ||
|
||
module.exports = { | ||
meta: { | ||
schema: [], | ||
defaultOptions: [{}], // defaultOptions is not needed when schema is empty | ||
}, | ||
create(context) { | ||
/* ... */ | ||
}, | ||
}; | ||
|
||
module.exports = { | ||
meta: { | ||
schema: [ | ||
{ | ||
/* ... */ | ||
}, | ||
], | ||
defaultOptions: {}, // defaultOptions should be an array | ||
}, | ||
create(context) { | ||
/* ... */ | ||
}, | ||
}; | ||
|
||
module.exports = { | ||
meta: { | ||
schema: [ | ||
{ | ||
/* ... */ | ||
}, | ||
], | ||
defaultOptions: [], // defaultOptions should not be empty | ||
}, | ||
create(context) { | ||
/* ... */ | ||
}, | ||
}; | ||
``` | ||
|
||
Examples of **correct** code for this rule: | ||
|
||
```js | ||
/* eslint eslint-plugin/require-meta-default-options: error */ | ||
|
||
module.exports = { | ||
meta: { schema: [] }, // no defaultOptions needed when schema is empty | ||
create(context) { | ||
/* ... */ | ||
}, | ||
}; | ||
|
||
module.exports = { | ||
meta: { | ||
schema: [ | ||
{ | ||
type: 'object', | ||
properties: { | ||
exceptRange: { | ||
type: 'boolean', | ||
}, | ||
}, | ||
additionalProperties: false, | ||
}, | ||
], | ||
defaultOptions: [{ exceptRange: false }], | ||
}, | ||
create(context) { | ||
/* ... */ | ||
}, | ||
}; | ||
``` | ||
|
||
## Further Reading | ||
|
||
- [ESLint rule docs: Option Defaults](https://eslint.org/docs/latest/extend/custom-rules#option-defaults) | ||
- [RFC introducing `meta.defaultOptions`](https://github.com/eslint/rfcs/blob/main/designs/2023-rule-options-defaults/README.md) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
'use strict'; | ||
|
||
const utils = require('../utils'); | ||
|
||
/** @type {import('eslint').Rule.RuleModule} */ | ||
module.exports = { | ||
meta: { | ||
type: 'suggestion', | ||
docs: { | ||
description: | ||
'require only rules with options to implement a `meta.defaultOptions` property', | ||
category: 'Rules', | ||
recommended: false, | ||
url: 'https://github.com/eslint-community/eslint-plugin-eslint-plugin/tree/HEAD/docs/rules/require-meta-default-options.md', | ||
}, | ||
fixable: 'code', | ||
schema: [], | ||
messages: { | ||
missingDefaultOptions: | ||
'Rule with non-empty schema is missing a `meta.defaultOptions` property.', | ||
unnecessaryDefaultOptions: | ||
'Rule with empty schema should not have a `meta.defaultOptions` property.', | ||
defaultOptionsMustBeArray: 'Default options must be an array.', | ||
defaultOptionsMustNotBeEmpty: 'Default options must not be empty.', | ||
}, | ||
}, | ||
|
||
create(context) { | ||
const sourceCode = context.sourceCode || context.getSourceCode(); // TODO: just use context.sourceCode when dropping eslint < v9 | ||
const { scopeManager } = sourceCode; | ||
const ruleInfo = utils.getRuleInfo(sourceCode); | ||
if (!ruleInfo) { | ||
return {}; | ||
} | ||
|
||
const metaNode = ruleInfo.meta; | ||
|
||
const schemaNode = utils.getMetaSchemaNode(metaNode, scopeManager); | ||
const schemaProperty = utils.getMetaSchemaNodeProperty( | ||
schemaNode, | ||
scopeManager, | ||
); | ||
if (!schemaProperty) { | ||
return {}; | ||
} | ||
|
||
const metaDefaultOptions = utils | ||
.evaluateObjectProperties(metaNode, scopeManager) | ||
.find( | ||
(p) => | ||
p.type === 'Property' && utils.getKeyName(p) === 'defaultOptions', | ||
); | ||
|
||
if ( | ||
schemaProperty.type === 'ArrayExpression' && | ||
schemaProperty.elements.length === 0 | ||
) { | ||
if (metaDefaultOptions) { | ||
context.report({ | ||
node: metaDefaultOptions, | ||
messageId: 'unnecessaryDefaultOptions', | ||
fix(fixer) { | ||
return fixer.remove(metaDefaultOptions); | ||
}, | ||
}); | ||
} | ||
return {}; | ||
} | ||
|
||
if (!metaDefaultOptions) { | ||
context.report({ | ||
node: metaNode, | ||
messageId: 'missingDefaultOptions', | ||
fix(fixer) { | ||
return fixer.insertTextAfter(schemaProperty, ', defaultOptions: []'); | ||
}, | ||
}); | ||
return {}; | ||
} | ||
|
||
if (metaDefaultOptions.value.type !== 'ArrayExpression') { | ||
context.report({ | ||
node: metaDefaultOptions.value, | ||
messageId: 'defaultOptionsMustBeArray', | ||
}); | ||
return {}; | ||
} | ||
|
||
const isArrayRootSchema = | ||
schemaProperty.type === 'ObjectExpression' && | ||
schemaProperty.properties.find((property) => property.key.name === 'type') | ||
?.value.value === 'array'; | ||
|
||
if (metaDefaultOptions.value.elements.length === 0 && !isArrayRootSchema) { | ||
context.report({ | ||
node: metaDefaultOptions.value, | ||
messageId: 'defaultOptionsMustNotBeEmpty', | ||
}); | ||
return {}; | ||
} | ||
|
||
return {}; | ||
}, | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.