Skip to content

Commit

Permalink
Merge pull request #149 from hashicorp/fix-block-label-brace
Browse files Browse the repository at this point in the history
(TFECO-7531) fix(hcl): don't parse expressions containing an open curly brace as blocks
  • Loading branch information
ansgarm authored Sep 20, 2024
2 parents 2eeee06 + cc2b4d4 commit d1b19d3
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 6 deletions.
6 changes: 3 additions & 3 deletions src/_main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ repository:
block:
name: meta.block.hcl
comment: This will match HCL blocks like `thing1 "one" "two" {` or `thing2 {`
begin: ([\w][\-\w]*)([^?\r\n]*)(\{)
begin: ([\w][\-\w]*)(([^\S\r\n]*([\w][\-_\w]*|\"[^\"\r\n]*\"))*)[^\S\r\n]*(\{)
beginCaptures:
"1":
patterns:
Expand All @@ -89,9 +89,9 @@ repository:
comment: Block label (String Literal)
match: '\"[^\"\r\n]*\"'
- name: variable.other.enummember.hcl
comment: Block label (Indentifier)
comment: Block label (Identifier)
match: "[[:alpha:]][[:alnum:]_-]*"
"3":
"5":
name: punctuation.section.block.begin.hcl
end: \}
endCaptures:
Expand Down
6 changes: 3 additions & 3 deletions syntaxes/hcl.tmGrammar.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions syntaxes/terraform.tmGrammar.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions tests/snapshot/hcl/block_labels.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ block-single-char-indentifier-newline a {
byte_match_statement_rules = local.enabled && var.byte_match_statement_rules != null ? {
}

# braces are fun (now)
a = not_a_block == "{"
brace_label "{" {}
path "secrets/data/users/{{identity.entity.name}}/*" {
capabilities = ["create", "update", "patch", "read", "delete", "list"]
}
Expand Down
22 changes: 22 additions & 0 deletions tests/snapshot/hcl/block_labels.hcl.snap
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,28 @@
>}
#^ source.hcl meta.braces.hcl punctuation.section.braces.end.hcl
>
># braces are fun (now)
#^ source.hcl comment.line.number-sign.hcl punctuation.definition.comment.hcl
# ^^^^^^^^^^^^^^^^^^^^^ source.hcl comment.line.number-sign.hcl
>a = not_a_block == "{"
#^ source.hcl variable.declaration.hcl variable.other.readwrite.hcl
# ^ source.hcl variable.declaration.hcl
# ^ source.hcl variable.declaration.hcl keyword.operator.assignment.hcl
# ^ source.hcl variable.declaration.hcl
# ^^^^^^^^^^^^ source.hcl
# ^^ source.hcl keyword.operator.hcl
# ^ source.hcl
# ^ source.hcl string.quoted.double.hcl punctuation.definition.string.begin.hcl
# ^ source.hcl string.quoted.double.hcl
# ^ source.hcl string.quoted.double.hcl punctuation.definition.string.end.hcl
>brace_label "{" {}
#^^^^^^^^^^^ source.hcl meta.block.hcl entity.name.type.hcl
# ^ source.hcl meta.block.hcl
# ^^^ source.hcl meta.block.hcl variable.other.enummember.hcl
# ^ source.hcl meta.block.hcl
# ^ source.hcl meta.block.hcl punctuation.section.block.begin.hcl
# ^ source.hcl meta.block.hcl punctuation.section.block.end.hcl
>
>path "secrets/data/users/{{identity.entity.name}}/*" {
#^^^^ source.hcl meta.block.hcl entity.name.type.hcl
# ^ source.hcl meta.block.hcl
Expand Down

0 comments on commit d1b19d3

Please sign in to comment.