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

deprecated_index: restore evaluation of JSON expressions #101

Merged
merged 1 commit into from
May 31, 2023

Conversation

bendrucker
Copy link
Member

@bendrucker bendrucker commented May 31, 2023

  • Fixes a false negative for legacy dot syntax usage in JSON files.
  • Adds test coverage against valid JSON expressions.

Background

#90 fixed the problem of duplicate emitted issues for nested expressions, since WalkExpressions visits them recursively for HCL files. In doing so, it both fixed a bug related to good JSON files (#100) and created a regression in detecting dot syntax usage in JSON. There was no test coverage so I'm only now noticing this in hindsight.

Changes

This PR restores evaluation of JSON expressions, without reintroducing the bug described in #100. That issue seems to describe a case where hclsyntax.LexExpression is called on a raw range of source bytes:

tokens, diags := hclsyntax.LexExpression(bytes, filename, variable.SourceRange().Start)

When this range has escaped quotes, which are valid in the surrounding context, the raw content is not valid HCL. Given the previous implementation a more correct approach for JSON expressions would require parsing as JSON at some level to parse the escaped quotes:

var str
err := json.Unmarshal(bytes, &str)

Since WalkExpressions is not recursive for JSON, we still need to call Variables(). But from there we can then just pass each traversal to the existing checkLegacyTraversalIndex() function written for HCL, and it works!

Copy link
Member

@wata727 wata727 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Smart way! 👏

@bendrucker bendrucker merged commit f4458e8 into main May 31, 2023
@bendrucker bendrucker deleted the deprecated-index-json branch May 31, 2023 17:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants