Author: radu Date: Wed Nov 2 14:01:35 2016 New Revision: 1767685 URL: http://svn.apache.org/viewvc?rev=1767685&view=rev Log: 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 Added: sling/trunk/bundles/scripting/sightly/compiler/src/test/resources/error-1.html Modified: sling/trunk/bundles/scripting/sightly/compiler/src/main/java/org/apache/sling/scripting/sightly/compiler/SightlyCompiler.java sling/trunk/bundles/scripting/sightly/compiler/src/test/java/org/apache/sling/scripting/sightly/impl/compiler/SightlyCompilerTest.java sling/trunk/tooling/maven/htl-maven-plugin/pom.xml Modified: sling/trunk/bundles/scripting/sightly/compiler/src/main/java/org/apache/sling/scripting/sightly/compiler/SightlyCompiler.java URL: http://svn.apache.org/viewvc/sling/trunk/bundles/scripting/sightly/compiler/src/main/java/org/apache/sling/scripting/sightly/compiler/SightlyCompiler.java?rev=1767685&r1=1767684&r2=1767685&view=diff ============================================================================== --- sling/trunk/bundles/scripting/sightly/compiler/src/main/java/org/apache/sling/scripting/sightly/compiler/SightlyCompiler.java (original) +++ sling/trunk/bundles/scripting/sightly/compiler/src/main/java/org/apache/sling/scripting/sightly/compiler/SightlyCompiler.java Wed Nov 2 14:01:35 2016 @@ -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 { Modified: sling/trunk/bundles/scripting/sightly/compiler/src/test/java/org/apache/sling/scripting/sightly/impl/compiler/SightlyCompilerTest.java URL: http://svn.apache.org/viewvc/sling/trunk/bundles/scripting/sightly/compiler/src/test/java/org/apache/sling/scripting/sightly/impl/compiler/SightlyCompilerTest.java?rev=1767685&r1=1767684&r2=1767685&view=diff ============================================================================== --- sling/trunk/bundles/scripting/sightly/compiler/src/test/java/org/apache/sling/scripting/sightly/impl/compiler/SightlyCompilerTest.java (original) +++ sling/trunk/bundles/scripting/sightly/compiler/src/test/java/org/apache/sling/scripting/sightly/impl/compiler/SightlyCompilerTest.java Wed Nov 2 14:01:35 2016 @@ -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); Added: sling/trunk/bundles/scripting/sightly/compiler/src/test/resources/error-1.html URL: http://svn.apache.org/viewvc/sling/trunk/bundles/scripting/sightly/compiler/src/test/resources/error-1.html?rev=1767685&view=auto ============================================================================== --- sling/trunk/bundles/scripting/sightly/compiler/src/test/resources/error-1.html (added) +++ sling/trunk/bundles/scripting/sightly/compiler/src/test/resources/error-1.html Wed Nov 2 14:01:35 2016 @@ -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> Modified: sling/trunk/tooling/maven/htl-maven-plugin/pom.xml URL: http://svn.apache.org/viewvc/sling/trunk/tooling/maven/htl-maven-plugin/pom.xml?rev=1767685&r1=1767684&r2=1767685&view=diff ============================================================================== --- sling/trunk/tooling/maven/htl-maven-plugin/pom.xml (original) +++ sling/trunk/tooling/maven/htl-maven-plugin/pom.xml Wed Nov 2 14:01:35 2016 @@ -46,7 +46,7 @@ <dependency> <groupId>org.apache.sling</groupId> <artifactId>org.apache.sling.scripting.sightly.compiler</artifactId> - <version>1.0.0</version> + <version>1.0.3-SNAPSHOT</version> </dependency> <dependency> <groupId>commons-io</groupId>