mirror of
https://github.com/cincheo/jsweet.git
synced 2025-12-15 15:29:22 +00:00
fix const variables in global scope + tests for const variables
This commit is contained in:
parent
534b5a66cb
commit
53ceebc01a
@ -19,7 +19,6 @@
|
|||||||
package org.jsweet.transpiler;
|
package org.jsweet.transpiler;
|
||||||
|
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.List;
|
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
import javax.lang.model.element.ElementKind;
|
import javax.lang.model.element.ElementKind;
|
||||||
@ -30,7 +29,6 @@ import com.sun.tools.javac.code.Symbol.VarSymbol;
|
|||||||
import com.sun.tools.javac.tree.JCTree;
|
import com.sun.tools.javac.tree.JCTree;
|
||||||
import com.sun.tools.javac.tree.JCTree.JCAssign;
|
import com.sun.tools.javac.tree.JCTree.JCAssign;
|
||||||
import com.sun.tools.javac.tree.JCTree.JCAssignOp;
|
import com.sun.tools.javac.tree.JCTree.JCAssignOp;
|
||||||
import com.sun.tools.javac.tree.JCTree.JCCompilationUnit;
|
|
||||||
import com.sun.tools.javac.tree.JCTree.JCIdent;
|
import com.sun.tools.javac.tree.JCTree.JCIdent;
|
||||||
import com.sun.tools.javac.tree.JCTree.JCUnary;
|
import com.sun.tools.javac.tree.JCTree.JCUnary;
|
||||||
|
|
||||||
@ -75,6 +73,7 @@ public class ConstAnalyzer extends AbstractTreeScanner {
|
|||||||
@Override
|
@Override
|
||||||
public void visitAssignop(JCAssignOp assignOp) {
|
public void visitAssignop(JCAssignOp assignOp) {
|
||||||
registerModification(assignOp.lhs);
|
registerModification(assignOp.lhs);
|
||||||
|
super.visitAssignop(assignOp);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -90,10 +89,4 @@ public class ConstAnalyzer extends AbstractTreeScanner {
|
|||||||
super.visitUnary(unary);
|
super.visitUnary(unary);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void process(List<JCCompilationUnit> compilationUnits) {
|
|
||||||
for (JCCompilationUnit compilationUnit : compilationUnits) {
|
|
||||||
scan(compilationUnit);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -3424,7 +3424,7 @@ public class Java2TypeScriptTranslator extends AbstractTreePrinter {
|
|||||||
if (isDefinitionScope) {
|
if (isDefinitionScope) {
|
||||||
print("var ");
|
print("var ");
|
||||||
} else {
|
} else {
|
||||||
if (varDecl.sym.getModifiers().contains(Modifier.FINAL) || (constAnalyzer != null
|
if (varDecl.sym.getModifiers().contains(Modifier.FINAL) || (!globals && constAnalyzer != null
|
||||||
&& !constAnalyzer.getModifiedVariables().contains(varDecl.sym))) {
|
&& !constAnalyzer.getModifiedVariables().contains(varDecl.sym))) {
|
||||||
print("const ");
|
print("const ");
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@ -22,16 +22,22 @@ import static org.junit.Assert.assertNull;
|
|||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
import static org.junit.Assert.fail;
|
import static org.junit.Assert.fail;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
import org.apache.commons.io.FileUtils;
|
import org.apache.commons.io.FileUtils;
|
||||||
|
import org.jsweet.test.transpiler.util.TranspilerTestRunner;
|
||||||
|
import org.jsweet.transpiler.JSweetContext;
|
||||||
|
import org.jsweet.transpiler.JSweetFactory;
|
||||||
import org.jsweet.transpiler.JSweetProblem;
|
import org.jsweet.transpiler.JSweetProblem;
|
||||||
import org.jsweet.transpiler.ModuleKind;
|
import org.jsweet.transpiler.ModuleKind;
|
||||||
import org.jsweet.transpiler.SourceFile;
|
import org.jsweet.transpiler.SourceFile;
|
||||||
|
import org.jsweet.transpiler.extension.PrinterAdapter;
|
||||||
import org.jsweet.transpiler.util.EvaluationResult;
|
import org.jsweet.transpiler.util.EvaluationResult;
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import source.extension.ToBeSorted;
|
||||||
import source.syntax.AnnotationQualifiedNames;
|
import source.syntax.AnnotationQualifiedNames;
|
||||||
import source.syntax.Casts;
|
import source.syntax.Casts;
|
||||||
import source.syntax.DocComments;
|
import source.syntax.DocComments;
|
||||||
@ -146,6 +152,23 @@ public class SyntaxTests extends AbstractTest {
|
|||||||
}, getSourceFile(FinalVariables.class));
|
}, getSourceFile(FinalVariables.class));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testConstVariables() throws IOException {
|
||||||
|
SourceFile f1 = getSourceFile(FinalVariables.class);
|
||||||
|
SourceFile f2 = getSourceFile(GlobalsInvocation.class);
|
||||||
|
transpilerTest().transpile(logHandler -> {
|
||||||
|
logHandler.assertNoProblems();
|
||||||
|
}, f1, f2);
|
||||||
|
String generatedCode = FileUtils.readFileToString(f1.getTsFile());
|
||||||
|
Assert.assertTrue(generatedCode.indexOf("const explicitFinalString") > -1);
|
||||||
|
Assert.assertTrue(generatedCode.indexOf("const implicitFinalString") > -1);
|
||||||
|
Assert.assertTrue(generatedCode.indexOf("let notFinalString") > -1);
|
||||||
|
generatedCode = FileUtils.readFileToString(f2.getTsFile());
|
||||||
|
Assert.assertTrue(generatedCode.indexOf("const explicitFinalGlobal") > -1);
|
||||||
|
Assert.assertTrue(generatedCode.indexOf("let implicitFinalGlobal") > -1);
|
||||||
|
Assert.assertTrue(generatedCode.indexOf("let notFinalGlobal") > -1);
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testFinalVariablesRuntime() {
|
public void testFinalVariablesRuntime() {
|
||||||
try {
|
try {
|
||||||
|
|||||||
@ -102,29 +102,29 @@ public class FinalVariables {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void explicitFinal() {
|
void explicitFinal() {
|
||||||
final String s = "abc";
|
final String explicitFinalString = "abc";
|
||||||
handler(new ANonFunctionalInterface() {
|
handler(new ANonFunctionalInterface() {
|
||||||
@Override
|
@Override
|
||||||
public void m() {
|
public void m() {
|
||||||
System.out.println(s);
|
System.out.println(explicitFinalString);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
void implicitFinal() {
|
void implicitFinal() {
|
||||||
String s = "abc";
|
String implicitFinalString = "abc";
|
||||||
handler(new ANonFunctionalInterface() {
|
handler(new ANonFunctionalInterface() {
|
||||||
@Override
|
@Override
|
||||||
public void m() {
|
public void m() {
|
||||||
System.out.println(s);
|
System.out.println(implicitFinalString);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
String notFinal() {
|
String notFinal() {
|
||||||
String s = "abc";
|
String notFinalString = "abc";
|
||||||
s = "bcd";
|
notFinalString = "bcd";
|
||||||
return s;
|
return notFinalString;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -143,3 +143,4 @@ interface Callback<T1, T2> {
|
|||||||
interface ANonFunctionalInterface {
|
interface ANonFunctionalInterface {
|
||||||
void m();
|
void m();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -50,4 +50,14 @@ class Globals {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
public static void m() {};
|
public static void m() {};
|
||||||
|
|
||||||
|
public final static int explicitFinalGlobal = 1;
|
||||||
|
public static int implicitFinalGlobal = 1;
|
||||||
|
public static int notFinalGlobal = 1;
|
||||||
|
|
||||||
|
public static void init() {
|
||||||
|
notFinalGlobal = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user