Skip to content

Commit

Permalink
added a test for React.js + various fixed to make it pass
Browse files Browse the repository at this point in the history
  • Loading branch information
renaudpawlak committed Apr 21, 2016
1 parent 9b96127 commit 891f194
Show file tree
Hide file tree
Showing 5 changed files with 366 additions and 27 deletions.
8 changes: 8 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,14 @@
<scope>test</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.jsweet.candies</groupId>
<artifactId>react-global</artifactId>
<version>0.14.0-SNAPSHOT</version>
<scope>test</scope>
<optional>true</optional>
</dependency>

</dependencies>
<organization>
<name>JSweet</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,12 @@ public boolean substituteMethodInvocation(JCMethodInvocation invocation) {
printCastMethodInvocation(invocation);
return true;
}
if (matchesMethod(targetClassName, targetMethodName, UTIL_CLASSNAME, "any")) {
getPrinter().print("(<any>");
printCastMethodInvocation(invocation);
getPrinter().print(")");
return true;
}
if (matchesMethod(targetClassName, targetMethodName, UTIL_CLASSNAME, "object")) {
printCastMethodInvocation(invocation);
return true;
Expand Down Expand Up @@ -708,8 +714,12 @@ public AbstractTreePrinter substituteAndPrintType(JCTree typeTree, boolean array
}
}
if (typeFullName.startsWith(Class.class.getName() + "<")) {
getPrinter().print("typeof ");
return substituteAndPrintType(typeApply.arguments.head, arrayComponent, inTypeParameters, completeRawTypes);
if (typeApply.arguments.head.type.tsym instanceof TypeVariableSymbol) {
return getPrinter().print("any");
} else {
getPrinter().print("typeof ");
return substituteAndPrintType(typeApply.arguments.head, arrayComponent, inTypeParameters, completeRawTypes);
}
}
} else {
if (typesMapping.containsKey(typeFullName)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1097,7 +1097,7 @@ public void visitMethodDef(JCMethodDecl methodDecl) {
paramPrinted = true;
}
int i = 0;
for (JCVariableDecl param : methodDecl.params) {
for (JCVariableDecl param : methodDecl.getParameters()) {
print(param);
if (inOverload && overload.isValid && overload.defaultValues.get(i) != null) {
print(" = ").print(overload.defaultValues.get(i));
Expand Down Expand Up @@ -1187,7 +1187,7 @@ public void visitMethodDef(JCMethodDecl methodDecl) {
printMethodParamsTest(methodDecl, method);
print(") ");
if (i == 0 || method.sym.isConstructor()) {
printInlinedConstructorBody(overload, method, methodDecl.params);
printInlinedConstructorBody(overload, method, methodDecl.getParameters());
} else {
print("{").println().startIndent().printIndent();
// temporary cast to any because of Java generics
Expand All @@ -1199,10 +1199,10 @@ public void visitMethodDef(JCMethodDecl methodDecl) {
print("this");
}
print(".").print(getOverloadMethodName(method)).print("(");
for (int j = 0; j < method.params.size(); j++) {
print(avoidJSKeyword(overload.coreMethod.params.get(j).name.toString())).print(", ");
for (int j = 0; j < method.getParameters().size(); j++) {
print(avoidJSKeyword(overload.coreMethod.getParameters().get(j).name.toString())).print(", ");
}
if (!method.params.isEmpty()) {
if (!method.getParameters().isEmpty()) {
removeLastChars(2);
}
print(");");
Expand Down Expand Up @@ -1254,20 +1254,20 @@ private String parseJSNI(String jsniCode) {

private void printInlinedConstructorBody(Overload overload, JCMethodDecl method, List<? extends JCTree> args) {
print("{").println().startIndent();
for (int j = 0; j < method.params.size(); j++) {
for (int j = 0; j < method.getParameters().size(); j++) {
if (args.get(j) instanceof JCVariableDecl) {
if (method.params.get(j).name.equals(((JCVariableDecl) args.get(j)).name)) {
if (method.getParameters().get(j).name.equals(((JCVariableDecl) args.get(j)).name)) {
continue;
} else {
printIndent().print("var ").printIdentifier(avoidJSKeyword(method.params.get(j).name.toString())).print(" : ").print("any").print(" = ")
.printIdentifier(avoidJSKeyword(((JCVariableDecl) args.get(j)).name.toString())).print(";").println();
printIndent().print("var ").printIdentifier(avoidJSKeyword(method.getParameters().get(j).name.toString())).print(" : ").print("any")
.print(" = ").printIdentifier(avoidJSKeyword(((JCVariableDecl) args.get(j)).name.toString())).print(";").println();
}
} else {
if (method.params.get(j).name.toString().equals(args.get(j).toString())) {
if (method.getParameters().get(j).name.toString().equals(args.get(j).toString())) {
continue;
} else {
printIndent().print("var ").printIdentifier(avoidJSKeyword(method.params.get(j).name.toString())).print(" : ").print("any").print(" = ")
.print(args.get(j)).print(";").println();
printIndent().print("var ").printIdentifier(avoidJSKeyword(method.getParameters().get(j).name.toString())).print(" : ").print("any")
.print(" = ").print(args.get(j)).print(";").println();
}
}
}
Expand Down Expand Up @@ -1323,24 +1323,24 @@ private String getOverloadMethodName(JCMethodDecl method) {
}
StringBuilder sb = new StringBuilder(method.getName().toString());
sb.append("$");
for (JCVariableDecl p : method.params) {
for (JCVariableDecl p : method.getParameters()) {
sb.append(p.type.tsym.getSimpleName());
sb.append("_");
}
if (!method.params.isEmpty()) {
if (!method.getParameters().isEmpty()) {
sb.deleteCharAt(sb.length() - 1);
}
return sb.toString();
}

private void printMethodParamsTest(JCMethodDecl coreMethod, JCMethodDecl m) {
int i = 0;
for (; i < m.params.size(); i++) {
printInstanceOf(avoidJSKeyword(coreMethod.params.get(i).name.toString()), null, m.params.get(i).type);
for (; i < m.getParameters().size(); i++) {
printInstanceOf(avoidJSKeyword(coreMethod.getParameters().get(i).name.toString()), null, m.getParameters().get(i).type);
print(" && ");
}
for (; i < coreMethod.params.size(); i++) {
print(avoidJSKeyword(coreMethod.params.get(i).name.toString())).print(" == null");
for (; i < coreMethod.getParameters().size(); i++) {
print(avoidJSKeyword(coreMethod.getParameters().get(i).name.toString())).print(" == null");
print(" && ");
}
removeLastChars(4);
Expand Down Expand Up @@ -1593,6 +1593,8 @@ public void visitSelect(JCFieldAccess fieldAccess) {
print(fieldAccess.selected);
} else if ("this".equals(fieldAccess.name.toString()) && getScope().innerClassNotStatic) {
print("this.__parent");
} else if("this".equals(fieldAccess.name.toString())) {
print("this");
} else {
// if (Util.isIntegral(fieldAccess.type)) {
// print("(");
Expand Down Expand Up @@ -1749,7 +1751,7 @@ public void visitApply(JCMethodInvocation inv) {
if (!hasVarargs(methSym) //
|| inv.args.last().type.getKind() != TypeKind.ARRAY
// we dont use apply if var args type differ
|| !((ArrayType) inv.args.last().type).elemtype.equals(((ArrayType) methSym.params().last().type).elemtype)) {
|| !((ArrayType) inv.args.last().type).elemtype.equals(((ArrayType) methSym.getParameters().last().type).elemtype)) {
applyVarargs = false;
}

Expand Down Expand Up @@ -2470,13 +2472,16 @@ public void visitLambda(JCLambda lamba) {
public void visitReference(JCMemberReference memberReference) {
if (memberReference.sym instanceof MethodSymbol) {
MethodSymbol method = (MethodSymbol) memberReference.sym;
if (getParent() instanceof JCTypeCast) {
print("(");
}
print("(");
if (method.params != null) {
for (VarSymbol var : method.params) {
if (method.getParameters() != null) {
for (VarSymbol var : method.getParameters()) {
print(var.name.toString());
print(",");
}
if (!method.params.isEmpty()) {
if (!method.getParameters().isEmpty()) {
removeLastChar();
}
}
Expand All @@ -2494,17 +2499,20 @@ public void visitReference(JCMemberReference memberReference) {
MethodSymbol method = (MethodSymbol) memberReference.sym;

print("(");
if (method.params != null) {
for (VarSymbol var : method.params) {
if (method.getParameters() != null) {
for (VarSymbol var : method.getParameters()) {
print(var.name.toString());
print(",");
}
if (!method.params.isEmpty()) {
if (!method.getParameters().isEmpty()) {
removeLastChar();
}
}
print(")");
print(" }");
if (getParent() instanceof JCTypeCast) {
print(")");
}
}

}
Expand Down
8 changes: 8 additions & 0 deletions src/test/java/org/jsweet/test/transpiler/CandiesTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import source.candies.GlobalsImport;
import source.candies.JQuery;
import source.candies.QualifiedNames;
import source.candies.ReactLib;
import source.candies.Threejs;

public class CandiesTests extends AbstractTest {
Expand Down Expand Up @@ -97,6 +98,13 @@ public void testThreejs() {
} , getSourceFile(Threejs.class));
}

@Test
public void testReactLib() {
transpile(ModuleKind.none, logHandler -> {
assertEquals(0, logHandler.getReportedProblems().size());
} , getSourceFile(ReactLib.class));
}

@Test
public void testBabylonjs() {
transpile(logHandler -> {
Expand Down
Loading

0 comments on commit 891f194

Please sign in to comment.