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

feat: set flags based on biome-version to add backwards compatibility #96

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/main/kotlin/com/github/biomejs/intellijbiome/BiomePackage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,24 @@ class BiomePackage(private val project: Project) {
}
return null
}

fun compareVersion(version1: String, version2: String): Int {
val parts1 = version1.split(".").map { it.toInt() }
val parts2 = version2.split(".").map { it.toInt() }

val maxLength = maxOf(parts1.size, parts2.size)

for (i in 0 until maxLength){
val v1 = if (i < parts1.size) parts1[i] else 0
val v2 = if (i < parts2.size) parts2[i] else 0

when {
v1 < v2 -> return -1
v1 > v2 -> return 1
}
}

return 0
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ interface BiomeRunner {
val DEFAULT_TIMEOUT = 30000.milliseconds
}

fun check(request: Request, features: EnumSet<Feature>): Response
fun check(request: Request, features: EnumSet<Feature>, biomePackage: BiomePackage): Response
fun createCommandLine(file: VirtualFile, action: String, args: List<String> = listOf()): GeneralCommandLine


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import kotlin.time.Duration
class BiomeStdinRunner(private val project: Project) : BiomeRunner {
private val biomePackage = BiomePackage(project)

override fun check(request: BiomeRunner.Request, features: EnumSet<Feature>): BiomeRunner.Response {
val commandLine = createCommandLine(request.virtualFile, "check", getCheckFlags(features))
override fun check(request: BiomeRunner.Request, features: EnumSet<Feature>, biomePackage: BiomePackage): BiomeRunner.Response {
val commandLine = createCommandLine(request.virtualFile, "check", getCheckFlags(features, biomePackage))
val file = request.virtualFile
val timeout = request.timeout
val failureMessage = BiomeBundle.message(
Expand Down Expand Up @@ -83,7 +83,7 @@ class BiomeStdinRunner(private val project: Project) : BiomeRunner {
return future
}

private fun getCheckFlags(features: EnumSet<Feature>): List<String> {
fun getCheckFlags(features: EnumSet<Feature>, biomePackage: BiomePackage): List<String> {
val args = SmartList<String>()

if (features.isEmpty()) return args
Expand All @@ -98,11 +98,22 @@ class BiomeStdinRunner(private val project: Project) : BiomeRunner {
args.add("--linter-enabled=false")
}

args.add("--write")
if (features.contains(Feature.UnsafeFixes)) {
args.add("--unsafe")
val version = biomePackage.versionNumber()
if (version === null || version.isEmpty() || biomePackage.compareVersion(version, "1.8.0") >= 0) {
args.add("--write")
if (features.contains(Feature.UnsafeFixes)) {
args.add("--unsafe")
}
} else {
if (features.contains(Feature.UnsafeFixes)) {
args.add("--apply-unsafe")
} else {
args.add("--apply")
}
}

args.add("--skip-errors")

return args
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package com.github.biomejs.intellijbiome.actions

import com.github.biomejs.intellijbiome.BiomeBundle
import com.github.biomejs.intellijbiome.BiomeRunner
import com.github.biomejs.intellijbiome.BiomeStdinRunner
import com.github.biomejs.intellijbiome.Feature
import com.github.biomejs.intellijbiome.*
import com.github.biomejs.intellijbiome.settings.BiomeSettings
import com.intellij.lang.javascript.linter.GlobPatternUtil
import com.intellij.openapi.command.WriteCommandAction
Expand Down Expand Up @@ -40,6 +37,7 @@ class BiomeCheckRunner {
val runner = BiomeStdinRunner(project)
val manager = FileDocumentManager.getInstance()
val settings = BiomeSettings.getInstance(project)
val biomePackage = BiomePackage(project)
val requests = documents
.mapNotNull { document -> manager.getFile(document)?.let { document to it } }
.filter { GlobPatternUtil.isFileMatchingGlobPattern(project, settings.filePattern, it.second) }
Expand All @@ -52,7 +50,7 @@ class BiomeCheckRunner {
indicator.text = commandDescription

requests.forEach { request ->
val response = runner.check(request, features)
val response = runner.check(request, features, biomePackage)

if (!indicator.isCanceled) {
applyChanges(project, request, response)
Expand Down
Loading