Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ tree-sitter-typescript = "0.23.2"
tree-sitter-vhdl = "<2"
tree-sitter-yaml = "0.7.2"
tree-sitter-zig = "<2"
tree-sitter-hcl = "1.1"
codebook-tree-sitter-just = "<0.2.0"
codebook-tree-sitter-latex = "<0.7.0"
codebook-tree-sitter-typst = "<0.13.0"
Expand Down
1 change: 1 addition & 0 deletions crates/codebook/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ codebook-tree-sitter-typst.workspace = true
tree-sitter-yaml.workspace = true
tree-sitter-zig.workspace = true
tree-sitter-c-sharp.workspace = true
tree-sitter-hcl.workspace = true
tree-sitter.workspace = true
unicase.workspace = true
unicode-script.workspace = true
Expand Down
9 changes: 9 additions & 0 deletions crates/codebook/src/queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ pub enum LanguageType {
VHDL,
YAML,
Zig,
Hcl,
}

impl FromStr for LanguageType {
Expand Down Expand Up @@ -312,6 +313,13 @@ pub static LANGUAGE_SETTINGS: &[LanguageSetting] = &[
query: include_str!("queries/vhdl.scm"),
extensions: &["vhd", "vhdl"],
},
LanguageSetting {
type_: LanguageType::Hcl,
ids: &["hcl", "terraform"],
dictionary_ids: &["hcl"],
query: include_str!("queries/hcl.scm"),
extensions: &["tf", "tfvars"],
},
];

#[derive(Debug)]
Expand Down Expand Up @@ -360,6 +368,7 @@ impl LanguageSetting {
LanguageType::VHDL => Some(tree_sitter_vhdl::LANGUAGE.into()),
LanguageType::YAML => Some(tree_sitter_yaml::LANGUAGE.into()),
LanguageType::Zig => Some(tree_sitter_zig::LANGUAGE.into()),
LanguageType::Hcl => Some(tree_sitter_hcl::LANGUAGE.into()),
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions crates/codebook/src/queries/hcl.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
(comment) @comment

(attribute (identifier) @identifier.field)

(template_literal) @string
1 change: 1 addition & 0 deletions crates/codebook/tests/languages/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ mod test_erlang;
mod test_files;
mod test_go;
mod test_haskell;
mod test_hcl;
mod test_java;
mod test_javascript;
mod test_just;
Expand Down
94 changes: 94 additions & 0 deletions crates/codebook/tests/languages/test_hcl.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
use codebook::queries::LanguageType;

use super::utils::{assert_spelling, assert_spelling_at};

#[test]
fn test_hcl_attribute_identifiers() {
let sample_text = r#"
resource "aws_instance" "web_server" {
instnace_type = "t3.micro"
descriptin = "Valid text"
}
"#;

assert_spelling(
LanguageType::Hcl,
sample_text,
&["instnace", "descriptin"],
&["resource", "aws_instance", "web_server"],
);
}

#[test]
fn test_hcl_strings_and_comments() {
let sample_text = r#"
# Comment with a speling error.

/* Multi-line comment
with an error on the seconnd line. */

variable "region" {
description = "Cloud regon for the deploymant."
default = "eu-west-1"
}

resource "aws_instance" "web_server" {
user_data = <<-EOT
Configure the servver for this environment.
Enable applicaton logging for ${var.region}.
EOT
}
"#;

assert_spelling_at(
LanguageType::Hcl,
sample_text,
&[
("speling", &[0]),
("seconnd", &[0]),
("regon", &[0]),
("deploymant", &[0]),
("servver", &[0]),
("applicaton", &[0]),
],
);
}

#[test]
fn test_hcl_only_checks_identifier_definitions() {
let sample_text = r#"
variable "source_value" {
default = "valid"
}

locals {
misspeled_value = var.source_value
}

output "result" {
value = local.misspeled_value
}
"#;

assert_spelling_at(LanguageType::Hcl, sample_text, &[("misspeled", &[0])]);
}

#[test]
fn test_hcl_string_interpolation() {
let sample_text = r#"
variable "region" {
default = "eu-west-1"
}

locals {
message = "Deploy to the regon ${var.misspeled_reference}"
}
"#;

assert_spelling(
LanguageType::Hcl,
sample_text,
&["regon"],
&["misspeled_reference"],
);
}
31 changes: 31 additions & 0 deletions examples/example.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Provision the applicaton infrastructure for the staging enviroment.

terraform {
required_version = ">= 1.5.0"
}

variable "region" {
description = "Cloud regon used for the deployment."
type = string
default = "eu-west-1"
}

resource "aws_instance" "web_server" {
ami = "ami-12345678"
instance_type = "t3.micro"

tags = {
Name = "Applicaton server"
Description = "Handles incoming requsts"
}

user_data = <<-EOT
Configure the servver for the staging environment.
Enable applicaton logging.
EOT
}

output "instance_id" {
description = "Identifer of the created instance."
value = aws_instance.web_server.id
}