hashicorp / hcl2

Former temporary home for experimental new version of HCL
https://github.com/hashicorp/hcl
Mozilla Public License 2.0
375 stars 64 forks source link

hclsyntax: Parse indexing brackets with a string literal as a traversal #114

Closed apparentlymart closed 5 years ago

apparentlymart commented 5 years ago

A sequence like "foo" is represented in the AST as a TemplateExpr with a single string literal inside rather than as a string literal node directly, so we need to recognize that situation during parsing and treat it as a special case so we can get the intended behavior of representing that index as a traversal step rather than as a dynamic index operation.

Most of the time this distinction doesn't matter, but it's important for static analysis use-cases. In particular, hcl.AbsTraversalForExpr will now accept an expression like foo["bar"] where before it would've rejected it.

This also includes a better error message for when an expression cannot be recognized as a single traversal. There isn't really any context here to return a direct reference to the construct that was problematic, which is what we'd ideally do, but at least this new message includes a summary of what is allowed and some examples of things that are not allowed as an aid to understanding what "static variable reference" means.

codecov-io commented 5 years ago

Codecov Report

Merging #114 into master will increase coverage by 0.01%. The diff coverage is 84.61%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #114      +/-   ##
==========================================
+ Coverage   68.75%   68.76%   +0.01%     
==========================================
  Files          98       98              
  Lines       10301    10313      +12     
==========================================
+ Hits         7082     7092      +10     
- Misses       2894     2895       +1     
- Partials      325      326       +1
Impacted Files Coverage Δ
hcl/hclsyntax/parser.go 66.36% <100%> (+0.17%) :arrow_up:
hcl/traversal_for_expr.go 100% <100%> (ø) :arrow_up:
hcl/hclsyntax/expression_template.go 28.94% <60%> (+1.42%) :arrow_up:

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update 4fba5e1...41c2d04. Read the comment docs.