diff --git a/ast/util.go b/ast/util.go index 978c0fe..45e9945 100644 --- a/ast/util.go +++ b/ast/util.go @@ -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 diff --git a/cmd/internal/cmd/utils.go b/cmd/internal/cmd/utils.go index 0f66907..15b08cf 100644 --- a/cmd/internal/cmd/utils.go +++ b/cmd/internal/cmd/utils.go @@ -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 ( diff --git a/internal/errors/static_error.go b/internal/errors/static_error.go index fdb8f0c..535da5e 100644 --- a/internal/errors/static_error.go +++ b/internal/errors/static_error.go @@ -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 ( diff --git a/internal/formatter/jsonnetfmt.go b/internal/formatter/jsonnetfmt.go index 3eeaed5..5007921 100644 --- a/internal/formatter/jsonnetfmt.go +++ b/internal/formatter/jsonnetfmt.go @@ -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 ( diff --git a/internal/pass/pass.go b/internal/pass/pass.go index 2c6b5df..b3475fe 100644 --- a/internal/pass/pass.go +++ b/internal/pass/pass.go @@ -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 ( diff --git a/internal/program/program.go b/internal/program/program.go index 32014d3..c135024 100644 --- a/internal/program/program.go +++ b/internal/program/program.go @@ -1,3 +1,4 @@ +// Package program provides API for AST pre-processing (desugaring, static analysis). package program import ( diff --git a/internal/testutils/test_utils.go b/internal/testutils/test_utils.go index ad215b7..af67294 100644 --- a/internal/testutils/test_utils.go +++ b/internal/testutils/test_utils.go @@ -1,3 +1,4 @@ +// Package testutils provides general testing utilities. package testutils import ( diff --git a/linter/internal/common/common.go b/linter/internal/common/common.go index 6156703..d41b8c9 100644 --- a/linter/internal/common/common.go +++ b/linter/internal/common/common.go @@ -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)) +} diff --git a/linter/internal/traversal/traversal.go b/linter/internal/traversal/traversal.go index d33e6c4..c57ffe9 100644 --- a/linter/internal/traversal/traversal.go +++ b/linter/internal/traversal/traversal.go @@ -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) diff --git a/linter/internal/types/check.go b/linter/internal/types/check.go index bbb3316..0c1276a 100644 --- a/linter/internal/types/check.go +++ b/linter/internal/types/check.go @@ -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) diff --git a/linter/internal/types/doc.go b/linter/internal/types/doc.go index ab831f2..bafb1ed 100644 --- a/linter/internal/types/doc.go +++ b/linter/internal/types/doc.go @@ -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 + diff --git a/linter/internal/utils/BUILD.bazel b/linter/internal/utils/BUILD.bazel deleted file mode 100644 index 3caa518..0000000 --- a/linter/internal/utils/BUILD.bazel +++ /dev/null @@ -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", - ], -) diff --git a/linter/internal/utils/utils.go b/linter/internal/utils/utils.go deleted file mode 100644 index 4074e2a..0000000 --- a/linter/internal/utils/utils.go +++ /dev/null @@ -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)) -} diff --git a/linter/internal/variables/find_variables.go b/linter/internal/variables/find_variables.go index baae023..cfdef8e 100644 --- a/linter/internal/variables/find_variables.go +++ b/linter/internal/variables/find_variables.go @@ -1,3 +1,5 @@ +// Package variables allows collecting the information about how variables +// are used. package variables import ( diff --git a/linter/linter.go b/linter/linter.go index 7063e21..7154dfa 100644 --- a/linter/linter.go +++ b/linter/linter.go @@ -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 {