From cdbc209bdc35fe25c8ce6b9737dcfff573002a19 Mon Sep 17 00:00:00 2001 From: negora Date: Sat, 8 Jul 2017 09:20:59 +0200 Subject: [PATCH] Fixes two types of errors when requiring modules. This commit fixes the errors that come up when generating the call to "require()" in these two situations: 1. When importing a module that represents a namespace and contains a class/function with the same name that the namespace. Example: namespace foo and function foo.foo. This belongs to the issue #318. 2. When importing a module that represents, at the same time, a namespace and a class/function. Example: namespace foo and function foo. This belongs to the issue #319. --- .../transpiler/Java2TypeScriptTranslator.java | 38 ++++++++++++++++++- .../extension/Java2TypeScriptAdapter.java | 2 +- 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/transpiler/src/main/java/org/jsweet/transpiler/Java2TypeScriptTranslator.java b/transpiler/src/main/java/org/jsweet/transpiler/Java2TypeScriptTranslator.java index 5780bba5..53fed2f5 100644 --- a/transpiler/src/main/java/org/jsweet/transpiler/Java2TypeScriptTranslator.java +++ b/transpiler/src/main/java/org/jsweet/transpiler/Java2TypeScriptTranslator.java @@ -491,9 +491,10 @@ public class Java2TypeScriptTranslator extends AbstractTreePrinter { if (qualified.sym != null) { // regular import case (qualified.sym is a package) if (context.hasAnnotationType(qualified.sym, JSweetConfig.ANNOTATION_MODULE)) { + String targetName = createImportAliasFromFieldAccess (qualified); String actualName = context.getAnnotationValue(qualified.sym, JSweetConfig.ANNOTATION_MODULE, String.class, null); - useModule(true, null, importDecl, qualified.name.toString(), actualName, + useModule(true, null, importDecl, targetName, actualName, ((PackageSymbol) qualified.sym)); } } else { @@ -506,10 +507,11 @@ public class Java2TypeScriptTranslator extends AbstractTreePrinter { if (qualified.name.equals(importedMember.getSimpleName())) { if (context.hasAnnotationType(importedMember, JSweetConfig.ANNOTATION_MODULE)) { + String targetName = createImportAliasFromSymbol (importedMember); String actualName = context.getAnnotationValue(importedMember, JSweetConfig.ANNOTATION_MODULE, String.class, null); useModule(true, null, importDecl, - importedMember.getSimpleName().toString(), actualName, + targetName, actualName, importedMember); break; } @@ -746,8 +748,40 @@ public class Java2TypeScriptTranslator extends AbstractTreePrinter { globalModule = false; + } + + + private String createImportAliasFromFieldAccess (JCFieldAccess access) { + String name = extractNameFromAnnotatedSymbol (access.sym); + if (name != null) { + return name; + } else { + return access.name.toString (); + } } + + private String createImportAliasFromSymbol (Symbol symbol) { + String name = extractNameFromAnnotatedSymbol (symbol); + if (name != null) { + return name; + } else { + return symbol.getSimpleName ().toString (); + } + } + + + private String extractNameFromAnnotatedSymbol (Symbol symbol) { + if (context.hasAnnotationType (symbol, JSweetConfig.ANNOTATION_NAME)) { + return context.getAnnotationValue ( + symbol, JSweetConfig.ANNOTATION_NAME, + String.class, null); + } else { + return null; + } + } + + private void printDocComment(JCTree element, boolean indent) { if (compilationUnit != null && compilationUnit.docComments != null) { Comment comment = compilationUnit.docComments.getComment(element); diff --git a/transpiler/src/main/java/org/jsweet/transpiler/extension/Java2TypeScriptAdapter.java b/transpiler/src/main/java/org/jsweet/transpiler/extension/Java2TypeScriptAdapter.java index 3147f9e8..efd54bce 100644 --- a/transpiler/src/main/java/org/jsweet/transpiler/extension/Java2TypeScriptAdapter.java +++ b/transpiler/src/main/java/org/jsweet/transpiler/extension/Java2TypeScriptAdapter.java @@ -270,7 +270,7 @@ public class Java2TypeScriptAdapter extends PrinterAdapter { case "java.lang.Math": return null; } - String name = getPrinter().getRootRelativeName(fa.selected.type.tsym, context.useModules); + String name = getPrinter().getRootRelativeName(fa.selected.type.tsym, false); String methodName = fa.name.toString(); // function is a top-level global function (no need to import)