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

Typesafe babel plugin development with TypeScript #10637

Open
hbroer opened this issue Nov 4, 2019 · 2 comments

Comments

@hbroer
Copy link

@hbroer hbroer commented Nov 4, 2019

Feature Request

Is your feature request related to a problem? Please describe.
I miss some types for plugin development in typescript which I need. I asked for some help at the slack channel but got no answer.

  1. I can't find the type for PluginPass (https://github.com/babel/babel/blob/master/packages/babel-core/src/transformation/plugin-pass.js). It should be exported to make it usable like in this example:
import * as types from "@babel/types";
import {ImportDeclaration} from "@babel/types";
import {NodePath, PluginPass} from "@babel/traverse";
// or import {NodePath, PluginPass} from "@babel/core";

export default function({ types: t }: {types: typeof types}) {
    return {
        name: "example plugin",
        visitor: {
            ImportDeclaration(path: NodePath<ImportDeclaration>, pass: PluginPass) {}
        }
    };
}
  1. The plugin function has no return type but it should to display errors and to make code completion possible, like this:
import {PluginDefinition} from "@babel/core";

export default function(): PluginDefinition {
    return {}; // this should be a TS error because `name` is missing
}
  1. {types: typeof types} is strange also would be this: typeof babel. This is a workaround which describes the plugin default function parameter. This should also a dedicated type:
import {PluginDefinition, Babel} from "@babel/core";

export default function({ types: t }: Babel): PluginDefinition {}

Describe the solution you'd like
Export/add the types which are missing.

Describe alternatives you've considered
None. Declare everything as any is not a solution :-)

Teachability, Documentation, Adoption, Migration Strategy
The documentation/handbook should contain a example how to write plugins in TypeScript.

@babel-bot

This comment has been minimized.

Copy link
Collaborator

@babel-bot babel-bot commented Nov 4, 2019

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

@hbroer

This comment has been minimized.

Copy link
Author

@hbroer hbroer commented Nov 6, 2019

Just noticed that only 50% of the available Types come from @babel, the other 50% come from @types. :/

Also noticed one of the types I missed I just overead: PluginObj is the type of the function return value. But still missing the PluginPass definition.

And now I don't know who to ask, or where to add, the missing type...

export default function({types: t}: typeof babel): PluginObj {
    return {
        name: "example",
        visitor: {
            ImportDeclaration(path: NodePath<ImportDeclaration>, pass: any /*PluginPass*/) {}
        }
    };
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.