-
-
Notifications
You must be signed in to change notification settings - Fork 30
Add new rule require-meta-docs-description
#89
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
not-an-aardvark
merged 2 commits into
eslint-community:master
from
bmish:require-meta-docs-description
Dec 30, 2019
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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,47 @@ | ||
# require rules to implement a meta.docs.description property (require-meta-docs-description) | ||
|
||
Defining a clear and consistent description for each rule helps developers understand what they're used for. | ||
|
||
In particular, each rule description should begin with an allowed prefix: | ||
* `enforce` | ||
* `require` | ||
* `disallow` | ||
|
||
## Rule Details | ||
|
||
This rule requires ESLint rules to have a valid `meta.docs.description` property. | ||
|
||
Examples of **incorrect** code for this rule: | ||
|
||
```js | ||
/* eslint eslint-plugin/require-meta-docs-description: error */ | ||
module.exports = { | ||
meta: {}, | ||
create: function(context) { /* ... */} | ||
}; | ||
|
||
module.exports = { | ||
meta: { description: 'this rule does ...' }, // missing allowed prefix | ||
create: function(context) { /* ... */} | ||
}; | ||
``` | ||
|
||
Examples of **correct** code for this rule: | ||
|
||
```js | ||
/* eslint eslint-plugin/require-meta-docs-description: error */ | ||
module.exports = { | ||
meta: { description: 'disallow unused variables' }, | ||
create: function(context) { /* ... */} | ||
}; | ||
``` | ||
|
||
## Options | ||
|
||
This rule takes an optional object containing: | ||
|
||
- `String` — `pattern` — A regular expression that the description must match. Use `'.+'` to allow anything. Defaults to `^(enforce|require|disallow)`. | ||
|
||
## 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
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,87 @@ | ||
'use strict'; | ||
|
||
const utils = require('../utils'); | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Rule Definition | ||
// ------------------------------------------------------------------------------ | ||
|
||
const DEFAULT_PATTERN = new RegExp('^(enforce|require|disallow)'); | ||
|
||
/** | ||
* Whether or not the node is a string literal | ||
* | ||
* @param {object} node | ||
* @returns {boolean} whether or not the node is a string literal | ||
*/ | ||
function isStringLiteral (node) { | ||
return node.type === 'Literal' && typeof node.value === 'string'; | ||
} | ||
|
||
module.exports = { | ||
meta: { | ||
docs: { | ||
description: 'require rules to implement a meta.docs.description property with the correct format', | ||
category: 'Rules', | ||
recommended: false, // TODO: enable it in a major release. | ||
}, | ||
type: 'suggestion', | ||
fixable: null, | ||
schema: [ | ||
{ | ||
type: 'object', | ||
properties: { | ||
pattern: { | ||
type: 'string', | ||
}, | ||
}, | ||
additionalProperties: false, | ||
}, | ||
], | ||
messages: { | ||
missing: '`meta.docs.description` is required.', | ||
wrongType: '`meta.docs.description` must be a non-empty string.', | ||
extraWhitespace: '`meta.docs.description` must not have leading nor trailing whitespace.', | ||
}, | ||
}, | ||
|
||
create (context) { | ||
const sourceCode = context.getSourceCode(); | ||
const info = utils.getRuleInfo(sourceCode.ast, sourceCode.scopeManager); | ||
|
||
return { | ||
Program () { | ||
if (info === null || info.meta === null) { | ||
return; | ||
} | ||
|
||
const pattern = context.options[0] && context.options[0].pattern ? new RegExp(context.options[0].pattern) : DEFAULT_PATTERN; | ||
|
||
const metaNode = info.meta; | ||
const docsNode = | ||
metaNode && | ||
metaNode.properties && | ||
metaNode.properties.find(p => p.type === 'Property' && utils.getKeyName(p) === 'docs'); | ||
|
||
const descriptionNode = | ||
docsNode && | ||
docsNode.value.properties && | ||
docsNode.value.properties.find(p => p.type === 'Property' && utils.getKeyName(p) === 'description'); | ||
|
||
if (!descriptionNode) { | ||
context.report({ node: docsNode ? docsNode : metaNode, messageId: 'missing' }); | ||
} else if (!isStringLiteral(descriptionNode.value) || descriptionNode.value.value === '') { | ||
context.report({ node: descriptionNode.value, messageId: 'wrongType' }); | ||
} else if (descriptionNode.value.value !== descriptionNode.value.value.trim()) { | ||
context.report({ node: descriptionNode.value, messageId: 'extraWhitespace' }); | ||
} else if (!pattern.test(descriptionNode.value.value)) { | ||
context.report({ | ||
node: descriptionNode.value, | ||
message: '`meta.docs.description` must match the regexp {{pattern}}.', | ||
data: { pattern }, | ||
}); | ||
} | ||
}, | ||
}; | ||
}, | ||
}; |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this one should probably be configurable with something like a regex (maybe with
enforce
/require
/disallow
as the default). As far as I know,enforce
/require
/disallow
is a convention used by the ESLint team but it's not universally adopted.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point, I'll update shortly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added an
allowedPrefixes
array option to the rule.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd really rather this be a regex option -- choosing a set of specific prefixes feels like an oddly specific restriction to impose on users of the rule.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@not-an-aardvark sounds good, I switched to a
pattern
option.