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

Extend symbols with concrete *hcl.(Attribute/Block/Expression) #409

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

magodo
Copy link

@magodo magodo commented Aug 18, 2024

I have a use case of this module: I'd like to firstly call the Decoder.Symbols to get the target symbols of a query. Then iterate each symbols (recursively visiting its nested symbols). Then check whether those symbols are reference origins/targets, by using ReferenceOriginsTargetingPos()/ReferenceTargetsForOriginAtPos(), with passing in the position of the symbols.

The problem is that the symbol's position is larger than what those two functions expected. E.g. Given following HCL:

provider "azurerm" {
  features {
    resource_group {
      prevent_deletion_if_contains_resources = false
    }
  }
}

resource "azurerm_resource_group" "src" {
  name     = "my-rg"
  location = "westeurope"
}

resource "azurerm_resource_group" "dst" {
  name     = azurerm_resource_group.src.name
  location = azurerm_resource_group.src.location
  tags = {
    source = azurerm_resource_group.src.id
  }
}

The symbol of azurerm_resource_group.dst's attribute name, its range spans from column 3 to column 45, which is effectively name = azurerm_resource_group.src.name, which is not recognised by ReferenceTargetsForOriginAtPos(). Instead, it wants the pos is within range column 14 to column 45.

Furthermore, we also want to get the exact position of, e.g. for AttributeSymbol, the key and value positions. These are missing in the current symbol types.

This PR adds:

  • The underlying *hcl.Attribute for AttributeSymbol
  • The underlying *hcl.Block for BlockSymbol
  • For ExprSymbol, it is a bit complex:
    • If the parent Expression is a *hclsyntax.TupleConsExpr, adds the hcl.Expression to Expr field
    • If the parent Expression is a *hclsyntax.ObjectConsExpr, adds the hclsyntax.ObjectConsItem to Item field

@magodo magodo requested a review from a team as a code owner August 18, 2024 13:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant