Skip to content

Commit

Permalink
feat(hcl): Scope locals blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
alexpovel committed Aug 4, 2024
1 parent a60a754 commit c22c475
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1397,6 +1397,7 @@ Language scopes:
- output: `output` blocks (in their entirety)
- provider: `provider` blocks (in their entirety)
- terraform: `terraform` blocks (in their entirety)
- locals: `locals` blocks (in their entirety)
- variables: Variable declarations and usages
- resource-names: `resource` name declarations and usages
- resource-types: `resource` type declarations and usages
Expand Down
10 changes: 10 additions & 0 deletions src/scoping/langs/hcl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ pub enum PreparedHclQuery {
Provider,
/// `terraform` blocks (in their entirety).
Terraform,
/// `locals` blocks (in their entirety).
Locals,
/// Variable declarations and usages.
Variables,
/// `resource` name declarations and usages.
Expand Down Expand Up @@ -108,6 +110,14 @@ impl From<PreparedHclQuery> for TSQuery {
) @block
"#
}
PreparedHclQuery::Locals => {
r#"
(block
(identifier) @name
(#eq? @name "locals")
) @block
"#
}
PreparedHclQuery::Variables => {
// Capturing nodes with names, such as `@id`, requires names to be
// unique across the *entire* query, else things break. Hence, us
Expand Down
5 changes: 5 additions & 0 deletions tests/langs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,11 @@ impl InScopeLinePart {
include_str!("hcl/base.tf"),
Hcl::new(CodeQuery::Prepared(PreparedHclQuery::Terraform)),
)]
#[case(
"base.tf_locals-block",
include_str!("hcl/base.tf"),
Hcl::new(CodeQuery::Prepared(PreparedHclQuery::Locals)),
)]
#[case(
"base.tf_variables",
include_str!("hcl/base.tf"),
Expand Down
16 changes: 16 additions & 0 deletions tests/langs/snapshots/r#mod__langs__base.tf_locals-block.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
source: tests/langs/mod.rs
expression: inscope_parts
---
- n: 51
l: "locals {\n"
m: ^^^^^^^^^^
- n: 52
l: " app_env = \"testing\"\n"
m: ^^^^^^^^^^^^^^^^^^^^^^^^^
- n: 53
l: " version = \"1.0.0\"\n"
m: ^^^^^^^^^^^^^^^^^^^^^^^
- n: 54
l: "}\n"
m: "^ "

0 comments on commit c22c475

Please sign in to comment.