Groovy BUG, Eclipse BUG

This is laughable, but 2 bugs nonetheless:

The code:

    /*
     imgs.each({
      if(it["alt"] && (it["alt"] =~ /^(?i).*image.*/)) {
        aMentionsImage.push(it)
      }
    })
    */

Screenshot of the issue in Katalon Studio:
image

How the Problems tab reports the problem (which is quite misleading in itself)
image

Interesting to note, even Discourse has the exact same issue when trying to parse it (though in Discourse’s case, perhaps more forgivable).

Did anyone catch what the problem is?

Let me spell it out. Take another look at the code, especially the screenshot of it.

image

On line 54 I’m starting a block comment with /*. The block continues up to and including line 60 ending with */

Now look carefully at line 56 in the middle of the code lines. Line 56 contains a groovy
/slashy string/. That is, a string delineated by / and /. Slashy strings are typically used for regular expressions – which is exactly what I’m using them for.

At this point, the eagle eyed, if they didn’t spot the problem before now, will have seen the cause of the issue:

The regex ends with .* followed immediately by / giving */

All perfectly understandable here in Discourse and maybe even forgivable that the KS/Eclipse syntax coloring routines get it wrong, but completely unforgivable that the Groovy compiler flatly refuses to compile perfectly legal code.

Put more succinctly, the Groovy compiler takes no account of slashy strings while parsing block comments.

Of course, the issue wasn’t helped by the KS Problems tab telling me there was a problem on line 73 :roll_eyes: but I can live with that since it almost certainly gets its info passed to it from a Groovy lib somewhere. But Groovy itself not understanding Groovy… that’s terrible.

1 Like

You can be a bit gentle to the Groovy parser by inserting a $ in between * and / as follows:

/*
    imgs.each({
      if (it["alt"] && (it["alt"] =~ /^(?i).*image.*$/)) {
	    aMentionsImage.push(it)
	  }
    })
*/

The inserted $ keeps the semantics of regex unchanged.

Good catch! But that would add “at end of string” pattern, which is not necessarily correct and certainly alters the semantics.

And anyway, I shouldn’t need to modify the code inside a comment to make a valid comment. Like I said at the top, that’s laughable. The only time I would be willing to do that, is if the comment block was trying to include an embedded comment block.

/*
Some code /* A comment */
more code
*/

That’s “fair” because we’re dealing with a comment block which does not allow embedded comment blocks.and has always been the case (for ~30 years that I know about).

The case I’m highlighting is an entirely different case: valid code (i.e. slashy strings, NOT regex) that cannot be commented out as a block.

/*

/**** TEST SLASHY STRING ********/

*/