Skip to content
Merged
Show file tree
Hide file tree
Changes from 12 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
147 changes: 74 additions & 73 deletions libraries/analysis-javascript/lib/target/TargetVisitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -333,77 +333,78 @@ export class TargetVisitor extends AbstractSyntaxTreeVisitor {
this.subTargets.push(target);
};

private _getParentClassName(
private _getParentClassId(
path: NodePath<
| t.ClassMethod
| t.ClassProperty
| t.ClassPrivateMethod
| t.ClassPrivateProperty
>
): string {
const parentNode = path.parentPath.parentPath.node;
const parentOfParentNode = path.parentPath.parentPath.parentPath.node;
if (parentNode.type === "ClassDeclaration") {
// e.g. class A { ... }
if (parentNode.id && parentNode.id.type === "Identifier") {
return parentNode.id.name;
} else if (
parentOfParentNode !== undefined &&
parentOfParentNode.type === "ExportDefaultDeclaration"
) {
// e.g. export default class { ... }
return "default";
} else {
// e.g. class { ... }
// unsupported
// should not be possible
throw new Error("unknown class method parent");
}
} else if (parentNode.type === "ClassExpression") {
// e.g. const x = class A { ... }
// e.g. const x = class { ... }
// e.g. { x: class A { ... } }
// in all cases the name should be x

if (
parentOfParentNode !== undefined &&
parentOfParentNode.type === "VariableDeclarator"
) {
// e.g. ? = class A { ... }
if (parentOfParentNode.id.type === "Identifier") {
// e.g. const x = class A { ... }
return parentOfParentNode.id.name;
} else {
// e.g. ? = class { ... }
// unsupported
// should not be possible
throw new Error("unknown class method parent");
}
} else if (
parentOfParentNode !== undefined &&
parentOfParentNode.type === "ObjectProperty"
) {
// e.g. { x: class A { ... } }
if (parentOfParentNode.key.type === "Identifier") {
return parentOfParentNode.key.name;
} else if (parentOfParentNode.key.type.includes("Literal")) {
// e.g. { "x": class A { ... } }
return "value" in parentOfParentNode.key
? parentOfParentNode.key.value.toString()
: "null";
} else {
// e.g. { ??: class { ... } }
// unsupported
throw new Error("unknown class method parent");
}
} else {
// unsupported
throw new Error("unknown class method parent");
}
} else {
// unsupported
throw new Error("unknown class method parent");
}
return this._getNodeId(path.parentPath.parentPath);
// const parentNode = path.parentPath.parentPath.node;
// const parentOfParentNode = path.parentPath.parentPath.parentPath.node;
// if (parentNode.type === "ClassDeclaration") {
// // e.g. class A { ... }
// if (parentNode.id && parentNode.id.type === "Identifier") {
// return parentNode.id.name;
// } else if (
// parentOfParentNode !== undefined &&
// parentOfParentNode.type === "ExportDefaultDeclaration"
// ) {
// // e.g. export default class { ... }
// return "default";
// } else {
// // e.g. class { ... }
// // unsupported
// // should not be possible
// throw new Error("unknown class method parent");
// }
// } else if (parentNode.type === "ClassExpression") {
// // e.g. const x = class A { ... }
// // e.g. const x = class { ... }
// // e.g. { x: class A { ... } }
// // in all cases the name should be x

// if (
// parentOfParentNode !== undefined &&
// parentOfParentNode.type === "VariableDeclarator"
// ) {
// // e.g. ? = class A { ... }
// if (parentOfParentNode.id.type === "Identifier") {
// // e.g. const x = class A { ... }
// return parentOfParentNode.id.name;
// } else {
// // e.g. ? = class { ... }
// // unsupported
// // should not be possible
// throw new Error("unknown class method parent");
// }
// } else if (
// parentOfParentNode !== undefined &&
// parentOfParentNode.type === "ObjectProperty"
// ) {
// // e.g. { x: class A { ... } }
// if (parentOfParentNode.key.type === "Identifier") {
// return parentOfParentNode.key.name;
// } else if (parentOfParentNode.key.type.includes("Literal")) {
// // e.g. { "x": class A { ... } }
// return "value" in parentOfParentNode.key
// ? parentOfParentNode.key.value.toString()
// : "null";
// } else {
// // e.g. { ??: class { ... } }
// // unsupported
// throw new Error("unknown class method parent");
// }
// } else {
// // unsupported
// throw new Error("unknown class method parent");
// }
// } else {
// // unsupported
// throw new Error("unknown class method parent");
// }
}

public ClassMethod: (path: NodePath<t.ClassMethod>) => void = (path) => {
Expand All @@ -413,7 +414,7 @@ export class TargetVisitor extends AbstractSyntaxTreeVisitor {
throw new Error("unknown class method parent");
}

const parentClassName: string = this._getParentClassName(path);
const parentClassId: string = this._getParentClassId(path);

if (path.node.key.type !== "Identifier") {
// e.g. class A { ?() {} }
Expand All @@ -435,7 +436,7 @@ export class TargetVisitor extends AbstractSyntaxTreeVisitor {
id: `${this._getNodeId(path)}`,
name: targetName,
type: TargetType.METHOD,
className: parentClassName,
classId: parentClassId,
isStatic: path.node.static,
isAsync: path.node.async,
methodType: path.node.kind,
Expand Down Expand Up @@ -506,7 +507,7 @@ export class TargetVisitor extends AbstractSyntaxTreeVisitor {
id: target.id,
type: TargetType.METHOD,
name: "constructor",
className: prototypeName,
classId: target.id,

visibility: VisibilityType.PUBLIC,

Expand All @@ -527,7 +528,7 @@ export class TargetVisitor extends AbstractSyntaxTreeVisitor {
id: `${this._getNodeId(path)}`,
type: TargetType.METHOD,
name: property.node.name,
className: prototypeName,
classId: target.id,

visibility: VisibilityType.PUBLIC,

Expand Down Expand Up @@ -603,7 +604,7 @@ export class TargetVisitor extends AbstractSyntaxTreeVisitor {
};
const objectFunctionTarget: ObjectFunctionTarget = {
type: TargetType.OBJECT_FUNCTION,
objectName: object.node.name,
objectId: `${this._getBindingId(object)}`,
name: functionName,
id: `${this._getNodeId(path)}`,
isAsync: path.node.async,
Expand Down Expand Up @@ -632,7 +633,7 @@ export class TargetVisitor extends AbstractSyntaxTreeVisitor {
}
case "ClassProperty": {
// e.g. class A { x = () => {} }
const parentClassName: string = this._getParentClassName(
const parentClassId: string = this._getParentClassId(
<NodePath<t.ClassProperty>>path.parentPath
);

Expand All @@ -646,7 +647,7 @@ export class TargetVisitor extends AbstractSyntaxTreeVisitor {

const target: MethodTarget = {
id: `${this._getNodeId(path)}`,
className: parentClassName,
classId: parentClassId,
name: targetName,
type: TargetType.METHOD,
isStatic: (<t.ClassProperty>parent.node).static,
Expand Down Expand Up @@ -961,8 +962,8 @@ export class TargetVisitor extends AbstractSyntaxTreeVisitor {
(<MethodTarget>subTarget).methodType &&
(<MethodTarget>t).isStatic ===
(<MethodTarget>subTarget).isStatic &&
(<MethodTarget>t).className ===
(<MethodTarget>subTarget).className
(<MethodTarget>t).classId ===
(<MethodTarget>subTarget).classId
: true)
);
})
Expand Down
Loading