Skip to content

Commit

Permalink
wip: Try to use kotlinx/ast
Browse files Browse the repository at this point in the history
kotlinx/ast does not support generating code from AST
kotlinx/ast#43
  • Loading branch information
orangain committed May 14, 2022
1 parent 4f2bb5a commit a3df096
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .idea/jarRepositories.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ plugins {
repositories {
// Use Maven Central for resolving dependencies.
mavenCentral()
maven("https://jitpack.io")
}

dependencies {
Expand All @@ -27,8 +28,8 @@ dependencies {
// Use the Kotlin JDK 8 standard library.
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")

// This dependency is used by the application.
implementation("com.google.guava:guava:30.1.1-jre")
implementation("com.github.kotlinx.ast:common:v0.1.0")
implementation("com.github.kotlinx.ast:grammar-kotlin-parser-antlr-kotlin:v0.1.0")
}

testing {
Expand Down
8 changes: 8 additions & 0 deletions app/src/main/kotlin/ktcodeshift/FlattenListTransform.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package ktcodeshift

class FlattenListTransform : Transform {
override fun transform(fileInfo: FileInfo, api: Api): String {
val ast = api.parse(fileInfo.source)
return ast.toString()
}
}
11 changes: 11 additions & 0 deletions app/src/main/kotlin/ktcodeshift/KtCodeShift.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package ktcodeshift

import kotlinx.ast.common.AstSource
import kotlinx.ast.common.ast.Ast
import kotlinx.ast.grammar.kotlin.target.antlr.kotlin.KotlinGrammarAntlrKotlinParser

fun applyTransform(transform: Transform, fileInfo: FileInfo): String {
val api = Api()
return transform.transform(fileInfo, api)
}

30 changes: 30 additions & 0 deletions app/src/main/kotlin/ktcodeshift/Transform.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package ktcodeshift

import kotlinx.ast.common.AstSource
import kotlinx.ast.common.ast.Ast
import kotlinx.ast.grammar.kotlin.target.antlr.kotlin.KotlinGrammarAntlrKotlinParser

interface Transform {
fun transform(fileInfo: FileInfo, api: Api/*, options: Options*/): String
}

interface FileInfo {
val path: String
val source: String
}

//interface Api {
//
//}

class Api {
fun parse(source: String): Ast {
val astSource = AstSource.String("file", source)
val ast = KotlinGrammarAntlrKotlinParser.parseKotlinFile(astSource)
return ast
}
}

//interface Options {
//
//}
20 changes: 17 additions & 3 deletions app/src/test/kotlin/ktcodeshift/AppTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,25 @@
package ktcodeshift

import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNotNull

class AppTest {
@Test fun appHasAGreeting() {
val classUnderTest = App()
assertNotNull(classUnderTest.greeting, "app should have a greeting")
@Test
fun transform() {
val changedSource = applyTransform(FlattenListTransform(), object : FileInfo {
override val path = "Test.kt"
override val source = """
package testing
val a = listOf(listOf(1, 2), listOf(3))
""".trimIndent()
})

assertEquals("""
package testing
val a = listOf(1, 2, 3)
""".trimIndent(), changedSource)
}
}

0 comments on commit a3df096

Please sign in to comment.