Paul King created GROOVY-12703:
----------------------------------

             Summary: @ASTTest fails to compile its test closure when a later 
'}' follows the closure on the same line
                 Key: GROOVY-12703
                 URL: https://issues.apache.org/jira/browse/GROOVY-12703
             Project: Groovy
          Issue Type: Bug
          Components: Compiler
    Affects Versions: 6.0.0
            Reporter: Paul King


h3. Description

{{@ASTTest}} recompiles its test closure from the source text. When another 
{{}}} follows the closure on the closure's last line, that text is taken as 
part of the closure and compiling it fails:

{code:groovy}
@groovy.transform.ASTTest(value={ true }) def a = [1].collect { it }
{code}

{noformat}
1 compilation error:

Missing '}' at line: 1, column: 34
{noformat}

{code:groovy}
def m() { @groovy.transform.ASTTest(value={ true }) def a = 1; a }
{code}

{noformat}
Unexpected '}' @ line 1, column 7.
{noformat}

Without a later {{}}} on that line, the same layout works, for example 
{{@groovy.transform.ASTTest(value={ true }) def a = 1; a}}, or the method above 
with its closing brace on the next line.

h3. Cause

{{ASTTestTransformation.ASTTester#test}} rebuilds the closure source from whole 
lines and cuts its end at the last {{}}} in that text, not at the end of the 
closure:

{code:groovy}
for (int i = testClosure.lineNumber, n = testClosure.lastLineNumber; i <= n; i 
+= 1) {
    sb.append(sourceUnit.source.getLine(i, new Janitor())).append('\n')
}
sb = sb[testClosure.columnNumber..<sb.length()]
String testSource = sb[0..<sb.lastIndexOf('}')]
{code}

So for the first example the test source is {{ true }) def a = [1].collect { it 
}}, which does not parse.

h3. Suggested fix

Cut the last line at the closure's end position 
({{testClosure.lastColumnNumber}}) before dropping the closing brace, instead 
of searching for the last {{}}} in the text.



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

Reply via email to