11import fs from "fs" ;
22
3- import { resolveFilePath } from "../helper/file" ;
43import { DependencyTree , Group , Endpoint } from "./types" ;
54import AnnotationManager from "../annotationManager" ;
6- import { getLanguagePluginFromFilePath } from "../languagesPlugins" ;
5+ import { getLanguagePlugin } from "../languagesPlugins" ;
76
87class DependencyTreeManager {
8+ entryPointPath : string ;
99 dependencyTree : DependencyTree ;
1010
11- constructor ( filePath : string ) {
12- const dependencyTree = this . #getDependencyTree( filePath ) ;
11+ constructor ( entryPointPath : string ) {
12+ this . entryPointPath = entryPointPath ;
13+ const dependencyTree = this . #getDependencyTree(
14+ this . entryPointPath ,
15+ this . entryPointPath ,
16+ ) ;
1317 this . dependencyTree = dependencyTree ;
1418 }
1519
16- #getDependencyTree( filePath : string ) : DependencyTree {
17- const sourceCode = fs . readFileSync ( filePath , "utf8" ) ;
20+ #getDependencyTree(
21+ entryPointPath : string ,
22+ currentFilePath : string ,
23+ ) : DependencyTree {
24+ const sourceCode = fs . readFileSync ( currentFilePath , "utf8" ) ;
1825
1926 const dependencyTree : DependencyTree = {
20- path : filePath ,
27+ path : currentFilePath ,
2128 sourceCode,
2229 children : [ ] ,
2330 } ;
2431
25- const languagePlugin = getLanguagePluginFromFilePath ( filePath ) ;
32+ const languagePlugin = getLanguagePlugin ( entryPointPath , currentFilePath ) ;
2633
2734 const tree = languagePlugin . parser . parse ( sourceCode ) ;
2835
29- let imports = languagePlugin . getImports ( tree . rootNode ) ;
30-
31- imports = imports . filter ( ( importPath ) => importPath . source . startsWith ( "." ) ) ;
36+ const imports = languagePlugin . getImports ( currentFilePath , tree . rootNode ) ;
3237
33- imports . forEach ( ( importPath ) => {
34- const resolvedPath = resolveFilePath ( importPath . source , filePath ) ;
35- if ( resolvedPath && fs . existsSync ( resolvedPath ) ) {
36- const childTree = this . #getDependencyTree( resolvedPath ) ;
37- dependencyTree . children . push ( childTree ) ;
38+ imports . forEach ( ( depImport ) => {
39+ if ( depImport . isExternal || ! depImport . source ) {
40+ // Ignore external dependencies
41+ return ;
3842 }
43+
44+ const childTree = this . #getDependencyTree(
45+ entryPointPath ,
46+ depImport . source ,
47+ ) ;
48+ dependencyTree . children . push ( childTree ) ;
3949 } ) ;
4050
4151 return dependencyTree ;
@@ -58,7 +68,10 @@ class DependencyTreeManager {
5868 parentFilePaths : string [ ] ,
5969 dependencyTree : DependencyTree ,
6070 ) {
61- const languagePlugin = getLanguagePluginFromFilePath ( dependencyTree . path ) ;
71+ const languagePlugin = getLanguagePlugin (
72+ this . entryPointPath ,
73+ dependencyTree . path ,
74+ ) ;
6275
6376 const tree = languagePlugin . parser . parse ( dependencyTree . sourceCode ) ;
6477
0 commit comments