question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Discussion on how to integrate with Babel's ability to use recast

See original GitHub issue

So after https://github.com/babel/babel/pull/3561 allows babel to use recast as the parser/generator and the other prs fixed the babel 6 ast nodes for ast-types/recast we should be able to use babel as the transformer for jscodeshift.

Is there anything we want to do to the api to make it easier or should users just add it in themselves? babel-core/recast are already dependencies, but yeah it’s not necessary (Ah I see babel-core is v5 for the babel 5 parser). Maybe it’s just a docs change.

module.exports = function(fileInfo, api) {
  return api.jscodeshift(fileInfo.source)
    .findVariableDeclarators('foo')
    .renameTo('bar')
    .toSource();
}
const transform = require('babel-core').transform;
const recast = require('recast');
const plugin = require('./plugin');

module.exports = function(fileInfo, api, options) {
  return transform(fileInfo.source, {
    parserOpts: {
      parser: recast.parse
    },
    generatorOpts: {
      generator: recast.print
    },
    plugins: [plugin]
  }).code;
};

// ./plugin.js
module.exports = {
  visitor: {
    VariableDeclarator({ node }) {
      if (node.id.name === "foo") {
        node.id.name = "bar";
      }
    }
  }
};

Also interesting sidenote would be using the jscodeshift type api in babel

cc @fkling, @cpojer, @drewml

Issue Analytics

  • State:open
  • Created 7 years ago
  • Comments:9 (7 by maintainers)

github_iconTop GitHub Comments

7reactions
eventualbuddhacommented, Mar 28, 2017

I’ve open-sourced what I have at https://github.com/square/babel-codemod. PRs/suggestions/comments welcome!

5reactions
benjamncommented, Nov 8, 2016

My two cents:

The Visitor-based transformation API used by Babel tends to be better for complicated transforms, and transforms that need to be run by other people who don’t fully understand what’s happening behind the scenes, which means handling more edge cases automatically, and not giving up in cases when it’s easier to edit the code by hand.

The Collection-based API is much better for hand-supervised codemods, and for transforms where it’s ok if you don’t handle every edge case, because you know that certain patterns of syntax are rare in your specific codebase.

These may not be the only possible options in the API landscape, but they’ve proven effective in their respective niches. I don’t think it necessarily makes sense to unify them into one API, but I’m totally in favor of making both kinds of transforms easier to write, if possible.

Read more comments on GitHub >

github_iconTop Results From Across the Web

The State of Babel
Babel is vital to the React and Flow ecosystems, and we work closely ... By passing in Recast in the options, Babel can...
Read more >
Patterns for Managing Source Code Branches - Martin Fowler
There are several patterns that can allow teams to use branching effectively, concentrating around integrating the work of multiple developers and ...
Read more >
The Client's Theory of Change - Better Outcomes Now
It is proposed that conducting therapy within the context of the client's own theory of change offers ways of integrating multiple therapy perspectives....
Read more >
Strategic enforcement - U.S. Department of Labor
we discuss how deterrence can be improved through changes in how investigations are carried out, penalties assessed and levied, and work of the...
Read more >
Amazon Fire TV Recast OTA DVR Setup and Review - YouTube
This video includes the setup and my review of the Amazon Fire TV Recast over the air DVR. ... Use my affiliate link...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found