Can we simplify TokenStreamRewriter's overloads?
The set of overloads in TokenStreamRewriter.tssoon` is seems over-complicated. I'm wondering if a little refactoring is in order, at least for the TypeScript port.
-
The optional
programNameargument at the beginning ofinsertAfter,insertBefore,replaceanddeletemethods seems unnecessary. Instead, clients using this API could create separate instances of TokenStreamRewriter, each one serving as it's own "program". -
The token index are overloaded with both
numberandTokentype seem like they could be replaced withnumber | Tokentypes, further reducing the number of overloads. -
Where the API includes a
textargument, is there any reason this shouldn't be typedstring
Thus I'd suggest the side-effecting methods be reduced to:
insertAfter(index: number|Token, text: string): void;
insertBefore(index: number|Token, text: string): void;
replace(index: number|Token, text: string): void ;
replace(from: number|Token, to: number|Token, text: string): void ;
delete(from: number|Token, to?: number|Token): void;
This seems like a pretty complete set of operations, but the class as currently defined seems to support sub-classing it, which exposes substantially more surface area (e.g. the nested operation classes). Was a useful application for sub-classing TokenStreamRewriter discovered in Java?
Years later, answering my own question. Yes, I'm convinced the overloads are wrong for TypeScript when optional parameters can be used. and TokenStreamRewriter is not the only case of this. I believe this calls for a review of the whole codebase.