You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ro...@apache.org on 2017/11/07 10:10:28 UTC

[sling-org-apache-sling-scripting-sightly-compiler] 08/31: SLING-6230 - The SightlyCompiler reports errors and warnings with an offset by one for the line count

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

rombert pushed a commit to annotated tag org.apache.sling.scripting.sightly.compiler-1.0.10
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-scripting-sightly-compiler.git

commit 19c228c7d14351aad5ccd42310189b103bbfb678
Author: Radu Cotescu <ra...@apache.org>
AuthorDate: Wed Nov 2 14:01:35 2016 +0000

    SLING-6230 - The SightlyCompiler reports errors and warnings with an offset by one for the line count
    
    * corrected error and warning reporting in the compiler
    * updated the htl-maven-plugin to use the new compiler version
    * added tests
    
    git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/bundles/scripting/sightly/compiler@1767685 13f79535-47bb-0310-9956-ffa450edef68
---
 .../scripting/sightly/compiler/SightlyCompiler.java  | 20 +++++++-------------
 .../sightly/impl/compiler/SightlyCompilerTest.java   | 13 +++++++++++++
 src/test/resources/error-1.html                      | 19 +++++++++++++++++++
 3 files changed, 39 insertions(+), 13 deletions(-)

diff --git a/src/main/java/org/apache/sling/scripting/sightly/compiler/SightlyCompiler.java b/src/main/java/org/apache/sling/scripting/sightly/compiler/SightlyCompiler.java
index 4bab4b2..008c89a 100644
--- a/src/main/java/org/apache/sling/scripting/sightly/compiler/SightlyCompiler.java
+++ b/src/main/java/org/apache/sling/scripting/sightly/compiler/SightlyCompiler.java
@@ -146,7 +146,7 @@ public final class SightlyCompiler {
             }
             frontend.compile(stream, scriptSource);
             for (PushStream.StreamMessage w : stream.getWarnings()) {
-                ScriptError warning = getScriptError(scriptSource, w.getCode(), 0, 0, w.getMessage());
+                ScriptError warning = getScriptError(scriptSource, w.getCode(), 1, 0, w.getMessage());
                 compilationResult.getWarnings().add(new CompilerMessageImpl(scriptName, warning.errorMessage, warning.lineNumber, warning
                         .column));
             }
@@ -161,26 +161,20 @@ public final class SightlyCompiler {
         return compilationResult;
     }
 
-    private ScriptError getScriptError(String documentFragment, String offendingInput, int lineOffset, int column, String message) {
+    private ScriptError getScriptError(String documentFragment, String offendingInput, int lineOffset, int columnOffset, String message) {
         if (StringUtils.isNotEmpty(offendingInput)) {
             int offendingInputIndex = documentFragment.indexOf(offendingInput);
             if (offendingInputIndex > -1) {
                 String textBeforeError = documentFragment.substring(0, offendingInputIndex);
-                int line = 1;
-                int newLine = 0;
-                while (textBeforeError.length() > 0 && newLine != -1) {
-                    newLine = textBeforeError.indexOf(System.lineSeparator());
-                    if (newLine != -1) {
-                        line++;
-                        textBeforeError = textBeforeError.substring(newLine + 1, textBeforeError.length());
-                    }
+                int line = lineOffset + textBeforeError.length() - textBeforeError.replaceAll(System.lineSeparator(), "").length();
+                int column = textBeforeError.substring(textBeforeError.lastIndexOf(System.lineSeparator())).length();
+                if (column != columnOffset) {
+                    column +=columnOffset;
                 }
-                line = line + lineOffset;
-                column = textBeforeError.length() + column + 1;
                 return new ScriptError(line, column, offendingInput + ": " + message);
             }
         }
-        return new ScriptError(lineOffset, column, message);
+        return new ScriptError(lineOffset, columnOffset, message);
     }
 
     private static class ScriptError {
diff --git a/src/test/java/org/apache/sling/scripting/sightly/impl/compiler/SightlyCompilerTest.java b/src/test/java/org/apache/sling/scripting/sightly/impl/compiler/SightlyCompilerTest.java
index f9921fc..4af5713 100644
--- a/src/test/java/org/apache/sling/scripting/sightly/impl/compiler/SightlyCompilerTest.java
+++ b/src/test/java/org/apache/sling/scripting/sightly/impl/compiler/SightlyCompilerTest.java
@@ -47,6 +47,7 @@ public class SightlyCompilerTest {
         List<CompilerMessage> warnings = result.getWarnings();
         assertTrue("Expected compilation warnings.", warnings.size() == 1);
         CompilerMessage warningMessage = warnings.get(0);
+        assertEquals("Expected warning on a different line.", 18, warningMessage.getLine());
         assertTrue(script.equals(warningMessage.getScriptName()));
         assertEquals("${some.value}: Element script requires that all expressions have an explicit context specified. The expression will" +
                 " be replaced with an empty string.", warningMessage.getMessage());
@@ -61,6 +62,8 @@ public class SightlyCompilerTest {
         CompilerMessage _1stWarning = warnings.get(0);
         CompilerMessage _2ndWarning = warnings.get(1);
         assertEquals(script, _1stWarning.getScriptName());
+        assertEquals("Expected warning on a different line.", 17, _1stWarning.getLine());
+        assertEquals("Expected warning on a different line.", 18, _2ndWarning.getLine());
         assertEquals(script, _2ndWarning.getScriptName());
         assertEquals("${style.string}: Expressions within the value of attribute style need to have an explicit context option. The " +
                 "expression will be replaced with an empty string.", _1stWarning.getMessage());
@@ -69,6 +72,16 @@ public class SightlyCompilerTest {
 
     }
 
+    @Test
+    public void testErrorReporting1() {
+        String script = "/error-1.html";
+        CompilationResult result = compile(script);
+        List<CompilerMessage> errors = result.getErrors();
+        assertTrue("Expected compilation errors.", errors.size() == 1);
+        CompilerMessage error = errors.get(0);
+        assertEquals("Error is not reported at the expected line.", 18, error.getLine());
+    }
+
     private CompilationResult compile(String file) {
         CompilationUnit compilationUnit = TestUtils.readScriptFromClasspath(file);
         return compiler.compile(compilationUnit);
diff --git a/src/test/resources/error-1.html b/src/test/resources/error-1.html
new file mode 100644
index 0000000..5be6155
--- /dev/null
+++ b/src/test/resources/error-1.html
@@ -0,0 +1,19 @@
+<!--/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+  ~ 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.
+  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/-->
+<ul data-sly-list="${list}">
+    <li>${item</li>
+</ul>

-- 
To stop receiving notification emails like this one, please contact
"commits@sling.apache.org" <co...@sling.apache.org>.