@@ -27,24 +27,27 @@ import { DependencyFactory } from "./dependency/DependencyFactory";
2727import { Export } from "./target/export/Export" ;
2828import { TargetFactory } from "./target/TargetFactory" ;
2929import { TypeModelFactory } from "./type/resolving/TypeModelFactory" ;
30- import { readFile } from "./utils/fileSystem" ;
30+ import { getAllFiles , readFile } from "./utils/fileSystem" ;
3131import { ExportFactory } from "./target/export/ExportFactory" ;
3232import { TypeExtractor } from "./type/discovery/TypeExtractor" ;
3333import { TypeModel } from "./type/resolving/TypeModel" ;
3434import { Element } from "./type/discovery/element/Element" ;
3535import { DiscoveredObjectType } from "./type/discovery/object/DiscoveredType" ;
3636import { Relation } from "./type/discovery/relation/Relation" ;
37+ import { TypePool } from "./type/resolving/TypePool" ;
3738
3839export class RootContext extends CoreRootContext < t . Node > {
3940 protected _exportFactory : ExportFactory ;
4041 protected _typeExtractor : TypeExtractor ;
4142 protected _typeResolver : TypeModelFactory ;
4243
44+ protected _files : string [ ] ;
4345 protected _elementMap : Map < string , Element > ;
4446 protected _relationMap : Map < string , Relation > ;
4547 protected _objectMap : Map < string , DiscoveredObjectType > ;
4648
4749 protected _typeModel : TypeModel ;
50+ protected _typePool : TypePool ;
4851
4952 // Mapping: filepath -> target name -> Exports
5053 protected _exportMap : Map < string , Export [ ] > ;
@@ -70,8 +73,6 @@ export class RootContext extends CoreRootContext<t.Node> {
7073 this . _exportFactory = exportFactory ;
7174 this . _typeExtractor = typeExtractor ;
7275 this . _typeResolver = typeResolver ;
73-
74- this . _exportMap = new Map ( ) ;
7576 }
7677
7778 get rootPath ( ) : string {
@@ -80,6 +81,19 @@ export class RootContext extends CoreRootContext<t.Node> {
8081
8182 // TODO something with the types
8283
84+ getFiles ( ) {
85+ if ( ! this . _files ) {
86+ this . _files = getAllFiles ( this . rootPath , ".js" ) . filter (
87+ ( x ) =>
88+ ! x . includes ( "/test/" ) &&
89+ ! x . includes ( ".test.js" ) &&
90+ ! x . includes ( "node_modules" )
91+ ) ; // maybe we should also take those into account
92+ }
93+
94+ return this . _files ;
95+ }
96+
8397 override getSource ( filePath : string ) {
8498 let absoluteTargetPath = this . resolvePath ( filePath ) ;
8599
@@ -112,22 +126,30 @@ export class RootContext extends CoreRootContext<t.Node> {
112126 return this . _sources . get ( absoluteTargetPath ) ;
113127 }
114128
115- getExports ( filePath : string ) : Export [ ] {
129+ private getExports ( filePath : string ) : Export [ ] {
116130 const absolutePath = this . resolvePath ( filePath ) ;
117131
118132 if ( ! this . _exportMap . has ( absolutePath ) ) {
119- this . _exportMap . set (
133+ return this . _exportFactory . extract (
120134 absolutePath ,
121- this . _exportFactory . extract (
122- absolutePath ,
123- this . getAbstractSyntaxTree ( absolutePath )
124- )
135+ this . getAbstractSyntaxTree ( absolutePath )
125136 ) ;
126137 }
127138
128139 return this . _exportMap . get ( absolutePath ) ;
129140 }
130141
142+ getAllExports ( ) : Map < string , Export [ ] > {
143+ if ( ! this . _exportMap ) {
144+ this . _exportMap = new Map ( ) ;
145+
146+ for ( const filepath of this . getFiles ( ) ) {
147+ this . _exportMap . set ( filepath , this . getExports ( filepath ) ) ;
148+ }
149+ }
150+ return this . _exportMap ;
151+ }
152+
131153 extractTypes ( ) : void {
132154 if ( ! this . _elementMap || ! this . _relationMap || ! this . _objectMap ) {
133155 this . _typeExtractor . extractAll ( this ) ;
@@ -138,25 +160,35 @@ export class RootContext extends CoreRootContext<t.Node> {
138160 }
139161
140162 resolveTypes ( ) : void {
141- if ( ! this . _typeModel ) {
163+ if ( ! this . _elementMap || ! this . _relationMap || ! this . _objectMap ) {
142164 this . extractTypes ( ) ;
165+ }
166+
167+ if ( ! this . _typeModel ) {
143168 this . _typeModel = this . _typeResolver . resolveTypes (
144169 this . _elementMap ,
145170 this . _relationMap
146- ) ; //, this._objectMap);
171+ ) ;
172+ this . _typePool = new TypePool ( this . _objectMap , this . getAllExports ( ) ) ;
147173 }
148174 }
149175
150176 getTypeModel ( ) : TypeModel {
151177 if ( ! this . _typeModel ) {
152- this . extractTypes ( ) ;
153178 this . resolveTypes ( ) ;
154- // or should this always be done beforehand?
155179 }
156180
157181 return this . _typeModel ;
158182 }
159183
184+ getTypePool ( ) : TypePool {
185+ if ( ! this . _typePool ) {
186+ this . resolveTypes ( ) ;
187+ }
188+
189+ return this . _typePool ;
190+ }
191+
160192 getElement ( id : string ) : Element {
161193 if ( ! this . _elementMap || ! this . _elementMap . has ( id ) ) {
162194 this . extractTypes ( ) ;
0 commit comments