Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import React, { Component } from 'react';

class C extends Component {
constructor(props) {
super(props)
this.state = {
count: 0,
}
}

render() {
const { onChange, type, value } = this.props
return (
<input onChange={onChange} type={type} value={value} />
)
}
};

C.defaultProps = {
value: '',
type: 'text',
};

export default C;
2 changes: 1 addition & 1 deletion __tests__/react/func-default-props-to-params.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const transformTests = [
'forward-ref-function-component-w-default-export',
]

const noTransformTests = ['function-component-wo-default-props']
const noTransformTests = ['function-component-wo-default-props', 'class-component']

const fixturesDir = path.join(
__dirname,
Expand Down
10 changes: 9 additions & 1 deletion transforms/react/func-default-props-to-params.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,17 @@ module.exports = (fileInfo, api, options) => {
const defaultPropsNames = defaultPropsPropertiesNodePath.map((prop) => prop.node.key.name)
const defaultPropsValues = defaultPropsPropertiesNodePath.map((prop) => prop.node.value)

const componentPropsObjectPropertiesNodePath = root
const componentCol = root
.find(j.VariableDeclarator, { id: { name: componentName } }) // returns a jscodeshift Collection
.find(j.ArrowFunctionExpression)
if (componentCol.length <= 0) {
process.stdout.write(
'Class components will continue to support `defaultProps` on React 19 since there is no ES6 alternative. Please consider using `static defaultProps` to remove this alert.\r',
)
return null // skip transform
}

const componentPropsObjectPropertiesNodePath = componentCol
.at(0) // returns the 1st ast-types NodePath, with name 'init', or 0 for the argument (assuming there is only one ObjectPattern in the params, so no: ({ a }, { b }) => {...}) of a forwardRef CallExpression
.get('params', 0, 'properties') // returns an ast-types NodePath

Expand Down