mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-11-10 04:57:26 +01:00
.github/workflows/vettools: add a static analysis for image importing
Updates #2336 Closes #2337
This commit is contained in:
parent
8de4a59416
commit
08d54e9751
2
.github/workflows/test.yml
vendored
2
.github/workflows/test.yml
vendored
@ -73,7 +73,7 @@ jobs:
|
||||
run: |
|
||||
go vet -tags=example -v ./...
|
||||
|
||||
- name: go vet (atomic align, error check)
|
||||
- name: go vet (vettool)
|
||||
run: |
|
||||
(cd .github/workflows/vettools; go install .)
|
||||
go vet -vettool=$(which vettools)${{ startsWith(matrix.os, 'windows-') && '.exe' || '' }} -tags=example -v ./...
|
||||
|
83
.github/workflows/vettools/imageimportcheck.go
vendored
Normal file
83
.github/workflows/vettools/imageimportcheck.go
vendored
Normal file
@ -0,0 +1,83 @@
|
||||
// Copyright 2022 The Ebitengine Authors
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"go/token"
|
||||
"reflect"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"golang.org/x/tools/go/analysis"
|
||||
)
|
||||
|
||||
// imageImportCheckAnalyzer is an analyzer to check whether unexpected `image/*` packages are imported.
|
||||
// Importing `image/gif`, `image/jpeg`, and `image/png` registers their recorders at `init` functions, so
|
||||
// have affect the result of `image.Decode`. Ebitengine should not have such side-effects.
|
||||
var imageImportCheckAnalyzer = &analysis.Analyzer{
|
||||
Name: "imageimportcheck",
|
||||
Doc: "check importing image/gif, image/jpeg, and image/png packages",
|
||||
Run: runImageImportCheck,
|
||||
ResultType: reflect.TypeOf(imageImportCheckResult{}),
|
||||
}
|
||||
|
||||
type imageImportCheckResult struct {
|
||||
Errors []imageImportCheckError
|
||||
}
|
||||
|
||||
type imageImportCheckError struct {
|
||||
Pos token.Pos
|
||||
Import string
|
||||
}
|
||||
|
||||
func runImageImportCheck(pass *analysis.Pass) (interface{}, error) {
|
||||
pkgPath := pass.Pkg.Path()
|
||||
if strings.HasPrefix(pkgPath, "github.com/hajimehoshi/ebiten/v2/example") {
|
||||
return nil, nil
|
||||
}
|
||||
if strings.HasSuffix(pkgPath, "_test") {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// TODO: Remove this exception after v3 is released (#2336).
|
||||
if strings.HasPrefix(pkgPath, "github.com/hajimehoshi/ebiten/v2/ebitenutil") {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
var errs []imageImportCheckError
|
||||
for _, f := range pass.Files {
|
||||
for _, i := range f.Imports {
|
||||
path, err := strconv.Unquote(i.Path.Value)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if path == "image/gif" || path == "image/jpeg" || path == "image/png" {
|
||||
err := imageImportCheckError{
|
||||
Pos: pass.Fset.File(f.Pos()).Pos(int(i.Pos())),
|
||||
Import: path,
|
||||
}
|
||||
errs = append(errs, err)
|
||||
pass.Report(analysis.Diagnostic{
|
||||
Pos: err.Pos,
|
||||
Message: fmt.Sprintf("unexpected import %q", err.Import),
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
return imageImportCheckResult{
|
||||
Errors: errs,
|
||||
}, nil
|
||||
}
|
4
.github/workflows/vettools/main.go
vendored
4
.github/workflows/vettools/main.go
vendored
@ -23,9 +23,9 @@ import (
|
||||
)
|
||||
|
||||
func main() {
|
||||
filename := ".errcheck_excludes"
|
||||
const filename = ".errcheck_excludes"
|
||||
if _, err := os.Stat(filename); err == nil {
|
||||
errcheck.Analyzer.Flags.Set("exclude", filename)
|
||||
}
|
||||
multichecker.Main(atomicalign.Analyzer, errcheck.Analyzer)
|
||||
multichecker.Main(atomicalign.Analyzer, errcheck.Analyzer, imageImportCheckAnalyzer)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user