Skip to content

Commit 534a2f6

Browse files
rewrite rule
1 parent 1aa1de6 commit 534a2f6

File tree

5 files changed

+141
-97
lines changed

5 files changed

+141
-97
lines changed

docs/rules/force-types-on-object-props.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ description: enforce adding type declarations to object props
99
> enforce adding type declarations to object props
1010
1111
- :exclamation: <badge text="This rule has not been released yet." vertical="middle" type="error"> ***This rule has not been released yet.*** </badge>
12-
- :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"`.
1312

1413
## :book: Rule Details
1514

@@ -26,7 +25,7 @@ export default {
2625
type: Array,
2726

2827
// ✓ GOOD
29-
type: Object as Props<Anything>,
28+
type: Object as PropType<Anything>,
3029
type: String, // or any other primitive type
3130
}
3231
}

docs/rules/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ Rules in this category are enabled for all presets provided by eslint-plugin-vue
2828
| Rule ID | Description | | |
2929
|:--------|:------------|:--:|:--:|
3030
| [vue/comment-directive](./comment-directive.md) | support comment-directives in `<template>` | | :warning: |
31-
| [vue/force-types-on-object-props](./force-types-on-object-props.md) | enforce adding type declarations to object props | | :hammer: |
3231
| [vue/jsx-uses-vars](./jsx-uses-vars.md) | prevent variables used in JSX to be marked as unused | | :warning: |
3332

3433
</rules-table>
@@ -216,6 +215,7 @@ For example:
216215
| [vue/define-emits-declaration](./define-emits-declaration.md) | enforce declaration style of `defineEmits` | | :hammer: |
217216
| [vue/define-macros-order](./define-macros-order.md) | enforce order of `defineEmits` and `defineProps` compiler macros | :wrench: | :lipstick: |
218217
| [vue/define-props-declaration](./define-props-declaration.md) | enforce declaration style of `defineProps` | | :hammer: |
218+
| [vue/force-types-on-object-props](./force-types-on-object-props.md) | enforce adding type declarations to object props | | :hammer: |
219219
| [vue/html-button-has-type](./html-button-has-type.md) | disallow usage of button without an explicit type attribute | | :hammer: |
220220
| [vue/html-comment-content-newline](./html-comment-content-newline.md) | enforce unified line brake in HTML comments | :wrench: | :lipstick: |
221221
| [vue/html-comment-content-spacing](./html-comment-content-spacing.md) | enforce unified spacing in HTML comments | :wrench: | :lipstick: |

lib/configs/base.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ module.exports = {
1616
plugins: ['vue'],
1717
rules: {
1818
'vue/comment-directive': 'error',
19-
'vue/force-types-on-object-props': 'error',
2019
'vue/jsx-uses-vars': 'error'
2120
}
2221
}

lib/rules/force-types-on-object-props.js

Lines changed: 8 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,6 @@ const utils = require('../utils')
1313
// Helpers
1414
// ------------------------------------------------------------------------------
1515

16-
/**
17-
* Check if all keys and values from second object are resent in first object
18-
*
19-
* @param {{ [key: string]: any }} a object to
20-
* @param {{ [key: string]: any }} b The string to escape.
21-
* @returns {boolean} Returns the escaped string.
22-
*/
23-
const isLooksLike = (a, b) =>
24-
a &&
25-
b &&
26-
Object.keys(b).every((bKey) => {
27-
const bVal = b[bKey]
28-
const aVal = a[bKey]
29-
if (typeof bVal === 'function') {
30-
return bVal(aVal)
31-
}
32-
return bVal == null || /^[bns]/.test(typeof bVal)
33-
? bVal === aVal
34-
: isLooksLike(aVal, bVal)
35-
})
36-
3716
/**
3817
* @param {ComponentProp} property
3918
* @param {RuleContext} context
@@ -44,7 +23,8 @@ const checkProperty = (property, context) => {
4423
}
4524

4625
if (
47-
isLooksLike(property.value, { type: 'Identifier', name: 'Object' }) &&
26+
property.value.type === 'Identifier' &&
27+
property.value.name === 'Object' &&
4828
property.node.value.type !== 'TSAsExpression'
4929
) {
5030
context.report({
@@ -58,16 +38,17 @@ const checkProperty = (property, context) => {
5838
property.value.type === 'ObjectExpression' &&
5939
property.node.value.type === 'ObjectExpression'
6040
) {
61-
const typePropert = property.node.value.properties.find(
41+
const typeProperty = property.node.value.properties.find(
6242
(prop) =>
6343
prop.type === 'Property' &&
6444
prop.key.type === 'Identifier' &&
6545
prop.key.name === 'type'
6646
)
6747
if (
68-
typePropert &&
69-
typePropert.type === 'Property' &&
70-
isLooksLike(typePropert.value, { type: 'Identifier', name: 'Object' })
48+
typeProperty &&
49+
typeProperty.type === 'Property' &&
50+
typeProperty.value.type === 'Identifier' &&
51+
typeProperty.value.name === 'Object'
7152
) {
7253
context.report({
7354
node: property.node,
@@ -134,7 +115,7 @@ module.exports = {
134115
type: 'suggestion',
135116
docs: {
136117
description: 'enforce adding type declarations to object props',
137-
categories: ['base'],
118+
categories: undefined,
138119
recommended: false,
139120
url: 'https://eslint.vuejs.org/rules/force-types-on-object-props.html'
140121
},

0 commit comments

Comments
 (0)