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

Fix KT-18706 in CodeWriter.generateImports #1920

Merged
merged 1 commit into from
Jul 11, 2024

Conversation

mitasov-ra
Copy link
Contributor

@mitasov-ra mitasov-ra commented Jun 4, 2024

  • docs/changelog.md has been updated if applicable.
  • CLA signed.

Because of KT-18706
bug all aliases escaped with backticks are not resolved by the Kotlin compiler.

So I've added a new function to Utils - String.escapeAsAlias, which converts aliases to Java identifiers, so that backticks are no longer needed.

I tried to make a more "global" fix by adding escapeAsAlias to the Import constructor, but since Import is a data class the "easy way" is not possible. Import should be converted to class to do this.

So I've decided just to fix the generateImports function, so that aliases, generated by kotlinpoet itself, would work. This small fix helps a lot with code generation tools.

@JakeWharton
Copy link
Collaborator

Can you write an integration test? In general we prefer to test against the public API and generated code so that we can ensure we cover the cases that consumers see. The unit tests are sufficient for coverage of the new function's behavior, but we need to see it wired up in practice at least once.

@mitasov-ra
Copy link
Contributor Author

@JakeWharton Sure, no problem. Can you please navigate me a bit, where are exactly other integration tests in this project?

@JakeWharton
Copy link
Collaborator

If your change doesn't affect imports you can write tests for the TypeAliasSpec in here:

If it does affect imports, add a test to FileSpecTest:

@Test fun simpleTypeAliases() {
val source = FileSpec.builder("com.squareup.tacos", "Taco")
.addTypeAlias(TypeAliasSpec.builder("Int8", Byte::class).build())
.addTypeAlias(
TypeAliasSpec.builder(
"FileTable",
Map::class.parameterizedBy(String::class, Int::class),
).build(),
)
.build()
assertThat(source.toString()).isEqualTo(
"""
|package com.squareup.tacos
|
|import kotlin.Byte
|import kotlin.Int
|import kotlin.String
|import kotlin.collections.Map
|
|public typealias Int8 = Byte
|
|public typealias FileTable = Map<String, Int>
|
""".trimMargin(),
)
}

@mitasov-ra
Copy link
Contributor Author

@JakeWharton I think you've confused import aliases with typealias

This PR only affects import aliases, and only the code that generates them in cases where there's more than one type with the same simple name is used in CodeBlock

I've just added another test specifically for that case, but I've put it in separate file. Is that ok?

@JakeWharton
Copy link
Collaborator

Ah, all the more reason it needed an integration test. There's no indication what actual output the change it affecting without close inspection.

I would expect this test to change:

@Test fun escapeSpacesInAliasedImports() {
val tacoFactory = ClassName("com.squareup.taco factory", "TacoFactory")
val file = FileSpec.builder("com.example", "TacoFactoryDemo")
.addAliasedImport(tacoFactory, "La Taqueria")
.addFunction(
FunSpec.builder("main")
.addStatement("println(%T.produceTacos())", tacoFactory)
.build(),
)
.build()
assertThat(file.toString()).isEqualTo(
"""
|package com.example
|
|import com.squareup.`taco factory`.TacoFactory as `La Taqueria`
|
|public fun main() {
| println(`La Taqueria`.produceTacos())
|}
|
""".trimMargin(),
)
}
. Since import emissions behavior is owned by FileSpec I would prefer any tests for it to go under FileSpecTest, especially since we already have import alias test cases there and below in that link.

@mitasov-ra
Copy link
Contributor Author

mitasov-ra commented Jun 4, 2024

I would expect this test to change

At first I tried to fix this case too, but as I said earlier, it became too complicated without rewriting Import from data class to class.

So I decided to fix just the internal code inside CodeWriter (this make sense since this is the only Import.alias related code, which cannot be fixed on consumer side)

I would prefer any tests for it to go under FileSpecTest

Done.

Copy link
Collaborator

@Egorand Egorand left a comment

Choose a reason for hiding this comment

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

Nice!

@Egorand
Copy link
Collaborator

Egorand commented Jun 11, 2024

@mitasov-ra one more thing: could you please update the "Unreleased" section of the changelog? Thanks!

mitasov-ra added a commit to mitasov-ra/kotlinpoet that referenced this pull request Jun 11, 2024
@mitasov-ra
Copy link
Contributor Author

@Egorand All done! Please rerun checks

@@ -6,6 +6,7 @@ Change Log
* **Fix**: Don't expand typealiases of function types to `LambdaTypeName`s in `KSTypeReference.toTypeName()`.
* **Fix**: Small double and float values were set to 0.0 in %L translation (#1919)
* **Fix**: Fix typealias type argument resolution in KSP2.
* **Fix**: Fix KT-18706 in CodeWriter.generateImports (#1920)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we change this to be a bit more user friendly so someone reading this would understand what the impact on their code is.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've updated changelog, if new description is still seems off to you, please provide your version

@Egorand
Copy link
Collaborator

Egorand commented Jun 21, 2024

All looks good to me, thanks!

@JakeWharton please merge if you're happy with the changes.

@mitasov-ra mitasov-ra requested a review from JakeWharton June 30, 2024 14:20
@Egorand
Copy link
Collaborator

Egorand commented Jul 5, 2024

@mitasov-ra looks like there are conflicts blocking the merge (sorry for the delay in merging your PR), and it doesn't seem like I can resolve them myself - can you please rebase? I'm in the process of releasing 1.18.0, and will release 1.18.1 with your change once the PR is merged. Thanks!

@mitasov-ra
Copy link
Contributor Author

@Egorand Done

@Egorand Egorand merged commit 4ee8fc4 into square:main Jul 11, 2024
4 checks passed
@Egorand
Copy link
Collaborator

Egorand commented Jul 11, 2024

Merged, thank you!

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.

3 participants