Skip to content

HCL: allow if and in as attribute and object key names#7542

Closed
scuba10steve wants to merge 1 commit intomainfrom
stompkins/hcl-allow-if-in-as-names
Closed

HCL: allow if and in as attribute and object key names#7542
scuba10steve wants to merge 1 commit intomainfrom
stompkins/hcl-allow-if-in-as-names

Conversation

@scuba10steve
Copy link
Copy Markdown
Contributor

@scuba10steve scuba10steve commented May 1, 2026

Summary

  • The HCL lexer reserves IF and IN as dedicated tokens for use in for-expressions, but the HCL spec does not reserve these as keywords at the body level. Terraform itself accepts if and in as attribute names and object keys.

  • Extend attribute and qualifiedIdentifier rules in HCLParser.g4 to accept IF and IN alongside Identifier. The visitor walks qualifiedIdentifier children in source order so dotted-name positions stay correct when alternatives are interleaved.

  • For-expression parsing (for x in xs : x if cond) is unaffected — those tokens are still consumed at the for-rule level. Verified by the existing HclForTest suite.

  • Fixes HclParser might fail if you use if as property name #6446

Test plan

  • rewrite-hcl:test — 9 new + existing tests pass:
    • usingIfAsAttributeName / usingInAsAttributeName — issue's reproducer
    • keywordsAsTopLevelAttributes — body-level form, no enclosing block
    • keywordAttributeAlongsideForExpression — coexistence with for ... in ... if ...
    • keywordAttributeWithCommentAndUnusualSpacing — formatting variations
    • keywordsAsObjectKeys / keywordObjectKeyMixedWithRegularKeys — object literal keys
  • rewrite-hcl:check — license, lint, all green
  • Full repo ./gradlew buildBUILD SUCCESSFUL in 10m 13s, 442 tasks, no regressions in any downstream consumer
  • HclForTest — for-expression regression coverage still passes

The HCL lexer reserves `IF` and `IN` as dedicated tokens for use in
for-expressions, but the spec doesn't reserve these as keywords at the
body level. Terraform itself accepts `if` and `in` as attribute names
and object keys, so the parser should too.

Extend the `attribute` and `qualifiedIdentifier` rules to accept `IF`
and `IN` alongside `Identifier`. The visitor walks `qualifiedIdentifier`
children in source order to keep dotted-name positions correct when the
alternatives are interleaved.

Fixes #6446
@timtebeek
Copy link
Copy Markdown
Member

Thanks! We'd had a parallel effort open as well with a slightly different approach:

For both I'm wondering whether we should have these keywords called out in the grammar and generated parser, or whether we can better scope when to interpret and if as a conditional. No immediate suggestions here, but thought to call it out. Greg or Knut might have some ideas as well.

@scuba10steve
Copy link
Copy Markdown
Contributor Author

Thanks! We'd had a parallel effort open as well with a slightly different approach:

For both I'm wondering whether we should have these keywords called out in the grammar and generated parser, or whether we can better scope when to interpret and if as a conditional. No immediate suggestions here, but thought to call it out. Greg or Knut might have some ideas as well.

ha!

If you already had something going let's just take yours. I haven't really tested this at all so it was just something I threw together.

@github-project-automation github-project-automation Bot moved this from In Progress to Done in OpenRewrite May 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

HclParser might fail if you use if as property name

2 participants