data: fix getting the first line for empty content

Signed-off-by: Miguel Molina <miguel@erizocosmi.co>
This commit is contained in:
Miguel Molina
2020-05-28 11:22:58 +02:00
parent e1f1b57a84
commit 79398a925d
2 changed files with 18 additions and 2 deletions

View File

@ -188,7 +188,11 @@ func isSourceMap(path, _ string, content []byte) bool {
return true
}
firstLine := getLines(content, 1)[0]
firstLine := getFirstLine(content)
if len(firstLine) == 0 {
return false
}
for _, r := range sourceMapRegexps {
if r.Match(firstLine) {
return true
@ -203,7 +207,7 @@ func isCompiledCoffeeScript(path, ext string, content []byte) bool {
return false
}
firstLine := getLines(content, 1)[0]
firstLine := getFirstLine(content)
lastLines := getLines(content, -2)
if string(firstLine) == "(function() {" &&
@ -753,6 +757,14 @@ func isGeneratedJooq(_, ext string, content []byte) bool {
return false
}
func getFirstLine(content []byte) []byte {
lines := getLines(content, 1)
if len(lines) > 0 {
return lines[0]
}
return nil
}
// getLines returns up to the first n lines. A negative index will return up to
// the last n lines in reverse order.
func getLines(content []byte, n int) [][]byte {