mirror of
https://github.com/google/go-jsonnet.git
synced 2026-05-05 03:56:11 +02:00
Support for various kinds of strings in imports
This commit is contained in:
parent
121a77c66f
commit
0b52ea4d40
@ -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
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
12
desugarer.go
12
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)
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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())
|
||||
|
||||
1
testdata/".golden
vendored
Normal file
1
testdata/".golden
vendored
Normal file
@ -0,0 +1 @@
|
||||
testdata/":2:1 Unexpected end of file.
|
||||
1
testdata/".jsonnet
vendored
Normal file
1
testdata/".jsonnet
vendored
Normal file
@ -0,0 +1 @@
|
||||
// This file is there only for its filename: to test escaping in imports
|
||||
1
testdata/'.golden
vendored
Normal file
1
testdata/'.golden
vendored
Normal file
@ -0,0 +1 @@
|
||||
testdata/':2:1 Unexpected end of file.
|
||||
1
testdata/'.jsonnet
vendored
Normal file
1
testdata/'.jsonnet
vendored
Normal file
@ -0,0 +1 @@
|
||||
// This file is there only for its filename: to test escaping in imports
|
||||
1
testdata/import_block_literal.golden
vendored
Normal file
1
testdata/import_block_literal.golden
vendored
Normal file
@ -0,0 +1 @@
|
||||
testdata/import_block_literal:(1:8)-(3:4) Block string literals not allowed in imports
|
||||
3
testdata/import_block_literal.jsonnet
vendored
Normal file
3
testdata/import_block_literal.jsonnet
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
import |||
|
||||
block_literals_for_imports_are_not_allowed_and_make_exactly_zero_sense
|
||||
|||
|
||||
1
testdata/import_computed.golden
vendored
Normal file
1
testdata/import_computed.golden
vendored
Normal file
@ -0,0 +1 @@
|
||||
testdata/import_computed:1:9-16 Computed imports are not allowed
|
||||
1
testdata/import_computed.jsonnet
vendored
Normal file
1
testdata/import_computed.jsonnet
vendored
Normal file
@ -0,0 +1 @@
|
||||
import "a" + "b"
|
||||
6
testdata/import_various_literals.golden
vendored
Normal file
6
testdata/import_various_literals.golden
vendored
Normal file
@ -0,0 +1,6 @@
|
||||
[
|
||||
true,
|
||||
true,
|
||||
true,
|
||||
true
|
||||
]
|
||||
6
testdata/import_various_literals.jsonnet
vendored
Normal file
6
testdata/import_various_literals.jsonnet
vendored
Normal file
@ -0,0 +1,6 @@
|
||||
[
|
||||
import "true.jsonnet",
|
||||
import 'true.jsonnet',
|
||||
import @"true.jsonnet",
|
||||
import @'true.jsonnet',
|
||||
]
|
||||
1
testdata/import_various_literals_escaped.golden
vendored
Normal file
1
testdata/import_various_literals_escaped.golden
vendored
Normal file
@ -0,0 +1 @@
|
||||
testdata/".jsonnet:2:1 Unexpected end of file.
|
||||
6
testdata/import_various_literals_escaped.jsonnet
vendored
Normal file
6
testdata/import_various_literals_escaped.jsonnet
vendored
Normal file
@ -0,0 +1,6 @@
|
||||
[
|
||||
import "\u0074rue.jsonnet",
|
||||
import '\u0074rue.jsonnet',
|
||||
import @""".jsonnet",
|
||||
import @'''.jsonnet',
|
||||
]
|
||||
1
testdata/importstr_block_literal.golden
vendored
Normal file
1
testdata/importstr_block_literal.golden
vendored
Normal file
@ -0,0 +1 @@
|
||||
testdata/importstr_block_literal:(1:11)-(3:4) Block string literals not allowed in imports
|
||||
3
testdata/importstr_block_literal.jsonnet
vendored
Normal file
3
testdata/importstr_block_literal.jsonnet
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
importstr |||
|
||||
block_literals_for_imports_are_not_allowed_and_make_exactly_zero_sense
|
||||
|||
|
||||
1
testdata/importstr_computed.golden
vendored
Normal file
1
testdata/importstr_computed.golden
vendored
Normal file
@ -0,0 +1 @@
|
||||
testdata/importstr_computed:1:12-19 Computed imports are not allowed
|
||||
1
testdata/importstr_computed.jsonnet
vendored
Normal file
1
testdata/importstr_computed.jsonnet
vendored
Normal file
@ -0,0 +1 @@
|
||||
importstr "a" + "b"
|
||||
6
testdata/importstr_various_literals_escaped.golden
vendored
Normal file
6
testdata/importstr_various_literals_escaped.golden
vendored
Normal file
@ -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"
|
||||
]
|
||||
6
testdata/importstr_various_literals_escaped.jsonnet
vendored
Normal file
6
testdata/importstr_various_literals_escaped.jsonnet
vendored
Normal file
@ -0,0 +1,6 @@
|
||||
[
|
||||
importstr "\u0074rue.jsonnet",
|
||||
importstr '\u0074rue.jsonnet',
|
||||
importstr @""".jsonnet",
|
||||
importstr @'''.jsonnet',
|
||||
]
|
||||
2
testdata/nonexistent_import_crazy.golden
vendored
2
testdata/nonexistent_import_crazy.golden
vendored
@ -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.
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user