Skip to content

Commit 2e838d5

Browse files
committed
fix: consider that props might not be used in the component
1 parent 09b4da4 commit 2e838d5

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

dist/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ exports.default = (fileInfo, { j }) => {
2626
const outerNewTypeAnnotation = extractPropsDefinitionFromReactFC(j, reactFcOrSfcNode);
2727
// build the new nodes
2828
const componentFunctionNode = n.node.init;
29+
// if no params, it could be that the component is not actually using props, so nothing to do here
30+
if (componentFunctionNode.params.length === 0) {
31+
return;
32+
}
2933
const firstParam = componentFunctionNode.params[0];
3034
let componentFunctionFirstParameter;
3135
// form of (props) =>

transform.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,18 @@ const testCases: TestCase[] = [
283283
}
284284
`,
285285
},
286+
{
287+
input: `
288+
import React from 'react'
289+
290+
const NoPropsComponent: React.FC<UnusedProps> = () => <span>foo</span>
291+
`,
292+
output: `
293+
import React from 'react'
294+
295+
const NoPropsComponent = () => <span>foo</span>
296+
`,
297+
},
286298
]
287299

288300
function escapeLineEndingsAndMultiWhiteSpaces(text: string | null | undefined) {

transform.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ export default (fileInfo: FileInfo, { j }: API) => {
3636
const outerNewTypeAnnotation = extractPropsDefinitionFromReactFC(j, reactFcOrSfcNode)
3737
// build the new nodes
3838
const componentFunctionNode = n.node.init as ArrowFunctionExpression | FunctionExpression
39+
// if no params, it could be that the component is not actually using props, so nothing to do here
40+
if (componentFunctionNode.params.length === 0) {
41+
return
42+
}
3943
const firstParam = componentFunctionNode.params[0]
4044

4145
let componentFunctionFirstParameter: Identifier | ObjectPattern | undefined

0 commit comments

Comments
 (0)