mirror of
https://github.com/hanzoai/expr.git
synced 2026-09-24 16:58:58 +00:00
Return correct type for builtin functions
This commit is contained in:
+1
-4
@@ -139,10 +139,7 @@ func (v *visitor) IdentifierNode(node *ast.IdentifierNode) (reflect.Type, info)
|
||||
return mapType, info{}
|
||||
}
|
||||
if fn, ok := v.config.Functions[node.Value]; ok {
|
||||
// Return anyType instead of func type as we don't know the arguments yet.
|
||||
// The func type can be one of the fn.Types. The type will be resolved
|
||||
// when the arguments are known in CallNode.
|
||||
return anyType, info{fn: fn}
|
||||
return functionType, info{fn: fn}
|
||||
}
|
||||
if t, ok := v.config.Types[node.Value]; ok {
|
||||
if t.Ambiguous {
|
||||
|
||||
@@ -962,3 +962,24 @@ func TestCheck_env_keyword(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheck_builtin_without_call(t *testing.T) {
|
||||
tests := []struct {
|
||||
input string
|
||||
err string
|
||||
}{
|
||||
{`len + 1`, "invalid operation: + (mismatched types func(...interface {}) (interface {}, error) and int) (1:5)\n | len + 1\n | ....^"},
|
||||
{`string.A`, "type func(...interface {}) (interface {}, error)[string] is undefined (1:8)\n | string.A\n | .......^"},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.input, func(t *testing.T) {
|
||||
tree, err := parser.Parse(test.input)
|
||||
require.NoError(t, err)
|
||||
|
||||
_, err = checker.Check(tree, conf.New(nil))
|
||||
require.Error(t, err)
|
||||
require.Equal(t, test.err, err.Error())
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user