Skip to content

Commit

Permalink
New warning: Unused imports should be removed
Browse files Browse the repository at this point in the history
### What's done:
* Added new warning
* Added new test
  • Loading branch information
Cheshiriks committed Jan 26, 2021
1 parent b410b66 commit aea803a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import com.pinterest.ktlint.reporter.plain.PlainReporter
import org.apache.maven.plugin.AbstractMojo
import org.apache.maven.plugin.MojoExecutionException
import org.apache.maven.plugin.MojoFailureException
import org.apache.maven.plugins.annotations.Mojo
import org.apache.maven.plugins.annotations.Parameter
import org.apache.maven.project.MavenProject

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import org.cqfn.diktat.common.config.rules.RulesConfig
import org.cqfn.diktat.common.config.rules.getCommonConfiguration
import org.cqfn.diktat.common.config.rules.getRuleConfig
import org.cqfn.diktat.ruleset.constants.EmitType
import org.cqfn.diktat.ruleset.constants.Warnings
import org.cqfn.diktat.ruleset.constants.Warnings.FILE_CONTAINS_ONLY_COMMENTS
import org.cqfn.diktat.ruleset.constants.Warnings.FILE_INCORRECT_BLOCKS_ORDER
import org.cqfn.diktat.ruleset.constants.Warnings.FILE_NO_BLANK_LINE_BETWEEN_BLOCKS
Expand All @@ -18,6 +17,7 @@ import org.cqfn.diktat.ruleset.utils.copyrightWords
import org.cqfn.diktat.ruleset.utils.findAllNodesWithSpecificType
import org.cqfn.diktat.ruleset.utils.handleIncorrectOrder
import org.cqfn.diktat.ruleset.utils.moveChildBefore
import org.cqfn.diktat.ruleset.utils.operatorMap

import com.pinterest.ktlint.core.Rule
import com.pinterest.ktlint.core.ast.ElementType.BLOCK_COMMENT
Expand Down Expand Up @@ -73,12 +73,6 @@ class FileStructureRule(private val configRules: List<RulesConfig>) : Rule("file
}
private val refSet: MutableSet<String> = mutableSetOf()
private var packageName = ""
private val operatorMap = mapOf(
"unaryPlus" to "+", "unaryMinus" to "-", "not" to "!",
"plus" to "+", "minus" to "-", "times" to "*", "div" to "/", "rem" to "%", "mod" to "%", "rangeTo" to "..",
"inc" to "++", "dec" to "--", "contains" to "in",
"plusAssign" to "+=", "minusAssign" to "-=", "timesAssign" to "*=", "divAssign" to "/=", "modAssign" to "%="
)
private lateinit var emitWarn: EmitType

override fun visit(node: ASTNode,
Expand All @@ -94,10 +88,7 @@ class FileStructureRule(private val configRules: List<RulesConfig>) : Rule("file
val importsGroupingConfig = ImportsGroupingConfig(
this.configRules.getRuleConfig(FILE_UNORDERED_IMPORTS)?.configuration ?: emptyMap()
)
val unusedImportConfig = UnusedImportConfig(
this.configRules.getRuleConfig(UNUSED_IMPORT)?.configuration ?: emptyMap()
)
checkUnusedImport(node, unusedImportConfig)
checkUnusedImport(node)
node.findChildByType(IMPORT_LIST)
?.let { checkImportsOrder(it, wildcardImportsConfig, importsGroupingConfig) }
if (checkFileHasCode(node)) {
Expand Down Expand Up @@ -222,8 +213,7 @@ class FileStructureRule(private val configRules: List<RulesConfig>) : Rule("file

@Suppress("UnsafeCallOnNullableType")
private fun checkUnusedImport(
node: ASTNode,
unusedImportConfig: UnusedImportConfig
node: ASTNode
) {
findAllReferences(node)
packageName = (node.findChildByType(PACKAGE_DIRECTIVE)?.psi as KtPackageDirective).qualifiedName
Expand All @@ -236,35 +226,28 @@ class FileStructureRule(private val configRules: List<RulesConfig>) : Rule("file
packageName.isNotEmpty() && importPath.startsWith("$packageName.") &&
importPath.substring(packageName.length + 1).indexOf('.') == -1
) {
if (unusedImportConfig.deleteUnusedImport) {
Warnings.UNUSED_IMPORT.warnAndFix(
configRules, emitWarn, isFixMode,
"$importPath - unused import",
node.startOffset, node
) { ktImportDirective.delete() }
}
} else if (importName != null && !refSet.contains(getValueMap(importName)) && !refSet.contains(
// this branch corresponds to imports from the same package
deleteImport(importPath, node, ktImportDirective)
} else if (importName != null && !refSet.contains(operatorMap.getOrDefault(importName, null)) && !refSet.contains(
importName
)
) {
if (unusedImportConfig.deleteUnusedImport) {
Warnings.UNUSED_IMPORT.warnAndFix(
configRules, emitWarn, isFixMode,
"$importPath - unused import",
node.startOffset, node
) { ktImportDirective.delete() }
}
// this import is not used anywhere
deleteImport(importPath, node, ktImportDirective)
}
}
}

private fun getValueMap(key: String): String? {
val value: String? = try {
operatorMap.getValue(key)
} catch (e: NoSuchElementException) {
null
}
return value
private fun deleteImport(
importPath: String,
node: ASTNode,
ktImportDirective: KtImportDirective
) {
UNUSED_IMPORT.warnAndFix(
configRules, emitWarn, isFixMode,
"$importPath - unused import",
node.startOffset, node
) { ktImportDirective.delete() }
}

private fun findAllReferences(node: ASTNode) {
Expand Down Expand Up @@ -390,14 +373,4 @@ class FileStructureRule(private val configRules: List<RulesConfig>) : Rule("file
*/
val useRecommendedImportsOrder = config["useRecommendedImportsOrder"]?.toBoolean() ?: true
}

/**
* [RuleConfiguration] for unused import
*/
class UnusedImportConfig(config: Map<String, String>) : RuleConfiguration(config) {
/**
* delete unused import
*/
val deleteUnusedImport = config["deleteUnusedImport"]?.toBoolean() ?: true
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,10 @@ enum class StandardPlatforms(val packages: List<String>) {
KOTLIN(listOf("kotlin", "kotlinx")),
;
}

internal val operatorMap = mapOf(
"unaryPlus" to "+", "unaryMinus" to "-", "not" to "!",
"plus" to "+", "minus" to "-", "times" to "*", "div" to "/", "rem" to "%", "mod" to "%", "rangeTo" to "..",
"inc" to "++", "dec" to "--", "contains" to "in",
"plusAssign" to "+=", "minusAssign" to "-=", "timesAssign" to "*=", "divAssign" to "/=", "modAssign" to "%="
)

0 comments on commit aea803a

Please sign in to comment.