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

ASTVisitor: use type intersection instead of type union #2959

Merged
merged 1 commit into from Mar 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 9 additions & 9 deletions src/language/visitor.d.ts
Expand Up @@ -6,17 +6,17 @@ import { ASTNode, ASTKindToNode } from './ast';
* A visitor is provided to visit, it contains the collection of
* relevant functions to be called during the visitor's traversal.
*/
export type ASTVisitor = EnterLeave<ASTVisitFn<ASTNode>> | ShapeMapVisitor;
export type ASTVisitor = EnterLeaveVisitor<ASTNode> & KindVisitor;

interface EnterLeave<T> {
readonly enter?: T;
readonly leave?: T;
}

type ShapeMapVisitor = {
[K in keyof ASTKindToNode]?:
type KindVisitor = {
readonly [K in keyof ASTKindToNode]?:
| ASTVisitFn<ASTKindToNode[K]>
| EnterLeave<ASTVisitFn<ASTKindToNode[K]>>;
| EnterLeaveVisitor<ASTKindToNode[K]>;
};

type EnterLeaveVisitor<TVisitedNode extends ASTNode> = {
readonly enter?: ASTVisitFn<TVisitedNode>;
readonly leave?: ASTVisitFn<TVisitedNode>;
};

/**
Expand Down
20 changes: 11 additions & 9 deletions src/language/visitor.js
Expand Up @@ -7,14 +7,17 @@ import { isNode } from './ast';
* A visitor is provided to visit, it contains the collection of
* relevant functions to be called during the visitor's traversal.
*/
export type ASTVisitor =
| EnterLeave<ASTVisitFn<ASTNode>>
| ShapeMap<
ASTKindToNode,
<Node>(Node) => ASTVisitFn<Node> | EnterLeave<ASTVisitFn<Node>>,
>;
type EnterLeave<T> = {| +enter?: T, +leave?: T |};
type ShapeMap<O, F> = $Shape<$ObjMap<O, F>>;
export type ASTVisitor = $Shape<EnterLeaveVisitor<ASTNode> & KindVisitor>;

type KindVisitor = $ObjMap<
ASTKindToNode,
<Node>(Node) => ASTVisitFn<Node> | EnterLeaveVisitor<Node>,
>;

type EnterLeaveVisitor<TVisitedNode: ASTNode> = {|
+enter?: ASTVisitFn<TVisitedNode>,
+leave?: ASTVisitFn<TVisitedNode>,
|};

/**
* A visitor is comprised of visit functions, which are called on each node
Expand Down Expand Up @@ -389,7 +392,6 @@ export function getVisitFn(
return kindSpecificVisitor;
}
} else {
// $FlowFixMe[prop-missing]
const specificVisitor = isLeaving ? visitor.leave : visitor.enter;
if (specificVisitor) {
// { enter() {}, leave() {} }
Expand Down