mirror of
https://github.com/cincheo/jsweet.git
synced 2025-12-15 07:19: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;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
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.JCAssign;
|
||||
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.JCUnary;
|
||||
|
||||
@ -75,6 +73,7 @@ public class ConstAnalyzer extends AbstractTreeScanner {
|
||||
@Override
|
||||
public void visitAssignop(JCAssignOp assignOp) {
|
||||
registerModification(assignOp.lhs);
|
||||
super.visitAssignop(assignOp);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -90,10 +89,4 @@ public class ConstAnalyzer extends AbstractTreeScanner {
|
||||
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) {
|
||||
print("var ");
|
||||
} 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))) {
|
||||
print("const ");
|
||||
} else {
|
||||
|
||||
@ -22,16 +22,22 @@ import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
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.ModuleKind;
|
||||
import org.jsweet.transpiler.SourceFile;
|
||||
import org.jsweet.transpiler.extension.PrinterAdapter;
|
||||
import org.jsweet.transpiler.util.EvaluationResult;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import source.extension.ToBeSorted;
|
||||
import source.syntax.AnnotationQualifiedNames;
|
||||
import source.syntax.Casts;
|
||||
import source.syntax.DocComments;
|
||||
@ -146,6 +152,23 @@ public class SyntaxTests extends AbstractTest {
|
||||
}, 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
|
||||
public void testFinalVariablesRuntime() {
|
||||
try {
|
||||
|
||||
@ -102,29 +102,29 @@ public class FinalVariables {
|
||||
}
|
||||
|
||||
void explicitFinal() {
|
||||
final String s = "abc";
|
||||
final String explicitFinalString = "abc";
|
||||
handler(new ANonFunctionalInterface() {
|
||||
@Override
|
||||
public void m() {
|
||||
System.out.println(s);
|
||||
System.out.println(explicitFinalString);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void implicitFinal() {
|
||||
String s = "abc";
|
||||
String implicitFinalString = "abc";
|
||||
handler(new ANonFunctionalInterface() {
|
||||
@Override
|
||||
public void m() {
|
||||
System.out.println(s);
|
||||
System.out.println(implicitFinalString);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
String notFinal() {
|
||||
String s = "abc";
|
||||
s = "bcd";
|
||||
return s;
|
||||
String notFinalString = "abc";
|
||||
notFinalString = "bcd";
|
||||
return notFinalString;
|
||||
}
|
||||
|
||||
}
|
||||
@ -143,3 +143,4 @@ interface Callback<T1, T2> {
|
||||
interface ANonFunctionalInterface {
|
||||
void m();
|
||||
}
|
||||
|
||||
|
||||
@ -50,4 +50,14 @@ class Globals {
|
||||
});
|
||||
}
|
||||
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