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

[BUG] KtParameter of a constructor cannot be transformed #871

Open
vnermolaev opened this issue Sep 28, 2021 · 1 comment
Open

[BUG] KtParameter of a constructor cannot be transformed #871

vnermolaev opened this issue Sep 28, 2021 · 1 comment
Labels
bug Something isn't working

Comments

@vnermolaev
Copy link
Contributor

A CliPlugin targeting constructor parameters of data classes fails to apply any transformation on such parameters. Such transformations include equivalent transformations too.
A use-case for such functionality is adding an additional annotation to the fields defined in the class constructor, e.g.,

data class TestMe(val s: String)

to transform to

data class TestMe(val s: @ASCII String)

where ASCII is an appropriately defined annotation.

To reproduce
Define a simple

val Meta.ConstructorParameterAnnotator: CliPlugin
    get() = "Constructor Parameter Annotator" {
        meta(
            parameter(this, match = {
                /**
                 * Returns true if this parameter is a val/var parameter of a constructor.
                 */        
                hasValOrVar()
            }) { ktParameter ->
                // Fails!
                Transform.replace(
                     replacing = ktParameter,
                     newDeclaration = ktParameter.scope()
                )
            }
        )
    }

Invocation of such a plugin leads to an exception
java.lang.IllegalStateException: Unrecognized expression type from VALUE_PARAMETER

The exception points to at arrow.meta.internal.kastree.ast.psi.Converter.convertExpr(Converter.kt:385)
which contain a when expression which does not handle KtParameter.

Expected behavior
The parameter is expected to be replaced with the new declaration.

Environment (please complete the following information):

  • Kotlin version: 1.5.20
  • Arrow Meta version: 1.5.0-SNAPSHOT

I have not found any workaround so far, e.g., by converting the respective text representation into (just for example) an expression: ktParameter.text.expression.

@vnermolaev vnermolaev added the bug Something isn't working label Sep 28, 2021
@vnermolaev
Copy link
Contributor Author

For those facing a similar problem, I ended up matching the full class and then process parameters of the constructor and do the full class reconstruction with the altered contructor.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant