Skip to content
Merged
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
89 changes: 68 additions & 21 deletions crates/ast-engine/src/language.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,26 @@ mod test {
use super::*;
use crate::tree_sitter::{LanguageExt, StrDoc, TSLanguage};

// Shared helpers for test Language impls backed by tree-sitter-typescript.
fn tsx_kind_to_id(kind: &str) -> u16 {
let ts_lang: TSLanguage = tree_sitter_typescript::LANGUAGE_TSX.into();
ts_lang.id_for_node_kind(kind, /* named */ true)
}

fn tsx_field_to_id(field: &str) -> Option<u16> {
let ts_lang: TSLanguage = tree_sitter_typescript::LANGUAGE_TSX.into();
ts_lang.field_id_for_name(field).map(|f| f.get())
}

fn tsx_ts_language() -> TSLanguage {
tree_sitter_typescript::LANGUAGE_TSX.into()
}

#[derive(Clone, Debug)]
pub struct Tsx;
impl Language for Tsx {
fn kind_to_id(&self, kind: &str) -> u16 {
let ts_lang: TSLanguage = tree_sitter_typescript::LANGUAGE_TSX.into();
ts_lang.id_for_node_kind(kind, /* named */ true)
tsx_kind_to_id(kind)
}
fn field_to_id(&self, field: &str) -> Option<u16> {
self.get_ts_language()
Expand All @@ -112,37 +126,70 @@ mod test {
}
impl LanguageExt for Tsx {
fn get_ts_language(&self) -> TSLanguage {
tree_sitter_typescript::LANGUAGE_TSX.into()
tsx_ts_language()
}
}

#[test]
#[should_panic(
expected = "Language::from_path is not implemented for type `thread_ast_engine::language::test::Tsx`. Override Language::from_path for this type if path-based detection is required."
)]
fn test_from_path_panics_by_default() {
let _ = Tsx::from_path("some_file.tsx");
/// A minimal `Language` impl that does *not* override `from_path`, used to
/// verify that the default implementation panics.
#[derive(Clone, Debug)]
struct NoFromPath;
impl Language for NoFromPath {
fn kind_to_id(&self, kind: &str) -> u16 {
tsx_kind_to_id(kind)
}
fn field_to_id(&self, field: &str) -> Option<u16> {
tsx_field_to_id(field)
}
#[cfg(feature = "matching")]
fn build_pattern(&self, builder: &PatternBuilder) -> Result<Pattern, PatternError> {
builder.build(|src| StrDoc::try_new(src, self.clone()))
}
}
impl LanguageExt for NoFromPath {
fn get_ts_language(&self) -> TSLanguage {
tsx_ts_language()
}
}

/// A `Language` impl that *does* override `from_path`, used to verify that
/// overriding the default works correctly.
#[derive(Clone, Debug)]
pub struct CustomLang;
impl Language for CustomLang {
fn from_path<P: AsRef<Path>>(_path: P) -> Option<Self> {
Some(CustomLang)
struct TsxWithFromPath;
impl Language for TsxWithFromPath {
fn kind_to_id(&self, kind: &str) -> u16 {
tsx_kind_to_id(kind)
}
fn field_to_id(&self, field: &str) -> Option<u16> {
tsx_field_to_id(field)
}
fn kind_to_id(&self, _kind: &str) -> u16 {
0
#[cfg(feature = "matching")]
fn build_pattern(&self, builder: &PatternBuilder) -> Result<Pattern, PatternError> {
builder.build(|src| StrDoc::try_new(src, self.clone()))
}
fn field_to_id(&self, _field: &str) -> Option<u16> {
None
fn from_path<P: AsRef<Path>>(path: P) -> Option<Self> {
path.as_ref()
.extension()
.and_then(|e| e.to_str())
.filter(|&e| e == "tsx")
.map(|_| Self)
}
fn build_pattern(&self, _builder: &PatternBuilder) -> Result<Pattern, PatternError> {
unimplemented!()
}
impl LanguageExt for TsxWithFromPath {
fn get_ts_language(&self) -> TSLanguage {
tsx_ts_language()
}
}

#[test]
fn test_from_path_override() {
assert!(CustomLang::from_path("some_file.custom").is_some());
#[should_panic(expected = "Language::from_path is not implemented for type")]
fn default_from_path_panics() {
let _ = NoFromPath::from_path("some/file.rs");
}

#[test]
fn overridden_from_path_does_not_panic() {
assert!(TsxWithFromPath::from_path("component.tsx").is_some());
assert!(TsxWithFromPath::from_path("main.rs").is_none());
}
}
Loading