GithubHelp home page GithubHelp logo

acorn-types's People

Contributors

marijnh avatar rwalle avatar

Watchers

 avatar

Forkers

marijnh

acorn-types's Issues

Provide built-in type guard methods after typing is available?

@marijnh

I have been creating these functions in my own project:

function isLiteral (node: acorn.Node): node is Literal {
    return node.type === 'Literal';
}

function isCallExpression (node: acorn.Node): node is CallExpression {
    return node.type === 'CallExpression';
}

I assume that after the proper typing is available, downstream users will start to adopt them and creating type guards themselves, and likely many of these. It would be nice if acorn provides these as built-in JavaScript functions, and then these type declaration files also provide the matching functions with the type guard signature.

A small pain point related to this is that when I was using acorn I found it a bit surprising that the "type" values are not recorded in some sort of enum, and as a result I need to do node.type === 'TaggedTemplateExpression' and make sure I don't have a typo. Having these functions should help.

This GitHub issue probably belongs to acorn. I can bring this up later once the current work is done. For now I want to know your opinion on this.

Integrating into main distribution

Since our API is very stable, and I don't expect these to have to change very often, I was thinking to just copy the d.ts files into the Acorn repository, without any of the supporting machinery, and maintain them there, going forward. Does that sound reasonable to you?

Are you aware of anything we should address before publishing the new types?

Could greatly abbreviate the walkers using mapped types and tagged union magic

If we were to have a type that is a union of all possible type-tagged node types (the @types/estree types use Node for this, but we'd need another type, say AnyNode),

type AnyNode = Statement | Expression | SwitchCase | CatchClaush | ...

Then we can use Extract<AnyNode, {type: someNodeName}> to get the node type(s)* with a given name, and use that in a mapped type to magically get the right function type for every walk function.

export type AncestorVisitors<TState> = {
  [type in AnyNode["type"]]?: (node: Extract<AnyNode, {type: type}>, state: TState, ancestors: Node[]) => void
}

(* note that the current walkers are too narrow—FunctionDeclaration walkers must also accept AnonymousDefaultExportedFunctionDeclaration nodes)

Maybe avoid the `extends Omit<...>` pattern

It seems messy (removing a property to add it back with a different type) compared to, in some cases (FunctionBody, ModuleSpecifier, StaticBlock) just duplicating a few field or in others (ForOfStatement, ArrowFunctionExpression, AssignmentProperty) introducing base interfaces with the shared properties.

AnonymousDefaultExportedClassDeclaration seems a bit much

Same for the other similar types. My first impulse was that we could do away with these by giving ClassDeclaration an optional id property and just putting a ClassDeclaration in there, but I guess that loses valuable information (the non-nullability of id in a real class declaration).

So given that I'd suggest renaming these AnonymousClassDeclaration/AnonymousFunctionDeclaration.

Are RegExpLiteral and BigIntLiteral needed?

Would putting optional properties for this on Literal instead cause any real trouble? I guess you can no longer define a function that takes, precisely, a RegExpLiteral, but I'm not sure that's important enough to warrant the extra exported types.

Don't put a base interface in ChainElement's expression property type

That loses the information that this is going to hold either a MemberExpression or a CallExpression, and tells TS only that it's going to hold some Node object with an optional property.

I'd suggest inlining optional into MemberExpression and CallExpression, and making the type of ChainExpression.expression just MemberExpression | CallExpression.

I think we can leave out types for most of the internal stuff

Since using types when writing plugins is not going to be a reality anyway (you'd need the entire internal Parser API for that, and that would be way too much to put into the types that normal users are going to use), I think it would make sense to omit anything not mentioned in the readme from the types. So that means most of the properties on Parser, stuff like token contexts, identifier character test functions, whitespace regexps, and most of the properties that TokenType has.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.