Skip to content

Commit

Permalink
Fix groovy parser parsing package definition (#5142)
Browse files Browse the repository at this point in the history
* Fix groovy parser parsing package definition

* Format

* Backtick not allowed in groovy
  • Loading branch information
Laurens-W authored Mar 7, 2025
1 parent 73399fa commit bd9c7df
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public class GroovyParser implements Parser {

@Override
public Stream<SourceFile> parse(@Language("groovy") String... sources) {
Pattern packagePattern = Pattern.compile("^package\\s+([^;]+);");
Pattern packagePattern = Pattern.compile("\\bpackage\\s+([.\\w]+)");
Pattern classPattern = Pattern.compile("(class|interface|enum)\\s*(<[^>]*>)?\\s+(\\w+)");

Function<String, @Nullable String> simpleName = sourceStr -> {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright 2025 the original author or authors.
* <p>
* Licensed 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
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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.
*/
package org.openrewrite.groovy;

import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThatCode;

class GroovyParserTest {

@Test
void groovyPackageDefinition() {
assertThatCode(() -> {
GroovyParser.builder().build()
.parse(
"""
package org.openrewrite.groovy
class A {
static void main(String[] args) {
String name = "John"
println(name)
}
}
""",
"""
package org.openrewrite.groovy;
class B {
static void main(String[] args) {
String name = "Doe"
println(name)
}
}
"""
);
}).doesNotThrowAnyException();
}

}

0 comments on commit bd9c7df

Please sign in to comment.