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
Chore: add internal rule to enforce meta.docs conventions (fixes #6954) #7155
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
342e3b7
Chore: add internal rule to enforce meta.docs conventions (fixes #6954)
vitorbal 8b998a2
Chore: add more tests for non-lowercase
vitorbal f835537
Chore: add more tests and check for leading whitespaces
vitorbal f847c52
Chore: add separate reports for whitespace issues
vitorbal 69f71e7
Chore: fix typos
vitorbal File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains 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 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,3 @@ | ||
rules: | ||
internal-no-invalid-meta: "error" | ||
internal-consistent-docs-description: "error" |
131 changes: 131 additions & 0 deletions
131
lib/internal-rules/internal-consistent-docs-description.js
This file contains 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,131 @@ | ||
/** | ||
* @fileoverview Internal rule to enforce meta.docs.description conventions. | ||
* @author Vitor Balocco | ||
*/ | ||
|
||
"use strict"; | ||
|
||
const ALLOWED_FIRST_WORDS = [ | ||
"enforce", | ||
"require", | ||
"disallow" | ||
]; | ||
|
||
//------------------------------------------------------------------------------ | ||
// Helpers | ||
//------------------------------------------------------------------------------ | ||
|
||
/** | ||
* Gets the property of the Object node passed in that has the name specified. | ||
* | ||
* @param {string} property Name of the property to return. | ||
* @param {ASTNode} node The ObjectExpression node. | ||
* @returns {ASTNode} The Property node or null if not found. | ||
*/ | ||
function getPropertyFromObject(property, node) { | ||
const properties = node.properties; | ||
|
||
for (let i = 0; i < properties.length; i++) { | ||
if (properties[i].key.name === property) { | ||
return properties[i]; | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
|
||
/** | ||
* Verifies that the meta.docs.description property follows our internal conventions. | ||
* | ||
* @param {RuleContext} context The ESLint rule context. | ||
* @param {ASTNode} exportsNode ObjectExpression node that the rule exports. | ||
* @returns {void} | ||
*/ | ||
function checkMetaDocsDescription(context, exportsNode) { | ||
if (exportsNode.type !== "ObjectExpression") { | ||
|
||
// if the exported node is not the correct format, "internal-no-invalid-meta" will already report this. | ||
return; | ||
} | ||
|
||
const metaProperty = getPropertyFromObject("meta", exportsNode); | ||
const metaDocs = metaProperty && getPropertyFromObject("docs", metaProperty.value); | ||
const metaDocsDescription = metaDocs && getPropertyFromObject("description", metaDocs.value); | ||
|
||
if (!metaDocsDescription) { | ||
|
||
// if there is no `meta.docs.description` property, "internal-no-invalid-meta" will already report this. | ||
return; | ||
} | ||
|
||
const description = metaDocsDescription.value.value; | ||
|
||
if (typeof description !== "string") { | ||
context.report({ | ||
node: metaDocsDescription.value, | ||
message: "`meta.docs.description` should be a string." | ||
}); | ||
return; | ||
} | ||
|
||
if (description === "") { | ||
context.report({ | ||
node: metaDocsDescription.value, | ||
message: "`meta.docs.description` should not be empty.", | ||
}); | ||
return; | ||
} | ||
|
||
if (description.indexOf(" ") === 0) { | ||
context.report({ | ||
node: metaDocsDescription.value, | ||
message: "`meta.docs.description` should not start with whitespace." | ||
}); | ||
return; | ||
} | ||
|
||
const firstWord = description.split(" ")[0]; | ||
|
||
if (ALLOWED_FIRST_WORDS.indexOf(firstWord) === -1) { | ||
context.report({ | ||
node: metaDocsDescription.value, | ||
message: "`meta.docs.description` should start with one of the following words: {{ allowedWords }}. Started with \"{{ firstWord }}\" instead.", | ||
data: { | ||
allowedWords: ALLOWED_FIRST_WORDS.join(", "), | ||
firstWord | ||
} | ||
}); | ||
return; | ||
} | ||
} | ||
|
||
//------------------------------------------------------------------------------ | ||
// Rule Definition | ||
//------------------------------------------------------------------------------ | ||
|
||
module.exports = { | ||
meta: { | ||
docs: { | ||
description: "enforce correct conventions of `meta.docs.description` property in core rules", | ||
category: "Internal", | ||
recommended: false | ||
}, | ||
|
||
schema: [] | ||
}, | ||
|
||
create(context) { | ||
return { | ||
AssignmentExpression(node) { | ||
if (node.left && | ||
node.right && | ||
node.left.type === "MemberExpression" && | ||
node.left.object.name === "module" && | ||
node.left.property.name === "exports") { | ||
|
||
checkMetaDocsDescription(context, node.right); | ||
} | ||
} | ||
}; | ||
} | ||
}; |
This file contains 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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
rules: | ||
internal-no-invalid-meta: "error" | ||
internal-consistent-docs-description: "error" |
This file contains 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.
What if the description is
" requires spaces"
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.
@gyandeeps That's a good point, thanks! I pushed a new commit that uses a regular expression to try to get the first word instead of relying on
.split()
. I also added some extra tests for that. Let me know what you think!