report error on wildcard imports + test + tests for @Root structural

issues
This commit is contained in:
Renaud Pawlak 2015-11-24 04:53:04 +01:00
parent 3c14bd4780
commit 7b990e2b05
12 changed files with 81 additions and 8 deletions

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
/.jsweet/
/target/
/tempOut/

View File

@ -144,7 +144,7 @@ public class JSweetCommandLineLauncher {
switchArg = new Switch("verbose");
switchArg.setLongFlag("verbose");
switchArg.setShortFlag('v');
switchArg.setHelp("Turn all levels of logging.");
switchArg.setHelp("Turn on all levels of logging.");
switchArg.setDefault("false");
jsap.registerParameter(switchArg);

View File

@ -236,7 +236,11 @@ public enum JSweetProblem {
* Raised when a package is named after an invalid name (typically a
* TypeScript keyword).
*/
PACKAGE_NAME_CONTAINS_KEYWORD(Severity.ERROR);
PACKAGE_NAME_CONTAINS_KEYWORD(Severity.ERROR),
/**
* Raised when a wildcard import is used.
*/
WILDCARD_IMPORT(Severity.ERROR);
private Severity severity;
@ -354,6 +358,8 @@ public enum JSweetProblem {
return String.format("no bundle file generated: no entries found, you must define at least one main method", params);
case PACKAGE_NAME_CONTAINS_KEYWORD:
return String.format("a package name cannot contain top-level keyword(s): %s", params);
case WILDCARD_IMPORT:
return String.format("imports cannot use * wildcards: please import a specific element", params);
}
return null;
}

View File

@ -11,7 +11,7 @@
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
*/
package org.jsweet.transpiler.typescript;
import static org.apache.commons.lang3.StringUtils.isBlank;
@ -882,6 +882,10 @@ public class Java2TypeScriptTranslator extends AbstractTreePrinter {
public void visitImport(JCImport importDecl) {
imports.add(importDecl);
String qualId = importDecl.getQualifiedIdentifier().toString();
if (qualId.endsWith("*")) {
report(importDecl, JSweetProblem.WILDCARD_IMPORT);
return;
}
String adaptedQualId = getAdapter().needsImport(importDecl, qualId);
if (adaptedQualId != null && adaptedQualId.contains(".")) {
String[] namePath = adaptedQualId.split("\\.");

View File

@ -11,10 +11,9 @@
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
*/
package org.jsweet.test.transpiler;
import static org.jsweet.transpiler.JSweetProblem.GLOBAL_CANNOT_BE_INSTANTIATED;
import static org.jsweet.transpiler.JSweetProblem.GLOBAL_CONSTRUCTOR_DEF;
import static org.jsweet.transpiler.JSweetProblem.GLOBAL_DELETE;
@ -34,6 +33,7 @@ import org.jsweet.test.transpiler.source.structural.Inheritance;
import org.jsweet.test.transpiler.source.structural.InnerClass;
import org.jsweet.test.transpiler.source.structural.NameClashes;
import org.jsweet.test.transpiler.source.structural.NoInstanceofForInterfaces;
import org.jsweet.test.transpiler.source.structural.NoWildcardsInImports;
import org.jsweet.test.transpiler.source.structural.TwoClassesInSameFile;
import org.jsweet.test.transpiler.source.structural.WrongConstructsInEnums;
import org.jsweet.test.transpiler.source.structural.WrongConstructsInInterfaces;
@ -46,8 +46,12 @@ import org.jsweet.test.transpiler.source.structural.globalclasses.f.GlobalFuncti
import org.jsweet.test.transpiler.source.structural.globalclasses.g.GlobalFunctionDelete;
import org.jsweet.test.transpiler.source.structural.globalclasses.noroot.a.GlobalsInNoRoot;
import org.jsweet.test.transpiler.source.structural.globalclasses.root.a.GlobalsInRoot;
import org.jsweet.test.transpiler.source.structural.rootparent1.InvalidClassLocation;
import org.jsweet.test.transpiler.source.structural.rootparent1.root.NoClassesInRootParent;
import org.jsweet.test.transpiler.source.structural.rootparent2.root.NoRootInRoot;
import org.jsweet.transpiler.JSweetProblem;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
public class StructuralTests extends AbstractTest {
@ -201,8 +205,7 @@ public class StructuralTests extends AbstractTest {
Assert.assertEquals("invoked", r.get("test"));
Assert.assertEquals("invoked1_2", r.get("Static"));
Assert.assertEquals("invoked1_2", r.get("test2"));
} , getSourceFile(Globals.class),
getSourceFile(org.jsweet.test.transpiler.source.structural.globalclasses.e.Globals.class),
} , getSourceFile(Globals.class), getSourceFile(org.jsweet.test.transpiler.source.structural.globalclasses.e.Globals.class),
getSourceFile(GlobalFunctionAccessFromMain.class));
}
@ -223,7 +226,30 @@ public class StructuralTests extends AbstractTest {
Assert.assertEquals(true, r.get("m2"));
} , getSourceFile(GlobalsInNoRoot.class));
}
@Ignore
@Test
public void testNoClassesInRootParent() {
transpile((logHandler) -> {
assertEquals(1, logHandler.reportedProblems.size());
} , getSourceFile(InvalidClassLocation.class), getSourceFile(NoClassesInRootParent.class));
}
@Ignore
@Test
public void testNoRootInRoot() {
transpile((logHandler) -> {
assertEquals(1, logHandler.reportedProblems.size());
} , getSourceFile(NoRootInRoot.class));
}
@Test
public void testWildcardsInImports() {
transpile((logHandler) -> {
logHandler.assertReportedProblems(JSweetProblem.WILDCARD_IMPORT, JSweetProblem.WILDCARD_IMPORT);
} , getSourceFile(NoWildcardsInImports.class));
}
@Test
public void testAutoImportClassesInSamePackage() {
eval((logHandler, r) -> {

View File

@ -0,0 +1,13 @@
package org.jsweet.test.transpiler.source.structural;
import static jsweet.dom.Globals.*;
import jsweet.dom.*;
public class NoWildcardsInImports {
public static void main(String[] args) {
HTMLElement element = document.getElementById("test");
element.innerHTML = "test";
}
}

View File

@ -0,0 +1,5 @@
package org.jsweet.test.transpiler.source.structural.rootparent1;
public class InvalidClassLocation {
}

View File

@ -0,0 +1,5 @@
package org.jsweet.test.transpiler.source.structural.rootparent1.root;
public class NoClassesInRootParent {
}

View File

@ -0,0 +1,2 @@
@jsweet.lang.Root
package org.jsweet.test.transpiler.source.structural.rootparent1.root;

View File

@ -0,0 +1,2 @@
@jsweet.lang.Root
package org.jsweet.test.transpiler.source.structural.rootparent2;

View File

@ -0,0 +1,5 @@
package org.jsweet.test.transpiler.source.structural.rootparent2.root;
public class NoRootInRoot {
}

View File

@ -0,0 +1,2 @@
@jsweet.lang.Root
package org.jsweet.test.transpiler.source.structural.rootparent2.root;