diff --git a/README.md b/README.md index 904c05f9..68d1c97c 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/scoping/langs/hcl.rs b/src/scoping/langs/hcl.rs index 777667fd..5998bd05 100644 --- a/src/scoping/langs/hcl.rs +++ b/src/scoping/langs/hcl.rs @@ -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. @@ -108,6 +110,14 @@ impl From 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 diff --git a/tests/langs/mod.rs b/tests/langs/mod.rs index c384dc00..9ee52517 100644 --- a/tests/langs/mod.rs +++ b/tests/langs/mod.rs @@ -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"), diff --git a/tests/langs/snapshots/r#mod__langs__base.tf_locals-block.snap b/tests/langs/snapshots/r#mod__langs__base.tf_locals-block.snap new file mode 100644 index 00000000..9d9cc8e9 --- /dev/null +++ b/tests/langs/snapshots/r#mod__langs__base.tf_locals-block.snap @@ -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: "^ "