diff --git a/ast/ast.go b/ast/ast.go index 2a19dfb..cee669a 100644 --- a/ast/ast.go +++ b/ast/ast.go @@ -300,7 +300,7 @@ type Parameters struct { // Import represents import "file". type Import struct { NodeBase - File string + File *LiteralString } // --------------------------------------------------------------------------- @@ -308,7 +308,7 @@ type Import struct { // ImportStr represents importstr "file". type ImportStr struct { NodeBase - File string + File *LiteralString } // --------------------------------------------------------------------------- diff --git a/desugarer.go b/desugarer.go index d9a0ad6..60d6135 100644 --- a/desugarer.go +++ b/desugarer.go @@ -438,10 +438,18 @@ func desugar(astPtr *ast.Node, objLevel int) (err error) { } case *ast.Import: - // Nothing to do. + var file ast.Node = node.File + err = desugar(&file, objLevel) + if err != nil { + return + } case *ast.ImportStr: - // Nothing to do. + var file ast.Node = node.File + err = desugar(&file, objLevel) + if err != nil { + return + } case *ast.Index: err = desugar(&node.Target, objLevel) diff --git a/interpreter.go b/interpreter.go index 84e750f..3aae6fc 100644 --- a/interpreter.go +++ b/interpreter.go @@ -379,11 +379,11 @@ func (i *interpreter) evaluate(a ast.Node, context *TraceContext) (value, error) case *ast.Import: codeDir := path.Dir(ast.Loc().FileName) - return i.importCache.ImportCode(codeDir, ast.File, e) + return i.importCache.ImportCode(codeDir, ast.File.Value, e) case *ast.ImportStr: codeDir := path.Dir(ast.Loc().FileName) - return i.importCache.ImportString(codeDir, ast.File, e) + return i.importCache.ImportString(codeDir, ast.File.Value, e) case *ast.LiteralBoolean: return makeValueBoolean(ast.Value), nil diff --git a/parser/parser.go b/parser/parser.go index d248d5d..bf8ce15 100644 --- a/parser/parser.go +++ b/parser/parser.go @@ -715,7 +715,7 @@ func (p *parser) parseTerminal() (ast.Node, error) { return &ast.LiteralString{ NodeBase: ast.NewNodeBaseLoc(tok.loc), Value: tok.data, - Kind: ast.StringDouble, + Kind: ast.StringBlock, BlockIndent: tok.stringBlockIndent, }, nil case tokenVerbatimStringDouble: @@ -902,9 +902,12 @@ func (p *parser) parse(prec precedence) (ast.Node, error) { return nil, err } if lit, ok := body.(*ast.LiteralString); ok { + if lit.Kind == ast.StringBlock { + return nil, MakeStaticError("Block string literals not allowed in imports", *body.Loc()) + } return &ast.Import{ NodeBase: ast.NewNodeBaseLoc(locFromTokenAST(begin, body)), - File: lit.Value, + File: lit, }, nil } return nil, MakeStaticError("Computed imports are not allowed", *body.Loc()) @@ -916,9 +919,12 @@ func (p *parser) parse(prec precedence) (ast.Node, error) { return nil, err } if lit, ok := body.(*ast.LiteralString); ok { + if lit.Kind == ast.StringBlock { + return nil, MakeStaticError("Block string literals not allowed in imports", *body.Loc()) + } return &ast.ImportStr{ NodeBase: ast.NewNodeBaseLoc(locFromTokenAST(begin, body)), - File: lit.Value, + File: lit, }, nil } return nil, MakeStaticError("Computed imports are not allowed", *body.Loc()) diff --git "a/testdata/\".golden" "b/testdata/\".golden" new file mode 100644 index 0000000..4f12b35 --- /dev/null +++ "b/testdata/\".golden" @@ -0,0 +1 @@ +testdata/":2:1 Unexpected end of file. diff --git "a/testdata/\".jsonnet" "b/testdata/\".jsonnet" new file mode 100644 index 0000000..64ed8e6 --- /dev/null +++ "b/testdata/\".jsonnet" @@ -0,0 +1 @@ +// This file is there only for its filename: to test escaping in imports diff --git a/testdata/'.golden b/testdata/'.golden new file mode 100644 index 0000000..a4592f4 --- /dev/null +++ b/testdata/'.golden @@ -0,0 +1 @@ +testdata/':2:1 Unexpected end of file. diff --git a/testdata/'.jsonnet b/testdata/'.jsonnet new file mode 100644 index 0000000..64ed8e6 --- /dev/null +++ b/testdata/'.jsonnet @@ -0,0 +1 @@ +// This file is there only for its filename: to test escaping in imports diff --git a/testdata/import_block_literal.golden b/testdata/import_block_literal.golden new file mode 100644 index 0000000..e1956fd --- /dev/null +++ b/testdata/import_block_literal.golden @@ -0,0 +1 @@ +testdata/import_block_literal:(1:8)-(3:4) Block string literals not allowed in imports diff --git a/testdata/import_block_literal.jsonnet b/testdata/import_block_literal.jsonnet new file mode 100644 index 0000000..8daaada --- /dev/null +++ b/testdata/import_block_literal.jsonnet @@ -0,0 +1,3 @@ +import ||| + block_literals_for_imports_are_not_allowed_and_make_exactly_zero_sense +||| diff --git a/testdata/import_computed.golden b/testdata/import_computed.golden new file mode 100644 index 0000000..a4e3fa5 --- /dev/null +++ b/testdata/import_computed.golden @@ -0,0 +1 @@ +testdata/import_computed:1:9-16 Computed imports are not allowed diff --git a/testdata/import_computed.jsonnet b/testdata/import_computed.jsonnet new file mode 100644 index 0000000..a2d59f7 --- /dev/null +++ b/testdata/import_computed.jsonnet @@ -0,0 +1 @@ +import "a" + "b" diff --git a/testdata/import_various_literals.golden b/testdata/import_various_literals.golden new file mode 100644 index 0000000..f4143bd --- /dev/null +++ b/testdata/import_various_literals.golden @@ -0,0 +1,6 @@ +[ + true, + true, + true, + true +] diff --git a/testdata/import_various_literals.jsonnet b/testdata/import_various_literals.jsonnet new file mode 100644 index 0000000..164ae3e --- /dev/null +++ b/testdata/import_various_literals.jsonnet @@ -0,0 +1,6 @@ +[ + import "true.jsonnet", + import 'true.jsonnet', + import @"true.jsonnet", + import @'true.jsonnet', +] diff --git a/testdata/import_various_literals_escaped.golden b/testdata/import_various_literals_escaped.golden new file mode 100644 index 0000000..4b290ab --- /dev/null +++ b/testdata/import_various_literals_escaped.golden @@ -0,0 +1 @@ +testdata/".jsonnet:2:1 Unexpected end of file. diff --git a/testdata/import_various_literals_escaped.jsonnet b/testdata/import_various_literals_escaped.jsonnet new file mode 100644 index 0000000..a932f2f --- /dev/null +++ b/testdata/import_various_literals_escaped.jsonnet @@ -0,0 +1,6 @@ +[ + import "\u0074rue.jsonnet", + import '\u0074rue.jsonnet', + import @""".jsonnet", + import @'''.jsonnet', +] diff --git a/testdata/importstr_block_literal.golden b/testdata/importstr_block_literal.golden new file mode 100644 index 0000000..b81de14 --- /dev/null +++ b/testdata/importstr_block_literal.golden @@ -0,0 +1 @@ +testdata/importstr_block_literal:(1:11)-(3:4) Block string literals not allowed in imports diff --git a/testdata/importstr_block_literal.jsonnet b/testdata/importstr_block_literal.jsonnet new file mode 100644 index 0000000..8c6ee7e --- /dev/null +++ b/testdata/importstr_block_literal.jsonnet @@ -0,0 +1,3 @@ +importstr ||| + block_literals_for_imports_are_not_allowed_and_make_exactly_zero_sense +||| diff --git a/testdata/importstr_computed.golden b/testdata/importstr_computed.golden new file mode 100644 index 0000000..abcafe8 --- /dev/null +++ b/testdata/importstr_computed.golden @@ -0,0 +1 @@ +testdata/importstr_computed:1:12-19 Computed imports are not allowed diff --git a/testdata/importstr_computed.jsonnet b/testdata/importstr_computed.jsonnet new file mode 100644 index 0000000..623672e --- /dev/null +++ b/testdata/importstr_computed.jsonnet @@ -0,0 +1 @@ +importstr "a" + "b" diff --git a/testdata/importstr_various_literals_escaped.golden b/testdata/importstr_various_literals_escaped.golden new file mode 100644 index 0000000..0d618d0 --- /dev/null +++ b/testdata/importstr_various_literals_escaped.golden @@ -0,0 +1,6 @@ +[ + "true\n", + "true\n", + "// This file is there only for its filename: to test escaping in imports\n", + "// This file is there only for its filename: to test escaping in imports\n" +] diff --git a/testdata/importstr_various_literals_escaped.jsonnet b/testdata/importstr_various_literals_escaped.jsonnet new file mode 100644 index 0000000..8529808 --- /dev/null +++ b/testdata/importstr_various_literals_escaped.jsonnet @@ -0,0 +1,6 @@ +[ + importstr "\u0074rue.jsonnet", + importstr '\u0074rue.jsonnet', + importstr @""".jsonnet", + importstr @'''.jsonnet', +] diff --git a/testdata/nonexistent_import_crazy.golden b/testdata/nonexistent_import_crazy.golden index 709c9eb..7169f78 100644 --- a/testdata/nonexistent_import_crazy.golden +++ b/testdata/nonexistent_import_crazy.golden @@ -1 +1 @@ -RUNTIME ERROR: Couldn't open import "ąęółńśćźż \\\" \\' \\n\\n\\t\\t": No match locally or in the Jsonnet library paths. +RUNTIME ERROR: Couldn't open import "ąęółńśćźż \" ' \n\n\t\t": No match locally or in the Jsonnet library paths.