Skip to content

Commit

Permalink
Merge pull request #805 from andrey1s/golangci
Browse files Browse the repository at this point in the history
enable-all linters on golangci-lint
  • Loading branch information
vektah authored Aug 7, 2019
2 parents bcddd7a + 504a96b commit 410d832
Show file tree
Hide file tree
Showing 26 changed files with 127 additions and 90 deletions.
2 changes: 1 addition & 1 deletion .circleci/check-linting
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

set -euo pipefail

curl -sL --fail https://github.com/golangci/golangci-lint/releases/download/v1.13/golangci-lint-1.13-linux-amd64.tar.gz | tar zxv --strip-components=1 --dir=/go/bin
curl -sL --fail https://github.com/golangci/golangci-lint/releases/download/v1.17.1/golangci-lint-1.17.1-linux-amd64.tar.gz | tar zxv --strip-components=1 --dir=/go/bin

golangci-lint run
40 changes: 40 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,43 @@
run:
tests: true
skip-dirs:
- bin

linters-settings:
errcheck:
ignore: fmt:.*,[rR]ead|[wW]rite|[cC]lose,io:Copy

linters:
disable-all: true
enable:
- bodyclose
- deadcode
- depguard
- dupl
- errcheck
- gocritic
- gofmt
- goimports
- gosec
- gosimple
- govet
- ineffassign
- interfacer
- misspell
- nakedret
- prealloc
- staticcheck
- structcheck
- stylecheck
- typecheck
- unconvert
- unparam
- unused
- varcheck

issues:
exclude-rules:
# Exclude some linters from running on tests files.
- path: _test\.go
linters:
- dupl
14 changes: 7 additions & 7 deletions codegen/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
"github.com/pkg/errors"
"github.com/vektah/gqlparser"
"github.com/vektah/gqlparser/ast"
yaml "gopkg.in/yaml.v2"
"gopkg.in/yaml.v2"
)

