lgrignon / jsweet-gradle-plugin

Brings the power of JSweet to Gradle
Apache License 2.0
17 stars 15 forks source link

Generated code causing "Property 'value' is used before being assigned" #30

Closed Skillzore closed 3 years ago

Skillzore commented 3 years ago

Now that my app is finally trying to run the jSweet code, it contains an error. I get this error when trying to compile my frontend:

Failed to compile.

~/git/poc-jsweet/frontend/src/jsweet/typescript/com/myapp/pocjsweetl/validate.ts
TypeScript error in ~/git/poc-jsweet/frontend/src/jsweet/typescript/com/myapp/pocjsweetl/validate.ts(6,18):
Property 'value' is used before being assigned.  TS2565

    4 | 
    5 |     public constructor(input: string) {
  > 6 |         if (this.value === undefined) { this.value = null; }
      |                  ^
    7 |         this.value = input;
    8 |     }
    9 |

The corresponding java constructor looks like this:

public class validate {
    private final String value;

    public validate(String input) {
        value = input;
    }

        ...
}

Is it the fact that the variable is final that causes the undefined check which results in the issue? Why would you do this at all? That code isn't present in java.

lgrignon commented 3 years ago

As you can imagine, transpiling Java to TypeScript requires some tiny adaptations in the generated code. JSweet tries to stick to the original code as much as it can. The commitment consists in providing a fairly readable TS code, in order to reuse it if needed & as close as possible to the original code.

That being said, the transpiled TS code does not seem to be valid with strict mode indeed. Generated TypeScript code is transpiled (hence validated) with a recent version of tsc, but without strict option. I suggest you disable the TS strict option, if that's something you can do. Otherwise, please open an issue on the JSweet repository, to add strict mode support to JSweet, but it would require quite a lot of work.

Please tell me if this answers your question, and close the issue if so.

lgrignon commented 3 years ago

Hello @Skillzore Did you find time to try this out? I guess given the time of the year that you are probably in holidays :) Please keep us updated on how it's going on.

Happy new year, bye

lgrignon commented 3 years ago

Hello @Skillzore Do you have any news on this please?

Bye bye

Skillzore commented 3 years ago

Hi @lgrignon! Yes, I have been on a long holiday vacation. Furthermore, I was trying out JSweet as a PoC for possibly transpiling our existing backend java validations into our new react frontend, to use as client validation. Sadly I got stuck on this issue and the time for the PoC is now over. I would imagine that disabling the TS strict option would solve the problem, but I won't be able to confirm it. Thanks for your help!

lgrignon commented 3 years ago

Thanks for the feedback @Skillzore Just out of curiosity, could you please tell me which tool did you finally choose?