Skip to content

Commit

Permalink
feat(hcl): Scope resource blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
alexpovel committed Aug 4, 2024
1 parent 6b8dcdc commit 963d9a4
Show file tree
Hide file tree
Showing 4 changed files with 269 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1392,6 +1392,7 @@ Language scopes:

Possible values:
- variable: `variable` blocks (in their entirety)
- resource: `resource` 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 @@ -15,6 +15,8 @@ pub type HclQuery = CodeQuery<CustomHclQuery, PreparedHclQuery>;
pub enum PreparedHclQuery {
/// `variable` blocks (in their entirety).
Variable,
/// `resource` blocks (in their entirety).
Resource,
/// Variable declarations and usages.
Variables,
/// `resource` name declarations and usages.
Expand Down Expand Up @@ -58,6 +60,14 @@ impl From<PreparedHclQuery> for TSQuery {
) @block
"#
}
PreparedHclQuery::Resource => {
r#"
(block
(identifier) @name
(#match? @name "resource")
) @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 @@ -202,6 +202,11 @@ impl InScopeLinePart {
include_str!("hcl/base.tf"),
Hcl::new(CodeQuery::Prepared(PreparedHclQuery::Variable)),
)]
#[case(
"base.tf_resource-block",
include_str!("hcl/base.tf"),
Hcl::new(CodeQuery::Prepared(PreparedHclQuery::Resource)),
)]
#[case(
"base.tf_variables",
include_str!("hcl/base.tf"),
Expand Down
253 changes: 253 additions & 0 deletions tests/langs/snapshots/r#mod__langs__base.tf_resource-block.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,253 @@
---
source: tests/langs/mod.rs
expression: inscope_parts
---
- n: 73
l: "resource \"aws_instance\" \"app_server\" {\n"
m: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- n: 74
l: " count = var.instance_count\n"
m: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- n: 75
l: " ami = data.aws_ami.latest_ubuntu.id\n"
m: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- n: 76
l: " instance_type = \"t2.micro\"\n"
m: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- n: 77
l: " tags = {\n"
m: ^^^^^^^^^^^^
- n: 78
l: " Name = \"${var.app_name}-${count.index}\"\n"
m: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- n: 79
l: " Environment = local.app_env\n"
m: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- n: 80
l: " Version = local.version\n"
m: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- n: 81
l: " }\n"
m: ^^^^^
- n: 82
l: "\n"
m: ^^
- n: 83
l: " // Dynamic block for user data\n"
m: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- n: 84
l: " dynamic \"user_data\" {\n"
m: ^^^^^^^^^^^^^^^^^^^^^^^^^^^
- n: 85
l: " for_each = var.enable_feature_x ? [1] : []\n"
m: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- n: 86
l: " content {\n"
m: ^^^^^^^^^^^^^^^
- n: 87
l: " data = <<EOF\n"
m: ^^^^^^^^^^^^^^^^^^^^
- n: 88
l: "#!/bin/bash\n"
m: ^^^^^^^^^^^^^
- n: 89
l: "echo \"Feature ${upper(var.app_name)} enabled\"\n"
m: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- n: 90
l: "EOF\n"
m: ^^^^^
- n: 91
l: " }\n"
m: ^^^^^^^
- n: 92
l: " }\n"
m: ^^^^^
- n: 93
l: "}\n"
m: "^ "
- n: 113
l: "resource \"aws_s3_bucket\" \"app_bucket\" {\n"
m: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- n: 114
l: " bucket = var.enable_feature_x ? \"${var.app_name}-feature-x\" : \"${var.app_name}\"\n"
m: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- n: 115
l: " acl = \"private\"\n"
m: ^^^^^^^^^^^^^^^^^^^^^^^^
- n: 116
l: "}\n"
m: "^ "
- n: 119
l: "resource \"null_resource\" \"template_example\" {\n"
m: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- n: 120
l: " provisioner \"local-exec\" {\n"
m: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- n: 121
l: " command = <<EOT\n"
m: ^^^^^^^^^^^^^^^^^^^^^
- n: 122
l: "echo \"Application: ${var.app_name}\"\n"
m: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- n: 123
l: "echo \"Environment: ${local.app_env}\"\n"
m: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- n: 124
l: "EOT\n"
m: ^^^^^
- n: 125
l: " }\n"
m: ^^^^^
- n: 126
l: "}\n"
m: "^ "
- n: 129
l: "resource \"aws_security_group\" \"web_sg\" {\n"
m: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- n: 130
l: " name = \"${var.app_name}-web-sg\"\n"
m: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- n: 131
l: " description = \"Web security group for ${var.app_name}\"\n"
m: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- n: 132
l: " vpc_id = module.network.vpc_id\n"
m: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- n: 133
l: "\n"
m: ^^
- n: 134
l: " ingress {\n"
m: ^^^^^^^^^^^^^
- n: 135
l: " from_port = 80\n"
m: ^^^^^^^^^^^^^^^^^^^^^^
- n: 136
l: " to_port = 80\n"
m: ^^^^^^^^^^^^^^^^^^^^^^
- n: 137
l: " protocol = \"tcp\"\n"
m: ^^^^^^^^^^^^^^^^^^^^^^^^^^^
- n: 138
l: " cidr_blocks = [\"0.0.0.0/0\"]\n"
m: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- n: 139
l: " }\n"
m: ^^^^^
- n: 140
l: "\n"
m: ^^
- n: 141
l: " egress {\n"
m: ^^^^^^^^^^^^
- n: 142
l: " from_port = 0\n"
m: ^^^^^^^^^^^^^^^^^^^^^
- n: 143
l: " to_port = 0\n"
m: ^^^^^^^^^^^^^^^^^^^^^
- n: 144
l: " protocol = \"-1\"\n"
m: ^^^^^^^^^^^^^^^^^^^^^^^^^^
- n: 145
l: " cidr_blocks = [\"0.0.0.0/0\"]\n"
m: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- n: 146
l: " }\n"
m: ^^^^^
- n: 147
l: "}\n"
m: "^ "
- n: 150
l: "resource \"aws_iam_role\" \"app_role\" {\n"
m: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- n: 151
l: " name = \"${var.app_name}_role\"\n"
m: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- n: 152
l: "\n"
m: ^^
- n: 153
l: " assume_role_policy = jsonencode({\n"
m: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- n: 154
l: " Version = \"2012-10-17\",\n"
m: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- n: 155
l: " Statement = [\n"
m: ^^^^^^^^^^^^^^^^^^^
- n: 156
l: " {\n"
m: ^^^^^^^^^
- n: 157
l: " Action = \"sts:AssumeRole\",\n"
m: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- n: 158
l: " Effect = \"Allow\",\n"
m: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- n: 159
l: " Principal = {\n"
m: ^^^^^^^^^^^^^^^^^^^^^^^
- n: 160
l: " Service = \"ec2.amazonaws.com\"\n"
m: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- n: 161
l: " }\n"
m: ^^^^^^^^^^^
- n: 162
l: " }\n"
m: ^^^^^^^^^
- n: 163
l: " ]\n"
m: ^^^^^^^
- n: 164
l: " })\n"
m: ^^^^^^
- n: 165
l: "\n"
m: ^^
- n: 166
l: " inline_policy {\n"
m: ^^^^^^^^^^^^^^^^^^^
- n: 167
l: " name = \"app_policy\"\n"
m: ^^^^^^^^^^^^^^^^^^^^^^^^^^^
- n: 168
l: "\n"
m: ^^
- n: 169
l: " policy = jsonencode({\n"
m: ^^^^^^^^^^^^^^^^^^^^^^^^^^^
- n: 170
l: " Version = \"2012-10-17\",\n"
m: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- n: 171
l: " Statement = [\n"
m: ^^^^^^^^^^^^^^^^^^^^^
- n: 172
l: " {\n"
m: ^^^^^^^^^^^
- n: 173
l: " Action = [\"s3:ListBucket\"],\n"
m: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- n: 174
l: " Effect = \"Allow\",\n"
m: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- n: 175
l: " Resource = \"*\"\n"
m: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- n: 176
l: " }\n"
m: ^^^^^^^^^^^
- n: 177
l: " ]\n"
m: ^^^^^^^^^
- n: 178
l: " })\n"
m: ^^^^^^^^
- n: 179
l: " }\n"
m: ^^^^^
- n: 180
l: "}\n"
m: "^ "

0 comments on commit 963d9a4

Please sign in to comment.