Skip to content

Commit

Permalink
rewrite rule
Browse files Browse the repository at this point in the history
  • Loading branch information
przemyslawjanpietrzak committed Jun 5, 2023
1 parent 1aa1de6 commit 534a2f6
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 97 deletions.
3 changes: 1 addition & 2 deletions docs/rules/force-types-on-object-props.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ description: enforce adding type declarations to object props
> enforce adding type declarations to object props
- :exclamation: <badge text="This rule has not been released yet." vertical="middle" type="error"> ***This rule has not been released yet.*** </badge>
- :gear: This rule is included in all of `"plugin:vue/base"`, `"plugin:vue/essential"`, `"plugin:vue/vue3-essential"`, `"plugin:vue/strongly-recommended"`, `"plugin:vue/vue3-strongly-recommended"`, `"plugin:vue/recommended"` and `"plugin:vue/vue3-recommended"`.

## :book: Rule Details

Expand All @@ -26,7 +25,7 @@ export default {
type: Array,

// ✓ GOOD
type: Object as Props<Anything>,
type: Object as PropType<Anything>,
type: String, // or any other primitive type
}
}
Expand Down
2 changes: 1 addition & 1 deletion docs/rules/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ Rules in this category are enabled for all presets provided by eslint-plugin-vue
| Rule ID | Description | | |
|:--------|:------------|:--:|:--:|
| [vue/comment-directive](./comment-directive.md) | support comment-directives in `<template>` | | :warning: |
| [vue/force-types-on-object-props](./force-types-on-object-props.md) | enforce adding type declarations to object props | | :hammer: |
| [vue/jsx-uses-vars](./jsx-uses-vars.md) | prevent variables used in JSX to be marked as unused | | :warning: |

</rules-table>
Expand Down Expand Up @@ -216,6 +215,7 @@ For example:
| [vue/define-emits-declaration](./define-emits-declaration.md) | enforce declaration style of `defineEmits` | | :hammer: |
| [vue/define-macros-order](./define-macros-order.md) | enforce order of `defineEmits` and `defineProps` compiler macros | :wrench: | :lipstick: |
| [vue/define-props-declaration](./define-props-declaration.md) | enforce declaration style of `defineProps` | | :hammer: |
| [vue/force-types-on-object-props](./force-types-on-object-props.md) | enforce adding type declarations to object props | | :hammer: |
| [vue/html-button-has-type](./html-button-has-type.md) | disallow usage of button without an explicit type attribute | | :hammer: |
| [vue/html-comment-content-newline](./html-comment-content-newline.md) | enforce unified line brake in HTML comments | :wrench: | :lipstick: |
| [vue/html-comment-content-spacing](./html-comment-content-spacing.md) | enforce unified spacing in HTML comments | :wrench: | :lipstick: |
Expand Down
1 change: 0 additions & 1 deletion lib/configs/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ module.exports = {
plugins: ['vue'],
rules: {
'vue/comment-directive': 'error',
'vue/force-types-on-object-props': 'error',
'vue/jsx-uses-vars': 'error'
}
}
35 changes: 8 additions & 27 deletions lib/rules/force-types-on-object-props.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,6 @@ const utils = require('../utils')
// Helpers
// ------------------------------------------------------------------------------

/**
* Check if all keys and values from second object are resent in first object
*
* @param {{ [key: string]: any }} a object to
* @param {{ [key: string]: any }} b The string to escape.
* @returns {boolean} Returns the escaped string.
*/
const isLooksLike = (a, b) =>
a &&
b &&
Object.keys(b).every((bKey) => {
const bVal = b[bKey]
const aVal = a[bKey]
if (typeof bVal === 'function') {
return bVal(aVal)
}
return bVal == null || /^[bns]/.test(typeof bVal)
? bVal === aVal
: isLooksLike(aVal, bVal)
})

/**
* @param {ComponentProp} property
* @param {RuleContext} context
Expand All @@ -44,7 +23,8 @@ const checkProperty = (property, context) => {
}

if (
isLooksLike(property.value, { type: 'Identifier', name: 'Object' }) &&
property.value.type === 'Identifier' &&
property.value.name === 'Object' &&
property.node.value.type !== 'TSAsExpression'
) {
context.report({
Expand All @@ -58,16 +38,17 @@ const checkProperty = (property, context) => {
property.value.type === 'ObjectExpression' &&
property.node.value.type === 'ObjectExpression'
) {
const typePropert = property.node.value.properties.find(
const typeProperty = property.node.value.properties.find(
(prop) =>
prop.type === 'Property' &&
prop.key.type === 'Identifier' &&
prop.key.name === 'type'
)
if (
typePropert &&
typePropert.type === 'Property' &&
isLooksLike(typePropert.value, { type: 'Identifier', name: 'Object' })
typeProperty &&
typeProperty.type === 'Property' &&
typeProperty.value.type === 'Identifier' &&
typeProperty.value.name === 'Object'
) {
context.report({
node: property.node,
Expand Down Expand Up @@ -134,7 +115,7 @@ module.exports = {
type: 'suggestion',
docs: {
description: 'enforce adding type declarations to object props',
categories: ['base'],
categories: undefined,
recommended: false,
url: 'https://eslint.vuejs.org/rules/force-types-on-object-props.html'
},
Expand Down

0 comments on commit 534a2f6

Please sign in to comment.