Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 45 additions & 13 deletions libraries/analysis-javascript/lib/RootContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,27 @@ import { DependencyFactory } from "./dependency/DependencyFactory";
import { Export } from "./target/export/Export";
import { TargetFactory } from "./target/TargetFactory";
import { TypeModelFactory } from "./type/resolving/TypeModelFactory";
import { readFile } from "./utils/fileSystem";
import { getAllFiles, readFile } from "./utils/fileSystem";
import { ExportFactory } from "./target/export/ExportFactory";
import { TypeExtractor } from "./type/discovery/TypeExtractor";
import { TypeModel } from "./type/resolving/TypeModel";
import { Element } from "./type/discovery/element/Element";
import { DiscoveredObjectType } from "./type/discovery/object/DiscoveredType";
import { Relation } from "./type/discovery/relation/Relation";
import { TypePool } from "./type/resolving/TypePool";

export class RootContext extends CoreRootContext<t.Node> {
protected _exportFactory: ExportFactory;
protected _typeExtractor: TypeExtractor;
protected _typeResolver: TypeModelFactory;

protected _files: string[];
protected _elementMap: Map<string, Element>;
protected _relationMap: Map<string, Relation>;
protected _objectMap: Map<string, DiscoveredObjectType>;

protected _typeModel: TypeModel;
protected _typePool: TypePool;

// Mapping: filepath -> target name -> Exports
protected _exportMap: Map<string, Export[]>;
Expand All @@ -70,8 +73,6 @@ export class RootContext extends CoreRootContext<t.Node> {
this._exportFactory = exportFactory;
this._typeExtractor = typeExtractor;
this._typeResolver = typeResolver;

this._exportMap = new Map();
}

get rootPath(): string {
Expand All @@ -80,6 +81,19 @@ export class RootContext extends CoreRootContext<t.Node> {

// TODO something with the types

getFiles() {
if (!this._files) {
this._files = getAllFiles(this.rootPath, ".js").filter(
(x) =>
!x.includes("/test/") &&
!x.includes(".test.js") &&
!x.includes("node_modules")
); // maybe we should also take those into account
}

return this._files;
}

override getSource(filePath: string) {
let absoluteTargetPath = this.resolvePath(filePath);

Expand Down Expand Up @@ -112,22 +126,30 @@ export class RootContext extends CoreRootContext<t.Node> {
return this._sources.get(absoluteTargetPath);
}

getExports(filePath: string): Export[] {
private getExports(filePath: string): Export[] {
const absolutePath = this.resolvePath(filePath);

if (!this._exportMap.has(absolutePath)) {
this._exportMap.set(
return this._exportFactory.extract(
absolutePath,
this._exportFactory.extract(
absolutePath,
this.getAbstractSyntaxTree(absolutePath)
)
this.getAbstractSyntaxTree(absolutePath)
);
}

return this._exportMap.get(absolutePath);
}

getAllExports(): Map<string, Export[]> {
if (!this._exportMap) {
this._exportMap = new Map();

for (const filepath of this.getFiles()) {
this._exportMap.set(filepath, this.getExports(filepath));
}
}
return this._exportMap;
}

extractTypes(): void {
if (!this._elementMap || !this._relationMap || !this._objectMap) {
this._typeExtractor.extractAll(this);
Expand All @@ -138,25 +160,35 @@ export class RootContext extends CoreRootContext<t.Node> {
}

resolveTypes(): void {
if (!this._typeModel) {
if (!this._elementMap || !this._relationMap || !this._objectMap) {
this.extractTypes();
}

if (!this._typeModel) {
this._typeModel = this._typeResolver.resolveTypes(
this._elementMap,
this._relationMap
); //, this._objectMap);
);
this._typePool = new TypePool(this._objectMap, this.getAllExports());
}
}

getTypeModel(): TypeModel {
if (!this._typeModel) {
this.extractTypes();
this.resolveTypes();
// or should this always be done beforehand?
}

return this._typeModel;
}

getTypePool(): TypePool {
if (!this._typePool) {
this.resolveTypes();
}

return this._typePool;
}

getElement(id: string): Element {
if (!this._elementMap || !this._elementMap.has(id)) {
this.extractTypes();
Expand Down
4 changes: 2 additions & 2 deletions libraries/analysis-javascript/lib/target/Target.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export interface ClassTarget extends NamedSubTarget, Exportable {

export interface MethodTarget extends NamedSubTarget, Callable {
type: TargetType.METHOD;
className: string;
classId: string;

visibility: VisibilityType;

Expand All @@ -79,7 +79,7 @@ export interface ObjectTarget extends NamedSubTarget, Exportable {

export interface ObjectFunctionTarget extends NamedSubTarget, Callable {
type: TargetType.OBJECT_FUNCTION;
objectName: string;
objectId: string;
}

export interface PathTarget extends SubTarget {
Expand Down
Loading