Skip to content

Commit

Permalink
Support duplicate star imports with comments (#4990)
Browse files Browse the repository at this point in the history
* Support duplicate star imports with comments

* Support duplicate star imports with comments

* Support duplicate star imports with comments
  • Loading branch information
jevanlingen authored Feb 6, 2025
1 parent cd95e0e commit 22179f5
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ public class GroovyParserVisitor {

private int cursor = 0;

private static final String MULTILINE_COMMENT_REGEX = "(?s)/\\*.*?\\*/";
private static final Pattern whitespacePrefixPattern = Pattern.compile("^\\s*");

@SuppressWarnings("RegExpSimplifiable")
Expand Down Expand Up @@ -2786,10 +2787,10 @@ private List<ImportNode> getStaticStarImports(ModuleNode ast) {
}
String importSource = sourceLineNumberOffsets.length <= lastLineNumber ? source : source.substring(0, sourceLineNumberOffsets[lastLineNumber]);

// Create a node for each `import static`
String[] lines = importSource.split("\n");
// Create a node for each `import static`, don't parse comments
String[] lines = importSource.replaceAll(MULTILINE_COMMENT_REGEX, "").split("\n");
for (int i = 0; i < lines.length; i++) {
String line = lines[i];
String line = lines[i].split(SINGLE_LINE_COMMENT.open)[0];
int index = 0;

while (index < line.length()) {
Expand All @@ -2810,6 +2811,8 @@ private List<ImportNode> getStaticStarImports(ModuleNode ast) {

if (packageEnd < line.length() && line.charAt(packageEnd) == '*') {
ImportNode node = new ImportNode(staticStarImports.get(line.substring(packageBegin, packageEnd - 1)).getType());
// The line numbers can be off, because we filter away all the multiline comments.
// This does not really do any harm, because we only use the line numbers to order the nodes in the `sortedByPosition` var.
node.setLineNumber(i + 1);
node.setColumnNumber(importIndex + 1);
completeStaticStarImports.add(node);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,25 @@ void staticImportAlias() {
}

@Test
void duplicateImports() {
void duplicateImportsAndComments() {
rewriteRun(
groovy(
"""
import static java.util.Collections.* ; import static java.util.Collections.*
//import static java.util.Collections.* ; import static java.util.Collections.*
import java.util.Collections.* ; import static java.util.Collections.*
import java.util.Collections.*
import static java.util.Collections.singletonList as listOf
import static java.util.Collections.singletonList as listOf
import /*static java.util.Collections.singletonList;import static*/ java.util.Collections.*
import /*static java.util.Collections.singletonList;
import static java.util.Collections.**/java.util.Collections.*
import /*static java.util.Collections.singletonList;
import static java.util.Collections.**/static java.util.Collections.*
import static java.util.Collections.singletonList;import static java.util.Collections.*
import java.util.Collections.*
"""
Expand Down

0 comments on commit 22179f5

Please sign in to comment.