-
Notifications
You must be signed in to change notification settings - Fork 286
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(receiver-naming): distinguish types with parameters (#692)
* fix(receiver-naming): distinguish types with parameters * chore: run tests using supported Go versions matrix
- Loading branch information
Showing
13 changed files
with
153 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
name: Lint | ||
on: | ||
workflow_dispatch: | ||
pull_request: | ||
types: [opened, edited, synchronize, reopened] | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
name: Test | ||
on: | ||
workflow_dispatch: | ||
pull_request: | ||
types: [opened, edited, synchronize, reopened] | ||
|
||
jobs: | ||
test: | ||
name: Test | ||
runs-on: ubuntu-latest | ||
strategy: | ||
fail-fast: false | ||
matrix: | ||
go-version: | ||
- 1.16.x | ||
- 1.17.x | ||
- 1.18.x | ||
steps: | ||
- name: Checkout code | ||
uses: actions/checkout@v3.0.2 | ||
- name: Set up Go | ||
uses: actions/setup-go@v3.2.0 | ||
with: | ||
go-version: ${{ matrix.go-version }} | ||
cache: true | ||
cache-dependency-path: '**/go.sum' | ||
- name: Run tests | ||
run: go test -race ./... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// Package typeparams provides utilities for working with Go ASTs with support | ||
// for type parameters when built with Go 1.18 and higher. | ||
package typeparams | ||
|
||
import ( | ||
"go/ast" | ||
) | ||
|
||
// Enabled reports whether type parameters are enabled in the current build | ||
// environment. | ||
func Enabled() bool { | ||
return enabled | ||
} | ||
|
||
// ReceiverType returns the named type of the method receiver, sans "*" and type | ||
// parameters, or "invalid-type" if fn.Recv is ill formed. | ||
func ReceiverType(fn *ast.FuncDecl) string { | ||
e := fn.Recv.List[0].Type | ||
if s, ok := e.(*ast.StarExpr); ok { | ||
e = s.X | ||
} | ||
if enabled { | ||
e = unpackIndexExpr(e) | ||
} | ||
if id, ok := e.(*ast.Ident); ok { | ||
return id.Name | ||
} | ||
return "invalid-type" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
//go:build !go1.18 | ||
// +build !go1.18 | ||
|
||
package typeparams | ||
|
||
import ( | ||
"go/ast" | ||
) | ||
|
||
const enabled = false | ||
|
||
func unpackIndexExpr(e ast.Expr) ast.Expr { return e } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
//go:build go1.18 | ||
// +build go1.18 | ||
|
||
package typeparams | ||
|
||
import ( | ||
"go/ast" | ||
) | ||
|
||
const enabled = true | ||
|
||
func unpackIndexExpr(e ast.Expr) ast.Expr { | ||
switch e := e.(type) { | ||
case *ast.IndexExpr: | ||
return e.X | ||
case *ast.IndexListExpr: | ||
return e.X | ||
} | ||
return e | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/mgechev/revive/internal/typeparams" | ||
"github.com/mgechev/revive/rule" | ||
) | ||
|
||
func TestReceiverNamingTypeParams(t *testing.T) { | ||
if !typeparams.Enabled() { | ||
t.Skip("type parameters are not enabled in the current build environment") | ||
} | ||
testRule(t, "receiver-naming-issue-669", &rule.ReceiverNamingRule{}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package fixtures | ||
|
||
type gen1[T any] struct{} | ||
|
||
func (g gen1[T]) f1() {} | ||
|
||
func (g gen1[U]) f2() {} | ||
|
||
func (n gen1[T]) f3() {} // MATCH /receiver name n should be consistent with previous receiver name g for gen1/ | ||
|
||
func (n gen1[U]) f4() {} // MATCH /receiver name n should be consistent with previous receiver name g for gen1/ | ||
|
||
func (n gen1[V]) f5() {} // MATCH /receiver name n should be consistent with previous receiver name g for gen1/ | ||
|
||
func (n *gen1[T]) f6() {} // MATCH /receiver name n should be consistent with previous receiver name g for gen1/ | ||
|
||
func (n *gen1[U]) f7() {} // MATCH /receiver name n should be consistent with previous receiver name g for gen1/ | ||
|
||
func (n *gen1[V]) f8() {} // MATCH /receiver name n should be consistent with previous receiver name g for gen1/ | ||
|
||
type gen2[T1, T2 any] struct{} | ||
|
||
func (g gen2[T1, T2]) f1() {} | ||
|
||
func (g gen2[U1, U2]) f2() {} | ||
|
||
func (n gen2[T1, T2]) f3() {} // MATCH /receiver name n should be consistent with previous receiver name g for gen2/ | ||
|
||
func (n gen2[U1, U2]) f4() {} // MATCH /receiver name n should be consistent with previous receiver name g for gen2/ | ||
|
||
func (n gen2[V1, V2]) f5() {} // MATCH /receiver name n should be consistent with previous receiver name g for gen2/ | ||
|
||
func (n *gen2[T1, T2]) f6() {} // MATCH /receiver name n should be consistent with previous receiver name g for gen2/ | ||
|
||
func (n *gen2[U1, U2]) f7() {} // MATCH /receiver name n should be consistent with previous receiver name g for gen2/ | ||
|
||
func (n *gen2[V1, V2]) f8() {} // MATCH /receiver name n should be consistent with previous receiver name g for gen2/ |