Make go linter happy again.

This commit is contained in:
Stanisław Barzowski 2021-05-14 21:32:34 +02:00
parent ce76155e09
commit 5899996502
15 changed files with 46 additions and 51 deletions

View File

@ -29,7 +29,7 @@ func (i IdentifierSet) AddIdentifiers(idents Identifiers) {
// ToOrderedSlice returns the elements of the current set as an ordered slice.
func (i IdentifierSet) ToOrderedSlice() []Identifier {
s := make([]Identifier, len(i), len(i))
s := make([]Identifier, len(i))
j := 0
for v := range i {
s[j] = v

View File

@ -14,6 +14,8 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
// Package cmd provides utilities for parsing and handling command line
// arguments.
package cmd
import (

View File

@ -14,6 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
// Package errors provides internal representation of errors.
package errors
import (

View File

@ -14,6 +14,8 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
// Package formatter provides API for producing pretty-printed source
// from AST.
package formatter
import (

View File

@ -14,6 +14,8 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
// Package pass provides a visitor framework for source code analysis
// and transformation.
package pass
import (

View File

@ -1,3 +1,4 @@
// Package program provides API for AST pre-processing (desugaring, static analysis).
package program
import (

View File

@ -1,3 +1,4 @@
// Package testutils provides general testing utilities.
package testutils
import (

View File

@ -1,7 +1,10 @@
// Package common provides utilities to be used in multiple linter
// subpackages.
package common
import (
"github.com/google/go-jsonnet/ast"
"github.com/google/go-jsonnet/internal/errors"
)
// VariableKind allows distinguishing various kinds of variables.
@ -33,3 +36,19 @@ type VariableInfo struct {
// More precisely it maps every *ast.Var to the variable.
VarAt map[ast.Node]*Variable
}
// ErrCollector is a struct for accumulating warnings / errors from the linter.
// It is slightly more convenient and more clear than passing pointers to slices around.
type ErrCollector struct {
Errs []errors.StaticError
}
// Collect adds an error to the list
func (ec *ErrCollector) Collect(err errors.StaticError) {
ec.Errs = append(ec.Errs, err)
}
// StaticErr constructs a static error from msg and loc and adds it to the list.
func (ec *ErrCollector) StaticErr(msg string, loc *ast.LocationRange) {
ec.Collect(errors.MakeStaticError(msg, *loc))
}

View File

@ -1,19 +1,18 @@
package traversal
// Package traversal provides relatively lightweight checks
// which can all fit within one traversal of the AST.
// Currently available checks:
// * Loop detection
// TODO(sbarzowski) add more
package traversal
import (
"github.com/google/go-jsonnet/ast"
"github.com/google/go-jsonnet/linter/internal/utils"
"github.com/google/go-jsonnet/linter/internal/common"
"github.com/google/go-jsonnet/internal/parser"
)
func findLoopingInChildren(node ast.Node, vars map[ast.Identifier]ast.Node, runOf map[ast.Identifier]int, currentRun int, ec *utils.ErrCollector) bool {
func findLoopingInChildren(node ast.Node, vars map[ast.Identifier]ast.Node, runOf map[ast.Identifier]int, currentRun int, ec *common.ErrCollector) bool {
for _, c := range parser.DirectChildren(node) {
found := findLooping(c, vars, runOf, currentRun, ec)
if found {
@ -23,7 +22,7 @@ func findLoopingInChildren(node ast.Node, vars map[ast.Identifier]ast.Node, runO
return false
}
func findLooping(node ast.Node, vars map[ast.Identifier]ast.Node, runOf map[ast.Identifier]int, currentRun int, ec *utils.ErrCollector) bool {
func findLooping(node ast.Node, vars map[ast.Identifier]ast.Node, runOf map[ast.Identifier]int, currentRun int, ec *common.ErrCollector) bool {
switch node := node.(type) {
case *ast.Var:
_, varFromThisLocal := vars[node.Id]
@ -43,7 +42,7 @@ func findLooping(node ast.Node, vars map[ast.Identifier]ast.Node, runOf map[ast.
return findLoopingInChildren(node, vars, runOf, currentRun, ec)
}
func findLoopingInLocal(node *ast.Local, ec *utils.ErrCollector) {
func findLoopingInLocal(node *ast.Local, ec *common.ErrCollector) {
vars := make(map[ast.Identifier]ast.Node)
runOf := make(map[ast.Identifier]int)
for _, b := range node.Binds {
@ -62,7 +61,7 @@ func findLoopingInLocal(node *ast.Local, ec *utils.ErrCollector) {
// Traverse visits all nodes in the AST and runs appropriate
// checks.
func Traverse(node ast.Node, ec *utils.ErrCollector) {
func Traverse(node ast.Node, ec *common.ErrCollector) {
switch node := node.(type) {
case *ast.Local:
findLoopingInLocal(node, ec)

View File

@ -6,10 +6,9 @@ import (
"github.com/google/go-jsonnet/ast"
"github.com/google/go-jsonnet/internal/parser"
"github.com/google/go-jsonnet/linter/internal/common"
"github.com/google/go-jsonnet/linter/internal/utils"
)
func checkSubexpr(node ast.Node, typeOf exprTypes, ec *utils.ErrCollector) {
func checkSubexpr(node ast.Node, typeOf exprTypes, ec *common.ErrCollector) {
for _, child := range parser.Children(node) {
check(child, typeOf, ec)
}
@ -17,7 +16,7 @@ func checkSubexpr(node ast.Node, typeOf exprTypes, ec *utils.ErrCollector) {
// check verifies that the types are valid for a given program, given
// the previously resolved types.
func check(node ast.Node, typeOf exprTypes, ec *utils.ErrCollector) {
func check(node ast.Node, typeOf exprTypes, ec *common.ErrCollector) {
checkSubexpr(node, typeOf, ec)
switch node := node.(type) {
case *ast.Apply:
@ -89,7 +88,7 @@ func check(node ast.Node, typeOf exprTypes, ec *utils.ErrCollector) {
}
// TODO(sbarzowski) eliminate duplication with the interpreter maybe (this is AST-level and there it's value-level)
func checkArgs(params []ast.Parameter, args *ast.Arguments, loc *ast.LocationRange, ec *utils.ErrCollector) {
func checkArgs(params []ast.Parameter, args *ast.Arguments, loc *ast.LocationRange, ec *common.ErrCollector) {
received := make(map[ast.Identifier]bool)
accepted := make(map[ast.Identifier]bool)
@ -133,7 +132,7 @@ func checkArgs(params []ast.Parameter, args *ast.Arguments, loc *ast.LocationRan
// * root nodes of all (transitively) imported Jsonnet files
// * resolution of variables in all files
// * importFunc which allows resolving imports
func Check(mainNode ast.Node, roots map[string]ast.Node, vars map[string]map[ast.Node]*common.Variable, importFunc ImportFunc, ec *utils.ErrCollector) {
func Check(mainNode ast.Node, roots map[string]ast.Node, vars map[string]map[ast.Node]*common.Variable, importFunc ImportFunc, ec *common.ErrCollector) {
et := make(exprTypes)
g := newTypeGraph(importFunc)
g.addRoots(roots, vars)

View File

@ -1,5 +1,5 @@
package types
// Package types provides type inference functionality.
//
// Even though Jsonnet doesn't have a concept of static types
// we can infer for each expression what values it can take.
// Of course we cannot do this accurately at all times, but even
@ -17,3 +17,5 @@ package types
// First of all type processing split into two very distinct phases:
// 1) Finding a type - an upper bound for the set of possible values for each expression.
// 2) Checking all expressions in the AST using this information.
package types

View File

@ -1,12 +0,0 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library")
go_library(
name = "go_default_library",
srcs = ["utils.go"],
importpath = "github.com/google/go-jsonnet/linter/internal/utils",
visibility = ["//linter:__subpackages__"],
deps = [
"//ast:go_default_library",
"//internal/errors:go_default_library",
],
)

View File

@ -1,22 +0,0 @@
package utils
import (
"github.com/google/go-jsonnet/ast"
"github.com/google/go-jsonnet/internal/errors"
)
// ErrCollector is a struct for accumulating warnings / errors from the linter.
// It is slightly more convenient and more clear than passing pointers to slices around.
type ErrCollector struct {
Errs []errors.StaticError
}
// Collect adds an error to the list
func (ec *ErrCollector) Collect(err errors.StaticError) {
ec.Errs = append(ec.Errs, err)
}
// StaticErr constructs a static error from msg and loc and adds it to the list.
func (ec *ErrCollector) StaticErr(msg string, loc *ast.LocationRange) {
ec.Collect(errors.MakeStaticError(msg, *loc))
}

View File

@ -1,3 +1,5 @@
// Package variables allows collecting the information about how variables
// are used.
package variables
import (

View File

@ -12,7 +12,6 @@ import (
"github.com/google/go-jsonnet/linter/internal/common"
"github.com/google/go-jsonnet/linter/internal/traversal"
"github.com/google/go-jsonnet/linter/internal/types"
"github.com/google/go-jsonnet/linter/internal/utils"
"github.com/google/go-jsonnet/linter/internal/variables"
)
@ -66,7 +65,7 @@ func lint(vm *jsonnet.VM, node nodeWithLocation, errWriter *ErrorWriter) {
errWriter.writeError(vm, errors.MakeStaticError("Unused variable: "+string(v.Name), v.LocRange))
}
}
ec := utils.ErrCollector{}
ec := common.ErrCollector{}
vars := make(map[string]map[ast.Node]*common.Variable)
for importedPath, info := range variablesInFile {