Skip to content

Commit

Permalink
Fix tsconfig support (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
WarningImHack3r authored Jun 17, 2024
1 parent 4cc1887 commit fa49703
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 10 deletions.
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ repositories {

// Dependencies are managed with Gradle version catalog - read more: https://docs.gradle.org/current/userguide/platforms.html#sub:version-catalog
dependencies {
implementation(libs.serialization)
}

// Set the JVM language level used to build the project.
Expand Down
4 changes: 4 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
[versions]
kotlin = "1.9.24"
serialization = "1.6.3"
changelog = "2.2.0"
gradleIntelliJPlugin = "1.17.3"
qodana = "2024.1.5"
kover = "0.8.0"

[libraries]
serialization = { group = "org.jetbrains.kotlinx", name = "kotlinx-serialization-json", version.ref = "serialization" }

[plugins]
changelog = { id = "org.jetbrains.changelog", version.ref = "changelog" }
gradleIntelliJPlugin = { id = "org.jetbrains.intellij", version.ref = "gradleIntelliJPlugin" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,17 @@ import java.net.URI
import java.nio.file.NoSuchFileException

abstract class Source<C : Config>(val project: Project, private val serializer: KSerializer<C>) {
companion object {
private val tsConfigJson = Json {
// Lax parsing (unquoted keys, formatting, etc.)
isLenient = true
// Allow trailing commas
// allowTrailingCommas = true
// Allow comments
// allowComments = true
}
}

private val log = logger<Source<C>>()
private var config: C? = null

Expand Down Expand Up @@ -94,6 +105,21 @@ abstract class Source<C : Config>(val project: Project, private val serializer:

protected open fun adaptFileExtensionToConfig(extension: String): String = extension

protected fun parseTsConfig(config: String): JsonElement {
// Temporary workaround until kotlinx.serialization is upgraded
val cleanConfig = config
// Remove /* */ comments
.replace(Regex("/\\*.*?\\*/", RegexOption.DOT_MATCHES_ALL), "")
.split("\n").joinToString("\n") { line ->
// Remove // comments
line.substringBefore("//").trim()
}
// Remove trailing commas
.replace(Regex(",\\s*}"), "\n}")
.replace(Regex(",\\s*]"), "\n]")
return tsConfigJson.parseToJsonElement(cleanConfig)
}

protected abstract fun adaptFileToConfig(file: PsiFile)

protected open fun fetchComponent(componentName: String): ComponentWithContents {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ open class ReactSource(project: Project) : Source<ReactConfig>(project, ReactCon
val configFile = if (getLocalConfig().tsx) "tsconfig.json" else "jsconfig.json"
val tsConfig = FileManager(project).getFileContentsAtPath(configFile)
?: throw NoSuchFileException("$configFile not found")
val aliasPath = Json.parseToJsonElement(tsConfig)
val aliasPath = parseTsConfig(tsConfig)
.jsonObject["compilerOptions"]
?.jsonObject?.get("paths")
?.jsonObject?.get("${alias.substringBefore("/")}/*")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ open class SolidSource(project: Project) : Source<SolidConfig>(project, SolidCon
val configFile = "tsconfig.json"
val tsConfig = FileManager(project).getFileContentsAtPath(configFile)
?: throw NoSuchFileException("$configFile not found")
val aliasPath = Json.parseToJsonElement(tsConfig)
val aliasPath = parseTsConfig(tsConfig)
.jsonObject["compilerOptions"]
?.jsonObject?.get("paths")
?.jsonObject?.get("${alias.substringBefore("/")}/*")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import com.intellij.openapi.application.runReadAction
import com.intellij.openapi.diagnostic.logger
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiFile
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.jsonArray
import kotlinx.serialization.json.jsonObject
import kotlinx.serialization.json.jsonPrimitive
Expand Down Expand Up @@ -40,7 +39,7 @@ open class SolidUISource(project: Project) : Source<SolidUIConfig>(project, Soli
val configFile = if (getLocalConfig().tsx) "tsconfig.json" else "jsconfig.json"
val tsConfig = FileManager(project).getFileContentsAtPath(configFile)
?: throw NoSuchFileException("$configFile not found")
val aliasPath = Json.parseToJsonElement(tsConfig)
val aliasPath = parseTsConfig(tsConfig)
.jsonObject["compilerOptions"]
?.jsonObject?.get("paths")
?.jsonObject?.get("${alias.substringBefore("/")}/*")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ open class SvelteSource(project: Project) : Source<SvelteConfig>(project, Svelte
tsConfig =
fileManager.getFileContentsAtPath(configFile) ?: throw NoSuchFileException("Cannot get $configFile")
}
val aliasPath = Json.parseToJsonElement(tsConfig)
val aliasPath = parseTsConfig(tsConfig)
.jsonObject["compilerOptions"]
?.jsonObject?.get("paths")
?.jsonObject?.get(alias.substringBefore("/"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,7 @@ open class VueSource(project: Project) : Source<VueConfig>(project, VueConfig.se
}

fun resolvePath(configFile: String): String? {
return Json.parseToJsonElement(configFile
.split("\n")
.filterNot { it.trim().startsWith("//") } // remove comments
.joinToString("\n")
)
return parseTsConfig(configFile)
.jsonObject["compilerOptions"]
?.jsonObject?.get("paths")
?.jsonObject?.get("${alias.substringBefore("/")}/*")
Expand Down

0 comments on commit fa49703

Please sign in to comment.