Skip to content

Commit

Permalink
Fix jsonnet-lint erorr when linting importing files and imported file…
Browse files Browse the repository at this point in the history
…s at the same time: google#544
  • Loading branch information
thombashi committed Jul 11, 2021
1 parent 51daeb3 commit 1d393de
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 8 deletions.
9 changes: 9 additions & 0 deletions imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,15 @@ func (cache *importCache) importData(importedFrom, importedPath string) (content
return
}

func (cache *importCache) snippetToAST(diagnosticFilename ast.DiagnosticFileName, importedFilename, snippet string) (ast.Node, error) {
if cachedNode, isCached := cache.astCache[importedFilename]; isCached {
return cachedNode, nil
}
node, err := program.SnippetToAST(diagnosticFilename, importedFilename, snippet)
cache.astCache[importedFilename] = node
return node, err
}

func (cache *importCache) importAST(importedFrom, importedPath string) (ast.Node, string, error) {
contents, foundAt, err := cache.importData(importedFrom, importedPath)
if err != nil {
Expand Down
8 changes: 4 additions & 4 deletions jsonnet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,11 +290,11 @@ func assertVarOutput(t *testing.T, jsonStr string) {
}

func TestExtTypes(t *testing.T) {
node, err := SnippetToAST("var.jsonnet", `{ node: 'node' }`)
vm := MakeVM()
node, err := vm.SnippetToAST("var.jsonnet", `{ node: 'node' }`)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
vm := MakeVM()
vm.ExtVar("var", "var")
vm.ExtCode("code", `{ code: 'code'}`)
vm.ExtNode("node", node)
Expand All @@ -310,11 +310,11 @@ func TestExtTypes(t *testing.T) {
}

func TestTLATypes(t *testing.T) {
node, err := SnippetToAST("var.jsonnet", `{ node: 'node' }`)
vm := MakeVM()
node, err := vm.SnippetToAST("var.jsonnet", `{ node: 'node' }`)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
vm := MakeVM()
vm.TLAVar("var", "var")
vm.TLACode("code", `{ code: 'code'}`)
vm.TLANode("node", node)
Expand Down
2 changes: 1 addition & 1 deletion linter/linter.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func LintSnippet(vm *jsonnet.VM, output io.Writer, snippets []Snippet) bool {

var nodes []nodeWithLocation
for _, snippet := range snippets {
node, err := jsonnet.SnippetToAST(snippet.FileName, snippet.Code)
node, err := vm.SnippetToAST(snippet.FileName, snippet.Code)

if err != nil {
errWriter.writeError(vm, err.(errors.StaticError)) // ugly but true
Expand Down
17 changes: 17 additions & 0 deletions linter/linter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,23 @@ func runTests(t *testing.T, tests []*linterTest) {
if outData != "" && !errorsFound {
t.Error(fmt.Errorf("return value indicates no problems, but output is not empty:\n%v", outData))
}

// passing as args for both importing file and imported file.
var import_snippets []Snippet

for _, filepath := range []string{"testdata/import.jsonnet", "testdata/call_integer.jsonnet"} {
input := read(filepath)
import_snippets = append(import_snippets, Snippet{FileName: filepath, Code: string(input)})
}

errorsFound = LintSnippet(vm, &outBuilder, snippets)
outData = outBuilder.String()
if outData == "" && errorsFound {
t.Error(fmt.Errorf("return value indicates problems present, but no output was produced"))
}
if outData != "" && !errorsFound {
t.Error(fmt.Errorf("return value indicates no problems, but output is not empty:\n%v", outData))
}
}

func TestLinter(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ func runInternalJsonnet(i jsonnetInput) jsonnetResult {
}
testChildren(rawAST)

desugaredAST, err := SnippetToAST(i.name, string(i.input))
desugaredAST, err := vm.SnippetToAST(i.name, string(i.input))
if err != nil {
return jsonnetResult{
output: errFormatter.Format(err) + "\n",
Expand Down
4 changes: 2 additions & 2 deletions vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -514,8 +514,8 @@ func (vm *VM) ImportAST(importedFrom, importedPath string) (contents ast.Node, f
}

// SnippetToAST parses a snippet and returns the resulting AST.
func SnippetToAST(filename string, snippet string) (ast.Node, error) {
return program.SnippetToAST(ast.DiagnosticFileName(filename), filename, snippet)
func (vm *VM) SnippetToAST(filename string, snippet string) (ast.Node, error) {
return vm.importCache.snippetToAST(ast.DiagnosticFileName(filename), filename, snippet)
}

// Version returns the Jsonnet version number.
Expand Down

0 comments on commit 1d393de

Please sign in to comment.