Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide accurate constraints in index part of references #1596

Open
5 tasks
radeksimko opened this issue Jan 23, 2024 · 0 comments
Open
5 tasks

Provide accurate constraints in index part of references #1596

radeksimko opened this issue Jan 23, 2024 · 0 comments

Comments

@radeksimko
Copy link
Member

radeksimko commented Jan 23, 2024

Follow-up from hashicorp/hcl-lang#365 (comment)


Context

Currently we recognise index expression such as var.foo[var.bar], in the sense that we provide:

  • completion
  • hover
  • semantic tokens
  • go-to-definition/references

While this is helpful, we currently treat all [] (indexes) in references as if they were a map, i.e. something indexable with keys of type string.

https://github.com/hashicorp/hcl-lang/blob/f43c27231c1001f4682f6fe72011cdabe692bdef/decoder/expr_any_index.go#L19-L25

Because we check for type convertability and strings/numbers are convertible in cty, this does not impact the functionality in a major way. However, it can make it less helpful in some contexts.

  • we're unable to detect invalid or poor practice configuration (e.g. var.map[var.number] or var.list[var.string])
  • we're unable to sort completion candidates by prioritising number type for lists/tuples and string type for maps
  • the hover data may not be as accurate, or at least not tell the whole story

Proposal

  • Ensure constraints for the index [] are inferred from the type of the reference
    • This may require some refactoring as we may not know anything about the type until we have parsed the target of the reference
  • Produce diagnostics for invalid or poor practice configuration, e.g. var.map[var.number] or var.list[var.string]
  • Sort completion candidates by prioritising number type for lists/tuples and string type for maps
  • Display constraint in hover separately from real type #1512
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant