hcl2json icon indicating copy to clipboard operation
hcl2json copied to clipboard

Including range (start and end) value in the generated JSON

Open alfie-far opened this issue 4 years ago • 4 comments

Hi, thanks for building such a great utility for HCL We are currently using this library to perform static analysis checks on Terraform HCL files and we wanted to use the range (start and end position of tokens) info in order to point to the location of the resource in terraform template. I was wondering if you could help me find a solution for this issue

alfie-far avatar Oct 28 '21 08:10 alfie-far

hmmm, what would you expect the output json to look like? This feature may be out of scope for this project. Are you doing the static analysis with go, or outputting to json and doing static analysis with a different language?

tmccombs avatar Oct 29 '21 05:10 tmccombs

That's true @tmccombs, it could be outside the scope of this project but I was thinking if you could help me realise where in the code I can access the position (start and end range) metadata?

Are you doing the static analysis with go, or outputting to json and doing static analysis with a different language?

We're using a wrapper library: @cdktf/hcl2json in Node runtime to run our static analysis on the generated JSON file.

In terms of output format, I think adding position metadata requires creating an Abstract Syntax Tree so that we can keep metadata for each token of the HCL template.

alfie-far avatar Nov 27 '21 03:11 alfie-far

Basically the way it works is ConvertFile Recursively walks through the AST of an hclsyntax.Body, and each part of the AST has one or more "Range" properties that describe the byte indices for where a section starts and ends.

I hope that helps.

tmccombs avatar Nov 27 '21 09:11 tmccombs

Thank you @tmccombs! That does help, I'll probably fork the project and start making changes to see if I can keep range info as a metadata of the generated json file.

alfie-far avatar Nov 27 '21 11:11 alfie-far