spoon icon indicating copy to clipboard operation
spoon copied to clipboard

fix: prevent appending semicolon after enum constant if it is not there initially

Open algomaster99 opened this issue 4 years ago • 4 comments

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.

algomaster99 avatar Nov 24 '21 15:11 algomaster99

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?

algomaster99 avatar Nov 24 '21 15:11 algomaster99

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 avatar Nov 24 '21 21:11 slarse

@slarse thanks for the pointers.

Keeping this in my backlog.

algomaster99 avatar Nov 25 '21 07:11 algomaster99

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.

algomaster99 avatar Dec 21 '21 15:12 algomaster99

Closing this as stale per the contributing guidelines, feel free to reopen if you can invest time in this again.

slarse avatar Oct 12 '22 20:10 slarse