Skip to content

Commit

Permalink
Fix option to enable type analysis (#38)
Browse files Browse the repository at this point in the history
Option wasn't being passed from cli

Add some end-to-end tests to make sure this option works as expected.
  • Loading branch information
ashanbrown authored Jul 3, 2023
1 parent c506b28 commit f6adf99
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 2 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ setup:

test:
cd examples && diff <(sed 's|CURDIR|$(CURDIR)|' expected_results.txt) <(go run .. 2>&1 | sed '/^go: downloading/d')
cd examples && diff <(sed 's|CURDIR|$(CURDIR)|' expected_analyze_types_results.txt) <(go run .. -analyze_types '{p: "^sql\\.DB\\.Exec$$"}' -- 2>&1 | sed '/^go: downloading/d')

lint:
pre-commit run --all-files
Expand Down
10 changes: 10 additions & 0 deletions examples/analyze_types_example.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package examples

import (
"database/sql"
)

func AnaylzeTypesExample() {
var db *sql.DB
db.Exec("SELECT * FROM users")
}
1 change: 1 addition & 0 deletions examples/expected_analyze_types_results.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
use of `db.Exec` forbidden by pattern `^sql\.DB\.Exec$` at CURDIR/examples/analyze_types_example.go:9:2
5 changes: 3 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func main() {
setExitStatus := flag.Bool("set_exit_status", false, "Set exit status to 1 if any issues are found")
includeTests := flag.Bool("tests", true, "Include tests")
excludeGodocExamples := flag.Bool("exclude_godoc_examples", true, "Exclude code in godoc examples")
expand := flag.Bool("analyze_types", false, "Replace the literal source code based on the semantic of the code before matching against patterns")
analyzeTypes := flag.Bool("analyze_types", false, "Replace the literal source code based on the semantic of the code before matching against patterns")
flag.Parse()

var patterns = []string(nil)
Expand All @@ -35,6 +35,7 @@ func main() {
}
options := []forbidigo.Option{
forbidigo.OptionExcludeGodocExamples(*excludeGodocExamples),
forbidigo.OptionAnalyzeTypes(*analyzeTypes),
}
linter, err := forbidigo.NewLinter(patterns, options...)
if err != nil {
Expand All @@ -46,7 +47,7 @@ func main() {
Tests: *includeTests,
}

if *expand {
if *analyzeTypes {
cfg.Mode |= packages.NeedTypesInfo | packages.NeedDeps
}

Expand Down

0 comments on commit f6adf99

Please sign in to comment.