Use optional method syntax for enter/exit/visit #531
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What does it do?
Generate
visitX?(ctx: XContext): Result
syntax rather thanvisitX?: (ctx: XContext) => Result
for generated listener and visitor interface members.Why would we want this?
Usually, method-style implementations work fine on implementing classes even when the method is declared as a property in the interface.
However, in some edge cases, it does not work so cleanly. Consider this construct in which
AbstractParseTreeVisitor
andLangVisitor
are combined intoAbstractLangVisitor
(see microsoft/TypeScript#22815 (comment) for context on the shenanigans going on here)However, if we generate the member differently so that it looks like a method rather than like a property...
Importantly, this alternative form still works if you declare it as a property rather than a method.
One other small benefit...
This alternative form also gives better autocomplete when you ask the editor to generate a stub.
Drawbacks
As far as I can tell this shouldn't break any existing code. However, I'm also not familiar with the precise semantics of matching members with interface definitions, so It's possible that there is some edge case where this fails that I haven't thought of.
Also, if someone actually prefers the old style of autocomplete, this could be slightly more annoying for them.