hcl
hcl copied to clipboard
hclwrite: Add `RenameVariablePrefix` for Body
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.