[TypeScript] Class Visitor defines instance member property visit, but extended class defines it as instance member function.
The TypeScript target produces a visitor similar to the following:
export default class FooVisitor<Result> extends ParseTreeVisitor<Result> {
/**
* Visit a parse tree produced by `FooParser.bar`.
* @param ctx the parse tree
* @return the visitor result
*/
visitBar?: (ctx: BarContext) => Result;
}
When overridden as shown in the docs, the code doesn't work (override is never called) and the compiler will show an error explaining the reason:
TS2425: Class FooVisitor<X> defines instance member property visitBar, but extended class CustomVisitor defines it as instance member function.
As a workaround, it is possible to switch to a property:
class CustomVisitor extends FooVisitor<X> {
visitBar = (ctx: BarContext): X => {
return new X()
}
}
but I expect that the intent from the target is overloading, same as one would overload the visitor methods in plain JS target. To allow that, the target can be modified to
-export default class FooVisitor<Result> extends ParseTreeVisitor<Result> {
+export default abstract class FooVisitor<Result> extends ParseTreeVisitor<Result> {
/**
* Visit a parse tree produced by `FooParser.bar`.
* @param ctx the parse tree
* @return the visitor result
*/
- visitBar?: (ctx: BarContext) => Result;
+ visitBar?(ctx: BarContext) => Result;
}
Hi, thanks for this. Can you provide a pointer to the docs you are referring to?
https://github.com/antlr/antlr4/blob/dev/doc/typescript-target.md#how-do-i-create-and-run-a-visitor
For reference, here's an SO question which links to relevant TypeScript docs: https://stackoverflow.com/questions/44153378/typescript-abstract-optional-method
This also prohibits from using super in child class methods.
Obviously a bug, I wonder why it's not being fixed yet.