Skip to content

Commit

Permalink
rewrite rule
Browse files Browse the repository at this point in the history
  • Loading branch information
przemyslawjanpietrzak committed Mar 23, 2023
1 parent 1aa1de6 commit 3dd381f
Show file tree
Hide file tree
Showing 2 changed files with 139 additions and 93 deletions.
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,18 @@ 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'
// isLooksLike(typeProperty.value, { type: 'Identifier', name: 'Object' })
) {
context.report({
node: property.node,
Expand Down Expand Up @@ -134,7 +116,6 @@ module.exports = {
type: 'suggestion',
docs: {
description: 'enforce adding type declarations to object props',
categories: ['base'],
recommended: false,
url: 'https://eslint.vuejs.org/rules/force-types-on-object-props.html'
},
Expand Down

0 comments on commit 3dd381f

Please sign in to comment.