Skip to content

Commit

Permalink
Improve no-invalid-meta internal rule (#2647)
Browse files Browse the repository at this point in the history
  • Loading branch information
FloEdelmann authored Dec 16, 2024
1 parent 9453949 commit b5a8260
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 49 deletions.
61 changes: 22 additions & 39 deletions eslint-internal-rules/no-invalid-meta.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,38 +23,6 @@ function getPropertyFromObject(propertyName, node) {
return null
}

/**
* Extracts the `meta` property from the ObjectExpression that all rules export.
*
* @param {ASTNode} exportsNode ObjectExpression node that the rule exports.
* @returns {ASTNode} The `meta` Property node or null if not found.
*/
function getMetaPropertyFromExportsNode(exportsNode) {
return getPropertyFromObject('meta', exportsNode)
}

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

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

return metaDocs && getPropertyFromObject('categories', metaDocs.value)
}

/**
* Checks the validity of the meta definition of this rule and reports any errors found.
*
Expand All @@ -64,8 +32,7 @@ function hasMetaDocsCategories(metaPropertyNode) {
* @returns {void}
*/
function checkMetaValidity(context, exportsNode) {
const metaProperty = getMetaPropertyFromExportsNode(exportsNode)

const metaProperty = getPropertyFromObject('meta', exportsNode)
if (!metaProperty) {
context.report({
node: exportsNode,
Expand All @@ -74,21 +41,35 @@ function checkMetaValidity(context, exportsNode) {
return
}

if (!hasMetaDocs(metaProperty)) {
const metaDocs = getPropertyFromObject('docs', metaProperty.value)
if (!metaDocs) {
context.report({
node: 'metaDocs',
node: metaProperty,
messageId: 'missingMetaDocs'
})
return
}

if (!hasMetaDocsCategories(metaProperty)) {
const metaDocsCategories = getPropertyFromObject('categories', metaDocs.value)
if (!metaDocsCategories) {
context.report({
node: metaProperty,
node: metaDocs,
messageId: 'missingMetaDocsCategories'
})
return
}

const metaDocsRecommended = getPropertyFromObject(
'recommended',
metaDocs.value
)
if (metaDocsRecommended) {
context.report({
node: metaDocsRecommended,
messageId: 'invalidMetaDocsRecommended'
})
return
}
}

module.exports = {
Expand All @@ -103,7 +84,9 @@ module.exports = {
missingMeta: 'Rule is missing a meta property.',
missingMetaDocs: 'Rule is missing a meta.docs property.',
missingMetaDocsCategories:
'Rule is missing a meta.docs.categories property.'
'Rule is missing a meta.docs.categories property.',
invalidMetaDocsRecommended:
'Rule should not have a meta.docs.recommended property.'
}
},

Expand Down
1 change: 0 additions & 1 deletion lib/rules/no-duplicate-attr-inheritance.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ module.exports = {
description:
'enforce `inheritAttrs` to be set to `false` when using `v-bind="$attrs"`',
categories: undefined,
recommended: false,
url: 'https://eslint.vuejs.org/rules/no-duplicate-attr-inheritance.html'
},
fixable: null,
Expand Down
1 change: 0 additions & 1 deletion lib/rules/no-potential-component-option-typo.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ module.exports = {
docs: {
description: 'disallow a potential typo in your component property',
categories: undefined,
recommended: false,
url: 'https://eslint.vuejs.org/rules/no-potential-component-option-typo.html'
},
fixable: null,
Expand Down
4 changes: 2 additions & 2 deletions lib/rules/no-ref-object-destructure.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
const baseRule = require('./no-ref-object-reactivity-loss')

module.exports = {
// eslint-disable-next-line eslint-plugin/require-meta-schema, eslint-plugin/prefer-message-ids, internal/no-invalid-meta, eslint-plugin/require-meta-type -- inherit schema from base rule
// eslint-disable-next-line eslint-plugin/require-meta-schema, eslint-plugin/prefer-message-ids, eslint-plugin/require-meta-type -- inherit schema from base rule
meta: {
...baseRule.meta,
// eslint-disable-next-line eslint-plugin/require-meta-docs-description, internal/no-invalid-meta-docs-categories, eslint-plugin/meta-property-ordering
// eslint-disable-next-line eslint-plugin/require-meta-docs-description, internal/no-invalid-meta, internal/no-invalid-meta-docs-categories, eslint-plugin/meta-property-ordering
docs: {
...baseRule.meta.docs,
url: 'https://eslint.vuejs.org/rules/no-ref-object-destructure.html'
Expand Down
4 changes: 2 additions & 2 deletions lib/rules/no-setup-props-destructure.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
const baseRule = require('./no-setup-props-reactivity-loss')

module.exports = {
// eslint-disable-next-line eslint-plugin/require-meta-schema, eslint-plugin/prefer-message-ids, internal/no-invalid-meta, eslint-plugin/require-meta-type -- inherit schema from base rule
// eslint-disable-next-line eslint-plugin/require-meta-schema, eslint-plugin/prefer-message-ids, eslint-plugin/require-meta-type -- inherit schema from base rule
meta: {
...baseRule.meta,
// eslint-disable-next-line eslint-plugin/require-meta-docs-description, internal/no-invalid-meta-docs-categories, eslint-plugin/meta-property-ordering
// eslint-disable-next-line eslint-plugin/require-meta-docs-description, internal/no-invalid-meta, internal/no-invalid-meta-docs-categories, eslint-plugin/meta-property-ordering
docs: {
...baseRule.meta.docs,
url: 'https://eslint.vuejs.org/rules/no-setup-props-destructure.html'
Expand Down
1 change: 0 additions & 1 deletion lib/rules/require-typed-object-prop.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ module.exports = {
docs: {
description: 'enforce adding type declarations to object props',
categories: undefined,
recommended: false,
url: 'https://eslint.vuejs.org/rules/require-typed-object-prop.html'
},
fixable: null,
Expand Down
1 change: 0 additions & 1 deletion lib/rules/sort-keys.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ module.exports = {
description:
'enforce sort-keys in a manner that is compatible with order-in-components',
categories: null,
recommended: false,
url: 'https://eslint.vuejs.org/rules/sort-keys.html'
},
fixable: null,
Expand Down
1 change: 0 additions & 1 deletion lib/rules/v-for-delimiter-style.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ module.exports = {
docs: {
description: "enforce `v-for` directive's delimiter style",
categories: undefined,
recommended: false,
url: 'https://eslint.vuejs.org/rules/v-for-delimiter-style.html'
},
fixable: 'code',
Expand Down
1 change: 0 additions & 1 deletion lib/rules/v-if-else-key.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,6 @@ module.exports = {
description:
'require key attribute for conditionally rendered repeated components',
categories: null,
recommended: false,
url: 'https://eslint.vuejs.org/rules/v-if-else-key.html'
},
// eslint-disable-next-line eslint-plugin/require-meta-fixable -- fixer is not recognized
Expand Down

0 comments on commit b5a8260

Please sign in to comment.