spoon
spoon copied to clipboard
fix: prevent appending semicolon after enum constant if it is not there initially
Printing enum constants with sniper printer enforces ; at the end even though it is not present before. For example,
public enum Days {
SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
THURSDAY, FRIDAY, SATURDAY
}
is changed to
public enum Days {
SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
THURSDAY, FRIDAY, SATURDAY;
}
enum is compilable with or without the ; if there are no method declarations afterwards. Source: https://docs.oracle.com/javase/tutorial/java/javaOO/enum.html.
when there are fields and methods, the list of enum constants must end with a semicolon.
I think we should configure the sniper printer to check for ; and print it only if required. The current behaviour is not causing a compilation error, but it helps minimise the diff so we should consider it.
I have figured out where this happens. However, I am not sure how to fix this yet. I know that this fix would go into SniperJavaPrettyPrinter. This is the place where we build the context, but it spoils the genericity if we also check for such an edge case. @slarse @MartinWitt @nharrand , your thoughts?
The place where most of the sniper printing magic happens is in AbstractSourceFragmentPrinter::print, and the methods transitively called from there. That's usually where I start debugging when I'm trying to figure stuff like this out.
@slarse thanks for the pointers.
Keeping this in my backlog.
Debugged this further. I think the following is a wrong assumption. https://github.com/INRIA/spoon/blob/fa8f97c6d1d78e6e8657e5aa4dd4b45bbaa05a92/src/main/java/spoon/support/sniper/internal/AbstractSourceFragmentPrinter.java#L90-L93
The comment says that the token is not present in the original sources, however, it always prints them. We need to have a way that prevents that.
Closing this as stale per the contributing guidelines, feel free to reopen if you can invest time in this again.