Skip to content
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

Update ViewModelInjectionDetector to support a lint option #53

Merged

Conversation

WhosNickDoglio
Copy link
Contributor

@WhosNickDoglio WhosNickDoglio commented Feb 10, 2023

Replaces #46

@WhosNickDoglio WhosNickDoglio force-pushed the ndoglio-ViewModelInjection-LintOption branch from 24a815e to 2906193 Compare February 11, 2023 16:15
@ZacSweers
Copy link
Collaborator

@WhosNickDoglio this is still in draft, did you want to ping us when it's ready for review?

@WhosNickDoglio
Copy link
Contributor Author

@ZacSweers Yeah sorry, I threw something together quick and was going to spend some time on it this weekend but got busy, I plan to finish it tomorrow and will ping you when it's ready 👍

@WhosNickDoglio WhosNickDoglio force-pushed the ndoglio-ViewModelInjection-LintOption branch from 2906193 to 3064b71 Compare February 15, 2023 11:51
@WhosNickDoglio WhosNickDoglio marked this pull request as ready for review February 15, 2023 12:24
@WhosNickDoglio
Copy link
Contributor Author

@ZacSweers This is ready for review whenever you have time!

Comment on lines 75 to 76
KnownViewModelFactories.contains(it.calleeExpression?.text) ||
allowedNames.value.contains(it.calleeExpression?.text)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's merge the known and allowed factories into one set to avoid double checks. Let's also use the in operator

@WhosNickDoglio WhosNickDoglio force-pushed the ndoglio-ViewModelInjection-LintOption branch from 40e94b6 to 9419761 Compare February 15, 2023 17:51
@@ -8,15 +8,28 @@ import com.android.tools.lint.detector.api.Issue
import com.android.tools.lint.detector.api.JavaContext
import com.android.tools.lint.detector.api.Severity
import com.android.tools.lint.detector.api.SourceCodeScanner
import com.android.tools.lint.detector.api.StringOption
import org.jetbrains.kotlin.psi.KtCallExpression
import org.jetbrains.kotlin.psi.KtFunction
import org.jetbrains.kotlin.psi.KtProperty
import slack.lint.compose.util.*
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note for myself - we shouldn't have star imports, wonder why spotless isn't catching this 🤔

@@ -57,7 +71,10 @@ class ViewModelInjectionDetector : ComposableFunctionDetector(), SourceCodeScann
.flatMap { property ->
property
.findDirectChildrenByClass<KtCallExpression>()
.filter { KnownViewModelFactories.contains(it.calleeExpression?.text) }
.filter {
val allFactoryNames = KnownViewModelFactories + allowedNames.value
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you move this higher up? Doesn't need to be re-computed on each call

@WhosNickDoglio WhosNickDoglio force-pushed the ndoglio-ViewModelInjection-LintOption branch from 37ba043 to bf8422e Compare February 15, 2023 21:22

internal val ALLOW_LIST =
StringOption(
"allowed-viewmodel-injection",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: since it's not really an allow-list, let's improve the semantics. Maybe USER_FACTORIES and viewmodel-factories?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator

@ZacSweers ZacSweers left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@ZacSweers ZacSweers added this pull request to the merge queue Feb 18, 2023
Merged via the queue into slackhq:main with commit 00b3562 Feb 18, 2023
@WhosNickDoglio WhosNickDoglio deleted the ndoglio-ViewModelInjection-LintOption branch February 19, 2023 02:23
@jakoss
Copy link

jakoss commented Mar 9, 2023

@WhosNickDoglio Do you have example how user can set this parameter? I'm trying to find some way to pass custom option to lint but i think i'm missing something

@WhosNickDoglio
Copy link
Contributor Author

@jakoss

In your lint.xml file you would add this:

<issue id="ComposeViewModelInjection">
    <option name="viewmodel-factories" value="ViewModelFactory1,ViewModelFactory2, etc" />
</issue>

@jakoss
Copy link

jakoss commented Mar 9, 2023

@WhosNickDoglio Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants