Skip to content

Commit 78dfe17

Browse files
committed
refactor: improve TS definitions for VisitInfo class
1 parent d1a54ff commit 78dfe17

File tree

2 files changed

+21
-19
lines changed

2 files changed

+21
-19
lines changed

src/VisitInfo.ts

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,17 @@ import {
1616
} from './directoryToAst';
1717
import { FieldConfig } from './typeDefs';
1818

19-
interface VisitInfoData<TContext = any> {
20-
node: AstDirNode | AstFileNode | AstRootTypeNode;
19+
interface VisitInfoData<TNode extends AstDirNode | AstFileNode | AstRootTypeNode, TContext = any> {
20+
node: TNode;
2121
nodeParent: AstDirNode | AstRootTypeNode | AstRootNode;
2222
operation: RootTypeNames;
2323
fieldName: string;
2424
fieldPath: string[];
2525
schemaComposer: SchemaComposer<TContext>;
2626
}
2727

28-
export class VisitInfo<TContext = any> {
29-
node: AstDirNode | AstFileNode | AstRootTypeNode;
28+
export class VisitInfo<TNode extends AstDirNode | AstFileNode | AstRootTypeNode, TContext = any> {
29+
node: TNode;
3030
/** Parent AST node from directoryToAst */
3131
nodeParent: AstDirNode | AstRootTypeNode | AstRootNode;
3232
/** Brunch of schema under which is working visitor. Can be: query, mutation, subscription */
@@ -38,7 +38,7 @@ export class VisitInfo<TContext = any> {
3838
/** Type registry */
3939
schemaComposer: SchemaComposer<TContext>;
4040

41-
constructor(data: VisitInfoData<TContext>) {
41+
constructor(data: VisitInfoData<TNode, TContext>) {
4242
this.node = data.node;
4343
this.operation = data.operation;
4444
this.nodeParent = data.nodeParent;
@@ -121,14 +121,13 @@ export class VisitInfo<TContext = any> {
121121
* This is mutable object and is shared between all calls.
122122
*/
123123
get fieldConfig(): FieldConfig {
124-
if (this.node.kind === 'file') {
125-
return this.node.code?.default as FieldConfig;
126-
} else if (this.node.kind === 'dir' || this.node.kind === 'rootType') {
127-
return this.node.namespaceConfig?.code?.default as FieldConfig;
124+
const node = this.node;
125+
if (node.kind === 'file') {
126+
return node.code?.default as FieldConfig;
127+
} else if (node.kind === 'dir' || this.node.kind === 'rootType') {
128+
return node.namespaceConfig?.code?.default as FieldConfig;
128129
}
129-
throw new Error(
130-
`Cannot get fieldConfig. Node has some strange kind: ${(this.node as any).kind}`
131-
);
130+
throw new Error(`Cannot get fieldConfig. Node has some strange kind: ${node.kind}`);
132131
}
133132

134133
/**

src/astVisitor.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ export type VisitorEmptyResult =
1717
| typeof VISITOR_REMOVE_NODE
1818
| typeof VISITOR_SKIP_CHILDREN;
1919

20-
export type VisitKindFn<NodeKind> = (
20+
export type VisitKindFn<TNode extends AstDirNode | AstFileNode | AstRootTypeNode> = (
2121
/** Info & helper functions from visitor during traversing AST tree */
22-
info: VisitInfo
23-
) => VisitorEmptyResult | NodeKind;
22+
info: VisitInfo<TNode>
23+
) => VisitorEmptyResult | TNode;
2424

2525
/**
2626
* Functions for every type of AST nodes which will be called by visitor.
@@ -66,14 +66,17 @@ export function astVisitor(
6666
});
6767
}
6868

69-
export function visitNode(info: VisitInfo, visitor: AstVisitor): void {
69+
export function visitNode(
70+
info: VisitInfo<AstDirNode | AstFileNode | AstRootTypeNode>,
71+
visitor: AstVisitor
72+
): void {
7073
let result: VisitorEmptyResult | AstDirNode | AstFileNode | AstRootTypeNode;
7174
if (info.node.kind === 'dir') {
72-
if (visitor.DIR) result = visitor.DIR(info);
75+
if (visitor.DIR) result = visitor.DIR(info as VisitInfo<AstDirNode>);
7376
} else if (info.node.kind === 'file') {
74-
if (visitor.FILE) result = visitor.FILE(info);
77+
if (visitor.FILE) result = visitor.FILE(info as VisitInfo<AstFileNode>);
7578
} else if (info.node.kind === 'rootType') {
76-
if (visitor.ROOT_TYPE) result = visitor.ROOT_TYPE(info);
79+
if (visitor.ROOT_TYPE) result = visitor.ROOT_TYPE(info as VisitInfo<AstRootTypeNode>);
7780
}
7881

7982
if (result === VISITOR_REMOVE_NODE) {

0 commit comments

Comments
 (0)