babel-plugin-hyperscript-to-jsx (codmod usage)
It's a quite complex codemod to migrate from hyperscript to JSX.
Code Before:
; const StatelessComponent = ; const StatelessWithReturn = { return ;}; { return ;} const HyperscriptAsVariable = ; const HyperscriptWithExpressionAsChildren = // Should be ignored from transformingconst FirstArgTemplateLiteralWithComputedExpressions = ; // Not computed so should be fineconst FirstArgTemplateLiteral = ; // Should be ignoredconst WhenFirstArgumentIsFunctionThatIsCalled = const ComputedRootWithObjectPropertyDeclaration = ; const ThirdArgOnIgnoredIsNotArray = ; const SecondArgOnIgnoredIsNotArray = ; const MultiMemberExpressionWithClosingTag = // to handle h(Abc, { [kek]: 0, ["norm"]: 1 }) to < Abc {...{ [kek]: 0 }} {...{ ["norm" + lol]: 1 }} norm={1} />const ComplexComputedAttibutesHandling = // Should process children but ignore computed parent Component { return ; }
Code After:
import React from 'react'import h from 'react-hyperscript' import hx from 'shit' const StatelessComponent = <h1 /> const StatelessWithReturn = { return <div ="class" />} { return <h1 />} const HyperscriptAsVariable = <div ="lol" ="lol" /> const HyperscriptWithExpressionAsChildren = <AnotherComponent ="bar" = => arr </AnotherComponent> // Should be ignored from transformingconst FirstArgTemplateLiteralWithComputedExpressions = // Not computed so should be fineconst FirstArgTemplateLiteral = <div ="lol" ="lol" /> // Should be ignoredconst WhenFirstArgumentIsFunctionThatIsCalled = const ComputedRootWithObjectPropertyDeclaration = const ThirdArgOnIgnoredIsNotArray = ;<div>isCanada ? someProps : anotherProps</div>;<div >SomeChildren</div>; const SecondArgOnIgnoredIsNotArray = const MultiMemberExpressionWithClosingTag = <Pricing.lol.kek => <h1 /> </Pricing.lol.kek> // to handle h(Abc, { [kek]: 0, ["norm"]: 1 }) to < Abc {...{ [kek]: 0 }} {...{ ["norm" + lol]: 1 }} norm={1} />const ComplexComputedAttibutesHandling = <Abc = /> // Should process children but ignore computed parent Component { return <div ="example"> isStuff && <h1 ="heading" ="" /> isStuff ? <h1 ="heading" ="" /> : <h1 ="heading">heading</h1> <h1 ="heading" ="" /> <h1 ="heading">getChildren</h1> <h1 ="heading" > <div /> </h1> <div> <div>Some content</div> </div> <h1 ="heading">This is hyperscript</h1> <h2>creating Reactjs markup</h2> <AnotherComponent ="bar" = = > <li> <a ="http://whatever.com">One list item</a> </li> <li>Another list item</li> </AnotherComponent> </div> }
Usage
Install codemod npm install -g @codemod/cli
Then install in root of your project npm install babel-plugin-hyperscript-to-jsx
Run it like that from node_modules:
codemod --plugin ./node_modules/babel-plugin-hyperscript-to-jsx/src/index.js ./src
Also, you may like to pretty print it immediately using prettier
codemod --plugin ./node_modules/babel-plugin-hyperscript-to-jsx/src/index.js ./src --printer prettier
Remove babel-plugin-hyperscript-to-jsx
from package.json
If there is any issues, let me know in the issues tab here at GitHub.
You can run it against exact file too e.g. ./src/index.js
codemod --plugin ./node_modules/babel-plugin-hyperscript-to-jsx/src/index.js ./src/index.js --printer prettier
Limitations
- When
h
is called this way
;
Second argument will be a children (to break everything), cause to get whether second argument expression is returning children or props of object is almost impossible.
- All computed first arguments to
h
likeh(STUFF[computed])
orh(
.stuff ${anotherClass})
is impossible to codemod, so they will be ignored, and you will need to fix it yourself, they will be kept as is, but their array second and third arguments will be processed with the same approach.
Fix all that by yourself :)
(it's possible but will require further analysis of AST with hardcore traversal and I don't think it worth it)
Integration with WebStorm/VS Code to do it nicely file by file
Preconditions:
npm i -g @codemod/cli babel-plugin-hyperscript-to-jsx
WebStorm:
- Go to Preferences -> External Tools -> Click plus to add tool.
- Config:
Name: h to JSX
Program: codemod
Arguments: -p <your global node_modules location>/babel-plugin-hyperscript-to-jsx/src/index.js $FilePathRelativeToProjectRoot$
Working directory: $ProjectFileDir$
In advanced settings:
Tick on: Sync file after execution
- Open file you want to transform
Right Click -> External Tools -> h to JSX -> Apply prettier/code formatting -> Enjoy
- For even better experience go to.
Preferences -> Keymap -> External Tools -> External Tools -> h to JSX -> Attach some key combination
VS Code:
- Open command pallete
>Tasks: Configure Task
- Press Up -> Select:
Task from tasks.json template
(or something like that) - Copy and paste this:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "H to JSX",
"type": "shell",
"command": "codemod -p <your global node_modules location>/babel-plugin-hyperscript-to-jsx/src/index.js ${file}"
}
]
}
- Open command pallete and ask it to open
keybindings.json
- Add this:
{
"key": "cmd+e",
"command": "workbench.action.tasks.runTask",
"args": "H to JSX"
}
- Open any file and press cmd+e to apply codemod on file.
- Or if you don't want to bloat your
keybindings.json
just open Command pallete and type.Run task -> Enter -> Find in the list "H to JSX" -> Enter
(Usually will be on top) - Apply formatting and enjoy
For Revolut plugin to work.
Webstorm:
- Add to command line arguments
-o index={\"revolut\":true}
before the$FilePathRelativeToProjectRoot$
VS Code:
- Add to command line arguments
-o index='{\"revolut\":true}'
before the${file}