-
Notifications
You must be signed in to change notification settings - Fork 970
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
New lint rule that checks if a Tree
is planted for atleast an app variant.
#381
Open
tikurahul
wants to merge
1
commit into
JakeWharton:trunk
Choose a base branch
from
tikurahul:master
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
80 changes: 80 additions & 0 deletions
80
timber-lint/src/main/java/timber/lint/PlantATreeDetector.kt
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 |
---|---|---|
@@ -0,0 +1,80 @@ | ||
@file:Suppress("UnstableApiUsage") | ||
|
||
package timber.lint | ||
|
||
import com.android.tools.lint.detector.api.* | ||
import com.intellij.psi.PsiMethod | ||
import org.jetbrains.uast.UCallExpression | ||
import java.util.* | ||
|
||
/** | ||
* A [Detector] which makes sure than anytime Timer APIs are used, there is at-least a single tree | ||
* planted. | ||
*/ | ||
class PlantATreeDetector : Detector(), SourceCodeScanner { | ||
companion object { | ||
val ISSUE = Issue.create( | ||
id = "MustPlantATimberTree", | ||
briefDescription = "A Timber tree needs to be planted", | ||
explanation = """ | ||
When using Timber's logging APIs, a `Tree` must be planted on at least a single \ | ||
variant of the app. | ||
""", | ||
androidSpecific = true, | ||
category = Category.CORRECTNESS, | ||
severity = Severity.ERROR, | ||
implementation = Implementation( | ||
PlantATreeDetector::class.java, | ||
EnumSet.of(Scope.JAVA_FILE) | ||
) | ||
) | ||
|
||
// Methods on the companion object are marked as @JvmStatic | ||
// Therefore we need to check whether they can be resolved to either Timber or Forest. | ||
private const val TIMBER = "timber.log.Timber" | ||
private const val FOREST = "timber.log.Timber.Forest" | ||
} | ||
|
||
// Do we need to check if a Tree is planted | ||
private var checkForPlantedTrees = false | ||
private var hasPlantedTree = false | ||
private var location: Location? = null | ||
|
||
override fun getApplicableMethodNames() = listOf("v", "d", "i", "w", "e", "wtf", "plant") | ||
|
||
override fun visitMethodCall(context: JavaContext, node: UCallExpression, method: PsiMethod) { | ||
val methodName = method.name | ||
when (context.driver.phase) { | ||
1 -> { | ||
if (methodName.matches(Regex("(v|d|i|w|e|wtf)")) | ||
&& context.evaluator.isMemberInClass(method, FOREST) | ||
) { | ||
if (!checkForPlantedTrees) { | ||
location = context.getLocation(node) | ||
checkForPlantedTrees = true | ||
// Request a second scan with the same scope | ||
context.driver.requestRepeat(this, null) | ||
} | ||
} | ||
} | ||
else -> { | ||
if (methodName.matches(Regex("plant")) && | ||
(context.evaluator.isMemberInClass(method, TIMBER) || | ||
context.evaluator.isMemberInClass(method, FOREST)) | ||
) { | ||
hasPlantedTree = true | ||
} | ||
} | ||
} | ||
} | ||
|
||
override fun afterCheckRootProject(context: Context) { | ||
if (checkForPlantedTrees && !hasPlantedTree && context.driver.phase > 1) { | ||
context.report( | ||
issue = ISSUE, | ||
location = location ?: Location.create(context.file), | ||
message = "A `Tree` must be planted for at least a single variant of the application." | ||
) | ||
} | ||
} | ||
} |
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
145 changes: 145 additions & 0 deletions
145
timber-lint/src/test/java/timber/lint/PlantATreeDetectorTest.kt
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 |
---|---|---|
@@ -0,0 +1,145 @@ | ||
package timber.lint | ||
|
||
import com.android.tools.lint.checks.infrastructure.LintDetectorTest.java | ||
import com.android.tools.lint.checks.infrastructure.LintDetectorTest.kotlin | ||
import com.android.tools.lint.checks.infrastructure.TestLintTask.lint | ||
import org.junit.Test | ||
|
||
class PlantATreeDetectorTest { | ||
|
||
private val timber = kotlin("timber/log/Timber.kt", """ | ||
package timber.log | ||
class Timber private constructor() { | ||
abstract class Tree { | ||
|
||
} | ||
companion object Forest: Tree() { | ||
@JvmStatic | ||
fun e(message: String?, vararg args: Any?) { | ||
|
||
} | ||
@JvmStatic | ||
fun w(message: String?, vararg args: Any?) { | ||
|
||
} | ||
@JvmStatic | ||
fun i(message: String?, vararg args: Any?) { | ||
|
||
} | ||
@JvmStatic | ||
fun d(message: String?, vararg args: Any?) { | ||
|
||
} | ||
@JvmStatic | ||
fun v(message: String?, vararg args: Any?) { | ||
|
||
} | ||
@JvmStatic | ||
fun plant(tree: Tree) { | ||
|
||
} | ||
} | ||
} | ||
""").indented().within("src") | ||
|
||
@Test | ||
fun testNoTimberLoggingApisAreUsed() { | ||
val application = kotlin("com/example/App.kt", """ | ||
package com.example | ||
|
||
import timber.log.Timber | ||
|
||
class App { | ||
fun onCreate() { | ||
} | ||
} | ||
""").indented().within("src") | ||
|
||
lint() | ||
.files(timber, application) | ||
.issues(PlantATreeDetector.ISSUE) | ||
.run() | ||
.expectClean() | ||
} | ||
|
||
@Test | ||
fun testWhenTimberApisAreUsed() { | ||
val application = kotlin("com/example/App.kt", """ | ||
package com.example | ||
|
||
import timber.log.Timber | ||
|
||
class App { | ||
fun onCreate() { | ||
Timber.d("Log something") | ||
} | ||
} | ||
""").indented().within("src") | ||
|
||
lint() | ||
.files(timber, application) | ||
.issues(PlantATreeDetector.ISSUE) | ||
.run() | ||
.expect(""" | ||
src/com/example/App.kt:7: Error: A Tree must be planted for at least a single variant of the application. [MustPlantATimberTree] | ||
Timber.d("Log something") | ||
~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
1 errors, 0 warnings | ||
""".trimIndent()) | ||
} | ||
|
||
@Test | ||
fun testWhenTimberApisAreUsedAndTreeIsPlanted() { | ||
val application = kotlin("com/example/App.kt", """ | ||
package com.example | ||
|
||
import timber.log.Timber | ||
|
||
class App { | ||
fun onCreate() { | ||
plantTree() | ||
Timber.d("Log something") | ||
} | ||
|
||
private fun plantTree() { | ||
val tree = Timber.Tree() | ||
Timber.plant(tree) | ||
} | ||
} | ||
""").indented().within("src") | ||
|
||
lint() | ||
.files(timber, application) | ||
.issues(PlantATreeDetector.ISSUE) | ||
.run() | ||
.expectClean() | ||
} | ||
|
||
@Test | ||
fun testWhenTimberApisAreUsedAndTreeIsPlanted_java() { | ||
val application = java("com/example/App.java", """ | ||
package com.example; | ||
|
||
import timber.log.Timber; | ||
|
||
class App { | ||
void onCreate() { | ||
plantTree(); | ||
Timber.d("Log something"); | ||
} | ||
|
||
void plantTree() { | ||
val tree = Timber.Tree(); | ||
Timber.plant(tree); | ||
} | ||
} | ||
""").indented().within("src") | ||
|
||
lint() | ||
.files(timber, application) | ||
.issues(PlantATreeDetector.ISSUE) | ||
.run() | ||
.expectClean() | ||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's
context.driver.phase > 1
doing?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a 2 phase lint rule. The first phase checks for the use of Timber's logging APIs. Once we find instances of such use, we start phase 2 (line 52) to ensure that at least one
Timber.Tree
was planted.The
afterCheckRootProject
callback is called at the end of every phase, and therefore we only want to report an issue, after 2 phases are complete.