Skip to content

Commit

Permalink
Fix issue collapse when previous line has code (#19)
Browse files Browse the repository at this point in the history
* Fix issue collapse when previous line has code

Signed-off-by: Akshit Garg <[email protected]>

* Apply suggestions from review

Signed-off-by: Akshit Garg <[email protected]>

---------

Signed-off-by: Akshit Garg <[email protected]>
  • Loading branch information
akshit-deepsource authored Feb 24, 2023
1 parent 2a9c9b8 commit b622fdc
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 3 deletions.
23 changes: 20 additions & 3 deletions pragma/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,11 @@ func readLine(reader *bufio.Reader) (string, error) {
func (f *File) extractPragmas() {
reader := bufio.NewReader(strings.NewReader(f.Content))

currentLine := 0
currentLineNum := 0
previousLine := ""
var previousPragmaWithCode *Pragma
for {
currentLine++
currentLineNum++

line, err := readLine(reader)
if err != nil {
Expand All @@ -61,10 +62,11 @@ func (f *File) extractPragmas() {
break
}

previousLine = line
line = strings.TrimSpace(line)

var pragma *Pragma
lineNum := currentLine
lineNum := currentLineNum
for _, prefix := range f.CommentPrefix {
split := strings.Split(line, prefix)
if len(split) < 2 {
Expand Down Expand Up @@ -108,8 +110,23 @@ func (f *File) extractPragmas() {
continue
}

// Previous line also has code
if !hasPrefixes(previousLine, f.CommentPrefix) {
continue
}

pragma.merge(previousPragma)
delete(f.Pragmas, lineNum-1)
}
}
}

func hasPrefixes(line string, prefixes []string) bool {
for _, prefix := range prefixes {
if strings.HasPrefix(line, prefix) {
return true
}
}

return false
}
34 changes: 34 additions & 0 deletions pragma/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,40 @@ fmt.Println("Hello") // [GO-W1003]: 30
},
},
},
{
name: "issue collapse bug - cxx",
args: args{
content: `issueCaseNotRaised(00); // [CXX-S111]
issueCaseRaised(010); // [CXX-S112]`,
commentPrefix: []string{"//"},
},
want: map[int]*Pragma{
1: {
Issues: map[string][]*Issue{"CXX-S111": {}},
Hit: map[string]bool{"CXX-S111": false},
},
2: {
Issues: map[string][]*Issue{"CXX-S112": {}},
Hit: map[string]bool{"CXX-S112": false},
},
},
},
{
name: "issue collapse - blank line",
args: args{
content: `code();
// [CXX-S112]
issueCaseRaised(010);`,
commentPrefix: []string{"//"},
},
want: map[int]*Pragma{
4: {
Issues: map[string][]*Issue{"CXX-S112": {}},
Hit: map[string]bool{"CXX-S112": false},
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down

0 comments on commit b622fdc

Please sign in to comment.