Skip to content

Commit

Permalink
Merge pull request #23 from erizocosmico/fix/get-first-line
Browse files Browse the repository at this point in the history
data: fix getting the first line for empty content
  • Loading branch information
mcuadros committed May 28, 2020
2 parents e1f1b57 + 79398a9 commit 2880cca
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
16 changes: 14 additions & 2 deletions data/generated.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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() {" &&
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 4 additions & 0 deletions data/generated_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ import (
"github.com/stretchr/testify/require"
)

func TestGetFirstLineEmptyContent(t *testing.T) {
require.Nil(t, getFirstLine(nil))
}

func TestForEachLine(t *testing.T) {
const sample = "foo\nbar\nboomboom\nbleepbloop\n"
var lines = strings.Split(sample, "\n")
Expand Down

0 comments on commit 2880cca

Please sign in to comment.