mirror of
https://github.com/cincheo/jsweet.git
synced 2025-12-15 07:19:22 +00:00
allow variable intializer substitution
This commit is contained in:
parent
8a3fd1a387
commit
beef1b6731
@ -2693,11 +2693,11 @@ public class Java2TypeScriptTranslator extends AbstractTreePrinter {
|
||||
} else if (var.init == null) {
|
||||
if (doesMemberNameRequireQuotes(name)) {
|
||||
printIndent().print("if(").print("this['").print(name).print("']").print("===undefined) ")
|
||||
.print("this['").print(name).print("'] = ").print(Util.getTypeInitialValue(var.type)).print(";")
|
||||
.print("this['").print(name).print("'] = ").print(getAdapter().getVariableInitialValue(var.sym)).print(";")
|
||||
.println();
|
||||
} else {
|
||||
printIndent().print("if(").print("this.").print(name).print("===undefined) this.").print(name)
|
||||
.print(" = ").print(Util.getTypeInitialValue(var.type)).print(";").println();
|
||||
.print(" = ").print(getAdapter().getVariableInitialValue(var.sym)).print(";").println();
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -2863,7 +2863,7 @@ public class Java2TypeScriptTranslator extends AbstractTreePrinter {
|
||||
name = context.getFieldNameMapping(field.sym);
|
||||
}
|
||||
printIndent().print("if(").print("this.").print(name).print("===undefined) ").print("this.")
|
||||
.print(name).print(" = ").print(Util.getTypeInitialValue(field.type)).print(";").println();
|
||||
.print(name).print(" = ").print(getAdapter().getVariableInitialValue(field.sym)).print(";").println();
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -3340,7 +3340,7 @@ public class Java2TypeScriptTranslator extends AbstractTreePrinter {
|
||||
// var initialization is not allowed in definition
|
||||
if (!isDefinitionScope && !(ambient || (isTopLevelScope() && isDefinitionScope))
|
||||
&& varDecl.sym.isStatic() && varDecl.init == null) {
|
||||
print(" = ").print(Util.getTypeInitialValue(varDecl.sym.type));
|
||||
print(" = ").print(getAdapter().getVariableInitialValue(varDecl.sym));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -32,6 +32,7 @@ import javax.lang.model.element.Name;
|
||||
import javax.lang.model.element.PackageElement;
|
||||
import javax.lang.model.element.TypeElement;
|
||||
import javax.lang.model.element.TypeParameterElement;
|
||||
import javax.lang.model.element.VariableElement;
|
||||
import javax.lang.model.type.DeclaredType;
|
||||
import javax.lang.model.type.TypeMirror;
|
||||
import javax.lang.model.util.Types;
|
||||
@ -740,7 +741,19 @@ public class PrinterAdapter {
|
||||
*/
|
||||
public boolean substituteVariableAccess(VariableAccessElement variableAccess) {
|
||||
return parentAdapter == null ? false : parentAdapter.substituteVariableAccess(variableAccess);
|
||||
}
|
||||
|
||||
/**
|
||||
* Called if the initializer of a variable is undefined (in order to force a
|
||||
* default value).
|
||||
*
|
||||
* @param variable
|
||||
* the variable to return an initializer for
|
||||
* @return an initializer expression, null to keep undefined
|
||||
*/
|
||||
public String getVariableInitialValue(VariableElement variable) {
|
||||
return parentAdapter == null ? util().getTypeInitialValue(variable.asType())
|
||||
: parentAdapter.getVariableInitialValue(variable);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@ -106,4 +106,9 @@ public interface Util {
|
||||
*/
|
||||
String getRelativePath(String fromPath, String toPath);
|
||||
|
||||
/**
|
||||
* Returns the literal for a given type initial value.
|
||||
*/
|
||||
String getTypeInitialValue(TypeMirror type);
|
||||
|
||||
}
|
||||
|
||||
@ -23,6 +23,7 @@ import java.util.List;
|
||||
import javax.lang.model.element.Element;
|
||||
import javax.lang.model.element.TypeElement;
|
||||
import javax.lang.model.type.DeclaredType;
|
||||
import javax.lang.model.type.TypeKind;
|
||||
import javax.lang.model.type.TypeMirror;
|
||||
|
||||
import org.jsweet.transpiler.JSweetContext;
|
||||
@ -180,5 +181,22 @@ public class UtilSupport implements Util {
|
||||
public String getRelativePath(String fromPath, String toPath) {
|
||||
return org.jsweet.transpiler.util.Util.getRelativePath(fromPath, toPath);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getTypeInitialValue(TypeMirror type) {
|
||||
if (type == null) {
|
||||
return "null";
|
||||
}
|
||||
if (isNumber(type)) {
|
||||
return "0";
|
||||
} else if (type.getKind() == TypeKind.BOOLEAN) {
|
||||
return "false";
|
||||
} else if (type.getKind() == TypeKind.VOID) {
|
||||
return null;
|
||||
} else {
|
||||
return "null";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@ -1288,7 +1288,7 @@ public class Util {
|
||||
/**
|
||||
* Returns the literal for a given type inital value.
|
||||
*/
|
||||
public static String getTypeInitialValue(Type type) {
|
||||
public static String getTypeInitialValue(TypeMirror type) {
|
||||
if (type == null) {
|
||||
return "null";
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user