We talk about JavaScript. Each month in Warsaw, Poland.
@ Shedul, Warsaw
Full stack developer. Like challenges, JavaScript and Python.
Privately is passionate about triathlon.
"Do. Or do not. There is no try" (c) Yoda
Refactorng is the process of restructuring existing source code without changing its external behaviour.
One class, several methods ...
This can be done manually.
Think of renaming 100+ classes according to some naming schema ...
Modifying imports after moving files ...
Changing signature of some library function ...
At least partially !
<Form>
<Label>Some label</Label>
<Input hint="Hint text" />
...
</Form>
<Form>
<Label tid="123">Some label</Label>
<Input hint={tr("124", "Hint text")} />
...
</Form>
900 files, 15000+ replacements
couple of liters of coffee, almost two days w/o much sleep !
Found out that others also had this problem ... and found ... codemods !
Codemod is a tool/library to assist you with large-scale codebase refactors that can be partially automated but still require human oversight and occasional intervention.
This talk is about codemods. But not everything about them.
I will concentrate on:
Online sandbox for language inspection and transformation tools.
https://github.com/facebook/jscodeshift
A tool to run codemods on multiple files.
A library for AST to AST transformation and non-destructive printing
JavaScript parser as well as builder
> npm install -g jscodeshift
> npm install js-codemod
> jscodeshift -t CODEMOD_PATH PATH
module.exports = function (fileInfo, api, options) {
const { path, source } = fileInfo;
const { jscodeshift: j, stats } = api;
return j(source)
.forEach((path) => {
... do something with path ...
})
.toSource();
}