mirror of
https://github.com/cincheo/jsweet.git
synced 2025-12-15 15:29:22 +00:00
added support and tests for statements with optional blocks
This commit is contained in:
parent
f647af31e6
commit
99d725e884
@ -1454,12 +1454,14 @@ public class Java2TypeScriptTranslator extends AbstractTreePrinter {
|
|||||||
@Override
|
@Override
|
||||||
public void visitForeachLoop(JCEnhancedForLoop foreachLoop) {
|
public void visitForeachLoop(JCEnhancedForLoop foreachLoop) {
|
||||||
String indexVarName = "index" + Util.getId();
|
String indexVarName = "index" + Util.getId();
|
||||||
if (foreachLoop.expr instanceof JCIdent || foreachLoop.expr instanceof JCFieldAccess) {
|
boolean noVariable = foreachLoop.expr instanceof JCIdent || foreachLoop.expr instanceof JCFieldAccess;
|
||||||
|
if (noVariable) {
|
||||||
print("for(var " + indexVarName + "=0; " + indexVarName + " < ").print(foreachLoop.expr).print(".length; " + indexVarName + "++) {").println()
|
print("for(var " + indexVarName + "=0; " + indexVarName + " < ").print(foreachLoop.expr).print(".length; " + indexVarName + "++) {").println()
|
||||||
.startIndent().printIndent();
|
.startIndent().printIndent();
|
||||||
print("var " + foreachLoop.var.name.toString() + " = ").print(foreachLoop.expr).print("[" + indexVarName + "];").println();
|
print("var " + foreachLoop.var.name.toString() + " = ").print(foreachLoop.expr).print("[" + indexVarName + "];").println();
|
||||||
} else {
|
} else {
|
||||||
String arrayVarName = "array" + Util.getId();
|
String arrayVarName = "array" + Util.getId();
|
||||||
|
print("{").println().startIndent().printIndent();
|
||||||
print("var " + arrayVarName + " = ").print(foreachLoop.expr).print(";").println().printIndent();
|
print("var " + arrayVarName + " = ").print(foreachLoop.expr).print(";").println().printIndent();
|
||||||
print("for(var " + indexVarName + "=0; " + indexVarName + " < " + arrayVarName + ".length; " + indexVarName + "++) {").println().startIndent()
|
print("for(var " + indexVarName + "=0; " + indexVarName + " < " + arrayVarName + ".length; " + indexVarName + "++) {").println().startIndent()
|
||||||
.printIndent();
|
.printIndent();
|
||||||
@ -1467,6 +1469,9 @@ public class Java2TypeScriptTranslator extends AbstractTreePrinter {
|
|||||||
}
|
}
|
||||||
printIndent().print(foreachLoop.body);
|
printIndent().print(foreachLoop.body);
|
||||||
endIndent().println().printIndent().print("}");
|
endIndent().println().printIndent().print("}");
|
||||||
|
if (!noVariable) {
|
||||||
|
endIndent().println().printIndent().print("}");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -1496,9 +1501,19 @@ public class Java2TypeScriptTranslator extends AbstractTreePrinter {
|
|||||||
public void visitIf(JCIf ifStatement) {
|
public void visitIf(JCIf ifStatement) {
|
||||||
print("if(").print(ifStatement.cond).print(") ");
|
print("if(").print(ifStatement.cond).print(") ");
|
||||||
print(ifStatement.thenpart);
|
print(ifStatement.thenpart);
|
||||||
|
if(!(ifStatement.thenpart instanceof JCBlock)) {
|
||||||
|
if(!statementsWithNoSemis.contains(ifStatement.thenpart.getClass())) {
|
||||||
|
print(";");
|
||||||
|
}
|
||||||
|
}
|
||||||
if (ifStatement.elsepart != null) {
|
if (ifStatement.elsepart != null) {
|
||||||
print(" else ");
|
print(" else ");
|
||||||
print(ifStatement.elsepart);
|
print(ifStatement.elsepart);
|
||||||
|
if(!(ifStatement.elsepart instanceof JCBlock)) {
|
||||||
|
if(!statementsWithNoSemis.contains(ifStatement.elsepart.getClass())) {
|
||||||
|
print(";");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -35,6 +35,7 @@ import source.syntax.FinalVariables;
|
|||||||
import source.syntax.FinalVariablesRuntime;
|
import source.syntax.FinalVariablesRuntime;
|
||||||
import source.syntax.GlobalsCastMethod;
|
import source.syntax.GlobalsCastMethod;
|
||||||
import source.syntax.GlobalsInvocation;
|
import source.syntax.GlobalsInvocation;
|
||||||
|
import source.syntax.StatementsWithNoBlocks;
|
||||||
import source.syntax.IndexedAccessInStaticScope;
|
import source.syntax.IndexedAccessInStaticScope;
|
||||||
import source.syntax.Keywords;
|
import source.syntax.Keywords;
|
||||||
import source.syntax.Labels;
|
import source.syntax.Labels;
|
||||||
@ -64,6 +65,13 @@ public class SyntaxTests extends AbstractTest {
|
|||||||
} , getSourceFile(Keywords.class));
|
} , getSourceFile(Keywords.class));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testStatementsWithNoBlocks() {
|
||||||
|
transpile((logHandler) -> {
|
||||||
|
Assert.assertEquals("There should be no errors", 0, logHandler.reportedProblems.size());
|
||||||
|
} , getSourceFile(StatementsWithNoBlocks.class));
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testQualifiedNames() {
|
public void testQualifiedNames() {
|
||||||
transpile((logHandler) -> {
|
transpile((logHandler) -> {
|
||||||
|
|||||||
58
src/test/java/source/syntax/StatementsWithNoBlocks.java
Normal file
58
src/test/java/source/syntax/StatementsWithNoBlocks.java
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
package source.syntax;
|
||||||
|
|
||||||
|
import static jsweet.dom.Globals.console;
|
||||||
|
|
||||||
|
public class StatementsWithNoBlocks {
|
||||||
|
|
||||||
|
void m1() {
|
||||||
|
int i = 1;
|
||||||
|
if (i == 1)
|
||||||
|
console.log("is true");
|
||||||
|
else
|
||||||
|
console.log("is false");
|
||||||
|
}
|
||||||
|
|
||||||
|
void m2() {
|
||||||
|
int i = 1;
|
||||||
|
if (i == 1) {
|
||||||
|
console.log("is true");
|
||||||
|
} else {
|
||||||
|
console.log("is false");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void m3() {
|
||||||
|
int i = 1;
|
||||||
|
if (i == 1) {
|
||||||
|
console.log("is true");
|
||||||
|
} else
|
||||||
|
console.log("is false");
|
||||||
|
}
|
||||||
|
|
||||||
|
void m4() {
|
||||||
|
int i = 1;
|
||||||
|
if (i == 1)
|
||||||
|
console.log("is true");
|
||||||
|
else {
|
||||||
|
console.log("is false");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void m5() {
|
||||||
|
int i = 1;
|
||||||
|
if (i == 1)
|
||||||
|
for (String s : new String[] {})
|
||||||
|
console.log(s);
|
||||||
|
else {
|
||||||
|
console.log("is false");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void m6() {
|
||||||
|
for (String s : new String[] {})
|
||||||
|
console.log(s);
|
||||||
|
int i = 1;
|
||||||
|
console.log(i);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user