This is an automated email from the ASF dual-hosted git repository.
thiagohp pushed a commit to branch feature/es-module-support
in repository https://gitbox.apache.org/repos/asf/tapestry-5.git
The following commit(s) were added to refs/heads/feature/es-module-support by
this push:
new cd121bc5e TAP5-2803: supporting Number and Boolean in ES module's
.with()
cd121bc5e is described below
commit cd121bc5ecc276b0b3725806c3ad06c0a616c8ce
Author: Thiago H. de Paula Figueiredo <[email protected]>
AuthorDate: Wed Apr 2 18:06:55 2025 -0300
TAP5-2803: supporting Number and Boolean in ES module's .with()
plus tests to verify all values were passed correctly
---
.../services/javascript/EsModuleManagerImpl.java | 16 ++++++++++------
.../services/javascript/EsModuleInitialization.java | 4 +---
.../apache/tapestry5/integration/app1/EsModuleTests.java | 12 ++++++++++++
.../tapestry5/integration/app1/pages/EsModuleDemo.java | 7 ++++++-
.../services/javascript/EsModuleManagerImplTest.java | 8 ++++++--
.../assets/es-modules/parameter-type-default-export.js | 14 ++++++++++++++
.../tapestry5/integration/app1/pages/EsModuleDemo.tml | 1 +
7 files changed, 50 insertions(+), 12 deletions(-)
diff --git
a/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/javascript/EsModuleManagerImpl.java
b/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/javascript/EsModuleManagerImpl.java
index a36385c33..9f9bf5aea 100644
---
a/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/javascript/EsModuleManagerImpl.java
+++
b/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/javascript/EsModuleManagerImpl.java
@@ -31,7 +31,6 @@ import org.apache.tapestry5.ioc.annotations.PostInjection;
import org.apache.tapestry5.ioc.annotations.Symbol;
import org.apache.tapestry5.ioc.services.ClasspathMatcher;
import org.apache.tapestry5.ioc.services.ClasspathScanner;
-import org.apache.tapestry5.json.JSONArray;
import org.apache.tapestry5.json.JSONCollection;
import org.apache.tapestry5.json.JSONLiteral;
import org.apache.tapestry5.json.JSONObject;
@@ -45,6 +44,8 @@ import
org.apache.tapestry5.services.javascript.ImportPlacement;
public class EsModuleManagerImpl implements EsModuleManager
{
+ private static final String GENERIC_IMPORTED_VARIABLE = "m";
+
/**
* Name of the JSON object property containing the imports in an import
map.
*/
@@ -242,15 +243,14 @@ public class EsModuleManagerImpl implements
EsModuleManager
"import %s from '%s';\n"
+ "%s(%s);";
- final String importName = functionName != null ? functionName
: "m";
+ final String importName = functionName != null ? functionName
: GENERIC_IMPORTED_VARIABLE;
final String importDeclaration = functionName != null ?
"{ " + functionName + " }":
- "m";
+ GENERIC_IMPORTED_VARIABLE;
- final String code = String.format(moduleFunctionCallFormat,
+
moduleFunctionCall.text(String.format(moduleFunctionCallFormat,
importDeclaration, moduleId, importName,
- convertToJsFunctionParameters(arguments, compactJSON));
- moduleFunctionCall.text(code);
+ convertToJsFunctionParameters(arguments,
compactJSON)));
writeAttributes(moduleFunctionCall, init);
@@ -305,6 +305,10 @@ public class EsModuleManagerImpl implements EsModuleManager
{
result = "'" + object.toString() + "'";
}
+ else if (object instanceof Number || object instanceof Boolean)
+ {
+ result = object.toString();
+ }
else if (object instanceof JSONCollection)
{
result = ((JSONCollection) object).toString(compactJSON);
diff --git
a/tapestry-core/src/main/java/org/apache/tapestry5/services/javascript/EsModuleInitialization.java
b/tapestry-core/src/main/java/org/apache/tapestry5/services/javascript/EsModuleInitialization.java
index 6647f2e21..f56479398 100644
---
a/tapestry-core/src/main/java/org/apache/tapestry5/services/javascript/EsModuleInitialization.java
+++
b/tapestry-core/src/main/java/org/apache/tapestry5/services/javascript/EsModuleInitialization.java
@@ -84,9 +84,7 @@ public interface EsModuleInitialization extends
AbstractInitialization<EsModuleI
/**
* Specifies the arguments to be passed to the function. Often, just a
single {@link org.apache.tapestry5.json.JSONObject}
- * is passed. When multiple Initializations exist with the same function
name (or no function name), and no arguments,
- * they are coalesced into a single EsModuleInitialization: it is assumed
that an initialization with no parameters needs to
- * only be invoked once.
+ * is passed.
*
* @param arguments
* any number of values. Each value may be one of: null, String,
Boolean, Number,
diff --git
a/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/EsModuleTests.java
b/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/EsModuleTests.java
index 32e5311de..786d9ce0b 100644
---
a/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/EsModuleTests.java
+++
b/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/EsModuleTests.java
@@ -178,6 +178,18 @@ public class EsModuleTests extends App1TestCase
"Parameterless default export!");
}
+ /**
+ * Tests using whether parameter types are correctly passed to JS.
+ */
+ @Test
+ public void parameter_types() throws InterruptedException
+ {
+ openLinks(PAGE_NAME);
+ assertEquals(
+ getText("parameter-type-default-export-message"),
+ "Parameter types passed correctly!");
+ }
+
private void assertModulesDefinedByGlobalCallbacks(JSONObject importMap) {
assertModuleUrl(NON_OVERRIDDEN_ES_MODULE_ID,
NON_OVERRIDDEN_ES_MODULE_URL, importMap);
assertModuleUrl(OVERRIDDEN_ES_MODULE_ID, OVERRIDDEN_ES_MODULE_NEW_URL,
importMap);
diff --git
a/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/EsModuleDemo.java
b/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/EsModuleDemo.java
index bc69029e2..49c38710d 100644
---
a/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/EsModuleDemo.java
+++
b/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/EsModuleDemo.java
@@ -15,9 +15,10 @@ package org.apache.tapestry5.integration.app1.pages;
import org.apache.tapestry5.annotations.Import;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SetupRender;
-import org.apache.tapestry5.integration.app1.EsModuleTests;
import org.apache.tapestry5.integration.app1.services.AppModule;
import org.apache.tapestry5.ioc.annotations.Inject;
+import org.apache.tapestry5.json.JSONArray;
+import org.apache.tapestry5.json.JSONObject;
import org.apache.tapestry5.services.javascript.EsModuleConfigurationCallback;
import org.apache.tapestry5.services.javascript.ImportPlacement;
import org.apache.tapestry5.services.javascript.JavaScriptSupport;
@@ -67,6 +68,10 @@ public class EsModuleDemo
javaScriptSupport.importEsModule("parameterless-default-export")
.with();
+ javaScriptSupport.importEsModule("parameter-type-default-export")
+ .with(null, true, false, Math.PI * Math.E, "string", "jsonLiteral",
+ new JSONObject("key", "value"), new JSONArray(1, "2"));
+
if (overrideEsModuleImportAgain != null && overrideEsModuleImportAgain)
{
javaScriptSupport.addEsModuleConfigurationCallback(
diff --git
a/tapestry-core/src/test/java/org/apache/tapestry5/internal/services/javascript/EsModuleManagerImplTest.java
b/tapestry-core/src/test/java/org/apache/tapestry5/internal/services/javascript/EsModuleManagerImplTest.java
index b357f9e41..0e41bc12e 100644
---
a/tapestry-core/src/test/java/org/apache/tapestry5/internal/services/javascript/EsModuleManagerImplTest.java
+++
b/tapestry-core/src/test/java/org/apache/tapestry5/internal/services/javascript/EsModuleManagerImplTest.java
@@ -27,7 +27,7 @@ public class EsModuleManagerImplTest
private static final JSONArray JSON_ARRAY = new JSONArray("1", "true");
private static JSONObject JSON_OBJECT = new JSONObject("something",
"else", "array",
JSON_ARRAY, "literal", JSON_LITERAL);
-
+ private static Number NUMBER = Math.PI * Math.E;
@Test
public void test_null_arguments()
@@ -49,6 +49,10 @@ public class EsModuleManagerImplTest
assertEquals(convert(new Object[] {null}, false), null);
assertEquals(convert(new Object[] {STRING}, false), quote(STRING));
+
+ assertEquals(convert(new Object[] {NUMBER}, false), NUMBER.toString());
+ assertEquals(convert(new Object[] {Boolean.TRUE}, false),
Boolean.TRUE.toString());
+ assertEquals(convert(new Object[] {Boolean.FALSE}, false),
Boolean.FALSE.toString());
assertEquals(convert(new Object[] {JSON_LITERAL}, false),
quote(JSON_LITERAL.toString()));
@@ -57,7 +61,7 @@ public class EsModuleManagerImplTest
assertEquals(convert(new Object[] {JSON_OBJECT}, false),
JSON_OBJECT.toString(false));
assertEquals(convert(new Object[] {JSON_OBJECT}, true),
JSON_OBJECT.toString(true));
-
+
}
@Test
diff --git
a/tapestry-core/src/test/resources/META-INF/assets/es-modules/parameter-type-default-export.js
b/tapestry-core/src/test/resources/META-INF/assets/es-modules/parameter-type-default-export.js
new file mode 100644
index 000000000..e4c3272e6
--- /dev/null
+++
b/tapestry-core/src/test/resources/META-INF/assets/es-modules/parameter-type-default-export.js
@@ -0,0 +1,14 @@
+export default function(nullValue, trueValue, falseValue, piTimesE,
stringValue, jsonLiteralValue,
+ objectValue, arrayValue) {
+
+ if (nullValue === null && (typeof trueValue === "boolean") && trueValue
=== true &&
+ (typeof falseValue === "boolean") && falseValue === false &&
+ (typeof piTimesE === "number") && piTimesE === Math.PI * Math.E
&&
+ (typeof jsonLiteralValue === "string") && jsonLiteralValue ===
"jsonLiteral" &&
+ (typeof objectValue === "object") && objectValue.key ===
"value" &&
+ arrayValue.constructor === Array && arrayValue[0] === 1 &&
arrayValue[1] === "2") {
+
+
document.getElementById("parameter-type-default-export-message").innerHTML =
"Parameter types passed correctly!";
+
+ }
+}
\ No newline at end of file
diff --git
a/tapestry-core/src/test/resources/org/apache/tapestry5/integration/app1/pages/EsModuleDemo.tml
b/tapestry-core/src/test/resources/org/apache/tapestry5/integration/app1/pages/EsModuleDemo.tml
index df08c79ed..702834fb8 100644
---
a/tapestry-core/src/test/resources/org/apache/tapestry5/integration/app1/pages/EsModuleDemo.tml
+++
b/tapestry-core/src/test/resources/org/apache/tapestry5/integration/app1/pages/EsModuleDemo.tml
@@ -22,6 +22,7 @@
<p id="${DEFAULT_EXPORT_MESSAGE}"/>
<p id="non-default-export-message"/>
<p id="parameterless-default-export-message"/>
+ <p id="parameter-type-default-export-message"/>
<p>
Import map: