Including range (start and end) value in the generated JSON
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
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?
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.
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.
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.