Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 7d9e731

Browse files
chore: refactor fixTypeBased function
1 parent 100065d commit 7d9e731

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

‎lib/rules/define-props-declaration.js

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,16 @@ function* fixTypeBased(fixer, node, props, context) {
2020
const autoFixToSeparateInterface =
2121
context.options[1]?.autoFixToSeparateInterface || false
2222

23-
const propTypes = props.map((prop) =>
23+
const componentPropsData = props.map((prop) =>
2424
getComponentPropData(prop, sourceCode)
2525
)
2626

27-
const definePropsType = `{${propTypes
28-
.map(
29-
({ name, type, required, defaultValue })=>
30-
`${name}${required===false||defaultValue ? '?' : ''}: ${type}`
31-
)
32-
.join(PROPS_SEPARATOR)}}`
27+
const componentPropsTypeCode = `{${componentPropsData
28+
.map(({ name, type, required, defaultValue })=>{
29+
constisOptional= required===false||defaultValue
30+
return`${name}${isOptional ? '?' : ''}: ${type}`
31+
})
32+
.join(PROPS_SEPARATOR)}}`
3333

3434
// remove defineProps function parameters
3535
yield fixer.replaceText(node.arguments[0], '')
@@ -43,17 +43,19 @@ function* fixTypeBased(fixer, node, props, context) {
4343

4444
yield fixer.insertTextBefore(
4545
variableDeclarationNode,
46-
`interface Props ${definePropsType.replace(/;/g, ',')}; `
46+
`interface Props ${componentPropsTypeCode.replace(/;/g, ',')}; `
4747
)
4848
yield fixer.insertTextAfter(node.callee, `<Props>`)
4949
} else {
50-
yield fixer.insertTextAfter(node.callee, `<${definePropsType}>`)
50+
yield fixer.insertTextAfter(node.callee, `<${componentPropsTypeCode}>`)
5151
}
5252

5353
// add defaults if needed
54-
const defaults = propTypes.filter(({ defaultValue }) => defaultValue)
55-
if (defaults.length > 0) {
56-
const defaultsCode = defaults
54+
const propTypesDataWithDefaultValue = componentPropsData.filter(
55+
({ defaultValue }) => defaultValue
56+
)
57+
if (propTypesDataWithDefaultValue.length > 0) {
58+
const defaultsCode = propTypesDataWithDefaultValue
5759
.map(
5860
({ name, defaultValue }) =>
5961
`${name}: ${sourceCode.getText(defaultValue)}`

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /