stash/vendor/github.com/99designs/gqlgen/internal/rewrite/rewriter.go
SmallCoccinelle 45f700d6ea
Support Go 1.18: Upgrade gqlgen to v0.17.2 (#2443)
* Upgrade gqlgen to v0.17.2

This enables builds on Go 1.18. github.com/vektah/gqlparser is upgraded
to the newest version too.

Getting this to work is a bit of a hazzle. I had to first remove
vendoring from the repository, perform the upgrade and then re-introduce
the vendor directory. I think gqlgens analysis went wrong for some
reason on the upgrade. It would seem a clean-room installation fixed it.

* Bump project to 1.18

* Update all packages, address gqlgenc breaking changes

* Let `go mod tidy` handle the go.mod file

* Upgrade linter to 1.45.2

* Introduce v1.45.2 of the linter

The linter now correctly warns on `strings.Title` because it isn't
unicode-aware. Fix this by using the suggested fix from x/text/cases
to produce unicode-aware strings.

The mapping isn't entirely 1-1 as this new approach has a larger iface:
it spans all of unicode rather than just ASCII. It coincides for ASCII
however, so things should be largely the same.

* Ready ourselves for errchkjson and contextcheck.

* Revert dockerfile golang version changes for now

Co-authored-by: Kermie <kermie@isinthe.house>
Co-authored-by: WithoutPants <53250216+WithoutPants@users.noreply.github.com>
2022-04-02 18:08:14 +11:00

195 lines
3.6 KiB
Go

package rewrite
import (
"bytes"
"fmt"
"go/ast"
"go/token"
"io/ioutil"
"path/filepath"
"strconv"
"strings"
"github.com/99designs/gqlgen/internal/code"
"golang.org/x/tools/go/packages"
)
type Rewriter struct {
pkg *packages.Package
files map[string]string
copied map[ast.Decl]bool
}
func New(dir string) (*Rewriter, error) {
importPath := code.ImportPathForDir(dir)
if importPath == "" {
return nil, fmt.Errorf("import path not found for directory: %q", dir)
}
pkgs, err := packages.Load(&packages.Config{
Mode: packages.NeedSyntax | packages.NeedTypes,
}, importPath)
if err != nil {
return nil, err
}
if len(pkgs) == 0 {
return nil, fmt.Errorf("package not found for importPath: %s", importPath)
}
return &Rewriter{
pkg: pkgs[0],
files: map[string]string{},
copied: map[ast.Decl]bool{},
}, nil
}
func (r *Rewriter) getSource(start, end token.Pos) string {
startPos := r.pkg.Fset.Position(start)
endPos := r.pkg.Fset.Position(end)
if startPos.Filename != endPos.Filename {
panic("cant get source spanning multiple files")
}
file := r.getFile(startPos.Filename)
return file[startPos.Offset:endPos.Offset]
}
func (r *Rewriter) getFile(filename string) string {
if _, ok := r.files[filename]; !ok {
b, err := ioutil.ReadFile(filename)
if err != nil {
panic(fmt.Errorf("unable to load file, already exists: %w", err))
}
r.files[filename] = string(b)
}
return r.files[filename]
}
func (r *Rewriter) GetMethodBody(structname string, methodname string) string {
for _, f := range r.pkg.Syntax {
for _, d := range f.Decls {
d, isFunc := d.(*ast.FuncDecl)
if !isFunc {
continue
}
if d.Name.Name != methodname {
continue
}
if d.Recv == nil || len(d.Recv.List) == 0 {
continue
}
recv := d.Recv.List[0].Type
if star, isStar := recv.(*ast.StarExpr); isStar {
recv = star.X
}
ident, ok := recv.(*ast.Ident)
if !ok {
continue
}
if ident.Name != structname {
continue
}
r.copied[d] = true
return r.getSource(d.Body.Pos()+1, d.Body.End()-1)
}
}
return ""
}
func (r *Rewriter) MarkStructCopied(name string) {
for _, f := range r.pkg.Syntax {
for _, d := range f.Decls {
d, isGen := d.(*ast.GenDecl)
if !isGen {
continue
}
if d.Tok != token.TYPE || len(d.Specs) == 0 {
continue
}
spec, isTypeSpec := d.Specs[0].(*ast.TypeSpec)
if !isTypeSpec {
continue
}
if spec.Name.Name != name {
continue
}
r.copied[d] = true
}
}
}
func (r *Rewriter) ExistingImports(filename string) []Import {
filename, err := filepath.Abs(filename)
if err != nil {
panic(err)
}
for _, f := range r.pkg.Syntax {
pos := r.pkg.Fset.Position(f.Pos())
if filename != pos.Filename {
continue
}
var imps []Import
for _, i := range f.Imports {
name := ""
if i.Name != nil {
name = i.Name.Name
}
path, err := strconv.Unquote(i.Path.Value)
if err != nil {
panic(err)
}
imps = append(imps, Import{name, path})
}
return imps
}
return nil
}
func (r *Rewriter) RemainingSource(filename string) string {
filename, err := filepath.Abs(filename)
if err != nil {
panic(err)
}
for _, f := range r.pkg.Syntax {
pos := r.pkg.Fset.Position(f.Pos())
if filename != pos.Filename {
continue
}
var buf bytes.Buffer
for _, d := range f.Decls {
if r.copied[d] {
continue
}
if d, isGen := d.(*ast.GenDecl); isGen && d.Tok == token.IMPORT {
continue
}
buf.WriteString(r.getSource(d.Pos(), d.End()))
buf.WriteString("\n")
}
return strings.TrimSpace(buf.String())
}
return ""
}
type Import struct {
Alias string
ImportPath string
}