Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use correct length for GString substrings #4430

Merged
merged 1 commit into from
Aug 20, 2024

Conversation

timtebeek
Copy link
Contributor

What's changed?

For GString substrings restore the length calculations we saw before b1cb470, as this negatively affected projects using Groovy 2.5, or by extension Gradle 5 or 6.

What's your motivation?

Restore successful parsing for Gradle 5 and 6 projects that use GStrings.

Have you considered any alternatives or workarounds?

Any additional context

This was a bit tricky to test, since we use the latest Groovy in rewrite-groovy. I was able to unit test this by adding a quick sample project:

plugins {
    id("org.openrewrite.build.language-library")
}

dependencies {
    implementation(project(":rewrite-groovy"))
    implementation(project(":rewrite-test"))
    implementation('org.codehaus.groovy:groovy:2.5.23')
}

And adding some unit tests there:

package org.openrewrite.groovy.test;

import org.junit.jupiter.api.Test;
import org.openrewrite.FindParseFailures;
import org.openrewrite.groovy.GroovyParser;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;

import static org.openrewrite.groovy.Assertions.groovy;

class StringOffByOneTest implements RewriteTest {

    @Override
    public void defaults(RecipeSpec spec) {
        spec
          .parser(GroovyParser.builder())
          .recipe(new FindParseFailures(null, null, null));
    }

    @Test
    void plain() {
        rewriteRun(
          groovy(
            """
              def projectDir = "project"
              def location = "src/main/webapp"
              """
          )
        );
    }

    @Test
    void curlyBraced() {
        rewriteRun(
          groovy(
            """
              def projectDir = "project"
              def location = "${projectDir}-src/main/webapp"
              """
          )
        );
    }

    @Test
    void omittedCurlyBraces() {
        rewriteRun(
          groovy(
            """
              def projectDir = "project"
              def location = "$projectDir-src/main/webapp"
              """
          )
        );
    }


    @Test
    void slashyStringStartCharInRegularString() {
        rewriteRun(
          groovy(
            """
              def projectDir = "project"
              def location = "$projectDir/src/main/webapp"
              """
          )
        );
    }

    @Test
    void slashyString() { // Fails still
        rewriteRun(
          groovy(
            """
              def projectDir = "project"
              def location = /\\/folder/
              """
          )
        );
    }
}

We might want to consider adding a separate module using Groovy 2.5.+ for the purpose of catching these regressions.

@timtebeek timtebeek self-assigned this Aug 20, 2024
@timtebeek timtebeek added the bug Something isn't working label Aug 20, 2024
@timtebeek timtebeek requested a review from sambsnyd August 20, 2024 13:33
@timtebeek
Copy link
Contributor Author

Copying this existing test to my sample project also works to replicate the issue & fix.

@timtebeek timtebeek merged commit e0ce720 into main Aug 20, 2024
2 checks passed
@timtebeek timtebeek deleted the correct-length-for-gstring-substrings branch August 20, 2024 13:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working parser-groovy
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

2 participants