Parcourir la source

fixing a location off by one edge case for --no-git (#812)

Zachary Rice il y a 3 ans
Parent
commit
ba9e089e35
2 fichiers modifiés avec 10 ajouts et 11 suppressions
  1. 8 8
      detect/detect_test.go
  2. 2 3
      detect/location.go

+ 8 - 8
detect/detect_test.go

@@ -84,8 +84,8 @@ func TestDetect(t *testing.T) {
 					File:        "tmp.go",
 					RuleID:      "pypi-upload-token",
 					Tags:        []string{"key", "pypi"},
-					StartLine:   0,
-					EndLine:     0,
+					StartLine:   1,
+					EndLine:     1,
 					StartColumn: 1,
 					EndColumn:   86,
 					Entropy:     1.9606875,
@@ -106,8 +106,8 @@ func TestDetect(t *testing.T) {
 					File:        "tmp.go",
 					RuleID:      "aws-access-key",
 					Tags:        []string{"key", "AWS"},
-					StartLine:   0,
-					EndLine:     0,
+					StartLine:   1,
+					EndLine:     1,
 					StartColumn: 15,
 					EndColumn:   34,
 					Entropy:     3.0841837,
@@ -154,8 +154,8 @@ func TestDetect(t *testing.T) {
 					RuleID:      "discord-api-key",
 					Tags:        []string{},
 					Entropy:     3.7906237,
-					StartLine:   0,
-					EndLine:     0,
+					StartLine:   1,
+					EndLine:     1,
 					StartColumn: 7,
 					EndColumn:   93,
 				},
@@ -184,8 +184,8 @@ func TestDetect(t *testing.T) {
 					RuleID:      "generic-api-key",
 					Tags:        []string{},
 					Entropy:     3.7906237,
-					StartLine:   0,
-					EndLine:     0,
+					StartLine:   1,
+					EndLine:     1,
 					StartColumn: 22,
 					EndColumn:   93,
 				},

+ 2 - 3
detect/location.go

@@ -22,7 +22,6 @@ func location(fragment Fragment, matchIndex []int) Location {
 	end := matchIndex[1]
 
 	for lineNum, pair := range fragment.newlineIndices {
-		_lineNum = lineNum
 		newLineByteIndex := pair[0]
 		if prevNewLine <= start && start < newLineByteIndex {
 			lineSet = true
@@ -46,8 +45,8 @@ func location(fragment Fragment, matchIndex []int) Location {
 		// a newline
 		location.startColumn = (start - prevNewLine) + 1 // +1 because counting starts at 1
 		location.endColumn = (end - prevNewLine)
-		location.startLine = _lineNum
-		location.endLine = _lineNum
+		location.startLine = _lineNum + 1
+		location.endLine = _lineNum + 1
 		location.startLineIndex = start
 
 		// search for new line byte index