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

[acorn-walk] Type error when using VariableDeclarator with walk.simple #1294

Closed
namukang opened this issue Apr 18, 2024 · 1 comment
Closed

Comments

@namukang
Copy link

namukang commented Apr 18, 2024

This code worked fine in acorn-walk@8.2.0:

import * as acorn from "acorn";
import * as walk from "acorn-walk";

const node = acorn.parse(`const foo = "bar";`, { ecmaVersion: 2020 });
walk.simple(node, {
  VariableDeclarator: (node: any) => {
    console.log(node);
  },
});

Now there's a type error in acorn-walk@8.3.2 on VariableDeclarator:

Object literal may only specify known properties, but 'VariableDeclarator' does not exist in type 'SimpleVisitors<unknown>'. Did you mean to write 'VariableDeclaration'?ts(2561)

Given that the code still works at runtime, I'm assuming that this is a bug with the type definitions but let me know if I'm missing something!

Additional information

Previous type definitions (acorn-walk@8.2.0):

  type SimpleVisitors<TState> = {
    [type: string]: SimpleWalkerFn<TState>
  };

New type definitions (acorn-walk@8.3.2):

export type SimpleVisitors<TState> = {
  [type in acorn.AnyNode["type"]]?: (node: Extract<acorn.AnyNode, { type: type }>, state: TState) => void
} & {
  [type in keyof AggregateType]?: (node: AggregateType[type], state: TState) => void
}

// Maybe VariableDeclarator should be included here?
export type AnyNode = Statement | Expression | Declaration | ModuleDeclaration | Literal | Program | SwitchCase | CatchClause | Property | Super | SpreadElement | TemplateElement | AssignmentProperty | ObjectPattern | ArrayPattern | RestElement | AssignmentPattern | ClassBody | MethodDefinition | MetaProperty | ImportSpecifier | ImportDefaultSpecifier | ImportNamespaceSpecifier | ExportSpecifier | AnonymousFunctionDeclaration | AnonymousClassDeclaration | PropertyDefinition | PrivateIdentifier | StaticBlock
@marijnh
Copy link
Member

marijnh commented Apr 18, 2024

That node type was missed in the AnyNode type union. Attached patch adds it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants