This is an automated email from the ASF dual-hosted git repository.

radu pushed a commit to branch master
in repository 
https://gitbox.apache.org/repos/asf/sling-org-apache-sling-scripting-sightly-compiler-java.git


The following commit(s) were added to refs/heads/master by this push:
     new ba8e83a  SLING-9457 - Nested loops without an item identifier generate 
a Java compilation error
ba8e83a is described below

commit ba8e83a60d947614ff196a87784df49afe0411a9
Author: Radu Cotescu <[email protected]>
AuthorDate: Tue May 19 21:49:50 2020 +0200

    SLING-9457 - Nested loops without an item identifier generate a Java 
compilation error
    
    * brought back the method which was generating unique variable names for 
local scopes
    * added test for nested loop structures which use the same default item 
identifier
---
 .../java/compiler/impl/VariableAnalyzer.java       | 13 ++++++-
 .../java/JavaClassBackendCompilerTest.java         | 16 ++++++++
 src/test/resources/nested-lists.html               | 24 ++++++++++++
 src/test/resources/nested-lists.output.html        | 43 ++++++++++++++++++++++
 4 files changed, 95 insertions(+), 1 deletion(-)

diff --git 
a/src/main/java/org/apache/sling/scripting/sightly/java/compiler/impl/VariableAnalyzer.java
 
b/src/main/java/org/apache/sling/scripting/sightly/java/compiler/impl/VariableAnalyzer.java
index 35b9686..42497bc 100644
--- 
a/src/main/java/org/apache/sling/scripting/sightly/java/compiler/impl/VariableAnalyzer.java
+++ 
b/src/main/java/org/apache/sling/scripting/sightly/java/compiler/impl/VariableAnalyzer.java
@@ -47,8 +47,9 @@ public class VariableAnalyzer {
      */
     public VariableDescriptor declareVariable(String originalName, Type type) {
         originalName = originalName.toLowerCase();
+        String safeName = findSafeName(originalName);
         VariableDescriptor descriptor =
-                new VariableDescriptor(originalName, 
JavaEscapeHelper.getJavaIdentifier(originalName), type, VariableScope.SCOPED);
+                new VariableDescriptor(originalName, 
JavaEscapeHelper.getJavaIdentifier(safeName), type, VariableScope.SCOPED);
         tracker.pushVariable(originalName, descriptor);
         variables.add(descriptor);
         return descriptor;
@@ -153,4 +154,14 @@ public class VariableAnalyzer {
         }
         return name.toLowerCase();
     }
+
+    private String findSafeName(String original) {
+        int occurrenceCount = tracker.getOccurrenceCount(original);
+        String syntaxSafe = JavaEscapeHelper.getJavaIdentifier(original);
+        if (occurrenceCount == 0) {
+            return syntaxSafe; //no other declarations in scope. Use this very 
name
+        } else {
+            return original + "_" + occurrenceCount;
+        }
+    }
 }
diff --git 
a/src/test/java/org/apache/sling/scripting/sightly/compiler/java/JavaClassBackendCompilerTest.java
 
b/src/test/java/org/apache/sling/scripting/sightly/compiler/java/JavaClassBackendCompilerTest.java
index 9437054..da22be4 100644
--- 
a/src/test/java/org/apache/sling/scripting/sightly/compiler/java/JavaClassBackendCompilerTest.java
+++ 
b/src/test/java/org/apache/sling/scripting/sightly/compiler/java/JavaClassBackendCompilerTest.java
@@ -25,6 +25,7 @@ import javax.script.SimpleBindings;
 
 import org.apache.commons.io.IOUtils;
 import org.apache.commons.lang3.StringUtils;
+import org.apache.sling.scripting.sightly.compiler.CompilationResult;
 import org.apache.sling.scripting.sightly.compiler.CompilationUnit;
 import org.apache.sling.scripting.sightly.compiler.SightlyCompiler;
 import 
