-
-
Notifications
You must be signed in to change notification settings - Fork 31
Add new rule require-meta-schema-description
#490
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 6 commits into
eslint-community:main
from
JoshuaKGoldberg:require-meta-schema-description
Oct 24, 2024
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
69312a0
feat: add require-meta-schema-description rule
JoshuaKGoldberg c0616a6
Update lib/rules/require-meta-schema-description.js
JoshuaKGoldberg 607329c
Added tests too
JoshuaKGoldberg f787d23
recommended: false
JoshuaKGoldberg ba6f191
Fill in test coverage
JoshuaKGoldberg 0f4e095
npm run update:eslint-docs
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,90 @@ | ||
# Require rules `meta.schema` properties to include descriptions (`eslint-plugin/require-meta-schema-description`) | ||
|
||
💼 This rule is enabled in the ✅ `recommended` [config](https://github.com/eslint-community/eslint-plugin-eslint-plugin#presets). | ||
|
||
<!-- end auto-generated rule header --> | ||
|
||
Defining a description in the schema for each rule option helps explain that option to users. | ||
It also allows documentation generators such as [`eslint-doc-generator`](https://github.com/bmish/eslint-doc-generator) to generate more informative documentation for users. | ||
|
||
## Rule Details | ||
|
||
This rule requires that if a rule option has a property in the rule's `meta.schema`, it should have a `description`. | ||
|
||
Examples of **incorrect** code for this rule: | ||
|
||
```js | ||
/* eslint eslint-plugin/require-meta-schema-description: error */ | ||
|
||
module.exports = { | ||
meta: { | ||
schema: [ | ||
{ | ||
elements: { type: 'string' }, | ||
type: 'array', | ||
}, | ||
], | ||
}, | ||
create() {}, | ||
}; | ||
|
||
module.exports = { | ||
meta: { | ||
schema: [ | ||
{ | ||
properties: { | ||
something: { | ||
type: 'string', | ||
}, | ||
}, | ||
type: 'object', | ||
}, | ||
], | ||
}, | ||
create() {}, | ||
}; | ||
``` | ||
|
||
Examples of **correct** code for this rule: | ||
|
||
```js | ||
/* eslint eslint-plugin/require-meta-schema-description: error */ | ||
|
||
module.exports = { | ||
meta: { | ||
schema: [ | ||
{ | ||
description: 'Elements to allow.', | ||
elements: { type: 'string' }, | ||
type: 'array', | ||
}, | ||
], | ||
}, | ||
create() {}, | ||
}; | ||
|
||
module.exports = { | ||
meta: { | ||
schema: [ | ||
{ | ||
oneOf: [ | ||
{ | ||
description: 'Elements to allow.', | ||
elements: { type: 'string' }, | ||
type: 'array', | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
create() {}, | ||
}; | ||
``` | ||
|
||
## When Not To Use It | ||
|
||
If your rule options are very simple and well-named, and your rule isn't used by developers outside of your immediate team, you may not need this rule. | ||
|
||
## Further Reading | ||
|
||
- [working-with-rules#options-schemas](https://eslint.org/docs/developer-guide/working-with-rules#options-schemas) |
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,102 @@ | ||
'use strict'; | ||
|
||
const utils = require('../utils'); | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Rule Definition | ||
// ------------------------------------------------------------------------------ | ||
|
||
/** @type {import('eslint').Rule.RuleModule} */ | ||
module.exports = { | ||
meta: { | ||
type: 'suggestion', | ||
docs: { | ||
description: | ||
'require rules `meta.schema` properties to include descriptions', | ||
category: 'Rules', | ||
recommended: true, | ||
url: 'https://github.com/eslint-community/eslint-plugin-eslint-plugin/tree/HEAD/docs/rules/require-meta-schema-description.md', | ||
}, | ||
schema: [], | ||
messages: { | ||
missingDescription: 'Schema option is missing an ajv description.', | ||
}, | ||
}, | ||
|
||
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 schemaNode = utils.getMetaSchemaNode(ruleInfo.meta, scopeManager); | ||
if (!schemaNode) { | ||
return {}; | ||
} | ||
|
||
const schemaProperty = utils.getMetaSchemaNodeProperty( | ||
schemaNode, | ||
scopeManager, | ||
); | ||
if (schemaProperty?.type !== 'ArrayExpression') { | ||
return {}; | ||
} | ||
|
||
for (const element of schemaProperty.elements) { | ||
checkSchemaElement(element, true); | ||
} | ||
|
||
return {}; | ||
|
||
function checkSchemaElement(node, isRoot) { | ||
if (node.type !== 'ObjectExpression') { | ||
return; | ||
} | ||
|
||
let hadChildren = false; | ||
let hadDescription = false; | ||
|
||
for (const { key, value } of node.properties) { | ||
switch (key?.name) { | ||
JoshuaKGoldberg marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
case 'description': { | ||
hadDescription = true; | ||
break; | ||
} | ||
|
||
case 'oneOf': { | ||
hadChildren = true; | ||
|
||
if (value.type === 'ArrayExpression') { | ||
for (const element of value.elements) { | ||
checkSchemaElement(element, isRoot); | ||
} | ||
} | ||
|
||
break; | ||
} | ||
|
||
case 'properties': { | ||
hadChildren = true; | ||
|
||
for (const property of value.properties) { | ||
if (property.value?.type === 'ObjectExpression') { | ||
checkSchemaElement(property.value); | ||
} | ||
} | ||
|
||
break; | ||
} | ||
} | ||
} | ||
|
||
if (!hadDescription && !(hadChildren && isRoot)) { | ||
context.report({ | ||
messageId: 'missingDescription', | ||
node, | ||
}); | ||
} | ||
} | ||
}, | ||
}; |
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.