From c43fa8b1af2a5ced2174059d41d4b0f2b8da3983 Mon Sep 17 00:00:00 2001 From: nohehf Date: Thu, 30 May 2024 11:36:09 +0200 Subject: [PATCH] feat: use replace instead of custom function --- .../src/stack-graphs.tsg | 2 +- tree-sitter-stack-graphs/src/functions.rs | 37 ------------------- 2 files changed, 1 insertion(+), 38 deletions(-) diff --git a/languages/tree-sitter-stack-graphs-typescript/src/stack-graphs.tsg b/languages/tree-sitter-stack-graphs-typescript/src/stack-graphs.tsg index 7e6359280..b7478b4e4 100644 --- a/languages/tree-sitter-stack-graphs-typescript/src/stack-graphs.tsg +++ b/languages/tree-sitter-stack-graphs-typescript/src/stack-graphs.tsg @@ -541,7 +541,7 @@ attribute node_symbol = node => symbol = (source-text node), source_n ; module reference var mod_scope = mod_ref__ns ; normalize path and remove the extension as we want to match 'foo', 'foo.js', 'foo.ts', etc. - scan (path-remove-ext (path-normalize mod_path)) { + scan (path-normalize (replace mod_path "\.(js|ts|jsx|tsx)$" "")) { "([^/]+)/?" { node mod_ref attr (mod_ref) push_symbol = $1 diff --git a/tree-sitter-stack-graphs/src/functions.rs b/tree-sitter-stack-graphs/src/functions.rs index e1f9e94e5..9082ba427 100644 --- a/tree-sitter-stack-graphs/src/functions.rs +++ b/tree-sitter-stack-graphs/src/functions.rs @@ -43,10 +43,6 @@ pub mod path { path_fn(|p| normalize(p).map(|p| p.as_os_str().to_os_string())), ); functions.add("path-split".into(), PathSplit); - functions.add( - "path-remove-ext".into(), - path_fn(|p| remove_extension(p).map(|p| p.as_os_str().to_os_string())), - ); } pub fn path_fn(f: F) -> impl Function @@ -163,37 +159,4 @@ pub mod path { } Some(ret) } - - /// Removes the extension from a path. - /// eg. `foo/bar.rs` -> `foo/bar` - /// eg. `foo/bar` -> `foo/bar` - /// eg. `foo/bar.rs.bak` -> `foo/bar.rs` - pub fn remove_extension(path: &Path) -> Option { - path.extension() - .map_or(Some(path.into()), |_| path.with_extension("").into()) - } - - #[test] - fn test_remove_extension() { - assert_eq!( - remove_extension(Path::new("foo/bar.rs")), - Some(PathBuf::from("foo/bar")) - ); - assert_eq!( - remove_extension(Path::new("foo/bar")), - Some(PathBuf::from("foo/bar")) - ); - assert_eq!( - remove_extension(Path::new("foo/bar.rs.bak")), - Some(PathBuf::from("foo/bar.rs")) - ); - assert_eq!( - remove_extension(Path::new("foo")), - Some(PathBuf::from("foo")) - ); - assert_eq!( - remove_extension(Path::new("foo.rs")), - Some(PathBuf::from("foo")) - ); - } }