org.apache.sling.scripting.sightly.compiler.java.utils.CharSequenceJavaCompiler;
@@ -129,6 +130,21 @@ public class JavaClassBackendCompilerTest {
         assertEquals(expectedOutput, writer.toString());
     }
 
+    @Test
+    public void testNestedLists() throws Exception {
+        CompilationUnit compilationUnit = 
TestUtils.readScriptFromClasspath("/nested-lists.html");
+        JavaClassBackendCompiler backendCompiler = new 
JavaClassBackendCompiler();
+        SightlyCompiler sightlyCompiler = new SightlyCompiler();
+        sightlyCompiler.compile(compilationUnit, backendCompiler);
+        ClassInfo classInfo = buildClassInfo("nested_lists");
+        String source = backendCompiler.build(classInfo);
+        StringWriter writer = new StringWriter();
+        RenderContext renderContext = buildRenderContext(new SimpleBindings());
+        render(writer, classInfo, source, renderContext, new SimpleBindings());
+        String expectedOutput = 
IOUtils.toString(this.getClass().getResourceAsStream("/nested-lists.output.html"),
 "UTF-8");
+        assertEquals(expectedOutput, writer.toString());
+    }
+
     private static final String normalizeLineEndings(String input) {
         return StringUtils.replaceAll(input, "\r\n", "\n");
     }
diff --git a/src/test/resources/nested-lists.html 
b/src/test/resources/nested-lists.html
new file mode 100644
index 0000000..7eb3f63
--- /dev/null
+++ b/src/test/resources/nested-lists.html
@@ -0,0 +1,24 @@
+<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+  ~ Licensed to the Apache Software Foundation (ASF) under one
+  ~ or more contributor license agreements.  See the NOTICE file
+  ~ distributed with this work for additional information
+  ~ regarding copyright ownership.  The ASF licenses this file
+  ~ to you under the Apache License, Version 2.0 (the
+  ~ "License"); you may not use this file except in compliance
+  ~ with the License.  You may obtain a copy of the License at
+  ~
+  ~   http://www.apache.org/licenses/LICENSE-2.0
+  ~
+  ~ Unless required by applicable law or agreed to in writing,
+  ~ software distributed under the License is distributed on an
+  ~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+  ~ KIND, either express or implied.  See the License for the
+  ~ specific language governing permissions and limitations
+  ~ under the License.
+  
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
+<ol data-sly-list="${[1, 2, 3]}">
+    <li>
+        ${item}
+        <span data-sly-repeat="${['a', 'b', 'c']}">${item}</span>
+    </li>
+</ol>
diff --git a/src/test/resources/nested-lists.output.html 
b/src/test/resources/nested-lists.output.html
new file mode 100644
index 0000000..222b7a2
--- /dev/null
+++ b/src/test/resources/nested-lists.output.html
@@ -0,0 +1,43 @@
+<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+  ~ Licensed to the Apache Software Foundation (ASF) under one
+  ~ or more contributor license agreements.  See the NOTICE file
+  ~ distributed with this work for additional information
+  ~ regarding copyright ownership.  The ASF licenses this file
+  ~ to you under the Apache License, Version 2.0 (the
+  ~ "License"); you may not use this file except in compliance
+  ~ with the License.  You may obtain a copy of the License at
+  ~
+  ~   http://www.apache.org/licenses/LICENSE-2.0
+  ~
+  ~ Unless required by applicable law or agreed to in writing,
+  ~ software distributed under the License is distributed on an
+  ~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+  ~ KIND, either express or implied.  See the License for the
+  ~ specific language governing permissions and limitations
+  ~ under the License.
+  
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
+<ol>
+    <li>
+        1
+        <span>a</span>
+<span>b</span>
+<span>c</span>
+
+    </li>
+
+    <li>
+        2
+        <span>a</span>
+<span>b</span>
+<span>c</span>
+
+    </li>
+
+    <li>
+        3
+        <span>a</span>
+<span>b</span>
+<span>c</span>
+
+    </li>
+</ol>

Reply via email to