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

Support duplicate static star imports for Groovy #4982

Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,31 @@ public G.CompilationUnit visit(SourceUnit unit, ModuleNode ast) throws GroovyPar
for (ImportNode anImport : ast.getStaticImports().values()) {
sortedByPosition.computeIfAbsent(pos(anImport), i -> new ArrayList<>()).add(anImport);
}
for (ImportNode anImport : ast.getStaticStarImports().values()) {
sortedByPosition.computeIfAbsent(pos(anImport), i -> new ArrayList<>()).add(anImport);

// Duplicate imports do work out of the box for import, star-import and static-import.
jevanlingen marked this conversation as resolved.
Show resolved Hide resolved
// For static-star-import, this does work though.
// The groovy compiler does only save the last duplicate import instead of all, so parse all static star imports by hand.
Map<String, ImportNode> staticStarImports = ast.getStaticStarImports();
if (!staticStarImports.isEmpty()) {
// Take source code until last static star import for performance reasons
int lastLineNumber = -1;
for (ImportNode anImport : ast.getStaticStarImports().values()) {
lastLineNumber = Math.max(lastLineNumber, anImport.getLastLineNumber());
}
String importSource = sourceLineNumberOffsets.length <= lastLineNumber ? source : source.substring(0, sourceLineNumberOffsets[lastLineNumber]);
jevanlingen marked this conversation as resolved.
Show resolved Hide resolved

// Create a node for each `import static`
String[] lines = importSource.split("\n");
Pattern pattern = Pattern.compile("import\\s+static\\s+([\\w.]*)\\.\\*");
jevanlingen marked this conversation as resolved.
Show resolved Hide resolved
for (int i = 0; i < lines.length; i++) {
Matcher matcher = pattern.matcher(lines[i]);
while (matcher.find()) {
ImportNode node = new ImportNode(staticStarImports.get(matcher.group(1)).getType());
node.setLineNumber(i + 1);
node.setColumnNumber(matcher.start() + 1);
sortedByPosition.computeIfAbsent(pos(node), ii -> new ArrayList<>()).add(node);
}
}
}

for (ClassNode aClass : ast.getClasses()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,17 @@ void classImport() {
);
}

@Test
void multipleImportsOnOneLine() {
rewriteRun(
groovy(
"""
import java.util.List;import java.util.Set
"""
)
);
}

@Test
void starImport() {
rewriteRun(
Expand All @@ -45,6 +56,18 @@ void starImport() {
);
}

@Test
void duplicateStarImport() {
rewriteRun(
groovy(
"""
import java.util.*
import java.util.*
"""
)
);
}

@Test
void staticImport() {
rewriteRun(
Expand All @@ -56,6 +79,18 @@ void staticImport() {
);
}

@Test
void duplicateStaticImport() {
rewriteRun(
groovy(
"""
import static java.util.Collections.singletonList
import static java.util.Collections.singletonList
"""
)
);
}

@Test
void staticStarImport() {
rewriteRun(
Expand All @@ -67,6 +102,21 @@ void staticStarImport() {
);
}

@Test
void duplicateStaticStarImport() {
rewriteRun(
groovy(
"""
import static java.util.Collections.* ; import static java.util.Collections.*
import java.util.Collections.*
import java.util.Collections.*
import static java.util.Collections.*;import static java.util.Collections.*
import java.util.Collections.*
"""
)
);
}

@Test
void classImportAlias() {
rewriteRun(
Expand Down
Loading