Skip to content
This repository has been archived by the owner on Sep 21, 2021. It is now read-only.

Commit

Permalink
Cleanup ignore checking code
Browse files Browse the repository at this point in the history
  • Loading branch information
codyl-stripe committed Sep 10, 2019
1 parent 75db554 commit 200faa8
Showing 1 changed file with 16 additions and 27 deletions.
43 changes: 16 additions & 27 deletions safesql.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
package main

import (
"bufio"
"flag"
"fmt"
"go/build"
"go/token"
"go/types"
"io/ioutil"
"os"
"sort"

Expand All @@ -23,7 +23,7 @@ import (
"golang.org/x/tools/go/ssa/ssautil"
)

const IgnoreComment = "nolint:safesql"
const IgnoreComment = "//nolint:safesql"

type sqlPackage struct {
packageName string
Expand Down Expand Up @@ -200,46 +200,35 @@ func CheckIssues(lines []token.Position) ([]Issue, error) {
// ensure we have the lines in ascending order
sort.Slice(linesInFile, func(i, j int) bool { return linesInFile[i].Line < linesInFile[j].Line })

f, err := os.Open(file)
data, err := ioutil.ReadFile(file)
if err != nil {
return nil, err
}
defer f.Close()
s := bufio.NewScanner(f)
fileLines := strings.Split(string(data), "\n")

currentLine := 0
for _, line := range linesInFile {
// check the line before the problematic statement first
potentialCommentLine := line.Line - 1

// if there are 2 statements back to back that are ignored then we don't want to check the previous so skip
// ie
// db.Query(query, args) //IsSqlSafe
// db.Query(query2, args2)
if currentLine != potentialCommentLine {
for ; currentLine < potentialCommentLine; currentLine++ {
if !s.Scan() {
return nil, s.Err()
}
}
if HasIgnoreComment(s.Text()) {
issues = append(issues, Issue{statement: line, ignored: true})
continue
}
}
potentialCommentLine := line.Line - 2

// check the line of the statement
if !s.Scan() {
return nil, s.Err()
// check only if the previous line is strictly a line that begins with
// the ignore comment
if 0 <= potentialCommentLine && BeginsWithComment(fileLines[potentialCommentLine]) {
issues = append(issues, Issue{statement: line, ignored: true})
continue
}
isIgnored := HasIgnoreComment(s.Text())

isIgnored := HasIgnoreComment(fileLines[line.Line-1])
issues = append(issues, Issue{statement: line, ignored: isIgnored})
}
}

return issues, nil
}

func BeginsWithComment(line string) bool {
return strings.HasPrefix(strings.TrimSpace(line), IgnoreComment)
}

func HasIgnoreComment(line string) bool {
return strings.HasSuffix(strings.TrimSpace(line), IgnoreComment)
}
Expand Down

0 comments on commit 200faa8

Please sign in to comment.