Skip to content

Commit 44d9386

Browse files
authored
feat: type pool (#158)
- change the encoding structure from constructor -> methods to method -> constructor - create a statement pool per encoding such that constants, return values, and constructor instances can be reused - create a type pool that is used by the sampler to sample constructor instances over mocked objects - move some of the functionality of the sampler into generator classes
1 parent 0d2226e commit 44d9386

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+3681
-16361
lines changed

libraries/analysis-javascript/lib/RootContext.ts

Lines changed: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,24 +27,27 @@ import { DependencyFactory } from "./dependency/DependencyFactory";
2727
import { Export } from "./target/export/Export";
2828
import { TargetFactory } from "./target/TargetFactory";
2929
import { TypeModelFactory } from "./type/resolving/TypeModelFactory";
30-
import { readFile } from "./utils/fileSystem";
30+
import { getAllFiles, readFile } from "./utils/fileSystem";
3131
import { ExportFactory } from "./target/export/ExportFactory";
3232
import { TypeExtractor } from "./type/discovery/TypeExtractor";
3333
import { TypeModel } from "./type/resolving/TypeModel";
3434
import { Element } from "./type/discovery/element/Element";
3535
import { DiscoveredObjectType } from "./type/discovery/object/DiscoveredType";
3636
import { Relation } from "./type/discovery/relation/Relation";
37+
import { TypePool } from "./type/resolving/TypePool";
3738

3839
export 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();

libraries/analysis-javascript/lib/target/Target.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ export interface ClassTarget extends NamedSubTarget, Exportable {
6565

6666
export interface MethodTarget extends NamedSubTarget, Callable {
6767
type: TargetType.METHOD;
68-
className: string;
68+
classId: string;
6969

7070
visibility: VisibilityType;
7171

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

8080
export interface ObjectFunctionTarget extends NamedSubTarget, Callable {
8181
type: TargetType.OBJECT_FUNCTION;
82-
objectName: string;
82+
objectId: string;
8383
}
8484

8585
export interface PathTarget extends SubTarget {

0 commit comments

Comments
 (0)