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

Use name of parameter if text is not available #274

Merged
merged 6 commits into from
Feb 8, 2024

Conversation

kozaxinan
Copy link
Contributor

@kozaxinan kozaxinan commented Feb 7, 2024

Fixes #273

For standard compose functions, method.uastParameters returns parameter with delegated to KtUltraLightParameterForSource. KtUltraLightParameterForSource has proper text function to return.

When method is generic with reified, delegated uast becomes UastKotlinPsiParameter and getText of UastKotlinPsiParameter returns null.

Those are from my investigation.

In this PR I suggest checking name of the parameter if text fails. If you know better way/ correct way to get text when function is reified, I can update the solution.

Example reified usage is androidx.lifecycle.viewmodel.compose.viewModel. We had a wrapper with wrong parameter order to hit this issue.

Copy link

salesforce-cla bot commented Feb 7, 2024

Thanks for the contribution! Before we can merge this, we need @kozaxinan to sign the Salesforce Inc. Contributor License Agreement.

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.

Nice find and investigation, thanks!

Fix for src/test.kt line 20: Replace with (modifier: Modifier, text: String, lambda: Function0<Unit>):
@@ -20 +20
- inline fun <reified T> MyComposable(text: String = "123", modifier: Modifier = Modifier, lambda: () -> Unit) : T { }
+ inline fun <reified T> MyComposable(modifier: Modifier, text: String, lambda: Function0<Unit>) : T { }
Copy link
Collaborator

@ZacSweers ZacSweers Feb 8, 2024

Choose a reason for hiding this comment

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

Can we unpack lambdas? Feels like we should be able to get the PSI text directly for the parameters and maybe just rearrange them in that range?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Let me check. I don't know how to get lambda when text is not available. I will check about sorting that differently.

Copy link
Collaborator

Choose a reason for hiding this comment

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

You should be able to just get the text range for that PSI element in the worst case, which would let you just get the () -> function syntax. My thinking is that this is the kind of thing that could be just a rearrangement of existing text, and you could do that replace in a stringy way

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you for PSI element idea. I updated code to create correct error message and lint fix.

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.

Nice!

@ZacSweers ZacSweers added this pull request to the merge queue Feb 8, 2024
Merged via the queue into slackhq:main with commit 6caebbb Feb 8, 2024
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

reified usage on Compose function crashes ParameterOrderDetector
2 participants