From dc444a73962433f72ea7b3a60d20b981a01dbae0 Mon Sep 17 00:00:00 2001 From: Renaud Pawlak Date: Fri, 12 Feb 2021 17:09:22 +0100 Subject: [PATCH] add a method to substitute some new array expression --- .../jsweet/transpiler/Java2TypeScriptTranslator.java | 7 +++++-- .../jsweet/transpiler/extension/PrinterAdapter.java | 12 ++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/transpiler/src/main/java/org/jsweet/transpiler/Java2TypeScriptTranslator.java b/transpiler/src/main/java/org/jsweet/transpiler/Java2TypeScriptTranslator.java index 5444a444..d89cbebe 100644 --- a/transpiler/src/main/java/org/jsweet/transpiler/Java2TypeScriptTranslator.java +++ b/transpiler/src/main/java/org/jsweet/transpiler/Java2TypeScriptTranslator.java @@ -81,6 +81,7 @@ import org.jsweet.transpiler.model.support.ExtendedElementSupport; import org.jsweet.transpiler.model.support.ForeachLoopElementSupport; import org.jsweet.transpiler.model.support.ImportElementSupport; import org.jsweet.transpiler.model.support.MethodInvocationElementSupport; +import org.jsweet.transpiler.model.support.NewArrayElementSupport; import org.jsweet.transpiler.model.support.NewClassElementSupport; import org.jsweet.transpiler.model.support.UnaryOperatorElementSupport; import org.jsweet.transpiler.util.AbstractTreePrinter; @@ -5563,8 +5564,10 @@ public class Java2TypeScriptTranslator extends AbstractTreePrinter { } print("]"); } else { - print("(s => { let a=[]; while(s-->0) a.push(" + Util.getTypeInitialValue(newArray.elemtype.type) - + "); return a; })(").print(newArray.dims.head).print(")"); + if (!getAdapter().substituteNewArrayWithVariableLength(new NewArrayElementSupport(newArray))) { + print("(s => { let a=[]; while(s-->0) a.push(" + Util.getTypeInitialValue(newArray.elemtype.type) + + "); return a; })(").print(newArray.dims.head).print(")"); + } } } else { print(" (function(dims) { " + VAR_DECL_KEYWORD diff --git a/transpiler/src/main/java/org/jsweet/transpiler/extension/PrinterAdapter.java b/transpiler/src/main/java/org/jsweet/transpiler/extension/PrinterAdapter.java index bd3acc96..a84b9213 100644 --- a/transpiler/src/main/java/org/jsweet/transpiler/extension/PrinterAdapter.java +++ b/transpiler/src/main/java/org/jsweet/transpiler/extension/PrinterAdapter.java @@ -57,6 +57,7 @@ import org.jsweet.transpiler.model.ForeachLoopElement; import org.jsweet.transpiler.model.IdentifierElement; import org.jsweet.transpiler.model.ImportElement; import org.jsweet.transpiler.model.MethodInvocationElement; +import org.jsweet.transpiler.model.NewArrayElement; import org.jsweet.transpiler.model.NewClassElement; import org.jsweet.transpiler.model.TypeCastElement; import org.jsweet.transpiler.model.UnaryOperatorElement; @@ -901,6 +902,17 @@ public class PrinterAdapter { return parentAdapter == null ? false : parentAdapter.substituteMethodInvocation(invocation); } + /** + * Substitutes new array expressions, which length are initialized with a variable. + * + * @param newArray + * the new array being printed + * @return true if substituted + */ + public boolean substituteNewArrayWithVariableLength(NewArrayElement newArray) { + return parentAdapter == null ? false : parentAdapter.substituteNewArrayWithVariableLength(newArray); + } + /** * Substitutes a given type declaration. *