Skip to content
This repository was archived by the owner on Sep 9, 2025. It is now read-only.

Commit 62edae1

Browse files
author
Hendrik van Antwerpen
authored
Merge pull request #289 from github/tsconfig-panics
Make TsConfigAnalyzer more resilient against invalid path mappings
2 parents d093083 + 14e1dd7 commit 62edae1

File tree

2 files changed

+35
-5
lines changed

2 files changed

+35
-5
lines changed

languages/tree-sitter-stack-graphs-typescript/rust/tsconfig.rs

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ impl FileAnalyzer for TsConfigAnalyzer {
4444
// project scope
4545
let proj_scope = if let Some(proj_name) = proj_name {
4646
let proj_scope_id = graph.new_node_id(file);
47-
let proj_scope = graph.add_scope_node(proj_scope_id, false).unwrap();
47+
let proj_scope = graph
48+
.add_scope_node(proj_scope_id, false)
49+
.expect("no previous node for new id");
4850
add_debug_name(graph, proj_scope, "tsconfig.proj_scope");
4951

5052
// project definition
@@ -113,9 +115,12 @@ impl FileAnalyzer for TsConfigAnalyzer {
113115

114116
// path mappings
115117
for (from_idx, (from, tos)) in tsc.paths().iter().enumerate() {
116-
let is_prefix = from.file_name().map_or(true, |n| n == "*");
118+
let is_prefix = from.file_name().map_or(false, |n| n == "*");
117119
let from = if is_prefix {
118-
from.parent().unwrap()
120+
match from.parent() {
121+
Some(from) => from,
122+
None => continue,
123+
}
119124
} else {
120125
&from
121126
};
@@ -128,7 +133,17 @@ impl FileAnalyzer for TsConfigAnalyzer {
128133
&format!("tsconfig.paths[{}].from_def", from_idx),
129134
);
130135
for (to_idx, to) in tos.iter().enumerate() {
131-
let to = if is_prefix { to.parent().unwrap() } else { &to };
136+
if is_prefix && !to.file_name().map_or(false, |n| n == "*") {
137+
continue;
138+
}
139+
let to = if is_prefix {
140+
match to.parent() {
141+
Some(to) => to,
142+
None => continue,
143+
}
144+
} else {
145+
&to
146+
};
132147
let to_ref = add_module_pushes(
133148
graph,
134149
file,
@@ -274,7 +289,11 @@ impl TsConfig {
274289

275290
// compute patterns---invalid patterns are silently ignored
276291
es.into_iter()
277-
.filter_map(|e| Pattern::new(p.with_extension(e).to_str().unwrap()).ok())
292+
.filter_map(|e| {
293+
p.with_extension(e)
294+
.to_str()
295+
.and_then(|p| Pattern::new(p).ok())
296+
})
278297
.collect()
279298
}
280299

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/* --- path: ./tsconfig.json --- */
2+
{
3+
"compilerOptions": {
4+
"paths": {
5+
".": ["."],
6+
"a/": ["b/"],
7+
"c/*": ["d"],
8+
"e": ["f/*"],
9+
}
10+
}
11+
}

0 commit comments

Comments
 (0)