Advertisement
Guest User

Untitled

a guest
May 1st, 2016
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.89 KB | None | 0 0
  1. //test_1.js
  2. var var1 = 'var';
  3. const var2 = 'var2';
  4.  
  5. //start template
  6. export default function transformer(file, api) {
  7. const j = api.jscodeshift;
  8. const {expression, statement, statements} = j.template;
  9.  
  10. return j(file.source)
  11. //Using find to locate the code you want to modified
  12. //the first argument will be the type of targeting code.
  13. //the second arguments could be an object which is the subset of AST you want to find
  14. .find(j.VariableDeclaration, {
  15. kind: 'var',
  16. })
  17. //Using replaceWith or forEach to modified the source code you find
  18. //if you want to replace all node you find directly, use replaceWith directly.
  19. //However, if you only want to modify part of those code, or you want to further filter.
  20. //Then you could use forEach to execute more precise manipulation
  21. .forEach(p=>{
  22. p.value.kind = 'const';
  23. return p;
  24. })
  25. .toSource();
  26. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement