Spotless: Remove Unused Import from Groovy Files

Created on 8 May 2018  路  3Comments  路  Source: diffplug/spotless

I would like to remove unused import from groovy file.

enhancement

Most helpful comment

@nedtwigg @fvgh Yes i do agree codenarc has rules for removing unused imports for Groovy files. But it doesn't delete line of unused import like the diffspotless plugin works for java file. Codenarc rules lets user know about violation of rule (unused import). My Question is IF we have groovy file & has unused import , lets our spotless plugin identify & delete from groovy file. I hope this makes sense.

All 3 comments

I am not sure whether this is a real formatter task. You cannot check wildcard imports since the formatter only cares about a single file.
If it is implemented, I would propose to provided it for Java and Groovy, since there should be not much difference.
Not sure whether you are aware, that CodeNarc handles this issue very well:
http://codenarc.sourceforge.net/codenarc-rules-imports.html

@nedtwigg
I see that there is a demand and many people consider wild card imports anyway bad practice and can with the means of Spotless forbid wild cards. So in principal this feature might be nice for Groovy and Java, but I am not sure whether we should leave it to CodeNarc and FindBugs, since these tools are prepared to handle the topic completely.

We can (and do) remove unused imports from java files if that file doesn't have any wildcard imports. I think it's fine to have a groovy formatter that says if (hasWildcards) { return } else { removeUnusedImports() }.

I also think it would be okay to have a separate "no wildcard imports" rule, but it's a better fit for CodeNarc etc. than Spotless, because there's no way to implement it as F(unformatted) = formatted. If somebody wrote a "no wildcard imports" rule, I'd be happy to merge it.

@nedtwigg @fvgh Yes i do agree codenarc has rules for removing unused imports for Groovy files. But it doesn't delete line of unused import like the diffspotless plugin works for java file. Codenarc rules lets user know about violation of rule (unused import). My Question is IF we have groovy file & has unused import , lets our spotless plugin identify & delete from groovy file. I hope this makes sense.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Puneetha17 picture Puneetha17  路  6Comments

I-Vargas picture I-Vargas  路  3Comments

ubuntudroid picture ubuntudroid  路  6Comments

fabriziocucci picture fabriziocucci  路  5Comments

scphantm picture scphantm  路  7Comments