Skip to content

Commit 0f38fa2

Browse files
committed
minor refactor
1 parent 8ffbe00 commit 0f38fa2

File tree

2 files changed

+56
-55
lines changed

2 files changed

+56
-55
lines changed

lib/rules/require-valid-default-prop.js

Lines changed: 2 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -70,59 +70,6 @@ function getTypes(targetNode) {
7070
return []
7171
}
7272

73-
/**
74-
* Extracts default definitions using assignment patterns.
75-
* @param {CallExpression} node The node of defineProps
76-
* @returns { { [key: string]: Expression | undefined } }
77-
*/
78-
function getDefaultsPropExpressionsForAssignmentPatterns(node) {
79-
const left = getLeftOfDefineProps(node)
80-
if (!left || left.type !== 'ObjectPattern') {
81-
return {}
82-
}
83-
/** @type { { [key: string]: Expression | undefined } } */
84-
const result = Object.create(null)
85-
for (const prop of left.properties) {
86-
if (prop.type !== 'Property') continue
87-
const value = prop.value
88-
if (value.type !== 'AssignmentPattern') continue
89-
const defaultNode = value.right
90-
const name = utils.getStaticPropertyName(prop)
91-
if (name != null) {
92-
result[name] = defaultNode
93-
}
94-
}
95-
return result
96-
}
97-
98-
/**
99-
* Gets the pattern of the left operand of defineProps.
100-
* @param {CallExpression} node The node of defineProps
101-
* @returns {Pattern | null} The pattern of the left operand of defineProps
102-
*/
103-
function getLeftOfDefineProps(node) {
104-
let target = node
105-
if (
106-
target.parent &&
107-
target.parent.type === 'CallExpression' &&
108-
target.parent.arguments[0] === target &&
109-
target.parent.callee.type === 'Identifier' &&
110-
target.parent.callee.name === 'withDefaults'
111-
) {
112-
target = target.parent
113-
}
114-
if (!target.parent) {
115-
return null
116-
}
117-
if (
118-
target.parent.type === 'VariableDeclarator' &&
119-
target.parent.init === target
120-
) {
121-
return target.parent.id
122-
}
123-
return null
124-
}
125-
12673
module.exports = {
12774
meta: {
12875
type: 'suggestion',
@@ -477,11 +424,11 @@ module.exports = {
477424
const defaultsByWithDefaults =
478425
utils.getWithDefaultsPropExpressions(node)
479426
const defaultsByAssignmentPatterns =
480-
getDefaultsPropExpressionsForAssignmentPatterns(node)
427+
utils.getDefaultPropExpressionsForPropsDestructure(node)
481428
const propContexts = processPropDefs(props, (propName) =>
482429
[
483430
defaultsByWithDefaults[propName],
484-
defaultsByAssignmentPatterns[propName]
431+
defaultsByAssignmentPatterns[propName]?.expression
485432
].filter(utils.isDef)
486433
)
487434
scriptSetupPropsContexts.push({ node, props: propContexts })

lib/utils/index.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1537,6 +1537,13 @@ module.exports = {
15371537
* @returns { { [key: string]: Property | undefined } }
15381538
*/
15391539
getWithDefaultsProps,
1540+
/**
1541+
* Gets the default definition nodes for defineProp
1542+
* using the props destructure with assignment pattern.
1543+
* @param {CallExpression} node The node of defineProps
1544+
* @returns { Record<string, {prop: AssignmentProperty , expression: Expression} | undefined> }
1545+
*/
1546+
getDefaultPropExpressionsForPropsDestructure,
15401547

15411548
getVueObjectType,
15421549
/**
@@ -3144,6 +3151,53 @@ function getWithDefaultsProps(node) {
31443151
return result
31453152
}
31463153

3154+
/**
3155+
* Gets the default definition nodes for defineProp
3156+
* using the props destructure with assignment pattern.
3157+
* @param {CallExpression} node The node of defineProps
3158+
* @returns { Record<string, {prop: AssignmentProperty , expression: Expression} | undefined> }
3159+
*/
3160+
function getDefaultPropExpressionsForPropsDestructure(node) {
3161+
const left = getLeftOfDefineProps(node)
3162+
if (!left || left.type !== 'ObjectPattern') {
3163+
return {}
3164+
}
3165+
/** @type {ReturnType<typeof getDefaultPropExpressionsForPropsDestructure>} */
3166+
const result = Object.create(null)
3167+
for (const prop of left.properties) {
3168+
if (prop.type !== 'Property') continue
3169+
const value = prop.value
3170+
if (value.type !== 'AssignmentPattern') continue
3171+
const name = getStaticPropertyName(prop)
3172+
if (name != null) {
3173+
result[name] = { prop, expression: value.right }
3174+
}
3175+
}
3176+
return result
3177+
}
3178+
3179+
/**
3180+
* Gets the pattern of the left operand of defineProps.
3181+
* @param {CallExpression} node The node of defineProps
3182+
* @returns {Pattern | null} The pattern of the left operand of defineProps
3183+
*/
3184+
function getLeftOfDefineProps(node) {
3185+
let target = node
3186+
if (hasWithDefaults(target)) {
3187+
target = target.parent
3188+
}
3189+
if (!target.parent) {
3190+
return null
3191+
}
3192+
if (
3193+
target.parent.type === 'VariableDeclarator' &&
3194+
target.parent.init === target
3195+
) {
3196+
return target.parent.id
3197+
}
3198+
return null
3199+
}
3200+
31473201
/**
31483202
* Get all props from component options object.
31493203
* @param {ObjectExpression} componentObject Object with component definition

0 commit comments

Comments
 (0)