-
Notifications
You must be signed in to change notification settings - Fork 506
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow unreferenced provideDelegate if there is a by keyword in the fi…
…le (#513) * Allow unreferenced provideDelegate if there is a by keyword in the file Fixes #506 * fix test
- Loading branch information
Showing
2 changed files
with
71 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
package com.pinterest.ktlint.ruleset.standard | ||
|
||
import com.pinterest.ktlint.core.Rule | ||
import com.pinterest.ktlint.core.ast.ElementType.BY_KEYWORD | ||
import com.pinterest.ktlint.core.ast.ElementType.DOT_QUALIFIED_EXPRESSION | ||
import com.pinterest.ktlint.core.ast.ElementType.IMPORT_DIRECTIVE | ||
import com.pinterest.ktlint.core.ast.ElementType.OPERATION_REFERENCE | ||
|
@@ -44,18 +45,39 @@ class NoUnusedImportsRule : Rule("no-unused-imports") { | |
// by (https://github.com/shyiko/ktlint/issues/54) | ||
"getValue", "setValue" | ||
) | ||
|
||
private val conditionalWhitelist = mapOf<String, (node: ASTNode) -> Boolean>( | ||
Pair( | ||
// Ignore provideDelegate if there is a `by` anywhere in the file | ||
"org.gradle.kotlin.dsl.provideDelegate", | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
JakeWharton
Contributor
|
||
{ rootNode -> | ||
var hasByKeyword = false | ||
rootNode.visit { child -> | ||
if (child.elementType == BY_KEYWORD) { | ||
hasByKeyword = true | ||
return@visit | ||
} | ||
} | ||
hasByKeyword | ||
} | ||
) | ||
) | ||
|
||
private val ref = mutableSetOf<String>() | ||
private val imports = mutableSetOf<String>() | ||
private var packageName = "" | ||
private var rootNode: ASTNode? = null | ||
|
||
override fun visit( | ||
node: ASTNode, | ||
autoCorrect: Boolean, | ||
emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit | ||
) { | ||
if (node.isRoot()) { | ||
rootNode = node | ||
ref.clear() // rule can potentially be executed more than once (when formatting) | ||
ref.add("*") | ||
imports.clear() | ||
var parentExpression: String? = null | ||
node.visit { vnode -> | ||
val psi = vnode.psi | ||
|
@@ -92,7 +114,9 @@ class NoUnusedImportsRule : Rule("no-unused-imports") { | |
importDirective.delete() | ||
} | ||
} else if (name != null && (!ref.contains(name) || !isAValidImport(importPath)) && | ||
!operatorSet.contains(name) && !name.isComponentN() | ||
!operatorSet.contains(name) && | ||
!name.isComponentN() && | ||
conditionalWhitelist[importPath]?.invoke(rootNode!!) != true | ||
) { | ||
emit(node.startOffset, "Unused import", true) | ||
if (autoCorrect) { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Note that this isn't specific to
org.gradle.kotlin.dsl
! Any Kotlin library could contain such an operator function.See https://kotlinlang.org/docs/reference/delegated-properties.html#providing-a-delegate-since-11