Skip to content

Commit

Permalink
Rename to gocheckcompilerdirectives (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
leighmcculloch authored Jan 14, 2023
1 parent 1392fb2 commit ff499fb
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 52 deletions.
2 changes: 1 addition & 1 deletion .goreleaser.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
builds:
- main: ./cmd/gocheckdirectives
- main: ./cmd/gocheckcompilerdirectives
targets: [go_first_class]
flags: [-trimpath]
mod_timestamp: '{{.CommitTimestamp}}'
Expand Down
22 changes: 12 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
# gocheckdirectives
# gocheckcompilerdirectives

Check that go directives (`//go:` comments) are valid and catch easy mistakes.
Check that go compiler directives (`//go:` comments) are valid and catch easy
mistakes.

For example, directives like `//go:generate`, `//go:embed`, `//go:build`, etc.
Compiler directives are comments like `//go:generate`, `//go:embed`,
`//go:build`, etc.

## Why

Go directives are comments in the form of `//go:` that provide an instruction
to the compiler.
Go compiler directives are comments in the form of `//go:` that provide an
instruction to the compiler.

Go directives are easy to make mistakes with. The linter will detect the
following mistakes.
The directives are easy to make mistakes with. The linter will detect the
following mistakes:

1. Adding a space in between the comment bars and the first character, e.g. `//
go:`, will cause the compiler to silently ignore the comment.
Expand All @@ -20,15 +22,15 @@ go:`, will cause the compiler to silently ignore the comment.
## Install

```
go install github.com/leighmcculloch/gocheckdirectives@latest
go install 4d63.com/gocheckcompilerdirectives@latest
```

## Usage

```
gocheckdirectives [package]
gocheckcompilerdirectives [package]
```

```
gocheckdirectives ./...
gocheckcompilerdirectives ./...
```
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package checkdirectives
package checkcompilerdirectives

import (
"strings"
Expand All @@ -8,8 +8,8 @@ import (

func Analyzer() *analysis.Analyzer {
return &analysis.Analyzer{
Name: "gocheckdirectives",
Doc: "Checks that go directive commenst (//go:) are valid.",
Name: "gocheckcompilerdirectives",
Doc: "Checks that go compiler directive comments (//go:) are valid.",
Run: run,
}
}
Expand Down Expand Up @@ -49,13 +49,13 @@ func run(pass *analysis.Pass) (interface{}, error) {
// by the compiler with no error, causing it not to work. This
// is an easy mistake.
if spaces > 0 {
pass.ReportRangef(comment, "go directive contains leading space: %s", prefix)
pass.ReportRangef(comment, "compiler directive contains space: %s", prefix)
}
// If the directive is unknown it will be ignored by the
// compiler with no error. This is an easy mistake to make,
// especially if you typo a directive.
if !isKnown(directive) {
pass.ReportRangef(comment, "unrecognized go directive: %s", prefix)
pass.ReportRangef(comment, "compiler directive unrecognized: %s", prefix)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
package checkdirectives_test
package checkcompilerdirectives_test

import (
"go/ast"
"testing"

"github.com/leighmcculloch/gocheckdirectives/checkdirectives"
"4d63.com/gocheckcompilerdirectives/checkcompilerdirectives"
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/analysistest"
)

func TestRun(t *testing.T) {
testdata := analysistest.TestData()
analyzer := checkdirectives.Analyzer()
analyzer := checkcompilerdirectives.Analyzer()
analysistest.Run(t, testdata, analyzer)
}

func FuzzRun(f *testing.F) {
analyzer := checkdirectives.Analyzer()
analyzer := checkcompilerdirectives.Analyzer()
f.Add("hello world")
f.Add("go:generate echo hello world")
f.Add("go:embed")
Expand All @@ -40,7 +40,7 @@ func FuzzRun(f *testing.F) {
}

func BenchmarkRun(b *testing.B) {
analyzer := checkdirectives.Analyzer()
analyzer := checkcompilerdirectives.Analyzer()
pass := analysis.Pass{
Report: func(d analysis.Diagnostic) {},
Files: []*ast.File{
Expand Down
20 changes: 20 additions & 0 deletions checkcompilerdirectives/testdata/code.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package code

import _ "embed"

// Okay cases:

//go:generate echo hello world

//go:embed
var Value string

// go:

// Problematic cases:

// go:embed // want "compiler directive contains space: // go:embed"

// go:embed // want "compiler directive contains space: // go:embed"

//go:genrate // want "compiler directive unrecognized: //go:genrate"
20 changes: 0 additions & 20 deletions checkdirectives/testdata/code.go

This file was deleted.

10 changes: 10 additions & 0 deletions cmd/gocheckcompilerdirectives/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package main

import (
"4d63.com/gocheckcompilerdirectives/checkcompilerdirectives"
"golang.org/x/tools/go/analysis/singlechecker"
)

func main() {
singlechecker.Main(checkcompilerdirectives.Analyzer())
}
10 changes: 0 additions & 10 deletions cmd/gocheckdirectives/main.go

This file was deleted.

2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/leighmcculloch/gocheckdirectives
module 4d63.com/gocheckcompilerdirectives

go 1.19

Expand Down

0 comments on commit ff499fb

Please sign in to comment.