cincheo / jsweet

A Java to JavaScript transpiler.
http://www.jsweet.org
Other
1.45k stars 160 forks source link

Comments are dropped from translation #600

Open jrmuizel opened 4 years ago

jrmuizel commented 4 years ago

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.

lgrignon commented 4 years ago

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.

renaudpawlak commented 4 years ago

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);
jrmuizel commented 4 years ago

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

renaudpawlak commented 4 years ago

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: