babel / babel

🐠 Babel is a compiler for writing next generation JavaScript.
https://babel.dev
MIT License
43.24k stars 5.65k forks source link

Babylon: ESPree plugin #7369

Open tunnckoCore opened 6 years ago

tunnckoCore commented 6 years ago

Continuation of https://github.com/babel/babylon/issues/651? Still, can't understand what's the problem to move the babel-eslint's espree/ dir as plugin to Babylon, so finally can just pass babylon as eslint parser and pass parserOptions with plugins: ['espree'].

babel-bot commented 6 years ago

Hey @olstenlarck! We really appreciate you taking the time to report an issue. The collaborators on this project attempt to help as many people as possible, but we're a limited number of volunteers, so it's possible this won't be addressed swiftly.

If you need any help, or just have general Babel or JavaScript questions, we have a vibrant Slack community that typically always has someone willing to help. You can sign-up here for an invite.

tunnckoCore commented 6 years ago

So, after investing it further. The most easy thing is to add check for options.plugins.indexOf('espree') in Babylon's exported .parse and move what is found on babel-eslint/lib/babylon-to-espree folder to Babylon's repo. Then just call babylonToEspree with the coming ast from getParser().parse().

I'll try to find some time to raise a PR, cuz it is really simple thing. Can i PR without tests? Cuz won't have time for them, but will try it locally to see diffs.

tunnckoCore commented 6 years ago

Steps to do that:

  1. Add check after these lines and include p === 'espree' to the filter function. The check would be:
  if (
    pluginList.indexOf("estree") >= 0 &&
    pluginList.indexOf("espree") >= 0
  ) {
    throw new Error("Cannot combine ESTree and ESPree plugins.");
  }
  1. Replace all matches of getParser(options, input).parse() in the export function parse with detectESPree(options, input)

var tt = require("babylon").tokTypes;
var traverse = require("@babel/traverse").default;
var babylonToEspree = require("./babylon-to-espree");

function detectESPree (options, input) {
  const ast = getParser(options, input).parse();

  if (options.plugins.indexOf("espree") >= 0) {
    babylonToEspree(ast, traverse, tt, input);
  }

  return ast;
}

I don't think we need anything other. I seen the plugin API, but in reality we just need to reshape the final ast, so it not matter in what order the plugin is defined. So we don't need more check in getParserClass.