From 76941101b35cc263c8aa22a14e041240075bf4ae Mon Sep 17 00:00:00 2001 From: Paul Dingemans Date: Sun, 23 Jul 2023 15:29:08 +0200 Subject: [PATCH] Store relative path of file in baseline file (#2147) Closes #2146 --- CHANGELOG.md | 1 + .../cli/reporter/baseline/BaselineReporter.kt | 19 ++++++++++++++++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6c8f94e2e5..8d6b69c895 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,6 +23,7 @@ This project adheres to [Semantic Versioning](https://semver.org/). * Fix indent of IS_EXPRESSION, PREFIX_EXPRESSION and POSTFIX_EXPRESSION in case it contains a linebreak `indent` [#2094](https://github.com/pinterest/ktlint/issues/2094) * Fix false positive regarding property that potentially can contain data which is mutable. `property-naming` [#2140](https://github.com/pinterest/ktlint/issues/2140) * Add new experimental rule `function-literal`. This rule enforces the parameter list of a function literal to be formatted consistently. `function-literal` [#2121](https://github.com/pinterest/ktlint/issues/2121) +* Store relative path of file in baseline file [#2146](https://github.com/pinterest/ktlint/issues/2146) * Fix null pointer exception for if-else statement with empty THEN block `if-else-bracing` [#2135](https://github.com/pinterest/ktlint/issues/2135) ### Changed diff --git a/ktlint-cli-reporter-baseline/src/main/kotlin/com/pinterest/ktlint/cli/reporter/baseline/BaselineReporter.kt b/ktlint-cli-reporter-baseline/src/main/kotlin/com/pinterest/ktlint/cli/reporter/baseline/BaselineReporter.kt index 6201e8ccca..11898b3215 100644 --- a/ktlint-cli-reporter-baseline/src/main/kotlin/com/pinterest/ktlint/cli/reporter/baseline/BaselineReporter.kt +++ b/ktlint-cli-reporter-baseline/src/main/kotlin/com/pinterest/ktlint/cli/reporter/baseline/BaselineReporter.kt @@ -3,8 +3,13 @@ package com.pinterest.ktlint.cli.reporter.baseline import com.pinterest.ktlint.cli.reporter.core.api.KtlintCliError import com.pinterest.ktlint.cli.reporter.core.api.KtlintCliError.Status.FORMAT_IS_AUTOCORRECTED import com.pinterest.ktlint.cli.reporter.core.api.ReporterV2 +import java.io.File import java.io.PrintStream +import java.nio.file.Path +import java.nio.file.Paths import java.util.concurrent.ConcurrentHashMap +import kotlin.io.path.pathString +import kotlin.io.path.relativeToOrSelf public class BaselineReporter( private val out: PrintStream, @@ -24,7 +29,10 @@ public class BaselineReporter( out.println("""""") out.println("""""") for ((file, errList) in acc.entries.sortedBy { it.key }) { - out.println(""" """) + // Store error in baseline always a relative path. This allows a baseline file to be stored inside a repository and after + // checking out this repository on a different path, the baseline will still be respected. + val relativeFile = Paths.get(file).relativeLocation() + out.println(""" """) for ((line, col, ruleId, _) in errList) { out.println(""" """) } @@ -39,4 +47,13 @@ public class BaselineReporter( .replace("'", "'") .replace("<", "<") .replace(">", ">") + + private fun Path.relativeLocation() = + relativeToOrSelf(ROOT_DIR_PATH) + .pathString + .replace(File.separatorChar, '/') + + private companion object { + val ROOT_DIR_PATH: Path = Paths.get("").toAbsolutePath() + } }