// file
var a = "a" // what if this is import statement?
// jscodeshift
export default (file, api) => {
const j = api.jscodeshift;
const root = j(file.source);
root.find(j.VariableDeclaration)
.insertBefore("use strict");
return root.toSource();
}
How does insertBefore work if the first line of code differs across the files. e.g. (variable declaration, import statements)
It looks like you have to
"cast"
the node to thejscodeshift
.A solution is:
EDIT
For your clarification.
If you want to insert
use strict
at the beginning of the file no matter what:If you want to add
use strict
after theimport
declaration, if any: