cincheo / jsweet

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

Type mapping and @Erased annotation ignored #479

Closed chfeiler closed 6 years ago

chfeiler commented 6 years ago

Hello!

It seems like JSweet transpiler imports classes/types even though they were mapped to a different type. This behaviour can be observed when instructing the transpiler to use modules. Example: we add a type mapping in our custom PrinterAdapter:

addTypeMapping("some.package.Money", "number");

And still the import pops up in the transpiled type script file:

import { Money } from '../../some/package/Money';

It not only happens with mapped types, but also with @Erased classes. The expected behaviour would be that the transpiler does not import mapped types or erased classes. I guess the problem is caused by this for loop in visitTopLevel in the Java2TypeScriptTranslator: it calls useModule without checking whether the import is needed. Can you confirm this guess?

chfeiler commented 6 years ago

Hello again, it seems like the type mapping and especially erased classes/interfaces are not only ignored at 'import' statements but also in this for loop in 'visitClassDef' in 'Java2TypeScriptTranslator' which automatically creates abstract method signatures in abstract classes for methods which are inherited from interfaces. These interfaces might however been erased by an adapter or directly by an annotation; in that case the signatures shouldn't be created. If this issue with the type mappings and '@Erased' annotations is not a design choice on purpose I can work on a fix for this issue!

lgrignon commented 6 years ago

It's a bug indeed. Well done :) Feel free to PR, I will review and merge

chfeiler commented 6 years ago

Fixed with #492.