type Config struct {
Expand Down Expand Up @@ -310,10 +310,10 @@ func (tm TypeMap) ReferencedPackages() []string {
return pkgs
}

func (tm TypeMap) Add(Name string, goType string) {
modelCfg := tm[Name]
func (tm TypeMap) Add(name string, goType string) {
modelCfg := tm[name]
modelCfg.Model = append(modelCfg.Model, goType)
tm[Name] = modelCfg
tm[name] = modelCfg
}

type DirectiveConfig struct {
Expand Down Expand Up @@ -459,9 +459,9 @@ func (c *Config) InjectBuiltins(s *ast.Schema) {
func (c *Config) LoadSchema() (*ast.Schema, map[string]string, error) {
schemaStrings := map[string]string{}

var sources []*ast.Source
sources := make([]*ast.Source, len(c.SchemaFilename))

for _, filename := range c.SchemaFilename {
for i, filename := range c.SchemaFilename {
filename = filepath.ToSlash(filename)
var err error
var schemaRaw []byte
Expand All @@ -471,7 +471,7 @@ func (c *Config) LoadSchema() (*ast.Schema, map[string]string, error) {
os.Exit(1)
}
schemaStrings[filename] = string(schemaRaw)
sources = append(sources, &ast.Source{Name: filename, Input: schemaStrings[filename]})
sources[i] = &ast.Source{Name: filename, Input: schemaStrings[filename]}
}

schema, err := gqlparser.LoadSchema(sources...)
Expand Down
5 changes: 1 addition & 4 deletions codegen/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,7 @@ func BuildData(cfg *config.Config) (*Data, error) {
return nil, err
}

s.ReferencedTypes, err = b.buildTypes()
if err != nil {
return nil, err
}
s.ReferencedTypes = b.buildTypes()

sort.Slice(s.Objects, func(i, j int) bool {
return s.Objects[i].Definition.Name < s.Objects[j].Definition.Name
Expand Down
14 changes: 6 additions & 8 deletions codegen/field.go
Original file line number Diff line number Diff line change
Expand Up @@ -384,27 +384,25 @@ func (f *Field) ComplexitySignature() string {
}

func (f *Field) ComplexityArgs() string {
var args []string
for _, arg := range f.Args {
args = append(args, "args["+strconv.Quote(arg.Name)+"].("+templates.CurrentImports.LookupType(arg.TypeReference.GO)+")")
args := make([]string, len(f.Args))
for i, arg := range f.Args {
args[i] = "args[" + strconv.Quote(arg.Name) + "].(" + templates.CurrentImports.LookupType(arg.TypeReference.GO) + ")"
}

return strings.Join(args, ", ")
}

func (f *Field) CallArgs() string {
var args []string
args := make([]string, 0, len(f.Args)+2)

if f.IsResolver {
args = append(args, "rctx")

if !f.Object.Root {
args = append(args, "obj")
}
} else {
if f.MethodHasContext {
args = append(args, "ctx")
}
} else if f.MethodHasContext {
args = append(args, "ctx")
}

for _, arg := range f.Args {
Expand Down
4 changes: 2 additions & 2 deletions codegen/field_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ type Embed struct {
Test string
}
`
scope, err := parseScope(t, input, "test")
scope, err := parseScope(input, "test")
require.NoError(t, err)

std := scope.Lookup("Std").Type().Underlying().(*types.Struct)
Expand Down Expand Up @@ -76,7 +76,7 @@ type Embed struct {
}
}

func parseScope(t *testing.T, input interface{}, packageName string) (*types.Scope, error) {
func parseScope(input interface{}, packageName string) (*types.Scope, error) {
// test setup to parse the types
fset := token.NewFileSet()
f, err := parser.ParseFile(fset, "test.go", input, 0)
Expand Down
3 changes: 1 addition & 2 deletions codegen/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,7 @@ func (o *Object) HasUnmarshal() bool {
return true
}
for i := 0; i < o.Type.(*types.Named).NumMethods(); i++ {
switch o.Type.(*types.Named).Method(i).Name() {
case "UnmarshalGQL":
if o.Type.(*types.Named).Method(i).Name() == "UnmarshalGQL" {
return true
}
}
Expand Down
3 changes: 2 additions & 1 deletion codegen/templates/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"go/types"
"strconv"
"strings"

"github.com/99designs/gqlgen/internal/code"
)
Expand All @@ -20,7 +21,7 @@ type Imports struct {
}

func (i *Import) String() string {
if i.Alias == i.Name {
if strings.HasSuffix(i.Path, i.Alias) {
return strconv.Quote(i.Path)
}

Expand Down
2 changes: 1 addition & 1 deletion codegen/templates/import_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func TestImports(t *testing.T) {
t,
`"github.com/99designs/gqlgen/codegen/templates/testdata/a/bar"
bar1 "github.com/99designs/gqlgen/codegen/templates/testdata/b/bar"
"github.com/99designs/gqlgen/codegen/templates/testdata/pkg_mismatch"`,
turtles "github.com/99designs/gqlgen/codegen/templates/testdata/pkg_mismatch"`,
a.String(),
)
})
Expand Down
14 changes: 8 additions & 6 deletions codegen/templates/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,8 @@ func ToGoPrivate(name string) string {
first := true
wordWalker(name, func(info *wordInfo) {
word := info.Word
if first {
switch {
case first:
if strings.ToUpper(word) == word || strings.ToLower(word) == word {
// ID → id, CAMEL → camel
word = strings.ToLower(info.Word)
Expand All @@ -294,9 +295,9 @@ func ToGoPrivate(name string) string {
word = lcFirst(info.Word)
}
first = false
} else if info.MatchCommonInitial {
case info.MatchCommonInitial:
word = strings.ToUpper(word)
} else if !info.HasCommonInitial {
case !info.HasCommonInitial:
word = ucFirst(strings.ToLower(word))
}
runes = append(runes, []rune(word)...)
Expand All @@ -319,9 +320,10 @@ func wordWalker(str string, f func(*wordInfo)) {
hasCommonInitial := false
for i+1 <= len(runes) {
eow := false // whether we hit the end of a word
if i+1 == len(runes) {
switch {
case i+1 == len(runes):
eow = true
} else if isDelimiter(runes[i+1]) {
case isDelimiter(runes[i+1]):
// underscore; shift the remainder forward over any run of underscores
eow = true
n := 1
Expand All @@ -336,7 +338,7 @@ func wordWalker(str string, f func(*wordInfo)) {

copy(runes[i+1:], runes[i+n+1:])
runes = runes[:len(runes)-n]
} else if unicode.IsLower(runes[i]) && !unicode.IsLower(runes[i+1]) {
case unicode.IsLower(runes[i]) && !unicode.IsLower(runes[i+1]):
// lower->non-lower
eow = true
}
Expand Down
2 changes: 1 addition & 1 deletion codegen/testserver/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion codegen/testserver/middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func TestMiddleware(t *testing.T) {

err := c.Post(`query { user(id: 1) { id, friends { id } } }`, &resp)

// First resovles user which is a method
// First resolves user which is a method
// Next resolves id which is not a method
// Finally resolves friends which is a method
assert.Equal(t, []bool{true, false, true}, areMethods)
Expand Down
2 changes: 1 addition & 1 deletion codegen/testserver/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"

introspection1 "github.com/99designs/gqlgen/codegen/testserver/introspection"
"github.com/99designs/gqlgen/codegen/testserver/invalid-packagename"
invalid_packagename "github.com/99designs/gqlgen/codegen/testserver/invalid-packagename"
)

// THIS CODE IS A STARTING POINT ONLY. IT WILL NOT BE UPDATED WITH SCHEMA CHANGES.
Expand Down
2 changes: 1 addition & 1 deletion codegen/testserver/stub.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions codegen/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"github.com/99designs/gqlgen/codegen/config"
)

func (b *builder) buildTypes() (map[string]*config.TypeReference, error) {
func (b *builder) buildTypes() map[string]*config.TypeReference {
ret := map[string]*config.TypeReference{}

for _, ref := range b.Binder.References {
Expand All @@ -14,5 +14,5 @@ func (b *builder) buildTypes() (map[string]*config.TypeReference, error) {
ref = ref.Elem()
}
}
return ret, nil
return ret
}
Loading

0 comments on commit 410d832

Please sign in to comment.