-
-
Notifications
You must be signed in to change notification settings - Fork 30
feat: add require-meta-docs-recommended rule #447
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 8 commits into
eslint-community:main
from
JoshuaKGoldberg:require-meta-docs-recommended
Apr 25, 2024
Merged
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
8f8b6fe
feat: add require-meta-docs-recommended rule
JoshuaKGoldberg c16ca51
Merge branch 'main' into require-meta-docs-recommended
bmish 4813c95
Apply suggestions from code review
JoshuaKGoldberg 30b859c
Apply rename
JoshuaKGoldberg af9a83e
Merge branch 'main'
JoshuaKGoldberg 754890b
Fix lint complaints
JoshuaKGoldberg 76303c0
Added allowNonBoolean opt-in option
JoshuaKGoldberg 1b23436
Unit test for unknown value
JoshuaKGoldberg 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,45 @@ | ||
# Require rules to implement a `meta.docs.recommended` property (`eslint-plugin/require-meta-docs-recommended`) | ||
|
||
<!-- end auto-generated rule header --> | ||
|
||
Utilizing `meta.docs.recommended` makes it clear from each rule implementation whether a rule is part of the `recommended` config. Some plugins also have scripting for conveniently generating their config based on this flag. | ||
|
||
However, this flag may not be appropriate for all plugins: | ||
|
||
* Extra scripting/tooling is needed to keep the flags in sync with the config | ||
* The flag may not scale to plugins that have multiple/many configs or don't have a recommended config | ||
* Or some may simply prefer to keep the source of truth solely in the config rather than duplicating config membership data in the rules | ||
|
||
## Rule Details | ||
|
||
This rule requires ESLint rules to have a valid `meta.docs.recommended` property. | ||
|
||
Examples of **incorrect** code for this rule: | ||
|
||
```js | ||
/* eslint eslint-plugin/require-meta-docs-recommended: error */ | ||
|
||
module.exports = { | ||
meta: {}, | ||
create(context) { | ||
/* ... */ | ||
}, | ||
}; | ||
``` | ||
|
||
Examples of **correct** code for this rule: | ||
|
||
```js | ||
/* eslint eslint-plugin/require-meta-docs-recommended: error */ | ||
|
||
module.exports = { | ||
meta: { recommended: true }, | ||
create(context) { | ||
/* ... */ | ||
}, | ||
}; | ||
``` | ||
|
||
## Further Reading | ||
|
||
- [Rule Structure](https://eslint.org/docs/latest/extend/custom-rules#rule-structure) |
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,46 @@ | ||
'use strict'; | ||
|
||
const utils = require('../utils'); | ||
|
||
/** @type {import('eslint').Rule.RuleModule} */ | ||
module.exports = { | ||
meta: { | ||
type: 'suggestion', | ||
docs: { | ||
description: | ||
'require rules to implement a `meta.docs.recommended` property', | ||
category: 'Rules', | ||
recommended: false, | ||
url: 'https://github.com/eslint-community/eslint-plugin-eslint-plugin/tree/HEAD/docs/rules/require-meta-docs-recommended.md', | ||
}, | ||
fixable: null, | ||
schema: [], | ||
messages: { | ||
missing: '`meta.docs.recommended` is required.', | ||
}, | ||
}, | ||
|
||
create(context) { | ||
const sourceCode = context.sourceCode || context.getSourceCode(); // TODO: just use context.sourceCode when dropping eslint < v9 | ||
const ruleInfo = utils.getRuleInfo(sourceCode); | ||
if (!ruleInfo) { | ||
return {}; | ||
} | ||
|
||
const { scopeManager } = sourceCode; | ||
const { | ||
docsNode, | ||
metaNode, | ||
metaPropertyNode: descriptionNode, | ||
} = utils.getMetaDocsProperty('recommended', ruleInfo, scopeManager); | ||
|
||
if (!descriptionNode) { | ||
context.report({ | ||
node: docsNode || metaNode || ruleInfo.create, | ||
messageId: 'missing', | ||
}); | ||
} | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
'use strict'; | ||
|
||
const rule = require('../../../lib/rules/require-meta-docs-recommended'); | ||
const RuleTester = require('../eslint-rule-tester').RuleTester; | ||
|
||
const ruleTester = new RuleTester({ | ||
languageOptions: { sourceType: 'commonjs' }, | ||
}); | ||
|
||
ruleTester.run('require-meta-docs-recommended', rule, { | ||
valid: [ | ||
'foo()', | ||
'module.exports = {};', | ||
` | ||
module.exports = { | ||
meta: { docs: { recommended: true } }, | ||
create(context) {} | ||
}; | ||
`, | ||
{ | ||
code: ` | ||
export default { | ||
meta: { docs: { recommended: 'disallow unused variables' } }, | ||
JoshuaKGoldberg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
create(context) {} | ||
}; | ||
`, | ||
languageOptions: { sourceType: 'module' }, | ||
}, | ||
` | ||
const RECOMMENDED = true; | ||
module.exports = { | ||
meta: { docs: { recommended: RECOMMENDED } }, | ||
create(context) {} | ||
}; | ||
`, | ||
|
||
` | ||
const meta = { docs: { recommended: 'enforce foo' } }; | ||
module.exports = { | ||
meta, | ||
create(context) {} | ||
}; | ||
`, | ||
` | ||
const extraDocs = { recommended: 123 }; | ||
const extraMeta = { docs: { ...extraDocs } }; | ||
module.exports = { | ||
meta: { ...extraMeta }, | ||
create(context) {} | ||
}; | ||
`, | ||
], | ||
|
||
invalid: [ | ||
{ | ||
code: 'module.exports = { create(context) {} };', | ||
output: null, | ||
errors: [{ messageId: 'missing', type: 'FunctionExpression' }], | ||
}, | ||
{ | ||
code: ` | ||
module.exports = { | ||
meta: {}, | ||
create(context) {} | ||
}; | ||
`, | ||
output: null, | ||
errors: [{ messageId: 'missing', type: 'ObjectExpression' }], | ||
}, | ||
{ | ||
code: ` | ||
const extraDocs = { }; | ||
const extraMeta = { docs: { ...extraDocs } }; | ||
module.exports = { | ||
meta: { ...extraMeta }, | ||
create(context) {} | ||
}; | ||
`, | ||
output: null, | ||
errors: [{ messageId: 'missing', type: 'Property' }], | ||
}, | ||
], | ||
}); | ||
|
||
const ruleTesterTypeScript = new RuleTester({ | ||
languageOptions: { | ||
parser: require('@typescript-eslint/parser'), | ||
parserOptions: { sourceType: 'module' }, | ||
}, | ||
}); | ||
|
||
ruleTesterTypeScript.run('require-meta-docs-recommended (TypeScript)', rule, { | ||
valid: [ | ||
` | ||
export default createESLintRule<Options, MessageIds>({ | ||
meta: { docs: { recommended: true } }, | ||
create(context) {} | ||
}); | ||
`, | ||
], | ||
invalid: [ | ||
{ | ||
code: ` | ||
export default createESLintRule<Options, MessageIds>({ | ||
meta: {}, | ||
create(context) {} | ||
}); | ||
`, | ||
output: null, | ||
errors: [{ messageId: 'missing', type: 'ObjectExpression' }], | ||
}, | ||
], | ||
}); |
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.