All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.pinterest.ktlint.reporter.baseline.BaselineReporter.kt Maven / Gradle / Ivy

There is a newer version: 0.51.0-FINAL
Show newest version
package com.pinterest.ktlint.reporter.baseline

import com.pinterest.ktlint.core.LintError
import com.pinterest.ktlint.core.Reporter
import java.io.File
import java.io.PrintStream
import java.nio.file.Paths
import java.util.ArrayList
import java.util.concurrent.ConcurrentHashMap

class BaselineReporter(val out: PrintStream) : Reporter {

    private val acc = ConcurrentHashMap>()

    override fun onLintError(file: String, err: LintError, corrected: Boolean) {
        if (!corrected) {
            acc.getOrPut(file) { ArrayList() }.add(err)
        }
    }

    override fun afterAll() {
        out.println("""""")
        out.println("""""")
        for ((file, errList) in acc.entries.sortedBy { it.key }) {
            val fileName = try {
                val rootPath = Paths.get("").toAbsolutePath()
                val filePath = Paths.get(file)
                rootPath.relativize(filePath).toString().replace(File.separatorChar, '/')
            } catch (e: IllegalArgumentException) {
                file
            }
            out.println("""	""")
            for ((line, col, ruleId, _) in errList) {
                out.println(
                    """		"""
                )
            }
            out.println("""	""")
        }
        out.println("""""")
    }

    private fun String.escapeXMLAttrValue() =
        this.replace("&", "&").replace("\"", """).replace("'", "'")
            .replace("<", "<").replace(">", ">")
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy