Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve type inference model #169

Merged
merged 15 commits into from
Sep 11, 2023
1 change: 1 addition & 0 deletions libraries/analysis-javascript/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,5 @@ export * from "./lib/type/resolving/InferenceTypeModelFactory";

export * from "./lib/utils/fileSystem";

export * from "./lib/Events";
export * from "./lib/RootContext";
48 changes: 48 additions & 0 deletions libraries/analysis-javascript/lib/Events.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright 2020-2023 Delft University of Technology and SynTest contributors
*
* This file is part of SynTest Framework - SynTest JavaScript.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { RootContext } from "./RootContext";

export type Events = {
exportExtractionStart: (rootContext: RootContext, filepath: string) => void;
exportExtractionComplete: (
rootContext: RootContext,
filepath: string
) => void;
elementExtractionStart: (rootContext: RootContext, filepath: string) => void;
elementExtractionComplete: (
rootContext: RootContext,
filepath: string
) => void;
relationExtractionStart: (rootContext: RootContext, filepath: string) => void;
relationExtractionComplete: (
rootContext: RootContext,
filepath: string
) => void;
objectTypeExtractionStart: (
rootContext: RootContext,
filepath: string
) => void;
objectTypeExtractionComplete: (
rootContext: RootContext,
filepath: string
) => void;

typeResolvingStart: (rootContext: RootContext) => void;
typeResolvingComplete: (rootContext: RootContext) => void;
};
180 changes: 144 additions & 36 deletions libraries/analysis-javascript/lib/RootContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,21 @@ 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";
import TypedEmitter from "typed-emitter";
import { Events } from "./Events";

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>;
// filepath -> id -> element
protected _elementMap: Map<string, Map<string, Element>>;
// filepath -> id -> relation
protected _relationMap: Map<string, Map<string, Relation>>;
// filepath -> id -> object
protected _objectMap: Map<string, Map<string, DiscoveredObjectType>>;

protected _typeModel: TypeModel;
protected _typePool: TypePool;
Expand Down Expand Up @@ -126,13 +131,26 @@ export class RootContext extends CoreRootContext<t.Node> {
return this._sources.get(absoluteTargetPath);
}

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

