-
-
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 1 commit
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,39 @@ | ||
# Require rules to implement a `meta.docs.recommended` property (`eslint-plugin/require-meta-docs-recommended`) | ||
|
||
<!-- end auto-generated rule header --> | ||
|
||
Defining a whether recommended value for each rule can help developers understand whether they're recommended. | ||
|
||
## 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 | ||
|
||
- [working-with-rules#options-schemas](https://eslint.org/docs/developer-guide/working-with-rules#options-schemas) | ||
JoshuaKGoldberg marked this conversation as resolved.
Show resolved
Hide resolved
|
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.getMetaProperty('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,107 @@ | ||
'use strict'; | ||
|
||
const rule = require('../../../lib/rules/require-meta-docs-recommended'); | ||
const RuleTester = require('eslint').RuleTester; | ||
|
||
const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 9 } }); | ||
ruleTester.run('require-meta-docs-recommended', rule, { | ||
valid: [ | ||
JoshuaKGoldberg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
'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) {} | ||
}; | ||
`, | ||
parserOptions: { 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({ | ||
parserOptions: { sourceType: 'module' }, | ||
parser: require.resolve('@typescript-eslint/parser'), | ||
}); | ||
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' }], | ||
}, | ||
], | ||
}); |
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.