Add fixer for trailing new-line rule.

This commit is contained in:
Amy Boyd
2016-07-04 14:04:58 +01:00
parent 3463ab7d0f
commit 0e16bc1255
2 changed files with 53 additions and 0 deletions

View File

@@ -29,3 +29,24 @@ func FixEndOfLineRule(ruleValue string, fileContent string) string {
return fileContent
}
/**
* This must be called before FixEndOfLineRule so the \n added will be converted to whatever the
* 'end_of_line' rule dictates.
*/
func FixInsertFinalNewLineRule(ruleValue string, fileContent string) string {
ruleValueLowercase := strings.ToLower(ruleValue)
if ruleValueLowercase == "true" && !endsWithFinalNewLineRegexp.MatchString(fileContent) {
return fileContent + "\n"
}
if ruleValueLowercase == "false" {
for endsWithFinalNewLineRegexp.MatchString(fileContent) {
fileContent = endsWithFinalNewLineRegexp.ReplaceAllString(fileContent, "")
}
return fileContent
}
return fileContent
}

View File

@@ -22,3 +22,35 @@ func TestFixEndOfLineRule(t *testing.T) {
t.Error("Converting to CRLR did not work, got: " + GetErrorWithLineBreaksVisible(toCrlfResult))
}
}
func TestFixInsertFinalNewLineRule(t *testing.T) {
input1 := "a\nb\nc\n"
result1 := FixInsertFinalNewLineRule("true", input1)
if result1 != input1 {
t.Error("String was changed despite already having a line at the end")
}
input2 := "a\rb\rc\r\r"
result2 := FixInsertFinalNewLineRule("true", input2)
if result2 != input2 {
t.Error("String was changed despite already having a line at the end")
}
input3 := "a\r\nb\r\nc\r\n"
result3 := FixInsertFinalNewLineRule("true", input3)
if result3 != input3 {
t.Error("String was changed despite already having a line at the end")
}
input4 := "a\nb"
result4 := FixInsertFinalNewLineRule("true", input4)
if result4 != "a\nb\n" {
t.Error("Line was not added at the end")
}
input5 := "a\r\nb\r\nc\r\n\n\n\r"
result5 := FixInsertFinalNewLineRule("false", input5)
if result5 != "a\r\nb\r\nc" {
t.Error("Trailing lines were not removed")
}
}