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

Commit 3286f81

Browse files
author
Hendrik van Antwerpen
authored
Merge pull request #171 from github/npm-dependencies
[TypeScript] Use dependencies from `package.json`
2 parents 7887c30 + 798d703 commit 3286f81

File tree

7 files changed

+134
-42
lines changed

7 files changed

+134
-42
lines changed

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,27 @@ impl FileAnalyzer for NpmPackageAnalyzer {
8585
let pkg_ref = add_push(graph, file, proj_scope, PKG_M_NS, "npm_package.pkg_ref");
8686
add_edge(graph, pkg_def, pkg_ref, 0);
8787

88+
// dependencies (package references)
89+
for (i, (pkg_name, _)) in npm_pkg.dependencies.iter().enumerate() {
90+
let pkg_def = add_module_pops(
91+
graph,
92+
file,
93+
NON_REL_M_NS,
94+
Path::new(&pkg_name),
95+
proj_scope,
96+
&format!("npm_package.dep[{}]", i),
97+
);
98+
let pkg_ref = add_module_pushes(
99+
graph,
100+
file,
101+
NON_REL_M_NS,
102+
Path::new(&pkg_name),
103+
root,
104+
&format!("npm_package.dep[{}]", i),
105+
);
106+
add_edge(graph, pkg_def, pkg_ref, 0);
107+
}
108+
88109
Ok(())
89110
}
90111
}
@@ -93,4 +114,6 @@ impl FileAnalyzer for NpmPackageAnalyzer {
93114
#[serde(rename_all = "camelCase")]
94115
pub struct NpmPackage {
95116
pub name: String,
117+
#[serde(default)]
118+
pub dependencies: HashMap<String, serde_json::Value>,
96119
}

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

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -399,42 +399,49 @@ impl TsConfig {
399399
}
400400

401401
/// Returns an iterator over the input files of the project, taking `files`, `include`, and `exclude` into account.
402-
fn input_files<'a, PI>(&self, source_paths: PI) -> impl Iterator<Item = &'a Path>
402+
fn input_files<'a, PI>(&self, source_paths: PI) -> Vec<PathBuf>
403403
where
404404
PI: IntoIterator<Item = &'a Path>,
405405
{
406406
let files = self.files();
407407
let include = self.include();
408408
let exclude = self.exclude();
409409

410-
let project_dir = self.project_dir.clone();
411-
source_paths.into_iter().filter_map(move |p| {
412-
// compute relative path in this project
413-
let p = match p.strip_prefix(&project_dir) {
414-
Ok(p) => p,
415-
Err(_) => return None,
416-
};
417-
418-
// accept files in the file list
419-
for file in &files {
420-
if p == file {
421-
return Some(p);
410+
source_paths
411+
.into_iter()
412+
.filter_map(|p| {
413+
let p = match p.strip_prefix(&self.project_dir) {
414+
Ok(p) => p,
415+
Err(_) => return None,
416+
};
417+
418+
// normalize path
419+
let p = match NormalizedRelativePath::from_path(p) {
420+
Some(p) => p.into_path_buf(),
421+
None => return None,
422+
};
423+
424+
// accept files in the file list
425+
for file in &files {
426+
if &p == file {
427+
return Some(p);
428+
}
422429
}
423-
}
424430

425-
// reject files not in the include patterns
426-
if !include.iter().any(|i| i.matches_path(p)) {
427-
return None;
428-
}
431+
// reject files not in the include patterns
432+
if !include.iter().any(|i| i.matches_path(&p)) {
433+
return None;
434+
}
429435

430-
// reject files matching exclude patterns
431-
if exclude.iter().any(|e| e.matches_path(p)) {
432-
return None;
433-
}
436+
// reject files matching exclude patterns
437+
if exclude.iter().any(|e| e.matches_path(&p)) {
438+
return None;
439+
}
434440

435-
// file was included, and not excluded, so accept
436-
Some(p)
437-
})
441+
// file was included, and not excluded, so accept
442+
Some(p)
443+
})
444+
.collect()
438445
}
439446
}
440447

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/* --- path: ./my_lib/package.json --- */
2+
/* --- global: FILE_PATH=package.json --- */
3+
/* --- global: PROJECT_NAME=my_lib --- */
4+
{
5+
"name": "@my/lib"
6+
}
7+
8+
/* --- path: ./my_lib/tsconfig.json --- */
9+
/* --- global: FILE_PATH=tsconfig.json --- */
10+
/* --- global: PROJECT_NAME=my_lib --- */
11+
{
12+
}
13+
14+
/* --- path: ./my_lib/src/foo.ts --- */
15+
/* --- global: FILE_PATH=src/foo.ts --- */
16+
/* --- global: PROJECT_NAME=my_lib --- */
17+
export const bar = 42;
18+
19+
/* --- path: ./my_app/package.json --- */
20+
/* --- global: FILE_PATH=package.json --- */
21+
/* --- global: PROJECT_NAME=my_app --- */
22+
{
23+
"name": "@my/app",
24+
"dependencies": {
25+
"@my/lib": "0.1"
26+
}
27+
}
28+
29+
/* --- path: ./my_app/tsconfig.json --- */
30+
/* --- global: FILE_PATH=tsconfig.json --- */
31+
/* --- global: PROJECT_NAME=my_app --- */
32+
{
33+
}
34+
35+
/* --- path: ./my_app/src/index.ts --- */
36+
/* --- global: FILE_PATH=src/index.ts --- */
37+
/* --- global: PROJECT_NAME=my_app --- */
38+
import { bar } from "@my/lib/foo";
39+
// ^ defined: 17
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/* --- path: ./my_lib/package.json --- */
2+
/* --- global: FILE_PATH=package.json --- */
3+
/* --- global: PROJECT_NAME=my_lib --- */
4+
{
5+
"name": "@my/lib"
6+
}
7+
8+
/* --- path: ./my_lib/tsconfig.json --- */
9+
/* --- global: FILE_PATH=tsconfig.json --- */
10+
/* --- global: PROJECT_NAME=my_lib --- */
11+
{
12+
}
13+
14+
/* --- path: ./my_lib/src/foo.ts --- */
15+
/* --- global: FILE_PATH=src/foo.ts --- */
16+
/* --- global: PROJECT_NAME=my_lib --- */
17+
export const bar = 42;
18+
19+
/* --- path: ./my_app/package.json --- */
20+
/* --- global: FILE_PATH=package.json --- */
21+
/* --- global: PROJECT_NAME=my_app --- */
22+
{
23+
"name": "@my/app"
24+
}
25+
26+
/* --- path: ./my_app/tsconfig.json --- */
27+
/* --- global: FILE_PATH=tsconfig.json --- */
28+
/* --- global: PROJECT_NAME=my_app --- */
29+
{
30+
}
31+
32+
/* --- path: ./my_app/src/index.ts --- */
33+
/* --- global: FILE_PATH=src/index.ts --- */
34+
/* --- global: PROJECT_NAME=my_app --- */
35+
import { bar } from "@my/lib/foo";
36+
// ^ defined:

languages/tree-sitter-stack-graphs-typescript/test/projects/package-with-baseurl.ts

Lines changed: 0 additions & 15 deletions
This file was deleted.
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
/* --- path: a/foo.ts --- */
2+
/* --- global: FILE_PATH=foo.ts */
23
/* --- global: PROJECT_NAME=a */
34

45
export const baz = 42;
56

67
/* --- path: b/bar.ts --- */
8+
/* --- global: FILE_PATH=bar.ts */
79
/* --- global: PROJECT_NAME=b */
810

911
import { baz } from "./foo";
10-
// ^ defined:
12+
// ^ defined:

stack-graphs/src/cycles.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ impl HasPathKey for PartialPath {
8484
}
8585
}
8686

87-
const MAX_SIMILAR_PATH_COUNT: usize = 7;
87+
const MAX_SIMILAR_PATH_COUNT: usize = 12;
8888

8989
impl<P> CycleDetector<P>
9090
where

0 commit comments

Comments
 (0)