Skip to content

Commit

Permalink
feat: use replace instead of custom function
Browse files Browse the repository at this point in the history
  • Loading branch information
nohehf committed May 30, 2024
1 parent d0b940e commit c43fa8b
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
37 changes: 0 additions & 37 deletions tree-sitter-stack-graphs/src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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: F) -> impl Function
Expand Down Expand Up @@ -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<PathBuf> {
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"))
);
}
}

0 comments on commit c43fa8b

Please sign in to comment.