Skip to content

Commit bbdc134

Browse files
committed
fix: fix tests failing
1 parent 0715943 commit bbdc134

File tree

1 file changed

+42
-46
lines changed

1 file changed

+42
-46
lines changed

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

+42-46
Original file line numberDiff line numberDiff line change
@@ -15,62 +15,58 @@ const PROPS_SEPARATOR = ', '
1515
* @param {RuleContext} context
1616
*/
1717
function* fixTypeBased(fixer, node, props, context) {
18-
try {
19-
const sourceCode = context.getSourceCode()
20-
const autoFixToSeparateInterface =
21-
context.options[1]?.autoFixToSeparateInterface || false
18+
const sourceCode = context.getSourceCode()
19+
const autoFixToSeparateInterface =
20+
context.options[1]?.autoFixToSeparateInterface || false
2221

23-
const componentPropsData = props.map((prop) =>
24-
getComponentPropData(prop, sourceCode)
25-
)
26-
27-
const componentPropsTypes = componentPropsData.map(
28-
({ name, type, required, defaultValue }) => {
29-
const isOptional = required === false || defaultValue
30-
return `${name}${isOptional ? '?' : ''}: ${type}`
31-
}
32-
)
22+
const componentPropsData = props.map((prop) =>
23+
getComponentPropData(prop, sourceCode)
24+
)
3325

34-
const componentPropsTypeCode = `{${componentPropsTypes.join(PROPS_SEPARATOR)}}`
26+
const componentPropsTypes = componentPropsData.map(
27+
({ name, type, required, defaultValue }) => {
28+
const isOptional = required === false || defaultValue
29+
return `${name}${isOptional ? '?' : ''}: ${type}`
30+
}
31+
)
3532

36-
// remove defineProps function parameters
37-
yield fixer.replaceText(node.arguments[0], '')
33+
const componentPropsTypeCode = `{ ${componentPropsTypes.join(PROPS_SEPARATOR)} }`
3834

39-
// add type annotation
40-
if (autoFixToSeparateInterface) {
41-
const variableDeclarationNode = node.parent.parent
42-
if (!variableDeclarationNode) {
43-
return
44-
}
35+
// remove defineProps function parameters
36+
yield fixer.replaceText(node.arguments[0], '')
4537

46-
yield fixer.insertTextBefore(
47-
variableDeclarationNode,
48-
`interface Props ${componentPropsTypeCode.replace(/;/g, ',')}; `
49-
)
50-
yield fixer.insertTextAfter(node.callee, `<Props>`)
51-
} else {
52-
yield fixer.insertTextAfter(node.callee, `<${componentPropsTypeCode}>`)
38+
// add type annotation
39+
if (autoFixToSeparateInterface) {
40+
const variableDeclarationNode = node.parent.parent
41+
if (!variableDeclarationNode) {
42+
return
5343
}
5444

55-
// add defaults if needed
56-
const propTypesDataWithDefaultValue = componentPropsData.filter(
57-
({ defaultValue }) => defaultValue
45+
yield fixer.insertTextBefore(
46+
variableDeclarationNode,
47+
`interface Props ${componentPropsTypeCode.replace(/;/g, ',')}; `
5848
)
59-
if (propTypesDataWithDefaultValue.length > 0) {
60-
const defaultsCode = propTypesDataWithDefaultValue
61-
.map(
62-
({ name, defaultValue }) =>
63-
`${name}: ${sourceCode.getText(defaultValue)}`
64-
)
65-
.join(PROPS_SEPARATOR)
49+
yield fixer.insertTextAfter(node.callee, `<Props>`)
50+
} else {
51+
yield fixer.insertTextAfter(node.callee, `<${componentPropsTypeCode}>`)
52+
}
6653

67-
yield fixer.insertTextBefore(node, `withDefaults(`)
68-
yield fixer.insertTextAfter(node, `, { ${defaultsCode} })`)
69-
}
70-
return null
71-
} catch (error) {
72-
return null
54+
// add defaults if needed
55+
const propTypesDataWithDefaultValue = componentPropsData.filter(
56+
({ defaultValue }) => defaultValue
57+
)
58+
if (propTypesDataWithDefaultValue.length > 0) {
59+
const defaultsCode = propTypesDataWithDefaultValue
60+
.map(
61+
({ name, defaultValue }) =>
62+
`${name}: ${sourceCode.getText(defaultValue)}`
63+
)
64+
.join(PROPS_SEPARATOR)
65+
66+
yield fixer.insertTextBefore(node, `withDefaults(`)
67+
yield fixer.insertTextAfter(node, `, { ${defaultsCode} })`)
7368
}
69+
return null
7470
}
7571
const mapNativeType = (/** @type {string} */ nativeType) => {
7672
switch (nativeType) {

0 commit comments

Comments
 (0)