Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make parsers more easily pluggable #4640

Closed
nzakas opened this issue Dec 8, 2015 · 9 comments
Closed

Make parsers more easily pluggable #4640

nzakas opened this issue Dec 8, 2015 · 9 comments
Labels
archived due to age This issue has been archived; please open a new issue for any further discussion core Relates to ESLint's core APIs and features enhancement This change enhances an existing feature of ESLint evaluating The team will evaluate this issue to decide whether it meets the criteria for inclusion

Comments

@nzakas
Copy link
Member

nzakas commented Dec 8, 2015

I've been looking at some of the hoops we jump through (and babel-eslint jumps through) to get parsers to work in ESLint. It seems like we could dramatically improve things with just a few simple steps. Here's what I'm proposing:

  1. Parsers should export a Syntax object that contains the names of all AST nodes they support. Esprima and Espree already do this.
  2. Parsers should also export a VisitorKeys object containing the traversal information for the nodes they support. estraverse exports this currently.

When a custom parser is specified, ESLint will augment the Syntax and VisitorKeys estraverse objects with those that are exported from the parser itself, ensuring ESLint can work properly.

The benefits, as far as I can see them, are:

  1. Custom parsers won't have to monkey patch estraverse to get traversal to work correctly (as babel-eslint does today).
  2. The syntax information now lives in one place (the parser) rather than being split between the parser and ESLint. That means we can add more to Espree and ESLint will Just Work (tm).

This would also benefit typescript-eslint-parser, which is sure to have some nonstandard nodes to deal with.

Thoughts?

@hzoo

Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.

@nzakas nzakas added enhancement This change enhances an existing feature of ESLint core Relates to ESLint's core APIs and features evaluating The team will evaluate this issue to decide whether it meets the criteria for inclusion labels Dec 8, 2015
@hzoo
Copy link
Member

hzoo commented Dec 8, 2015

That would certainly help with patching up the visitorKeys - so instead of monkeypatching it in babel-eslint, it would export the visitorKeys and eslint would patch it instead? This would help to remove https://github.com/babel/babel-eslint/blob/master/index.js#L44-L62

@nzakas
Copy link
Member Author

nzakas commented Dec 8, 2015

Yup! Exactly.

@hzoo
Copy link
Member

hzoo commented Dec 8, 2015

Don't know if there is an easy way (or if you've thought of) doing the same for escope? There is a lot of patching done for flow, experimental features regarding no-unused-vars for that.

@nzakas
Copy link
Member Author

nzakas commented Dec 8, 2015

I haven't done much thinking around that, as it seemed like traversal was the lowest hanging fruit. Do you have a suggestion?

@hzoo
Copy link
Member

hzoo commented Dec 8, 2015

It's a similar thing but instead we have to patch the escope visitor methods somehow to also visit the nodes it doesn't know about (example with decorators)

function visitDecorators(node) {
    if (!node.decorators) {
      return;
    }
    for (var i = 0; i < node.decorators.length; i++) {
      if (node.decorators[i].expression) {
        this.visit(node.decorators[i]);
      }
    }
  }

And right it's definitely not as simple as traversal

@nzakas
Copy link
Member Author

nzakas commented Dec 9, 2015

It looks like you're doing more than just adding methods to the referencer, though. I'm not sure there's an easy way to do this generically.

@hzoo
Copy link
Member

hzoo commented Dec 9, 2015

Most of it is either visiting certain nodes by rewriting the function to do so and then calling the original referencer methods or creating a scope variable if needed. Maybe this is more for escope

@nzakas
Copy link
Member Author

nzakas commented Dec 9, 2015

Yeah, it seems like more of an escope thing. Though, I wonder if we could go through and use VisitorKeys to create default visitors for things to at least get part of the way there. We can keep thinking about it going forward, though I would like to see escope become more extensible so we're not reaching into files and hacking things in.

@nzakas
Copy link
Member Author

nzakas commented Mar 11, 2016

Closing in favor of #5476

@nzakas nzakas closed this as completed Mar 11, 2016
@eslint-deprecated eslint-deprecated bot locked and limited conversation to collaborators Feb 6, 2018
@eslint-deprecated eslint-deprecated bot added the archived due to age This issue has been archived; please open a new issue for any further discussion label Feb 6, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
archived due to age This issue has been archived; please open a new issue for any further discussion core Relates to ESLint's core APIs and features enhancement This change enhances an existing feature of ESLint evaluating The team will evaluate this issue to decide whether it meets the criteria for inclusion
Projects
None yet
Development

No branches or pull requests

2 participants