Skip to content

How to override babel-types when using a custom parser? #9009

Open

Description

@mbrowne

Is there a way to specify a custom version of @babel/types to be used when using a custom parser with the parserOverride option? I am working on an experimental plugin that requires custom syntax, so I forked @babel/parser and got the parsing part working, and parserOverride works well to tell Babel to use my custom version of babel-parser. I also forked babel-types and pointed my package.json to the forked version. But @babel/core is still loading the official version of @babel/types, so in my transform plugin, I can't write visitors for the new AST types I added.

If the ability to specify a custom version of babel-types isn't currently available, I think this would be an important feature to add, otherwise it definitely limits the usefulness of parserOverride. It occurred to me that I might be able to use yarn's resolutions feature as a workaround, but then anyone else who wants to try my plugin would also need this configuration in their project. I'm open to any other ideas of how to force @babel/core to use my forked version of @babel/types... Thanks!

Activity

  1. babel-bot commented on Nov 9, 2018

    @babel-bot
    Collaborator

    Hey @mbrowne! 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.

  2. nicolo-ribaudo commented on Nov 29, 2018

    @nicolo-ribaudo
    Member

    If we provide a typesOverride option, any plugin could still rely on @babel/types. We don't have a way of forcing them to use the user-provided parser.

    OTOH, overwriting the parser used by @babel/traverse is probably powerful enough: we can investigate it.

  3. mbrowne commented on Nov 29, 2018

    @mbrowne
    Author

    Thanks! In the meantime, I did go ahead and use yarn's 'resolutions' feature to force all other modules to use my fork of @babel/types and it works fine. But of course it's yarn-specific.

  4. joepie91 commented on Sep 17, 2021

    @joepie91

    Just pitching in here with a slightly different usecase: I'm trying to transpile an entirely different language (Nix) to JS, reusing Babel infrastructure where possible. I have a working parser for that language, as well as code that transforms its output into an AST that's shape-compatible with ESTree.

    But I can't use @babel/traverse to transform the Nix AST nodes to JS AST nodes, as it won't accept custom node types...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions