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

Added utf-8 BOM removal #630

Merged
merged 3 commits into from
Nov 12, 2019
Merged
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
21 changes: 18 additions & 3 deletions ktlint-core/src/main/kotlin/com/pinterest/ktlint/core/KtLint.kt
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ object KtLint {
val ANDROID_USER_DATA_KEY = Key<Boolean>("ANDROID")
val FILE_PATH_USER_DATA_KEY = Key<String>("FILE_PATH")
val DISABLED_RULES = Key<Set<String>>("DISABLED_RULES")
private const val UTF8_BOM = "\uFEFF"
const val STDIN_FILE = "<stdin>"

private val psiFileFactory: PsiFileFactory
Expand Down Expand Up @@ -126,7 +127,7 @@ object KtLint {
* @throws RuleExecutionException in case of internal failure caused by a bug in rule implementation
*/
fun lint(params: Params) {
val normalizedText = params.text.replace("\r\n", "\n").replace("\r", "\n")
val normalizedText = normalizeText(params.text)
val positionByOffset = calculateLineColByOffset(normalizedText)
val psiFileName = if (params.script) "file.kts" else "file.kt"
val psiFile = psiFileFactory.createFileFromText(psiFileName, KotlinLanguage.INSTANCE, normalizedText) as KtFile
Expand Down Expand Up @@ -165,6 +166,13 @@ object KtLint {
.forEach { e -> params.cb(e, false) }
}

private fun normalizeText(text: String): String {
return text
.replace("\r\n", "\n")
.replace("\r", "\n")
.replaceFirst(UTF8_BOM, "")
}

private fun userDataResolver(editorConfigPath: String?, debug: Boolean): (String?) -> Map<String, String> {
if (editorConfigPath != null) {
val userData = (
Expand Down Expand Up @@ -347,7 +355,8 @@ object KtLint {
* @throws RuleExecutionException in case of internal failure caused by a bug in rule implementation
*/
fun format(params: Params): String {
val normalizedText = params.text.replace("\r\n", "\n").replace("\r", "\n")
val hasUTF8BOM = params.text.startsWith(UTF8_BOM)
val normalizedText = normalizeText(params.text)
val positionByOffset = calculateLineColByOffset(normalizedText)
val psiFileName = if (params.script) "file.kts" else "file.kt"
val psiFile = psiFileFactory.createFileFromText(psiFileName, KotlinLanguage.INSTANCE, normalizedText) as KtFile
Expand Down Expand Up @@ -410,7 +419,13 @@ object KtLint {
.sortedWith(Comparator { (l), (r) -> if (l.line != r.line) l.line - r.line else l.col - r.col })
.forEach { (e, corrected) -> params.cb(e, corrected) }
}
return if (mutated) rootNode.text.replace("\n", determineLineSeparator(params.text, params.userData)) else params.text

if (!mutated) {
return params.text
}

return if (hasUTF8BOM) UTF8_BOM else "" + // Restore UTF8 BOM if it was present
rootNode.text.replace("\n", determineLineSeparator(params.text, params.userData))
}

private fun determineLineSeparator(fileContent: String, userData: Map<String, String>): String {
Expand Down