hashicorp / hashicorp/hcl

hclwrite: `RenameVariablePrefix` support index

Open
#421 0 comments 2 reactions 0 assignees View on GitHub
Dominant language
Go
Stars
5.8k
Forks
657
Avg merge
20h 36m
Merged PRs (30d)
6

Description

Currently, the `RenameVariablePrefix` function has the limitation as mentioned in the comment:

> // The search and replacement traversals must be the same length, or this
// method will panic. Only attribute access operations can be matched and
// replaced. Index steps never match the prefix.

This means, following test cases on top of [this test](https://github.com/hashicorp/hcl/pull/420/files#diff-2a16dcc75f374e16308fd88e17eb470f20593793a00b9bd2d988b1432852806fR1592) will not work:

```hcl
{
Name: "attr traverse of a relative traversal expr sourced by a index expr",
Config: `resource "foo" "b" {
count = 3
name = foo.a[count.index].name
}`,
OldVar: []string{"foo", "a", "name"},
NewVar: []string{"foo", "a", "alias"},
Want: `resource "foo" "b" {
count = 3
name = foo.a[count.index].alias
}`,
},
```

This is quite a limitation for applications to refactor HCL files for use cases like a language server.

One reason that `RenameVariablePrefix` not work for above hcl is because the `Expression.Variables()` in `hclsyntax` returns partial traversal for cases like the latter case, that the internal walking logic for `hclsyntax.RelativeTraversalExpr` only tackles with its `Source` component, but ignore the `Traversal` component at all.

Whilst the JB HCL plugin somehow could manage this case. So I would like to see the official `hclwrite` package could also support this.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.