cincheo / jsweet

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

Cannot import the generated TypeScript from another TypeScript file #64

Closed ghost closed 8 years ago

ghost commented 8 years ago

When compiling a simple Java class such as:

public class Foo {
...
}

The generated typescript is either (depending on whether the module option was turned on or not): module.ts:

class Foo {
...
}

or Foo.ts:

class Foo {
...
}

but in any case, the export keyword is missing so that the file is not a module and the Foo type cannot be used from another TypeScript file.

renaudpawlak commented 8 years ago

Precision: the issue only appears when the Foo class is in a @Root package (on in an empty package) and that the module option is on. In that cases only, it is a bug. For other cases (which were supposed to be the most common scenarios) it works fine.

Note that this issue is a side effect of #63.

renaudpawlak commented 8 years ago

There should not be an issue any more (if you use the latest snapshot). Now the behavior when using modules, is to always export and to allow one @Root package only. If several @Root packages are found, or if one @Root package is found and that some packages are not within that @Root package, then an error will be reported by JSweet. The reason for this constraint is that it is too complex to have several roots when using modules because modules refer to each others using relative paths. So it is ok to change the root globally for all modules at once, but it is not ok to have several path changes cohabitating.

ghost commented 8 years ago

When turning off the module option, the export disappears as well. Is there a reason why a public class is not transpiled to an export'ed class?

renaudpawlak commented 8 years ago

I assume that your class has no namespace (top-level class). I don't remember exactly, but yes there was a reason why a top-level class should not be exported (because it is not in a namespace). I did not understand the exact reason but tsc complains in some cases when exporting top-level classes. I advise to have a namespace for your classes... it is still better and you will get the export.

ghost commented 8 years ago

OK, I see. Indeed, my Java class is in a package, but that package is annotated with the @Root annotation, hence it appears out of any module.

If I move the class one level down in the the package path it appears in a module and the export is there as well. Thanks for your hints.

2016-02-17 21:06 GMT+01:00 Renaud Pawlak notifications@github.com:

I assume that your class has no namespace (top-level class). I don't remember exactly, but yes there was a reason why a top-level class should not be exported (because it is not in a namespace). I did not understand the exact reason but tsc complains in some cases when exporting top-level classes. I advise to have a namespace for your classes... it is still better and you will get the export.

— Reply to this email directly or view it on GitHub https://github.com/cincheo/jsweet/issues/64#issuecomment-185380713.