From b81b89580cbd8fa50ba4c11947006a32f28216fb Mon Sep 17 00:00:00 2001 From: Julien Delange Date: Mon, 6 Jan 2025 21:05:52 -0500 Subject: [PATCH] add elixir support --- crates/cli/src/file_utils.rs | 1 + crates/static-analysis-kernel/build.rs | 9 +++++++++ .../src/analysis/analyze.rs | 1 + .../src/analysis/tree_sitter.rs | 18 ++++++++++++++++++ .../static-analysis-kernel/src/model/common.rs | 3 +++ 5 files changed, 32 insertions(+) diff --git a/crates/cli/src/file_utils.rs b/crates/cli/src/file_utils.rs index a61ad9dc..97ef3312 100644 --- a/crates/cli/src/file_utils.rs +++ b/crates/cli/src/file_utils.rs @@ -16,6 +16,7 @@ use kernel::model::violation::Violation; static FILE_EXTENSIONS_PER_LANGUAGE_LIST: &[(Language, &[&str])] = &[ (Language::Csharp, &["cs"]), (Language::Dockerfile, &["docker", "dockerfile"]), + (Language::Elixir, &["ex"]), (Language::Go, &["go"]), (Language::Java, &["java"]), (Language::JavaScript, &["js", "jsx", "mjs", "cjs"]), diff --git a/crates/static-analysis-kernel/build.rs b/crates/static-analysis-kernel/build.rs index 22c833c2..5926e359 100644 --- a/crates/static-analysis-kernel/build.rs +++ b/crates/static-analysis-kernel/build.rs @@ -69,6 +69,15 @@ fn main() { files: vec!["parser.c".to_string()], cpp: false, }, + TreeSitterProject { + name: "tree-sitter-elixir".to_string(), + compilation_unit: "tree-sitter-elixir".to_string(), + repository: "https://github.com/elixir-lang/tree-sitter-elixir.git".to_string(), + commit_hash: "02a6f7fd4be28dd94ee4dd2ca19cb777053ea74e".to_string(), + build_dir: "src".into(), + files: vec!["parser.c".to_string(), "scanner.c".to_string()], + cpp: false, + }, TreeSitterProject { name: "tree-sitter-go".to_string(), compilation_unit: "tree-sitter-go".to_string(), diff --git a/crates/static-analysis-kernel/src/analysis/analyze.rs b/crates/static-analysis-kernel/src/analysis/analyze.rs index 3bd69198..848821c0 100644 --- a/crates/static-analysis-kernel/src/analysis/analyze.rs +++ b/crates/static-analysis-kernel/src/analysis/analyze.rs @@ -33,6 +33,7 @@ fn get_lines_to_ignore(code: &str, language: &Language) -> LinesToIgnore { Language::Python | Language::Starlark | Language::Dockerfile + | Language::Elixir | Language::Ruby | Language::Terraform | Language::Yaml diff --git a/crates/static-analysis-kernel/src/analysis/tree_sitter.rs b/crates/static-analysis-kernel/src/analysis/tree_sitter.rs index a92f2301..61ce808c 100644 --- a/crates/static-analysis-kernel/src/analysis/tree_sitter.rs +++ b/crates/static-analysis-kernel/src/analysis/tree_sitter.rs @@ -12,6 +12,7 @@ pub fn get_tree_sitter_language(language: &Language) -> tree_sitter::Language { extern "C" { fn tree_sitter_c_sharp() -> tree_sitter::Language; fn tree_sitter_dockerfile() -> tree_sitter::Language; + fn tree_sitter_elixir() -> tree_sitter::Language; fn tree_sitter_go() -> tree_sitter::Language; fn tree_sitter_java() -> tree_sitter::Language; fn tree_sitter_javascript() -> tree_sitter::Language; @@ -37,6 +38,7 @@ pub fn get_tree_sitter_language(language: &Language) -> tree_sitter::Language { Language::Csharp => unsafe { tree_sitter_c_sharp() }, Language::Dockerfile => unsafe { tree_sitter_dockerfile() }, Language::Go => unsafe { tree_sitter_go() }, + Language::Elixir => unsafe { tree_sitter_elixir() }, Language::Java => unsafe { tree_sitter_java() }, Language::JavaScript => unsafe { tree_sitter_javascript() }, Language::Kotlin => unsafe { tree_sitter_kotlin() }, @@ -646,6 +648,22 @@ print("Hello, World!") assert_eq!("program", t.root_node().kind()); } + #[test] + fn test_elixir_get_tree() { + let source_code = r#" +defmodule Sum do + def add(a, b) do + a + b + end +end +"#; + let t = get_tree(source_code, &Language::Elixir); + assert!(t.is_some()); + let t = t.unwrap(); + assert!(!t.root_node().has_error()); + assert_eq!("source", t.root_node().kind()); + } + #[test] fn test_sql_get_tree() { let source_code = r#" diff --git a/crates/static-analysis-kernel/src/model/common.rs b/crates/static-analysis-kernel/src/model/common.rs index 1ff61ba1..4e727b70 100644 --- a/crates/static-analysis-kernel/src/model/common.rs +++ b/crates/static-analysis-kernel/src/model/common.rs @@ -25,6 +25,8 @@ pub enum Language { Csharp, #[serde(rename = "DOCKERFILE")] Dockerfile, + #[serde(rename = "ELIXIR")] + Elixir, #[serde(rename = "GO")] Go, #[serde(rename = "JAVA")] @@ -93,6 +95,7 @@ impl fmt::Display for Language { Self::Csharp => "c#", Self::Dockerfile => "dockerfile", Self::Go => "go", + Self::Elixir => "elixir", Self::Java => "java", Self::JavaScript => "javascript", Self::Json => "json",