Function name is updated:
export {
- foo
+ bar
}
Find and replace 💪
import {
- foo
+ bar
} from './myLib.js'
// ...
- foo()
+ bar()
Old React code:
class MyComponent extends React.Component {
constructor(props) {
super(props);
this.state = { isOpen: true };
}
}
Let's modernise it to this:
function MyComponent() {
const [isOpen, setIsOpen] = useState(true);
}
Old React code:
- class MyComponent extends React.Component {
- constructor(props) {
- super(props);
- this.state = { isOpen: true };
- }
}
Let's modernise it to this:
+ function MyComponent() {
+ const [isOpen, setIsOpen] = useState(true);
}
No access to the structure of your code:
import { foo as fooA } from "./myLib.js";
import { foo as fooB } from "./otherLib.js";
obj.foo = 42;
sendMessage("foo");
export default function transform(file, api) {
const j = api.jscodeshift;
const root = j(file.source);
// ...transform root
return root.toSource();
}
export default function transformer(file, api) {
const j = api.jscodeshift;
return j(file.source)
.find(j.ClassDeclaration, {
superClass: {
object: { name: "React" },
property: { name: "Component" },
},
})
.forEach((path) => {
const component = j(path);
const name = component.get("id").value;
const initialState = component
.find(j.MethodDefinition, { kind: "constructor" })
.find(j.AssignmentExpression, {
left: { property: { name: "state" } },
})
.get("right").value;
const body = j.blockStatement([
j.variableDeclaration("const", [
j.variableDeclarator(
j.arrayPattern([j.identifier("state"), j.identifier("setState")]),
j.callExpression(j.identifier("useState"), [initialState])
),
]),
]);
const func = j.functionDeclaration(name, [], body, false, false);
component.replaceWith(func);
})
.toSource();
}