Skip to content

Commit

Permalink
[Linter] Fix super index type and handle "object or string" indexing …
Browse files Browse the repository at this point in the history
…correctly.
  • Loading branch information
sbarzowski committed Jun 6, 2021
1 parent 12bd29d commit 46d1fce
Show file tree
Hide file tree
Showing 8 changed files with 17 additions and 3 deletions.
2 changes: 1 addition & 1 deletion linter/internal/types/build_graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions linter/internal/types/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
Expand Down
5 changes: 5 additions & 0 deletions linter/testdata/object_or_array_indexing.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
local foo = if true then {"foo": "bar"} else ["f", "o", "o"];
[
foo[0],
foo["foo"]
]
Empty file.
5 changes: 5 additions & 0 deletions linter/testdata/object_or_string_indexing.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
local foo = if true then {"foo": "bar"} else "foo";
[
foo[0],
foo["foo"]
]
Empty file.
4 changes: 4 additions & 0 deletions linter/testdata/super_index_array.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
local foo = { config: [{ x: 'y' }] };
foo {
config: [super.config[0] { a: 'b' }],
}
Empty file.

0 comments on commit 46d1fce

Please sign in to comment.