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

Commit 90e2e11

Browse files
author
Hendrik van Antwerpen
authored
Merge pull request #158 from github/analyze-tsconfig
Analyze package.json
2 parents d1977e1 + fa11792 commit 90e2e11

File tree

9 files changed

+313
-183
lines changed

9 files changed

+313
-183
lines changed

languages/tree-sitter-stack-graphs-typescript/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ harness = false
2525
anyhow = "1.0"
2626
clap = "3"
2727
glob = "0.3"
28+
serde = { version = "1.0", features = ["derive"] }
29+
serde_json = "1.0"
2830
stack-graphs = { version = "~0.10", path = "../../stack-graphs" }
2931
tree-sitter-stack-graphs = { version = "~0.5", path = "../../tree-sitter-stack-graphs", features=["cli"] }
3032
tree-sitter-typescript = { git = "https://github.com/tree-sitter/tree-sitter-typescript", rev="082da44a5263599186dadafd2c974c19f3a73d28" }

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,16 @@
55
// Please see the LICENSE-APACHE or LICENSE-MIT files in this distribution for license details.
66
// ------------------------------------------------------------------------------------------------
77

8-
use crate::tsconfig::TsConfigAnalyzer;
98
use tree_sitter_stack_graphs::loader::FileAnalyzers;
109
use tree_sitter_stack_graphs::loader::LanguageConfiguration;
1110
use tree_sitter_stack_graphs::CancellationFlag;
1211

12+
use crate::npm_package::NpmPackageAnalyzer;
13+
use crate::tsconfig::TsConfigAnalyzer;
14+
15+
pub mod npm_package;
1316
pub mod tsconfig;
17+
pub mod util;
1418

1519
/// The stack graphs tsg source for this language
1620
pub const STACK_GRAPHS_TSG_SOURCE: &str = include_str!("../src/stack-graphs.tsg");
@@ -20,6 +24,11 @@ pub const STACK_GRAPHS_BUILTINS_CONFIG: &str = include_str!("../src/builtins.cfg
2024
/// The stack graphs builtins source for this language
2125
pub const STACK_GRAPHS_BUILTINS_SOURCE: &str = include_str!("../src/builtins.ts");
2226

27+
/// The name of the file path global variable
28+
pub const FILE_PATH_VAR: &str = "FILE_PATH";
29+
/// The name of the project name global variable
30+
pub const PROJECT_NAME_VAR: &str = "PROJECT_NAME";
31+
2332
pub fn language_configuration(cancellation_flag: &dyn CancellationFlag) -> LanguageConfiguration {
2433
LanguageConfiguration::from_tsg_str(
2534
tree_sitter_typescript::language_typescript(),
@@ -29,7 +38,9 @@ pub fn language_configuration(cancellation_flag: &dyn CancellationFlag) -> Langu
2938
STACK_GRAPHS_TSG_SOURCE,
3039
Some(STACK_GRAPHS_BUILTINS_SOURCE),
3140
Some(STACK_GRAPHS_BUILTINS_CONFIG),
32-
FileAnalyzers::new().add("tsconfig.json".to_string(), TsConfigAnalyzer {}),
41+
FileAnalyzers::new()
42+
.add("tsconfig.json".to_string(), TsConfigAnalyzer {})
43+
.add("package.json".to_string(), NpmPackageAnalyzer {}),
3344
cancellation_flag,
3445
)
3546
.unwrap()
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// -*- coding: utf-8 -*-
2+
// ------------------------------------------------------------------------------------------------
3+
// Copyright © 2022, stack-graphs authors.
4+
// Licensed under either of Apache License, Version 2.0, or MIT license, at your option.
5+
// Please see the LICENSE-APACHE or LICENSE-MIT files in this distribution for license details.
6+
// ------------------------------------------------------------------------------------------------
7+
8+
use serde::Deserialize;
9+
use std::collections::HashMap;
10+
use std::path::Path;
11+
12+
use stack_graphs::arena::Handle;
13+
use stack_graphs::graph::File;
14+
use stack_graphs::graph::StackGraph;
15+
use tree_sitter_stack_graphs::FileAnalyzer;
16+
use tree_sitter_stack_graphs::LoadError;
17+
18+
use crate::util::*;
19+
20+
pub struct NpmPackageAnalyzer {}
21+
22+
impl FileAnalyzer for NpmPackageAnalyzer {
23+
fn build_stack_graph_into<'a>(
24+
&self,
25+
graph: &mut StackGraph,
26+
file: Handle<File>,
27+
_path: &Path,
28+
source: &str,
29+
_all_paths: &mut dyn Iterator<Item = &'a Path>,
30+
globals: &HashMap<String, String>,
31+
_cancellation_flag: &dyn tree_sitter_stack_graphs::CancellationFlag,
32+
) -> Result<(), tree_sitter_stack_graphs::LoadError> {
33+
// read globals
34+
let proj_name = globals
35+
.get(crate::PROJECT_NAME_VAR)
36+
.map(String::as_str)
37+
.unwrap_or("");
38+
39+
// parse source
40+
let npm_pkg: NpmPackage =
41+
serde_json::from_str(source).map_err(|_| LoadError::ParseError)?;
42+
43+
// root node
44+
let root = StackGraph::root_node();
45+
46+
// project scope
47+
let proj_scope_id = graph.new_node_id(file);
48+
let proj_scope = graph.add_scope_node(proj_scope_id, false).unwrap();
49+
add_debug_name(graph, proj_scope, "npm_package.proj_scope");
50+
51+
// project definition
52+
let proj_def = add_ns_pop(graph, file, root, PROJ_NS, proj_name);
53+
add_debug_name(graph, proj_def, "npm_package.proj_def");
54+
add_edge(graph, proj_def, proj_scope, 0);
55+
56+
// project reference
57+
let proj_ref = add_ns_push(graph, file, root, PROJ_NS, proj_name);
58+
add_debug_name(graph, proj_ref, "npm_package.proj_ref");
59+
add_edge(graph, proj_scope, proj_ref, 0);
60+
61+
// package definition
62+
let pkg_def = add_module_pops(graph, file, NON_REL_M_NS, Path::new(&npm_pkg.name), root);
63+
add_debug_name(graph, pkg_def, "npm_package.pkg_def");
64+
let pkg_ref = add_push(graph, file, proj_scope, PKG_M_NS);
65+
add_debug_name(graph, pkg_ref, "npm_package.pkg_ref");
66+
add_edge(graph, pkg_def, pkg_ref, 0);
67+
68+
Ok(())
69+
}
70+
}
71+
72+
#[derive(Default, Debug, Clone, PartialEq, Deserialize)]
73+
#[serde(rename_all = "camelCase")]
74+
pub struct NpmPackage {
75+
pub name: String,
76+
}

0 commit comments

Comments
 (0)