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

relax node type from Node to BaseNode #17

Merged
merged 1 commit into from Jan 6, 2020
Merged

relax node type from Node to BaseNode #17

merged 1 commit into from Jan 6, 2020

Conversation

larsrh
Copy link
Contributor

@larsrh larsrh commented Jan 5, 2020

In @types/estree, Node is a discriminated union type for all known nodes. However, parsing JSX (or other syntactic additions) requires additional node types. This library copes with this just fine, as evidenced by this test:

it('walks a malformed node', () => {

However, this library declares the types of walk too restrictively. It could use BaseNode instead, which would make it more flexible when used from TS.

declare type Walker = {
enter?: WalkerHandler;
leave?: WalkerHandler;
};
export declare function walk(ast: Node, { enter, leave }: Walker): import("estree").Identifier | import("estree").SimpleLiteral | import("estree").RegExpLiteral | import("estree").Program | import("estree").FunctionDeclaration | import("estree").FunctionExpression | import("estree").ArrowFunctionExpression | import("estree").SwitchCase | import("estree").CatchClause | import("estree").VariableDeclarator | import("estree").ExpressionStatement | import("estree").BlockStatement | import("estree").EmptyStatement | import("estree").DebuggerStatement | import("estree").WithStatement | import("estree").ReturnStatement | import("estree").LabeledStatement | import("estree").BreakStatement | import("estree").ContinueStatement | import("estree").IfStatement | import("estree").SwitchStatement | import("estree").ThrowStatement | import("estree").TryStatement | import("estree").WhileStatement | import("estree").DoWhileStatement | import("estree").ForStatement | import("estree").ForInStatement | import("estree").ForOfStatement | import("estree").VariableDeclaration | import("estree").ClassDeclaration | import("estree").ThisExpression | import("estree").ArrayExpression | import("estree").ObjectExpression | import("estree").YieldExpression | import("estree").UnaryExpression | import("estree").UpdateExpression | import("estree").BinaryExpression | import("estree").AssignmentExpression | import("estree").LogicalExpression | import("estree").MemberExpression | import("estree").ConditionalExpression | import("estree").SimpleCallExpression | import("estree").NewExpression | import("estree").SequenceExpression | import("estree").TemplateLiteral | import("estree").TaggedTemplateExpression | import("estree").ClassExpression | import("estree").MetaProperty | import("estree").AwaitExpression | import("estree").Property | import("estree").Super | import("estree").TemplateElement | import("estree").SpreadElement | import("estree").ObjectPattern | import("estree").ArrayPattern | import("estree").RestElement | import("estree").AssignmentPattern | import("estree").ClassBody | import("estree").MethodDefinition | import("estree").ImportDeclaration | import("estree").ExportNamedDeclaration | import("estree").ExportDefaultDeclaration | import("estree").ExportAllDeclaration | import("estree").ImportSpecifier | import("estree").ImportDefaultSpecifier | import("estree").ImportNamespaceSpecifier | import("estree").ExportSpecifier;
export declare function walk(ast: BaseNode, { enter, leave }: Walker): BaseNode;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a nice side effect of this change: because BaseNode is not a type alias, tsc doesn't inline all of those union members.

@Rich-Harris Rich-Harris merged commit 3aa1e6e into Rich-Harris:master Jan 6, 2020
@Rich-Harris
Copy link
Owner

Thanks! This makes lots of sense. Released as 1.0.1

@larsrh
Copy link
Contributor Author

larsrh commented Jan 6, 2020

Cool, thanks!

@larsrh larsrh deleted the topic/relax-node-type branch January 6, 2020 21:33
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

Successfully merging this pull request may close these issues.

None yet

2 participants