Skip to content
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: Simplify internal no-invalid-meta rule #14842

Merged
merged 1 commit into from Jul 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions .eslintrc.js
Expand Up @@ -66,10 +66,13 @@ module.exports = {
rules: {
"eslint-plugin/consistent-output": "error",
"eslint-plugin/no-deprecated-context-methods": "error",
"eslint-plugin/no-only-tests": "error",
"eslint-plugin/prefer-output-null": "error",
"eslint-plugin/prefer-placeholders": "error",
"eslint-plugin/prefer-replace-text": "error",
"eslint-plugin/report-message-format": ["error", "[^a-z].*\\.$"],
"eslint-plugin/require-meta-docs-description": "error",
"eslint-plugin/require-meta-schema": "error",
"eslint-plugin/require-meta-type": "error",
"eslint-plugin/test-case-property-ordering": "error",
"eslint-plugin/test-case-shorthand-strings": "error",
Expand All @@ -80,6 +83,7 @@ module.exports = {
files: ["lib/rules/*", "tools/internal-rules/*"],
excludedFiles: ["index.js"],
rules: {
"eslint-plugin/prefer-object-rule": "error",
"internal-rules/no-invalid-meta": "error",
"internal-rules/consistent-meta-messages": "error"
}
Expand Down
62 changes: 0 additions & 62 deletions tests/tools/internal-rules/no-invalid-meta.js
Expand Up @@ -96,20 +96,6 @@ ruleTester.run("no-invalid-meta", rule, {
].join("\n")
],
invalid: [
{
code: [
"module.exports = function(context) {",
" return {",
" Program: function(node) {}",
" };",
"};"
].join("\n"),
errors: [{
messageId: "incorrectExport",
line: 1,
column: 18
}]
},
{
code: [
"module.exports = {",
Expand Down Expand Up @@ -164,30 +150,6 @@ ruleTester.run("no-invalid-meta", rule, {
column: 5
}]
},
{
code: [
"module.exports = {",
" meta: {",
" docs: {",
" category: 'Internal',",
" recommended: false",
" },",
" schema: []",
" },",

" create: function(context) {",
" return {",
" Program: function(node) {}",
" };",
" }",
"};"
].join("\n"),
errors: [{
messageId: "missingMetaDocsDescription",
line: 2,
column: 5
}]
},
{
code: [
"module.exports = {",
Expand Down Expand Up @@ -236,30 +198,6 @@ ruleTester.run("no-invalid-meta", rule, {
column: 5
}]
},
{
code: [
"module.exports = {",
" meta: {",
" docs: {",
" description: 'some rule',",
" category: 'Internal',",
" recommended: false",
" }",
" },",

" create: function(context) {",
" return {",
" Program: function(node) {}",
" };",
" }",
"};"
].join("\n"),
errors: [{
messageId: "missingMetaSchema",
line: 2,
column: 5
}]
},
{
code: "",
errors: [{
Expand Down
49 changes: 1 addition & 48 deletions tools/internal-rules/no-invalid-meta.js
Expand Up @@ -50,17 +50,6 @@ function hasMetaDocs(metaPropertyNode) {
return Boolean(getPropertyFromObject("docs", metaPropertyNode.value));
}

/**
* Whether this `meta` ObjectExpression has a `docs.description` property defined or not.
* @param {ASTNode} metaPropertyNode The `meta` ObjectExpression for this rule.
* @returns {boolean} `true` if a `docs.description` property exists.
*/
function hasMetaDocsDescription(metaPropertyNode) {
const metaDocs = getPropertyFromObject("docs", metaPropertyNode.value);

return metaDocs && getPropertyFromObject("description", metaDocs.value);
}

/**
* Whether this `meta` ObjectExpression has a `docs.category` property defined or not.
* @param {ASTNode} metaPropertyNode The `meta` ObjectExpression for this rule.
Expand All @@ -83,15 +72,6 @@ function hasMetaDocsRecommended(metaPropertyNode) {
return metaDocs && getPropertyFromObject("recommended", metaDocs.value);
}

/**
* Whether this `meta` ObjectExpression has a `schema` property defined or not.
* @param {ASTNode} metaPropertyNode The `meta` ObjectExpression for this rule.
* @returns {boolean} `true` if a `schema` property exists.
*/
function hasMetaSchema(metaPropertyNode) {
return getPropertyFromObject("schema", metaPropertyNode.value);
}

/**
* Checks the validity of the meta definition of this rule and reports any errors found.
* @param {RuleContext} context The ESLint rule context.
Expand All @@ -111,35 +91,16 @@ function checkMetaValidity(context, exportsNode) {
return;
}

if (!hasMetaDocsDescription(metaProperty)) {
context.report({ node: metaProperty, messageId: "missingMetaDocsDescription" });
return;
}

if (!hasMetaDocsCategory(metaProperty)) {
context.report({ node: metaProperty, messageId: "missingMetaDocsCategory" });
return;
}

if (!hasMetaDocsRecommended(metaProperty)) {
context.report({ node: metaProperty, messageId: "missingMetaDocsRecommended" });
return;
}

if (!hasMetaSchema(metaProperty)) {
context.report({ node: metaProperty, messageId: "missingMetaSchema" });
}
}

/**
* Whether this node is the correct format for a rule definition or not.
* @param {ASTNode} node node that the rule exports.
* @returns {boolean} `true` if the exported node is the correct format for a rule definition
*/
function isCorrectExportsFormat(node) {
return node.type === "ObjectExpression";
}

//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
Expand All @@ -156,12 +117,9 @@ module.exports = {
messages: {
missingMeta: "Rule is missing a meta property.",
missingMetaDocs: "Rule is missing a meta.docs property.",
missingMetaDocsDescription: "Rule is missing a meta.docs.description property.",
missingMetaDocsCategory: "Rule is missing a meta.docs.category property.",
missingMetaDocsRecommended: "Rule is missing a meta.docs.recommended property.",
missingMetaSchema: "Rule is missing a meta.schema property.",
noExport: "Rule does not export anything. Make sure rule exports an object according to new rule format.",
incorrectExport: "Rule does not export an Object. Make sure the rule follows the new rule format."
noExport: "Rule does not export anything. Make sure rule exports an object according to new rule format."
}
},

Expand All @@ -186,11 +144,6 @@ module.exports = {
node,
messageId: "noExport"
});
} else if (!isCorrectExportsFormat(exportsNode)) {
context.report({
node: exportsNode,
messageId: "incorrectExport"
});
} else {
checkMetaValidity(context, exportsNode);
}
Expand Down