Skip to content

Commit 1211240

Browse files
committed
feat: VoidVisitor
1 parent f07857e commit 1211240

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

src/index.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,23 +42,33 @@ export { JavaParserListener } from './parser/JavaParserListener';
4242
/**
4343
* Create a parse tree visitor
4444
*/
45+
export function createVisitor<T>(visitor: Visitor<T>): ConcreteVisitor<T>;
46+
export function createVisitor(visitor: VoidVisitor): ConcreteVisitor<void>;
4547
export function createVisitor<T>(visitor: Visitor<T>): ConcreteVisitor<T> {
4648
// we don't want users to write classes because it's not JavaScript-y
4749
// so we'll set implementation of abstract methods and other visit* methods in constructor
4850
// @ts-ignore
4951
return new class extends AbstractParseTreeVisitor<T> {
5052
constructor() {
5153
super();
52-
Object.assign(this, visitor);
54+
Object.assign(this, {
55+
defaultResult: () => undefined,
56+
aggregateResult: () => undefined,
57+
...visitor,
58+
});
5359
}
5460
}();
5561
}
5662

5763
export interface Visitor<T>
5864
extends AbstractVisitor<T>,
59-
Omit<JavaParserVisitor<T>, NonOverridableMethods> {}
65+
OmitStrict<JavaParserVisitor<T>, NonOverridableMethods> {}
66+
67+
export interface VoidVisitor
68+
extends OmitStrict<Visitor<void>, 'defaultResult' | 'aggregateResult'> {}
6069

6170
type NonOverridableMethods = keyof ParseTreeVisitor<any>;
71+
type OmitStrict<T, K extends keyof T> = Omit<T, K>;
6272

6373
// Just to create a better name
6474
export interface ConcreteVisitor<T> extends AbstractParseTreeVisitor<T> {}

0 commit comments

Comments
 (0)