diff --git a/.github/workflows/codeql-analysis.yml b/.github/workflows/codeql-analysis.yml index 8768c8d..264712a 100644 --- a/.github/workflows/codeql-analysis.yml +++ b/.github/workflows/codeql-analysis.yml @@ -15,20 +15,17 @@ jobs: security-events: write steps: - - name: Checkout repository + - name: checkout repository uses: actions/checkout@v4 with: fetch-depth: 1 # Initializes the CodeQL tools for scanning. - - name: Initialize CodeQL + - name: initialize codeql uses: github/codeql-action/init@v3 with: languages: go + build-mode: autobuild - # Autobuild attempts to build any compiled languages (C/C++, C#, or Java). - - name: Autobuild - uses: github/codeql-action/autobuild@v3 - - - name: Perform CodeQL Analysis + - name: perform codeql analysis uses: github/codeql-action/analyze@v3 diff --git a/.github/workflows/pr.yml b/.github/workflows/pr.yml index 412e886..01beb41 100644 --- a/.github/workflows/pr.yml +++ b/.github/workflows/pr.yml @@ -6,33 +6,52 @@ on: jobs: lint: - name: GolangCI Lint + name: golangci-lint runs-on: ubuntu-latest steps: - - name: Checkout repository + - name: checkout repository uses: actions/checkout@v4 with: fetch-depth: 1 - - name: Set up go - uses: actions/setup-go@v5 + - name: set up go + uses: WillAbides/setup-go-faster@v1 with: go-version-file: go.mod - - name: Run linter + - name: lint uses: golangci/golangci-lint-action@v6 with: version: latest + check: + name: go vet and staticcheck + runs-on: ubuntu-latest + steps: + - name: checkout repository + uses: actions/checkout@v4 + with: + fetch-depth: 1 + - name: set up go + uses: WillAbides/setup-go-faster@v1 + with: + go-version-file: go.mod + - name: run go vet + run: go vet ./... + - name: run staticcheck + uses: dominikh/staticcheck-action@v1 + with: + version: latest + tests: - name: Run unit tests with the race detector enabled + name: unit tests with race detector enabled runs-on: ubuntu-latest steps: - - name: Checkout repository + - name: checkout repository uses: actions/checkout@v4 with: fetch-depth: 1 - - name: Set up go - uses: actions/setup-go@v5 + - name: set up go + uses: WillAbides/setup-go-faster@v1 with: go-version-file: go.mod - - name: Run unit tests + - name: run unit tests run: go test -v -race ./... diff --git a/.golangci.yml b/.golangci.yml index 1712b7b..da1adb2 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -9,11 +9,14 @@ linters: - depguard # enforces that only deps on a whitelist can be used (meant for orgs, not small projects) - execinquery # deprecated - exhaustruct + - exportloopref # deprecated - forbidigo # we need to use fmt.Print*() - gomnd - - nolintlint - - nonamedreturns + - govet # we run the official go vet separately + - nolintlint # we do occasionally need to suppress linter false positives + - nonamedreturns # named returns often help readability - paralleltest # tests only take 2.5s to run. no need to parallelize + - staticcheck # dominickh doesn't recommend running staticcheck as part of golangci-lint, so we run Real Staticcheckâ„¢ separately - testpackage - varnamelen # makes bad suggestions - wsl diff --git a/args_test.go b/args_test.go index 0528b5c..24e42de 100644 --- a/args_test.go +++ b/args_test.go @@ -7,6 +7,7 @@ package q import ( "fmt" "go/ast" + "go/token" "testing" "github.com/kr/pretty" @@ -24,51 +25,48 @@ func TestExtractingArgsFromSourceText(t *testing.T) { }{ { id: 1, - arg: &ast.Ident{NamePos: 123, Obj: ast.NewObj(ast.Var, "myVar")}, + arg: &ast.Ident{Name: "myVar"}, want: "myVar", }, { id: 2, - arg: &ast.Ident{NamePos: 234, Obj: ast.NewObj(ast.Var, "awesomeVar")}, + arg: &ast.Ident{Name: "awesomeVar"}, want: "awesomeVar", }, { id: 3, - arg: &ast.Ident{NamePos: 456, Obj: ast.NewObj(ast.Bad, "myVar")}, - want: "", + arg: &ast.Ident{Name: "myVar"}, + want: "myVar", }, { id: 4, - arg: &ast.Ident{NamePos: 789, Obj: ast.NewObj(ast.Con, "myVar")}, + arg: &ast.Ident{Name: "myVar"}, want: "myVar", }, { id: 5, arg: &ast.BinaryExpr{ - X: &ast.BasicLit{ValuePos: 49, Kind: 5, Value: "1"}, - OpPos: 51, - Op: 12, - Y: &ast.BasicLit{ValuePos: 53, Kind: 5, Value: "2"}, + X: &ast.BasicLit{Kind: token.INT, Value: "1"}, + Op: token.ADD, + Y: &ast.BasicLit{Kind: token.INT, Value: "2"}, }, want: "1 + 2", }, { id: 6, arg: &ast.BinaryExpr{ - X: &ast.BasicLit{ValuePos: 89, Kind: 6, Value: "3.14"}, - OpPos: 94, - Op: 15, - Y: &ast.BasicLit{ValuePos: 96, Kind: 6, Value: "1.59"}, + X: &ast.BasicLit{Kind: token.FLOAT, Value: "3.14"}, + Op: token.QUO, + Y: &ast.BasicLit{Kind: token.FLOAT, Value: "1.59"}, }, want: "3.14 / 1.59", }, { id: 7, arg: &ast.BinaryExpr{ - X: &ast.BasicLit{ValuePos: 73, Kind: 5, Value: "123"}, - OpPos: 77, - Op: 14, - Y: &ast.BasicLit{ValuePos: 79, Kind: 5, Value: "234"}, + X: &ast.BasicLit{Kind: token.INT, Value: "123"}, + Op: token.MUL, + Y: &ast.BasicLit{Kind: token.INT, Value: "234"}, }, want: "123 * 234", }, @@ -76,65 +74,11 @@ func TestExtractingArgsFromSourceText(t *testing.T) { id: 8, arg: &ast.CallExpr{ Fun: &ast.Ident{ - NamePos: 30, - Name: "foo", - Obj: &ast.Object{ - Kind: 5, - Name: "foo", - Decl: &ast.FuncDecl{ - Doc: nil, - Recv: nil, - Name: &ast.Ident{ - NamePos: 44, - Name: "foo", - Obj: &ast.Object{}, - }, - Type: &ast.FuncType{ - Func: 39, - Params: &ast.FieldList{ - Opening: 47, - List: nil, - Closing: 48, - }, - Results: &ast.FieldList{ - Opening: 0, - List: []*ast.Field{ - { - Doc: nil, - Names: nil, - Type: &ast.Ident{ - NamePos: 50, - Name: "int", - Obj: nil, - }, - Tag: nil, - Comment: nil, - }, - }, - Closing: 0, - }, - }, - Body: &ast.BlockStmt{ - Lbrace: 54, - List: []ast.Stmt{ - &ast.ReturnStmt{ - Return: 57, - Results: []ast.Expr{ - &ast.BasicLit{ValuePos: 64, Kind: 5, Value: "123"}, - }, - }, - }, - Rbrace: 68, - }, - }, - Data: nil, - Type: nil, - }, + Name: "foo", }, - Lparen: 33, - Args: nil, - Ellipsis: 0, - Rparen: 34, + Lparen: token.NoPos, + Args: nil, + Rparen: token.NoPos, }, want: "foo()", }, @@ -142,49 +86,9 @@ func TestExtractingArgsFromSourceText(t *testing.T) { id: 9, arg: &ast.IndexExpr{ X: &ast.Ident{ - NamePos: 51, - Name: "a", - Obj: &ast.Object{ - Kind: 4, - Name: "a", - Decl: &ast.AssignStmt{ - Lhs: []ast.Expr{ - &ast.Ident{ - NamePos: 30, - Name: "a", - Obj: &ast.Object{}, - }, - }, - TokPos: 32, - Tok: 47, - Rhs: []ast.Expr{ - &ast.CompositeLit{ - Type: &ast.ArrayType{ - Lbrack: 35, - Len: nil, - Elt: &ast.Ident{ - NamePos: 37, - Name: "int", - Obj: nil, - }, - }, - Lbrace: 40, - Elts: []ast.Expr{ - &ast.BasicLit{ValuePos: 41, Kind: 5, Value: "1"}, - &ast.BasicLit{ValuePos: 44, Kind: 5, Value: "2"}, - &ast.BasicLit{ValuePos: 47, Kind: 5, Value: "3"}, - }, - Rbrace: 48, - }, - }, - }, - Data: nil, - Type: nil, - }, + Name: "a", }, - Lbrack: 52, - Index: &ast.BasicLit{ValuePos: 53, Kind: 5, Value: "1"}, - Rbrack: 54, + Index: &ast.BasicLit{Kind: token.INT, Value: "1"}, }, want: "a[1]", }, @@ -192,26 +96,20 @@ func TestExtractingArgsFromSourceText(t *testing.T) { id: 10, arg: &ast.KeyValueExpr{ Key: &ast.Ident{ - NamePos: 72, - Name: "Greeting", - Obj: nil, + Name: "Greeting", }, - Colon: 80, - Value: &ast.BasicLit{ValuePos: 82, Kind: 9, Value: "\"Hello\""}, + Value: &ast.BasicLit{Kind: token.STRING, Value: "\"Hello\""}, }, want: `Greeting: "Hello"`, }, { id: 11, arg: &ast.ParenExpr{ - Lparen: 35, X: &ast.BinaryExpr{ - X: &ast.BasicLit{ValuePos: 36, Kind: 5, Value: "2"}, - OpPos: 38, - Op: 14, - Y: &ast.BasicLit{ValuePos: 40, Kind: 5, Value: "3"}, + X: &ast.BasicLit{Kind: token.INT, Value: "2"}, + Op: token.MUL, + Y: &ast.BasicLit{Kind: token.INT, Value: "3"}, }, - Rparen: 41, }, want: "(2 * 3)", }, @@ -219,14 +117,10 @@ func TestExtractingArgsFromSourceText(t *testing.T) { id: 12, arg: &ast.SelectorExpr{ X: &ast.Ident{ - NamePos: 44, - Name: "fmt", - Obj: nil, + Name: "fmt", }, Sel: &ast.Ident{ - NamePos: 48, - Name: "Print", - Obj: nil, + Name: "Print", }, }, want: "fmt.Print", @@ -235,52 +129,12 @@ func TestExtractingArgsFromSourceText(t *testing.T) { id: 13, arg: &ast.SliceExpr{ X: &ast.Ident{ - NamePos: 51, - Name: "a", - Obj: &ast.Object{ - Kind: 4, - Name: "a", - Decl: &ast.AssignStmt{ - Lhs: []ast.Expr{ - &ast.Ident{ - NamePos: 30, - Name: "a", - Obj: &ast.Object{}, - }, - }, - TokPos: 32, - Tok: 47, - Rhs: []ast.Expr{ - &ast.CompositeLit{ - Type: &ast.ArrayType{ - Lbrack: 35, - Len: nil, - Elt: &ast.Ident{ - NamePos: 37, - Name: "int", - Obj: (*ast.Object)(nil), - }, - }, - Lbrace: 40, - Elts: []ast.Expr{ - &ast.BasicLit{ValuePos: 41, Kind: 5, Value: "1"}, - &ast.BasicLit{ValuePos: 44, Kind: 5, Value: "2"}, - &ast.BasicLit{ValuePos: 47, Kind: 5, Value: "3"}, - }, - Rbrace: 48, - }, - }, - }, - Data: nil, - Type: nil, - }, + Name: "a", }, - Lbrack: 52, - Low: &ast.BasicLit{ValuePos: 53, Kind: 5, Value: "0"}, - High: &ast.BasicLit{ValuePos: 55, Kind: 5, Value: "2"}, + Low: &ast.BasicLit{Kind: token.INT, Value: "0"}, + High: &ast.BasicLit{Kind: token.INT, Value: "2"}, Max: nil, Slice3: false, - Rbrack: 56, }, want: "a[0:2]", }, @@ -288,61 +142,26 @@ func TestExtractingArgsFromSourceText(t *testing.T) { id: 14, arg: &ast.TypeAssertExpr{ X: &ast.Ident{ - NamePos: 62, - Name: "a", - Obj: &ast.Object{ - Kind: 4, - Name: "a", - Decl: &ast.ValueSpec{ - Doc: nil, - Names: []*ast.Ident{ - { - NamePos: 34, - Name: "a", - Obj: &ast.Object{}, - }, - }, - Type: &ast.InterfaceType{ - Interface: 36, - Methods: &ast.FieldList{ - Opening: 45, - List: nil, - Closing: 46, - }, - Incomplete: false, - }, - Values: nil, - Comment: nil, - }, - Data: int(0), - Type: nil, - }, + Name: "a", }, - Lparen: 64, Type: &ast.Ident{ - NamePos: 65, - Name: "string", - Obj: nil, + Name: "string", }, - Rparen: 71, }, want: "a.(string)", }, { id: 15, arg: &ast.UnaryExpr{ - OpPos: 35, - Op: 13, - X: &ast.BasicLit{ValuePos: 36, Kind: 5, Value: "1"}, + Op: token.SUB, + X: &ast.BasicLit{Kind: token.INT, Value: "1"}, }, want: "-1", }, { id: 16, arg: &ast.Ident{ - NamePos: 65, - Name: "string", - Obj: nil, + Name: "string", }, want: "string", },