jsweet icon indicating copy to clipboard operation
jsweet copied to clipboard

Comments are dropped from translation

Open jrmuizel opened this issue 4 years ago • 4 comments

Given

package org.jsweet;

import static def.dom.Globals.*;

public class HelloWorld {
	public static void main(String[] args) {
		/* Hello */
		alert("Hi there!");
	}
}

The "Hello" comment is dropped in the translation.

jrmuizel avatar Jul 12 '20 17:07 jrmuizel

Hello @jrmuizel

That's true for line comments but not for JavaDoc comments.

If my memory is correct, Javac AST does not provide line comments so there is unfortunately no way to transpile them.

Please close this if this answers your question.

lgrignon avatar Jul 18 '20 15:07 lgrignon

A possible solution I can think of so far would be to create a Java source code preprocessor (for example with JFlex) that would replace all inlined comments with $insert JSweet macros. For example:

int i = 0; // initialize i

Would be preprocessed to give (would generate in turn the expected TypeScript code):

int i = 0; $insert("// initialize i");

It would not work as is for comments inserted within expressions such as:

int i = /* zero */ 0;

But, maybe if we extend the $insert macro to insert code around existing expressions it could work...

int i = $insertBefore("/* zero */ ", 0);

renaudpawlak avatar Jul 19 '20 07:07 renaudpawlak

Would using something like http://javaparser.org/ instead of the javac AST help?

jrmuizel avatar Jul 19 '20 13:07 jrmuizel

When I started JSweet, JavaParser was a small project and the AST was too simple to be actually useful for JSweet. Especially, there was no typing (attribution). I can see that the project has evolved a lot and it might be interesting to take a closer look again now. However, I can foresee several potential pitfalls:

  • Performance: Javac is by far the most optimized Java parser I have ever seen. Maybe Javaparser is fast too, but I have no clue at that point (I guess it is hard to tell without trying, and especially, we need to compare not only the parsing, but also the attribution phase).
  • Typing completeness: addressing full type attribution is complicated. I remember a time when the Java JDT (Eclipse) compiler behaved differently from Javac w.r.t generics. A good thing to using javac for the core parser is that in JSweet you can meet no differences against the official standard behavior.
  • Licence compatibility: we have to be careful of that point too.
  • LAST BUT NOT LEAST - Migration time: JSweet heavily relies on the javac (non-public) API. It means that migrating to JavaParser will take a lot of efforts and development time. I am not sure anyone can afford such a development just to gain inlined comments ;) However, if all the previously mentioned issues could be overcome, it could be worth thinking of implementing JSweet 3 (for Java 10+) with JavaParser. I would see an immediate benefit that would be to remove some restrictions in parsing Java core classes (required for J4TS).

renaudpawlak avatar Jul 20 '20 06:07 renaudpawlak