Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //test_1.js
- var var1 = 'var';
- const var2 = 'var2';
- //start template
- export default function transformer(file, api) {
- const j = api.jscodeshift;
- const {expression, statement, statements} = j.template;
- return j(file.source)
- //Using find to locate the code you want to modified
- //the first argument will be the type of targeting code.
- //the second arguments could be an object which is the subset of AST you want to find
- .find(j.VariableDeclaration, {
- kind: 'var',
- })
- //Using replaceWith or forEach to modified the source code you find
- //if you want to replace all node you find directly, use replaceWith directly.
- //However, if you only want to modify part of those code, or you want to further filter.
- //Then you could use forEach to execute more precise manipulation
- .forEach(p=>{
- p.value.kind = 'const';
- return p;
- })
- .toSource();
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement