Skip to content

Commit 0b43ebe

Browse files
committed
Add support for terraform. Closes #304
1 parent f6e4840 commit 0b43ebe

8 files changed

Lines changed: 161 additions & 0 deletions

File tree

Cargo.lock

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ tree-sitter-typescript = "0.23.2"
8080
tree-sitter-vhdl = "<2"
8181
tree-sitter-yaml = "0.7.2"
8282
tree-sitter-zig = "<2"
83+
tree-sitter-hcl = "1.1"
8384
codebook-tree-sitter-just = "<0.2.0"
8485
codebook-tree-sitter-latex = "<0.7.0"
8586
codebook-tree-sitter-typst = "<0.13.0"

crates/codebook/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ codebook-tree-sitter-typst.workspace = true
5858
tree-sitter-yaml.workspace = true
5959
tree-sitter-zig.workspace = true
6060
tree-sitter-c-sharp.workspace = true
61+
tree-sitter-hcl.workspace = true
6162
tree-sitter.workspace = true
6263
unicase.workspace = true
6364
unicode-script.workspace = true

crates/codebook/src/queries.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ pub enum LanguageType {
3737
VHDL,
3838
YAML,
3939
Zig,
40+
Hcl,
4041
}
4142

4243
impl FromStr for LanguageType {
@@ -312,6 +313,13 @@ pub static LANGUAGE_SETTINGS: &[LanguageSetting] = &[
312313
query: include_str!("queries/vhdl.scm"),
313314
extensions: &["vhd", "vhdl"],
314315
},
316+
LanguageSetting {
317+
type_: LanguageType::Hcl,
318+
ids: &["hcl", "terraform"],
319+
dictionary_ids: &["hcl"],
320+
query: include_str!("queries/hcl.scm"),
321+
extensions: &["tf", "tfvars"],
322+
},
315323
];
316324

317325
#[derive(Debug)]
@@ -360,6 +368,7 @@ impl LanguageSetting {
360368
LanguageType::VHDL => Some(tree_sitter_vhdl::LANGUAGE.into()),
361369
LanguageType::YAML => Some(tree_sitter_yaml::LANGUAGE.into()),
362370
LanguageType::Zig => Some(tree_sitter_zig::LANGUAGE.into()),
371+
LanguageType::Hcl => Some(tree_sitter_hcl::LANGUAGE.into()),
363372
}
364373
}
365374
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
(comment) @comment
2+
3+
(attribute (identifier) @identifier.field)
4+
5+
(template_literal) @string

crates/codebook/tests/languages/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ mod test_erlang;
1111
mod test_files;
1212
mod test_go;
1313
mod test_haskell;
14+
mod test_hcl;
1415
mod test_java;
1516
mod test_javascript;
1617
mod test_just;
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
use codebook::queries::LanguageType;
2+
3+
use super::utils::{assert_spelling, assert_spelling_at};
4+
5+
#[test]
6+
fn test_hcl_attribute_identifiers() {
7+
let sample_text = r#"
8+
resource "aws_instance" "web_server" {
9+
instnace_type = "t3.micro"
10+
descriptin = "Valid text"
11+
}
12+
"#;
13+
14+
assert_spelling(
15+
LanguageType::Hcl,
16+
sample_text,
17+
&["instnace", "descriptin"],
18+
&["resource", "aws_instance", "web_server"],
19+
);
20+
}
21+
22+
#[test]
23+
fn test_hcl_strings_and_comments() {
24+
let sample_text = r#"
25+
# Comment with a speling error.
26+
27+
/* Multi-line comment
28+
with an error on the seconnd line. */
29+
30+
variable "region" {
31+
description = "Cloud regon for the deploymant."
32+
default = "eu-west-1"
33+
}
34+
35+
resource "aws_instance" "web_server" {
36+
user_data = <<-EOT
37+
Configure the servver for this environment.
38+
Enable applicaton logging for ${var.region}.
39+
EOT
40+
}
41+
"#;
42+
43+
assert_spelling_at(
44+
LanguageType::Hcl,
45+
sample_text,
46+
&[
47+
("speling", &[0]),
48+
("seconnd", &[0]),
49+
("regon", &[0]),
50+
("deploymant", &[0]),
51+
("servver", &[0]),
52+
("applicaton", &[0]),
53+
],
54+
);
55+
}
56+
57+
#[test]
58+
fn test_hcl_only_checks_identifier_definitions() {
59+
let sample_text = r#"
60+
variable "source_value" {
61+
default = "valid"
62+
}
63+
64+
locals {
65+
misspeled_value = var.source_value
66+
}
67+
68+
output "result" {
69+
value = local.misspeled_value
70+
}
71+
"#;
72+
73+
assert_spelling_at(
74+
LanguageType::Hcl,
75+
sample_text,
76+
&[
77+
// The attribute name is checked at its definition, but the same
78+
// text in the traversal expression is not checked as a usage.
79+
("misspeled", &[0]),
80+
],
81+
);
82+
}
83+
84+
#[test]
85+
fn test_hcl_string_interpolation() {
86+
let sample_text = r#"
87+
variable "region" {
88+
default = "eu-west-1"
89+
}
90+
91+
locals {
92+
message = "Deploy to the regon ${var.misspeled_reference}"
93+
}
94+
"#;
95+
96+
assert_spelling(
97+
LanguageType::Hcl,
98+
sample_text,
99+
&["regon"],
100+
&["misspeled_reference"],
101+
);
102+
}

examples/example.tf

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Provision the applicaton infrastructure for the staging enviroment.
2+
3+
terraform {
4+
required_version = ">= 1.5.0"
5+
}
6+
7+
variable "region" {
8+
description = "Cloud regon used for the deployment."
9+
type = string
10+
default = "eu-west-1"
11+
}
12+
13+
resource "aws_instance" "web_server" {
14+
ami = "ami-12345678"
15+
instance_type = "t3.micro"
16+
17+
tags = {
18+
Name = "Applicaton server"
19+
Description = "Handles incoming requsts"
20+
}
21+
22+
user_data = <<-EOT
23+
Configure the servver for the staging environment.
24+
Enable applicaton logging.
25+
EOT
26+
}
27+
28+
output "instance_id" {
29+
description = "Identifer of the created instance."
30+
value = aws_instance.web_server.id
31+
}

0 commit comments

Comments
 (0)