Skip to content

Commit

Permalink
Add name mangling workaround (#253)
Browse files Browse the repository at this point in the history
* Add name mangling workaround

(best-effort) resolves #167. Feels safe enough to make it opt-out

* Update CHANGELOG.md
  • Loading branch information
ZacSweers authored Jan 25, 2024
1 parent a5cc45d commit d864bf5
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Changelog
- **Fix**: Fix allowed names config for Unit-returning functions.
- **Fix**: Ignore context receivers in multiple content emissions lint.
- **Fix**: Allow nullable types for trailing lambdas in `ComposeParameterOrder`.
- **Fix**: Best-effort work around name mangling when comparing name in M2ApiDetector's allow list.
- **Fix**: Fix `ComposePreviewPublic` to always just require private, remove preview parameter configuration.
- **Docs**: Improve docs for `ComposeContentEmitterReturningValues`
- Build against lint-api `31.2.1`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package slack.lint.compose

import com.android.tools.lint.client.api.UElementHandler
import com.android.tools.lint.detector.api.BooleanOption
import com.android.tools.lint.detector.api.Category.Companion.CORRECTNESS
import com.android.tools.lint.detector.api.Issue
import com.android.tools.lint.detector.api.JavaContext
Expand All @@ -23,8 +24,10 @@ import slack.lint.compose.util.sourceImplementation

internal class M2ApiDetector
@JvmOverloads
constructor(private val allowList: StringSetLintOption = StringSetLintOption(ALLOW_LIST)) :
OptionLoadingDetector(allowList), SourceCodeScanner {
constructor(
private val allowList: StringSetLintOption = StringSetLintOption(ALLOW_LIST),
private val workAroundMangling: BooleanOption = MANGLING_WORKAROUND,
) : OptionLoadingDetector(allowList), SourceCodeScanner {

companion object {
private const val M2Package = "androidx.compose.material"
Expand All @@ -37,6 +40,14 @@ constructor(private val allowList: StringSetLintOption = StringSetLintOption(ALL
"This property should define a comma-separated list of APIs in androidx.compose.material that should be allowed.",
)

internal val MANGLING_WORKAROUND =
BooleanOption(
"enable-mangling-workaround",
"Try to work around name mangling.",
false,
"See /~https://github.com/slackhq/compose-lints/issues/167",
)

val ISSUE =
Issue.create(
id = "ComposeM2Api",
Expand All @@ -51,7 +62,7 @@ constructor(private val allowList: StringSetLintOption = StringSetLintOption(ALL
severity = ERROR,
implementation = sourceImplementation<M2ApiDetector>(),
)
.setOptions(listOf(ALLOW_LIST))
.setOptions(listOf(ALLOW_LIST, MANGLING_WORKAROUND))
.setEnabledByDefault(false)
}

Expand Down Expand Up @@ -85,7 +96,15 @@ constructor(private val allowList: StringSetLintOption = StringSetLintOption(ALL
val packageName = context.evaluator.getPackage(resolved)?.qualifiedName ?: return
if (packageName == M2Package) {
// Ignore any in the allow-list.
if (resolved is PsiNamedElement && resolved.name in allowList.value) return
val resolvedName =
(resolved as? PsiNamedElement)?.name?.let {
if (workAroundMangling.getValue(context)) {
it.substringBefore("-")
} else {
it
}
}
if (resolvedName in allowList.value) return
context.report(
issue = ISSUE,
location = context.getLocation(node),
Expand Down
8 changes: 8 additions & 0 deletions docs/rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,14 @@ Material 3 (M3) reached stable in October 2022. In apps that have migrated to M3
</issue>
```

!!! note "Name mangling"
Kotlin will mangle the names of internal functions, which may match when resolving functions due to overloads. compose-lints will attempt to unmangle these names to match any in an allow-list, but can be disabled in case of any issues by setting the `enable-mangling-workaround` option in `lint.xml` to false.
```xml
<issue id="ComposeM2Api" severity="error">
<option name="enable-mangling-workaround" value="false" />
</issue>
```

**Related docs links**

- Announcement post: https://material.io/blog/material-3-compose-stable
Expand Down

0 comments on commit d864bf5

Please sign in to comment.