more standard formatting

This commit is contained in:
Renaud Pawlak 2020-09-11 16:16:26 +02:00
parent ea1aa4079b
commit 1ceedd955f
3 changed files with 49 additions and 46 deletions

View File

@ -1206,7 +1206,7 @@ public class Java2TypeScriptTranslator extends AbstractTreePrinter {
print("(");
if (typeName.startsWith("Int") || typeName.startsWith("Long")
|| typeName.startsWith("Double")) {
print("p0 : number");
print("p0: number");
} else {
printArguments(typeApply.arguments);
}
@ -1222,7 +1222,7 @@ public class Java2TypeScriptTranslator extends AbstractTreePrinter {
print("(");
if (typeName.startsWith("Int") || typeName.startsWith("Long")
|| typeName.startsWith("Double")) {
print("p0 : number");
print("p0: number");
} else {
printArguments(typeApply.arguments.subList(0, typeApply.arguments.length() - 1));
}
@ -1257,7 +1257,7 @@ public class Java2TypeScriptTranslator extends AbstractTreePrinter {
print("(");
if (typeName.startsWith("Int") || typeName.startsWith("Long")
|| typeName.startsWith("Double")) {
print("p0 : number");
print("p0: number");
} else {
printArguments(typeApply.arguments);
}
@ -1687,7 +1687,7 @@ public class Java2TypeScriptTranslator extends AbstractTreePrinter {
if (!globals && !getScope().enumScope && !context.isInterface(classdecl.sym)
&& context.getStaticInitializerCount(classdecl.sym) > 0) {
printIndent().print("static __static_initialized : boolean = false;").println();
printIndent().print("static __static_initialized: boolean = false;").println();
int liCount = context.getStaticInitializerCount(classdecl.sym);
String prefix = getClassName(classdecl.sym) + ".";
@ -1791,9 +1791,9 @@ public class Java2TypeScriptTranslator extends AbstractTreePrinter {
print(def);
if (getScope().enumScope && JCTree.Tag.VARDEF == def.getTag() && getScope().enumString) {
print(" = \"");
print(" = " + getStringLiteralQuote());
print(def);
print("\"");
print(getStringLiteralQuote());
}
if (getCurrentPosition() == pos) {
if (!getScope().enumScope) {
@ -1805,7 +1805,10 @@ public class Java2TypeScriptTranslator extends AbstractTreePrinter {
if (getScope().enumScope) {
print(", ");
} else {
print(";").println().println();
if (getLastPrintedChar() != '}') {
print(";");
}
println().println();
}
} else {
println().println();
@ -1928,10 +1931,10 @@ public class Java2TypeScriptTranslator extends AbstractTreePrinter {
removeLastChar();
if (getScope().enumWrapperClassScope && !getScope(1).anonymousClasses.contains(classdecl)) {
printIndent().print("public name() : string { return this." + ENUM_WRAPPER_CLASS_NAME + "; }").println();
printIndent().print("public ordinal() : number { return this." + ENUM_WRAPPER_CLASS_ORDINAL + "; }")
printIndent().print("public name(): string { return this." + ENUM_WRAPPER_CLASS_NAME + "; }").println();
printIndent().print("public ordinal(): number { return this." + ENUM_WRAPPER_CLASS_ORDINAL + "; }")
.println();
printIndent().print("public compareTo(other : any) : number { return this." + ENUM_WRAPPER_CLASS_ORDINAL
printIndent().print("public compareTo(other: any): number { return this." + ENUM_WRAPPER_CLASS_ORDINAL
+ " - (isNaN(other)?other." + ENUM_WRAPPER_CLASS_ORDINAL + ":other); }").println();
}
@ -2305,7 +2308,7 @@ public class Java2TypeScriptTranslator extends AbstractTreePrinter {
printMethodModifiers(methodDecl, parent, getScope().constructor, inOverload, overload);
print(getTSMethodName(methodDecl)).print("(");
printArgList(null, methodDecl.params);
print(") : ");
print("): ");
substituteAndPrintType(methodDecl.getReturnType());
print(" {").println();
startIndent().printIndent();
@ -2687,7 +2690,7 @@ public class Java2TypeScriptTranslator extends AbstractTreePrinter {
protected void printMethodReturnDeclaration(JCMethodDecl methodDecl, boolean inCoreWrongOverload) {
if (methodDecl.restype != null && methodDecl.restype.type.getTag() != TypeTag.VOID) {
print(" : ");
print(": ");
boolean promisify = isAsyncMethod(methodDecl)
&& !methodDecl.restype.type.tsym.getQualifiedName().toString().endsWith(".Promise");
@ -2756,8 +2759,8 @@ public class Java2TypeScriptTranslator extends AbstractTreePrinter {
paramPrinted = true;
}
if (scope.isConstructor() && scope.isEnumWrapperClassScope()) {
print((isAnonymousClass() ? "" : "protected ") + ENUM_WRAPPER_CLASS_ORDINAL + " : number, ");
print((isAnonymousClass() ? "" : "protected ") + ENUM_WRAPPER_CLASS_NAME + " : string, ");
print((isAnonymousClass() ? "" : "protected ") + ENUM_WRAPPER_CLASS_ORDINAL + ": number, ");
print((isAnonymousClass() ? "" : "protected ") + ENUM_WRAPPER_CLASS_NAME + ": string, ");
paramPrinted = true;
}
// boolean forceVarargs = overload.forceVarargs() ||
@ -2935,7 +2938,7 @@ public class Java2TypeScriptTranslator extends AbstractTreePrinter {
continue;
} else {
printIndent().print(VAR_DECL_KEYWORD + " ")
.print(avoidJSKeyword(method.getParameters().get(j).name.toString())).print(" : ")
.print(avoidJSKeyword(method.getParameters().get(j).name.toString())).print(": ")
.print("any").print(Util.isVarargs(method.getParameters().get(j)) ? "[]" : "").print(" = ")
.print("__args[" + j + "]").print(";").println();
}
@ -2946,7 +2949,7 @@ public class Java2TypeScriptTranslator extends AbstractTreePrinter {
getScope().inlinedConstructorArgs = method.getParameters().stream().map(p -> p.sym.name.toString())
.collect(Collectors.toList());
printIndent().print(VAR_DECL_KEYWORD + " ")
.print(avoidJSKeyword(method.getParameters().get(j).name.toString())).print(" : ")
.print(avoidJSKeyword(method.getParameters().get(j).name.toString())).print(": ")
.print("any").print(Util.isVarargs(method.getParameters().get(j)) ? "[]" : "").print(" = ")
.print(args.get(j)).print(";").println();
getScope().inlinedConstructorArgs = null;
@ -3499,7 +3502,7 @@ public class Java2TypeScriptTranslator extends AbstractTreePrinter {
}
if (!getScope().skipTypeAnnotations && !getScope().enumWrapperClassScope) {
if (typeChecker.checkType(varDecl, varDecl.name, varDecl.vartype)) {
print(" : ");
print(": ");
if (confictInDefinitionScope) {
print("any");
} else {
@ -3539,7 +3542,7 @@ public class Java2TypeScriptTranslator extends AbstractTreePrinter {
} else {
print("public static ");
}
print(name).print(STATIC_INITIALIZATION_SUFFIX + "() : ");
print(name).print(STATIC_INITIALIZATION_SUFFIX + "(): ");
substituteAndPrintType(varDecl.vartype);
print(" { ");
int liCount = context.getStaticInitializerCount(clazz.sym);
@ -3562,7 +3565,7 @@ public class Java2TypeScriptTranslator extends AbstractTreePrinter {
// }
print("; }");
}
print("return ").print(prefix).print(name).print("; }");
print(" return ").print(prefix).print(name).print("; }");
if (!globals && context.bundleMode) {
String qualifiedClassName = getQualifiedTypeName(clazz.sym, globals, true);
context.addTopFooterStatement((isBlank(qualifiedClassName) ? "" : qualifiedClassName + ".") + name
@ -5207,13 +5210,13 @@ public class Java2TypeScriptTranslator extends AbstractTreePrinter {
@Override
public void visitConditional(JCConditional conditional) {
print(conditional.cond);
print("?");
print(" ? ");
if (!substituteAssignedExpression(
rootConditionalAssignedTypes.isEmpty() ? null : rootConditionalAssignedTypes.peek(),
conditional.truepart)) {
print(conditional.truepart);
}
print(":");
print(" : ");
if (!substituteAssignedExpression(
rootConditionalAssignedTypes.isEmpty() ? null : rootConditionalAssignedTypes.peek(),
conditional.falsepart)) {
@ -5924,7 +5927,7 @@ public class Java2TypeScriptTranslator extends AbstractTreePrinter {
if (checkFirstArrayElement)
print("[0]");
print(".constructor[" + getStringLiteralQuote() + INTERFACES_FIELD_NAME + getStringLiteralQuote()
+ "].indexOf(\"").print(type.tsym.getQualifiedName().toString()).print("\") >= 0");
+ "].indexOf(" + getStringLiteralQuote()).print(type.tsym.getQualifiedName().toString()).print(getStringLiteralQuote() + ") >= 0");
if (CharSequence.class.getName().equals(type.tsym.getQualifiedName().toString())) {
print(" || typeof ");
print(exprStr, expr);
@ -6108,7 +6111,7 @@ public class Java2TypeScriptTranslator extends AbstractTreePrinter {
if (assignedType.tsym.isInterface() && !context.isFunctionalType(assignedType.tsym)) {
JCLambda lambda = (JCLambda) expression;
MethodSymbol method = (MethodSymbol) assignedType.tsym.getEnclosedElements().get(0);
print("{ " + method.getSimpleName() + " : ").print(lambda).print(" }");
print("{ " + method.getSimpleName() + ": ").print(lambda).print(" }");
return true;
}
} else if (expression instanceof JCNewClass) {

View File

@ -355,12 +355,12 @@ public class Java2TypeScriptAdapter extends PrinterAdapter {
? fieldAccess.selected.type.tsym.toString() : null;
if (typeName != null && "isInstance".equals(methName) && Class.class.getName().equals(typeName)) {
printMacroName(fieldAccess.toString());
print("((c:any,o:any) => { if(typeof c === 'string') return (o.constructor && o.constructor")
print("((c:any,o:any) => { if (typeof c === 'string') return (o.constructor && o.constructor")
.print("[\"" + Java2TypeScriptTranslator.INTERFACES_FIELD_NAME + "\"] && o.constructor")
.print("[\"" + Java2TypeScriptTranslator.INTERFACES_FIELD_NAME + "\"].indexOf(c) >= 0) || (o")
.print("[\"" + Java2TypeScriptTranslator.INTERFACES_FIELD_NAME + "\"] && o")
.print("[\"" + Java2TypeScriptTranslator.INTERFACES_FIELD_NAME
+ "\"].indexOf(c) >= 0); else if(typeof c === 'function') return (o instanceof c) || (o.constructor && o.constructor === c); })(");
+ "\"].indexOf(c) >= 0); else if (typeof c === 'function') return (o instanceof c) || (o.constructor && o.constructor === c); })(");
getPrinter().print(fieldAccess.selected).print(",").print(invocation.args.head).print(")");
return true;
}
@ -462,7 +462,7 @@ public class Java2TypeScriptAdapter extends PrinterAdapter {
printMacroName("Enum." + targetMethodName);
print("function() { " + VAR_DECL_KEYWORD + " result: number[] = []; for(" + VAR_DECL_KEYWORD
+ " val in ").print(relTarget).print(
") { if(!isNaN(<any>val)) { result.push(parseInt(val,10)); } } return result; }()");
") { if (!isNaN(<any>val)) { result.push(parseInt(val,10)); } } return result; }()");
return true;
case "valueOf":
printMacroName("Enum." + targetMethodName);
@ -520,7 +520,7 @@ public class Java2TypeScriptAdapter extends PrinterAdapter {
printMacroName("Enum." + targetMethodName);
print("function() { " + VAR_DECL_KEYWORD + " result: number[] = []; for(" + VAR_DECL_KEYWORD
+ " val in ").print(relTarget).print(
") { if(!isNaN(<any>val)) { result.push(parseInt(val,10)); } } return result; }()");
") { if (!isNaN(<any>val)) { result.push(parseInt(val,10)); } } return result; }()");
return true;
}
}
@ -1010,7 +1010,7 @@ public class Java2TypeScriptAdapter extends PrinterAdapter {
return true;
case "equalsIgnoreCase":
printMacroName(targetMethodName);
print("((o1, o2) => o1.toUpperCase() === (o2===null?o2:o2.toUpperCase()))(")
print("((o1, o2) => o1.toUpperCase() === (o2===null ? o2 : o2.toUpperCase()))(")
.print(invocationElement.getTargetExpression()).print(", ")
.printArgList(invocationElement.getArguments()).print(")");
return true;
@ -1198,7 +1198,7 @@ public class Java2TypeScriptAdapter extends PrinterAdapter {
return true;
case "rint":
printMacroName(targetMethodName);
print("(d => { if (d === Number.NaN) { return d; } else if (Number.POSITIVE_INFINITY === d || Number.NEGATIVE_INFINITY === d) { return d; } else if(d == 0) { return d; } else { return Math.round(d); } })(")
print("(d => { if (d === Number.NaN) { return d; } else if (Number.POSITIVE_INFINITY === d || Number.NEGATIVE_INFINITY === d) { return d; } else if (d == 0) { return d; } else { return Math.round(d); } })(")
.printArgList(invocationElement.getArguments()).print(")");
return true;
case "scalb":
@ -1250,17 +1250,17 @@ public class Java2TypeScriptAdapter extends PrinterAdapter {
switch (targetMethodName) {
case "getName":
printMacroName(targetMethodName);
getPrinter().print("(c => typeof c === 'string'?c:c[\"" + Java2TypeScriptTranslator.CLASS_NAME_IN_CONSTRUCTOR + "\"]?c[\""
+ Java2TypeScriptTranslator.CLASS_NAME_IN_CONSTRUCTOR + "\"]:c[\"name\"])(");
getPrinter().print("(c => typeof c === 'string' ? c : c[\"" + Java2TypeScriptTranslator.CLASS_NAME_IN_CONSTRUCTOR + "\"] ? c[\""
+ Java2TypeScriptTranslator.CLASS_NAME_IN_CONSTRUCTOR + "\"] : c[\"name\"])(");
printTarget(invocationElement.getTargetExpression());
print(")");
return true;
case "getSimpleName":
printMacroName(targetMethodName);
print("(c => typeof c === 'string'?(<any>c).substring((<any>c).lastIndexOf('.')+1):c[\"" + Java2TypeScriptTranslator.CLASS_NAME_IN_CONSTRUCTOR + "\"]?c[\""
print("(c => typeof c === 'string' ? (<any>c).substring((<any>c).lastIndexOf('.') + 1) : c[\"" + Java2TypeScriptTranslator.CLASS_NAME_IN_CONSTRUCTOR + "\"]?c[\""
+ Java2TypeScriptTranslator.CLASS_NAME_IN_CONSTRUCTOR + "\"].substring(c[\""
+ Java2TypeScriptTranslator.CLASS_NAME_IN_CONSTRUCTOR
+ "\"].lastIndexOf('.')+1):c[\"name\"].substring(c[\"name\"].lastIndexOf('.')+1))(");
+ "\"].lastIndexOf('.') + 1) : c[\"name\"].substring(c[\"name\"].lastIndexOf('.') + 1))(");
printTarget(invocationElement.getTargetExpression());
print(")");
return true;
@ -1309,7 +1309,7 @@ public class Java2TypeScriptAdapter extends PrinterAdapter {
}
} else {
printMacroName(targetMethodName);
print("(<any>((o1: any, o2: any) => { if(o1 && o1.equals) { return o1.equals(o2); } else { return o1 === o2; } })(");
print("(<any>((o1: any, o2: any) => { if (o1 && o1.equals) { return o1.equals(o2); } else { return o1 === o2; } })(");
printTarget(invocationElement.getTargetExpression()).print(",")
.print(invocationElement.getArgument(0));
print("))");
@ -1319,7 +1319,7 @@ public class Java2TypeScriptAdapter extends PrinterAdapter {
if (invocationElement.getArgumentCount() == 1
&& invocationElement.getTargetExpression() != null) {
printMacroName(targetMethodName);
print("(<any>((o1: any, o2: any) => { if(o1 && o1.compareTo) { return o1.compareTo(o2); } else { return o1 < o2 ? -1 : o2 < o1 ? 1 : 0; } })(");
print("(<any>((o1: any, o2: any) => { if (o1 && o1.compareTo) { return o1.compareTo(o2); } else { return o1 < o2 ? -1 : o2 < o1 ? 1 : 0; } })(");
printTarget(invocationElement.getTargetExpression()).print(",")
.print(invocationElement.getArgument(0));
print("))");
@ -1340,7 +1340,7 @@ public class Java2TypeScriptAdapter extends PrinterAdapter {
case "hashCode":
if (invocationElement.getArgumentCount() == 0) {
printMacroName(targetMethodName);
print("(<any>((o: any) => { if(o.hashCode) { return o.hashCode(); } else { "
print("(<any>((o: any) => { if (o.hashCode) { return o.hashCode(); } else { "
+ "return o.toString().split('').reduce((prevHash, currVal) => (((prevHash << 5) - prevHash) + currVal.charCodeAt(0))|0, 0); }})(");
printTarget(invocationElement.getTargetExpression());
print("))");
@ -1358,7 +1358,7 @@ public class Java2TypeScriptAdapter extends PrinterAdapter {
|| invocationElement.getTargetExpression().getTypeAsElement().getKind() == ElementKind.INTERFACE
|| invocationElement.getTargetExpression().getType().getKind() == TypeKind.TYPEVAR) {
printMacroName(targetMethodName);
print("(<any>((o1: any, o2: any) => { if(o1 && o1.equals) { return o1.equals(o2); } else { return o1 === o2; } })(");
print("(<any>((o1: any, o2: any) => { if (o1 && o1.equals) { return o1.equals(o2); } else { return o1 === o2; } })(");
printTarget(invocationElement.getTargetExpression()).print(",")
.print(invocationElement.getArgument(0));
print("))");
@ -1375,12 +1375,12 @@ public class Java2TypeScriptAdapter extends PrinterAdapter {
JCClassDecl parent = getPrinter().getParent(JCClassDecl.class);
if (parent.sym.getSuperclass() != null
&& !context.symtab.objectType.equals(parent.sym.getSuperclass())) {
print("((o:any) => { if(super.clone!=undefined) { return super.clone(); } else { let clone = Object.create(o); for(let p in o) { if (o.hasOwnProperty(p)) clone[p] = o[p]; } return clone; } })(this)");
print("((o:any) => { if (super.clone!=undefined) { return super.clone(); } else { let clone = Object.create(o); for(let p in o) { if (o.hasOwnProperty(p)) clone[p] = o[p]; } return clone; } })(this)");
} else {
print("((o:any) => { let clone = Object.create(o); for(let p in o) { if (o.hasOwnProperty(p)) clone[p] = o[p]; } return clone; })(this)");
}
} else {
print("((o:any) => { if(o.clone!=undefined) { return (<any>o).clone(); } else { let clone = Object.create(o); for(let p in o) { if (o.hasOwnProperty(p)) clone[p] = o[p]; } return clone; } })(");
print("((o:any) => { if (o.clone!=undefined) { return (<any>o).clone(); } else { let clone = Object.create(o); for(let p in o) { if (o.hasOwnProperty(p)) clone[p] = o[p]; } return clone; } })(");
printTarget(invocationElement.getTargetExpression());
print(")");
}
@ -1397,18 +1397,18 @@ public class Java2TypeScriptAdapter extends PrinterAdapter {
protected void printFunctionalInvocation(ExtendedElement target, String functionName,
List<ExtendedElement> arguments) {
if (target instanceof IdentifierElement) {
print("(typeof ").print(target).print(" === 'function'?target").print("(").printArgList(arguments)
.print("):(<any>target).").print(functionName).print("(").printArgList(arguments).print("))");
print("(typeof ").print(target).print(" === 'function' ? target").print("(").printArgList(arguments)
.print(") : (<any>target).").print(functionName).print("(").printArgList(arguments).print("))");
} else {
print("(target => (typeof target === 'function')?target").print("(").printArgList(arguments)
.print("):(<any>target).").print(functionName).print("(").printArgList(arguments).print("))(")
print("(target => (typeof target === 'function') ? target").print("(").printArgList(arguments)
.print(") : (<any>target).").print(functionName).print("(").printArgList(arguments).print("))(")
.print(target).print(")");
}
}
protected void printFunctionalInvocation2(ExtendedElement target, String functionName,
List<ExtendedElement> arguments) {
print("((target => (target['" + functionName + "'] === undefined)?target:target['" + functionName + "'])(")
print("((target => (target['" + functionName + "'] === undefined) ? target : target['" + functionName + "'])(")
.print(target).print("))").print("(").printArgList(arguments).print(")");
}

View File

@ -276,8 +276,8 @@ public class ExtensionTests extends AbstractTest {
}, f);
String generatedCode = FileUtils.readFileToString(f.getTsFile());
Assert.assertTrue(generatedCode.contains("this is a header comment"));
Assert.assertTrue(generatedCode.contains("date : string"));
Assert.assertFalse(generatedCode.contains("date : Date"));
Assert.assertTrue(generatedCode.contains("date: string"));
Assert.assertFalse(generatedCode.contains("date: Date"));
}
@Test