Skip to content

Commit f301546

Browse files
committed
feat: autofix in define-props-declaration: runtime syntax to type-based syntax (#2465)
refactoring
1 parent bc506f9 commit f301546

File tree

2 files changed

+27
-25
lines changed

2 files changed

+27
-25
lines changed

Diff for: lib/rules/define-props-declaration.js

+27-24
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,15 @@ const mapNativeType = (/** @type {string} */ nativeType) => {
4040
* @param {SourceCode} sourceCode
4141
*/
4242
function getComponentPropData(prop, sourceCode) {
43+
if (prop.propName === null) {
44+
throw new Error('Unexpected prop with null name.')
45+
}
4346
if (prop.type !== 'object') {
4447
throw new Error(`Unexpected prop type: ${prop.type}.`)
4548
}
4649
const type = optionGetType(prop.value, sourceCode)
4750
if (type === null) {
48-
throw new Error(`Unexpected prop type: ${prop.type}.`)
51+
throw new Error(`Unable to read prop type`)
4952
}
5053
const required = optionGetRequired(prop.value)
5154
const defaultValue = optionGetDefault(prop.value)
@@ -69,33 +72,10 @@ function optionGetType(node, sourceCode) {
6972
return mapNativeType(node.name)
7073
}
7174
case 'ObjectExpression': {
72-
// foo: {
7375
const typeProperty = utils.findProperty(node, 'type')
7476
if (typeProperty == null) {
7577
return null
7678
}
77-
if (typeProperty.value.type === 'TSAsExpression') {
78-
const typeAnnotation = typeProperty.value.typeAnnotation
79-
if (typeAnnotation.typeName.name !== 'PropType') {
80-
return null
81-
}
82-
83-
// in some project configuration parser populates deprecated field `typeParameters` instead of `typeArguments`
84-
const typeArguments =
85-
'typeArguments' in typeProperty.value
86-
? typeAnnotation.typeArguments
87-
: typeAnnotation.typeParameters
88-
89-
const typeArgument = Array.isArray(typeArguments)
90-
? typeArguments[0].params[0]
91-
: typeArguments.params[0]
92-
93-
if (typeArgument === undefined) {
94-
return null
95-
}
96-
97-
return sourceCode.getText(typeArgument)
98-
}
9979
return optionGetType(typeProperty.value, sourceCode)
10080
}
10181
case 'ArrayExpression': {
@@ -111,6 +91,29 @@ function optionGetType(node, sourceCode) {
11191
.filter(Boolean)
11292
.join(' | ')
11393
}
94+
case 'TSAsExpression': {
95+
const typeAnnotation = node.typeAnnotation
96+
if (typeAnnotation.typeName.name !== 'PropType') {
97+
return null
98+
}
99+
100+
// in some project configuration parser populates deprecated field `typeParameters` instead of `typeArguments`
101+
const typeArguments =
102+
'typeArguments' in node
103+
? typeAnnotation.typeArguments
104+
: typeAnnotation.typeParameters
105+
106+
const typeArgument = Array.isArray(typeArguments)
107+
? typeArguments[0].params[0]
108+
: typeArguments.params[0]
109+
110+
if (typeArgument === undefined) {
111+
return null
112+
}
113+
114+
return sourceCode.getText(typeArgument)
115+
}
116+
114117
case 'FunctionExpression':
115118
case 'ArrowFunctionExpression': {
116119
return null

Diff for: tests/lib/rules/define-props-declaration.js

-1
Original file line numberDiff line numberDiff line change
@@ -566,7 +566,6 @@ tester.run('define-props-declaration', rule, {
566566
},
567567
// Array of types
568568
{
569-
only: true,
570569
filename: 'test.vue',
571570
code: `
572571
<script setup lang="ts">

0 commit comments

Comments
 (0)