hcl icon indicating copy to clipboard operation
hcl copied to clipboard

hclwrite: Add `RenameVariablePrefix` for Body

Open magodo opened this issue 5 years ago • 1 comments

This PR extend the capability of RenameVariablePrefix to allow it to rename traversal with index in it. Previously, given following config:

resource "foo" "a" {
  block {
    attr = "abc"
  }
}

resource "foo" "b" {
  attr = foo.a.block.0.attr
}

If the user later deprecate attribute foo.x.block.attr in favor of foo.x.block.new_attr, the RenameVariablePrefix function in this package can not manage the renaming for the foo.b.attr expression (i.e. its value foo.a.block.0.attr will not be changed into foo.a.block.0.new_attr.

In the context of a refactor tool, above use case is always what users want, as an expression always reference to some attribute in some schema, and when that attribute get changed, usres will almost not differentiate index at all, but want to change all the reference to that attribute regardless any index in the traversal.

So this PR modifies the RenameVariablePrefix to skip any TraverseIndex in the original expression's abs traversals, then do the matching and replacing.

This PR also adds a RenameVariablePrefix for Body as a util to recursively rename variable for a block.

magodo avatar Nov 14 '20 09:11 magodo

CLA assistant check
All committers have signed the CLA.

hashicorp-cla avatar Sep 09 '21 09:09 hashicorp-cla