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
6 changes: 5 additions & 1 deletion jest.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,9 @@
"^.+\\.tsx?$": ["ts-jest", { }]
},
"testEnvironment": "jest-environment-node",
"verbose": true
"verbose": true,
"testPathIgnorePatterns": [
"/node_modules/",
"/vscode-extension/"
]
}
69 changes: 55 additions & 14 deletions src/analyze.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
import { Project } from "ts-morph";
import { Project, SourceFile } from "ts-morph";
import * as fs from 'fs';
import { FamixRepository } from "./lib/famix/famix_repository";
import { Logger } from "tslog";
import { EntityDictionary, EntityDictionaryConfig } from "./famix_functions/EntityDictionary";
import path from "path";
import { TypeScriptToFamixProcessor } from "./analyze_functions/process_functions";
import { getFamixIndexFileAnchorFileName } from "./helpers";
import { isSourceFileAModule } from "./famix_functions/helpersTsMorphElementsProcessing";
import { FamixBaseElement } from "./lib/famix/famix_base_element";
import { getDirectDependentAssociations, getSourceFilesToUpdate, removeDependentAssociations } from "./helpers";
import { getTransientDependentEntities } from "./helpers/transientDependencyResolverHelper";

export const logger = new Logger({ name: "ts2famix", minLevel: 2 });

export enum SourceFileChangeType {
Create = 0,
Update = 1,
Delete = 2,
}

/**
* This class is used to build a Famix model from a TypeScript source code
*/
Expand Down Expand Up @@ -59,20 +70,16 @@ export class Importer {
private processEntities(project: Project): void {
const onlyTypeScriptFiles = project.getSourceFiles().filter(f => f.getFilePath().endsWith('.ts'));
this.processFunctions.processFiles(onlyTypeScriptFiles);
const accesses = this.processFunctions.accessMap;
const methodsAndFunctionsWithId = this.processFunctions.methodsAndFunctionsWithId;
const classes = this.processFunctions.classes;
const interfaces = this.processFunctions.interfaces;
const modules = this.processFunctions.modules;
const exports = this.processFunctions.listOfExportMaps;

this.processFunctions.processImportClausesForImportEqualsDeclarations(project.getSourceFiles(), exports);
this.processFunctions.processImportClausesForModules(modules, exports);
this.processFunctions.processAccesses(accesses);
this.processFunctions.processInvocations(methodsAndFunctionsWithId);
this.processFunctions.processInheritances(classes, interfaces);
this.processFunctions.processConcretisations(classes, interfaces, methodsAndFunctionsWithId);

this.processReferences(onlyTypeScriptFiles, onlyTypeScriptFiles);
}

private processReferences(sourceFiles: SourceFile[], allExistingSourceFiles: SourceFile[]): void {
// TODO: process Access, Invocations, Concretisations
this.processFunctions.processImportClausesForImportEqualsDeclarations(allExistingSourceFiles);

const modules = sourceFiles.filter(f => isSourceFileAModule(f));
this.processFunctions.processImportClausesForModules(modules);
}

/**
Expand Down Expand Up @@ -103,13 +110,47 @@ export class Importer {

//const famixRep = this.famixRepFromPaths(sourceFileNames);

this.project = project;
this.initFamixRep(project);

this.processEntities(project);

return this.entityDictionary.famixRep;
}

public updateFamixModelIncrementally(sourceFileChangeMap: Map<SourceFileChangeType, SourceFile[]>): void {
const allChangedSourceFiles = Array.from(sourceFileChangeMap.values()).flat();

const removedEntities: FamixBaseElement[] = [];
allChangedSourceFiles.forEach(
file => {
const filePath = getFamixIndexFileAnchorFileName(file.getFilePath(), this.entityDictionary.getAbsolutePath());
const removed = this.entityDictionary.famixRep.removeEntitiesBySourceFile(filePath);
removedEntities.push(...removed);
}
);

const allSourceFiles = this.project.getSourceFiles();
const directDependentAssociations = getDirectDependentAssociations(removedEntities);
const transientDependentAssociations = getTransientDependentEntities(this.entityDictionary, sourceFileChangeMap);

const associationsToRemove = [...directDependentAssociations, ...transientDependentAssociations];

removeDependentAssociations(this.entityDictionary.famixRep, associationsToRemove);

const sourceFilesToEnsure = getSourceFilesToUpdate(
associationsToRemove, sourceFileChangeMap, allSourceFiles, this.entityDictionary.getAbsolutePath()
);

this.processFunctions.processFiles(sourceFilesToEnsure);
const sourceFilesToDelete = sourceFileChangeMap.get(SourceFileChangeType.Delete) || [];
const existingSourceFiles = allSourceFiles.filter(
file => !sourceFilesToDelete.includes(file)
);
this.processReferences(sourceFilesToEnsure, existingSourceFiles);

}

private initFamixRep(project: Project): void {
// get compiler options
const compilerOptions = project.getCompilerOptions();
Expand Down
Loading
Loading