if (!this._exportMap.has(absolutePath)) {
return this._exportFactory.extract(
(<TypedEmitter<Events>>process).emit(
"exportExtractionStart",
this,
absolutePath
);
this._exportMap.set(
absolutePath,
this.getAbstractSyntaxTree(absolutePath)
this._exportFactory.extract(
absolutePath,
this.getAbstractSyntaxTree(absolutePath)
)
);
(<TypedEmitter<Events>>process).emit(
"exportExtractionComplete",
this,
absolutePath
);
}

Expand All @@ -150,26 +168,137 @@ export class RootContext extends CoreRootContext<t.Node> {
return this._exportMap;
}

extractTypes(): void {
if (!this._elementMap || !this._relationMap || !this._objectMap) {
this._typeExtractor.extractAll(this);
this._elementMap = this._typeExtractor.elementMap;
this._relationMap = this._typeExtractor.relationMap;
this._objectMap = this._typeExtractor.objectMap;
getElements(filepath: string) {
const absolutePath = this.resolvePath(filepath);

if (!this._elementMap.has(absolutePath)) {
(<TypedEmitter<Events>>process).emit(
"elementExtractionStart",
this,
absolutePath
);
const elementMap = this._typeExtractor.extractElements(
absolutePath,
this.getAbstractSyntaxTree(absolutePath)
);

this._elementMap.set(absolutePath, elementMap);
(<TypedEmitter<Events>>process).emit(
"elementExtractionComplete",
this,
absolutePath
);
}

return this._elementMap.get(absolutePath);
}

getAllElements() {
if (!this._elementMap) {
this._elementMap = new Map();

for (const filepath of this.getFiles()) {
this._elementMap.set(filepath, this.getElements(filepath));
}
}
return this._elementMap;
}

getRelations(filepath: string) {
const absolutePath = this.resolvePath(filepath);

if (!this._relationMap.has(absolutePath)) {
(<TypedEmitter<Events>>process).emit(
"relationExtractionStart",
this,
absolutePath
);
const relationsMap = this._typeExtractor.extractRelations(
absolutePath,
this.getAbstractSyntaxTree(absolutePath)
);

this._relationMap.set(absolutePath, relationsMap);
(<TypedEmitter<Events>>process).emit(
"relationExtractionComplete",
this,
absolutePath
);
}

return this._relationMap.get(absolutePath);
}

getAllRelations() {
if (!this._relationMap) {
this._relationMap = new Map();

for (const filepath of this.getFiles()) {
this._relationMap.set(filepath, this.getRelations(filepath));
}
}
return this._relationMap;
}

getObjectTypes(filepath: string) {
const absolutePath = this.resolvePath(filepath);

if (!this._objectMap.has(absolutePath)) {
(<TypedEmitter<Events>>process).emit(
"objectTypeExtractionStart",
this,
absolutePath
);
const objectsMap = this._typeExtractor.extractObjectTypes(
absolutePath,
this.getAbstractSyntaxTree(absolutePath)
);

this._objectMap.set(absolutePath, objectsMap);
(<TypedEmitter<Events>>process).emit(
"objectTypeExtractionComplete",
this,
absolutePath
);
}

return this._objectMap.get(absolutePath);
}

getAllObjectTypes() {
if (!this._objectMap) {
this._objectMap = new Map();

for (const filepath of this.getFiles()) {
this._objectMap.set(filepath, this.getObjectTypes(filepath));
}
}
return this._objectMap;
}

resolveTypes(): void {
if (!this._elementMap || !this._relationMap || !this._objectMap) {
this.extractTypes();
// TODO allow sub selections of files (do not consider entire context)
if (!this._elementMap) {
this.getAllElements();
}
if (!this._relationMap) {
this.getAllRelations();
}
if (!this._objectMap) {
this.getAllObjectTypes();
}
if (!this._exportMap) {
this.getAllExports();
}

if (!this._typeModel) {
(<TypedEmitter<Events>>process).emit("typeResolvingStart", this);
this._typeModel = this._typeResolver.resolveTypes(
this._elementMap,
this._relationMap
);
this._typePool = new TypePool(this._objectMap, this.getAllExports());
this._typePool = new TypePool(this._objectMap, this._exportMap);
(<TypedEmitter<Events>>process).emit("typeResolvingComplete", this);
}
}

Expand All @@ -188,25 +317,4 @@ export class RootContext extends CoreRootContext<t.Node> {

return this._typePool;
}

getElement(id: string): Element {
if (!this._elementMap || !this._elementMap.has(id)) {
this.extractTypes();
}
return this._elementMap.get(id);
}

getRelation(id: string): Relation {
if (!this._relationMap || !this._relationMap.has(id)) {
this.extractTypes();
}
return this._relationMap.get(id);
}

getObject(id: string): DiscoveredObjectType {
if (!this._objectMap || !this._objectMap.has(id)) {
this.extractTypes();
}
return this._objectMap.get(id);
}
}
30 changes: 27 additions & 3 deletions libraries/analysis-javascript/lib/target/TargetVisitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -349,10 +349,34 @@ export class TargetVisitor extends AbstractSyntaxTreeVisitor {
// only thing left where these can be found is:
// call(() => {})
const targetName = this._getTargetNameOfExpression(path);
const id = this._getNodeId(path);
const export_ = this._getExport(id);

this._extractFromFunction(path, id, id, targetName, export_, false, false);
if (path.parentPath.isVariableDeclarator()) {
const id = this._getNodeId(path);
const export_ = this._getExport(id);

this._extractFromFunction(
path,
id,
id,
targetName,
export_,
false,
false
);
} else {
const id = this._getNodeId(path);
const export_ = this._getExport(id);

this._extractFromFunction(
path,
id,
id,
targetName,
export_,
false,
false
);
}

path.skip();
};
Expand Down
Loading
Loading