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

hclwrite: RenameVariablePrefix support index #421

Open
magodo opened this issue Nov 14, 2020 · 0 comments
Open

hclwrite: RenameVariablePrefix support index #421

magodo opened this issue Nov 14, 2020 · 0 comments

Comments

@magodo
Copy link

magodo commented Nov 14, 2020

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 will not work:

		{
			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.

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

No branches or pull requests

1 participant