mirror of
https://github.com/google/go-jsonnet.git
synced 2025-09-28 08:51:01 +02:00
[Linter] Fix super index type and handle "object or string" indexing correctly.
This commit is contained in:
parent
12bd29d164
commit
46d1fceb9c
@ -234,7 +234,7 @@ func calcTP(node ast.Node, varAt map[ast.Node]*common.Variable, g *typeGraph) ty
|
||||
// no recursion yet
|
||||
return tpRef(anyObjectType)
|
||||
case *ast.SuperIndex:
|
||||
return tpRef(anyObjectType)
|
||||
return tpRef(anyType)
|
||||
case *ast.InSuper:
|
||||
return tpRef(boolType)
|
||||
case *ast.Function:
|
||||
|
@ -55,8 +55,8 @@ func check(node ast.Node, typeOf exprTypes, ec *common.ErrCollector) {
|
||||
if !indexType.Number {
|
||||
ec.StaticErr("Indexed value is assumed to be "+assumedType+", but index is not a number", node.Loc())
|
||||
}
|
||||
} else if !targetType.Array() {
|
||||
// It's not an array so it must be an object
|
||||
} else if !targetType.Array() && !targetType.String {
|
||||
// It's not an array or a string so it must be an object
|
||||
if !indexType.String {
|
||||
ec.StaticErr("Indexed value is assumed to be an object, but index is not a string", node.Loc())
|
||||
}
|
||||
|
5
linter/testdata/object_or_array_indexing.jsonnet
vendored
Normal file
5
linter/testdata/object_or_array_indexing.jsonnet
vendored
Normal file
@ -0,0 +1,5 @@
|
||||
local foo = if true then {"foo": "bar"} else ["f", "o", "o"];
|
||||
[
|
||||
foo[0],
|
||||
foo["foo"]
|
||||
]
|
0
linter/testdata/object_or_array_indexing.linter.golden
vendored
Normal file
0
linter/testdata/object_or_array_indexing.linter.golden
vendored
Normal file
5
linter/testdata/object_or_string_indexing.jsonnet
vendored
Normal file
5
linter/testdata/object_or_string_indexing.jsonnet
vendored
Normal file
@ -0,0 +1,5 @@
|
||||
local foo = if true then {"foo": "bar"} else "foo";
|
||||
[
|
||||
foo[0],
|
||||
foo["foo"]
|
||||
]
|
0
linter/testdata/object_or_string_indexing.linter.golden
vendored
Normal file
0
linter/testdata/object_or_string_indexing.linter.golden
vendored
Normal file
4
linter/testdata/super_index_array.jsonnet
vendored
Normal file
4
linter/testdata/super_index_array.jsonnet
vendored
Normal file
@ -0,0 +1,4 @@
|
||||
local foo = { config: [{ x: 'y' }] };
|
||||
foo {
|
||||
config: [super.config[0] { a: 'b' }],
|
||||
}
|
0
linter/testdata/super_index_array.linter.golden
vendored
Normal file
0
linter/testdata/super_index_array.linter.golden
vendored
Normal file
Loading…
x
Reference in New Issue
Block a user