-
Notifications
You must be signed in to change notification settings - Fork 25
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
Implement M2ApiDetector #75
Changes from 1 commit
ddac23a
1d2426a
d1f8079
88b5bf1
43c41af
eac47aa
804a925
7930b9f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// Copyright (C) 2022 Salesforce, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
package slack.lint.compose | ||
|
||
import com.android.tools.lint.client.api.UElementHandler | ||
import com.android.tools.lint.detector.api.Category.Companion.CORRECTNESS | ||
import com.android.tools.lint.detector.api.Detector | ||
import com.android.tools.lint.detector.api.Issue | ||
import com.android.tools.lint.detector.api.JavaContext | ||
import com.android.tools.lint.detector.api.Severity.IGNORE | ||
import com.android.tools.lint.detector.api.SourceCodeScanner | ||
import com.android.tools.lint.detector.api.TextFormat | ||
import org.jetbrains.uast.UCallExpression | ||
import org.jetbrains.uast.UElement | ||
import org.jetbrains.uast.UImportStatement | ||
import org.jetbrains.uast.UQualifiedReferenceExpression | ||
import org.jetbrains.uast.UResolvable | ||
import slack.lint.compose.util.Priorities.NORMAL | ||
import slack.lint.compose.util.sourceImplementation | ||
|
||
internal class M2ApiDetector : Detector(), SourceCodeScanner { | ||
|
||
override fun getApplicableUastTypes() = | ||
listOf<Class<out UElement>>( | ||
UCallExpression::class.java, | ||
UImportStatement::class.java, | ||
UQualifiedReferenceExpression::class.java, | ||
) | ||
|
||
override fun createUastHandler(context: JavaContext) = | ||
object : UElementHandler() { | ||
override fun visitCallExpression(node: UCallExpression) = checkNode(node) | ||
|
||
override fun visitImportStatement(node: UImportStatement) = checkNode(node) | ||
|
||
override fun visitQualifiedReferenceExpression(node: UQualifiedReferenceExpression) = | ||
checkNode(node) | ||
|
||
private fun checkNode(node: UResolvable) { | ||
val resolved = node.resolve() ?: return | ||
val packageName = context.evaluator.getPackage(resolved)?.qualifiedName ?: return | ||
if (packageName == M2Package) { | ||
context.report( | ||
issue = ISSUE, | ||
location = context.getLocation(node), | ||
message = ISSUE.getExplanation(TextFormat.TEXT), | ||
) | ||
} | ||
} | ||
} | ||
|
||
companion object { | ||
private const val M2Package = "androidx.compose.material" | ||
|
||
val ISSUE = | ||
Issue.create( | ||
id = "ComposeM2Api", | ||
briefDescription = "Using a Compose M2 API is not recommended", | ||
explanation = | ||
""" | ||
Compose Material 2 (M2) is succeeded by Material 3 (M3). Please use M3 APIs. | ||
See https://slackhq.github.io/compose-lints/rules/#use-material-3 for more information. | ||
""", | ||
category = CORRECTNESS, | ||
priority = NORMAL, | ||
severity = IGNORE, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. informational? I think it's worth highlighting even if devs don't want to make it an error. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think I generally agree. Do you know if M2 is ever going to be fully deprecated? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah good point. We need to think about teams who are using M2 and don't want to migrate yet. We should probably add some docs to copy-pasta for changing the severity level? Or maybe just link to https://developer.android.com/studio/write/lint#gradle There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
implementation = sourceImplementation<M2ApiDetector>() | ||
) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -294,3 +294,22 @@ Composed modifiers may be created outside of composition, shared across elements | |
More info: [Modifier extensions](https://developer.android.com/reference/kotlin/androidx/compose/ui/package-summary#extension-functions), [Composed modifiers in Jetpack Compose by Jorge Castillo](https://jorgecastillo.dev/composed-modifiers-in-jetpack-compose) and [Composed modifiers in API guidelines](https://github.com/androidx/androidx/blob/androidx-main/compose/docs/compose-api-guidelines.md#composed-modifiers) | ||
|
||
Related rule: [`ComposeComposableModifier`](https://github.com/slackhq/compose-lints/blob/main/compose-lint-checks/src/main/java/slack/lint/compose/ModifierComposableDetector.kt) | ||
|
||
### Use Material 3 | ||
|
||
Rule: [`ComposeM2Api`](https://github.com/slackhq/compose-lints/blob/main/compose-lint-checks/src/main/java/slack/lint/compose/M2ApiDetector.kt) | ||
|
||
Material 3 (M3) reached stable in October 2022. In apps that have migrated to M3, there may be `androidx.compose.material` (M2) APIs still remaining on the classpath from libraries or dependencies that can cause confusing imports due to the many similar or colliding Composable names in the two libraries. The `ComposeM2Api` can be enabled + set to `ERROR` to prevent these from being used. | ||
ZacSweers marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Note that this rule is set to `IGNORE` by default and is opt-in. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ditto severity comment above |
||
|
||
|
||
**Related docs links** | ||
|
||
- Announcement post: https://material.io/blog/material-3-compose-stable | ||
- Docs: https://m3.material.io/develop/android/jetpack-compose | ||
- Migration guide: https://developer.android.com/jetpack/compose/themes/material2-material3 | ||
- Guidance: https://developer.android.com/jetpack/compose/themes/material3 | ||
- Reply (primary sample app): https://github.com/android/compose-samples/tree/main/Reply | ||
- More samples: https://github.com/android/compose-samples | ||
|
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.
Will the call expression contain the package name? It wouldn't in ktlint, but I guess it will in Lint? If so, I say we don't check the imports.
Having this handle imports means that devs have to use
@file:SuppressLint
to suppress the import warnings, and they're annoying 😆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.
when you call
resolve
on theUCallExpression
, it returns the called function and we check the package name of that function.Agreed on imports. May be a no-go since I don't think you can use
file
site target with@SuppressLint
🤔. I'll checkThere 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.
You can for Kotlin
@Suppress
but I don't think I've ever tried it forSuppressLint
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.
yah you can't since it's in the android jar itself. Remove imports 88b5bf1