Skip to content
Closed
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
2 changes: 1 addition & 1 deletion src/analyze.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import * as processFunctions from "./analyze_functions/process_functions";
import { EntityDictionary } from "./famix_functions/EntityDictionary";
import path from "path";

export const logger = new Logger({ name: "ts2famix", minLevel: 3 });
export const logger = new Logger({ name: "ts2famix", minLevel: 2 });
export const config = { "expectGraphemes": false };
export const entityDictionary = new EntityDictionary();

Expand Down
50 changes: 31 additions & 19 deletions src/analyze_functions/process_functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,19 +78,15 @@ export function getImplementedOrExtendedInterfaces(interfaces: Array<InterfaceDe
return implementedOrExtendedInterfaces;
}

/**
* Builds a Famix model for an array of source files
* @param sourceFiles An array of source files
*/
export function processFiles(sourceFiles: Array<SourceFile>): void {
sourceFiles.forEach(file => {
logger.info(`File: >>>>>>>>>> ${file.getFilePath()}`);

// Computes the cyclomatic complexity metrics for the current source file if it exists (i.e. if it is not from a jest test)
if (fs.existsSync(file.getFilePath()))
if (fs.existsSync(file.getFilePath())) {
currentCC = calculate(file.getFilePath());
else
} else {
currentCC = {};
}

processFile(file);
});
Expand All @@ -115,22 +111,18 @@ function processFile(f: SourceFile): void {
logger.debug(`processFile: file: ${f.getBaseName()}, fqn = ${fmxFile.fullyQualifiedName}`);

processComments(f, fmxFile);

processAliases(f, fmxFile);

processClasses(f, fmxFile);

processInterfaces(f, fmxFile);

processVariables(f, fmxFile);

processInterfaces(f, fmxFile);
processModules(f, fmxFile);
processVariables(f, fmxFile); // This will handle our object literal methods
processEnums(f, fmxFile);

processFunctions(f, fmxFile);


processModules(f, fmxFile);
}


export function isAmbient(node: ModuleDeclaration): boolean {
// An ambient module has the DeclareKeyword modifier.
return (node.getModifiers()?.some(modifier => modifier.getKind() === SyntaxKind.DeclareKeyword)) ?? false;
Expand Down Expand Up @@ -253,6 +245,26 @@ function processVariables(m: ContainerTypes, fmxScope: Famix.ScriptEntity | Fami
fmxVariables.forEach(fmxVariable => {
fmxScope.addVariable(fmxVariable);
});

// Check each VariableDeclaration for object literal methods
v.getDeclarations().forEach(varDecl => {
const varName = varDecl.getName();
console.log(`Checking variable: ${varName} at pos=${varDecl.getStart()}`);
const initializer = varDecl.getInitializer();
if (initializer && Node.isObjectLiteralExpression(initializer)) {
initializer.getProperties().forEach(prop => {
if (Node.isPropertyAssignment(prop)) {
const nested = prop.getInitializer();
if (nested && Node.isObjectLiteralExpression(nested)) {
nested.getDescendantsOfKind(SyntaxKind.MethodDeclaration).forEach(method => {
console.log(`Found object literal method: ${method.getName()} at pos=${method.getStart()}`);
entityDictionary.createOrGetFamixMethod(method, currentCC);
});
}
}
});
}
});
});
}

Expand Down Expand Up @@ -868,7 +880,7 @@ export function processImportClausesForImportEqualsDeclarations(sourceFiles: Arr
export function processImportClausesForModules(modules: Array<SourceFile>, exports: Array<ReadonlyMap<string, ExportedDeclarations[]>>): void {
logger.info(`Creating import clauses from ${modules.length} modules:`);
modules.forEach(module => {
const modulePath = module.getFilePath(); // + module.getBaseName();
const modulePath = module.getFilePath();
module.getImportDeclarations().forEach(impDecl => {
logger.info(`Importing ${impDecl.getModuleSpecifierValue()} in ${modulePath}`);
const path = getModulePath(impDecl);
Expand All @@ -877,6 +889,7 @@ export function processImportClausesForModules(modules: Array<SourceFile>, expor
logger.info(`Importing (named) ${namedImport.getName()} from ${impDecl.getModuleSpecifierValue()} in ${modulePath}`);
const importedEntityName = namedImport.getName();
const importFoundInExports = isInExports(exports, importedEntityName);
logger.debug(`Processing ImportSpecifier: ${namedImport.getText()}, pos=${namedImport.getStart()}`);
entityDictionary.oldCreateOrGetFamixImportClause({
importDeclaration: impDecl,
importerSourceFile: module,
Expand All @@ -890,7 +903,7 @@ export function processImportClausesForModules(modules: Array<SourceFile>, expor
const defaultImport = impDecl.getDefaultImport();
if (defaultImport !== undefined) {
logger.info(`Importing (default) ${defaultImport.getText()} from ${impDecl.getModuleSpecifierValue()} in ${modulePath}`);
// call with module, impDecl.getModuleSpecifierValue(), path, defaultImport, false, true
logger.debug(`Processing Default Import: ${defaultImport.getText()}, pos=${defaultImport.getStart()}`);
entityDictionary.oldCreateOrGetFamixImportClause({
importDeclaration: impDecl,
importerSourceFile: module,
Expand All @@ -912,7 +925,6 @@ export function processImportClausesForModules(modules: Array<SourceFile>, expor
isInExports: false,
isDefaultExport: false
});
// entityDictionary.createFamixImportClause(module, impDecl.getModuleSpecifierValue(), path, namespaceImport, false, false);
}
});
});
Expand Down
Loading