/**
* Generates the AstFile corresponding to a given path and a given AstFolder
* @param {string} path
* @param {AstFolderInterface} astFolder
* @returns AstFileInterface
*/
generate(path: string, astFolder: AstFolderInterface): AstFileInterface { // ------------------------------------------------------------------------- +0.6 Complexity index (+0.6 atomic)
if (!path || !astFolder) { // -------------------------------------------------------------------------------------------------------------------- +2.5 Complexity index (+0.5 atomic, +2 structural)
console.warn('No path or AstFolder : impossible to create AstFile'); // ---------------------------------------------------------------------- +1.3 Complexity index (+0.3 atomic, +1 structural)
return undefined; // ------------------------------------------------------------------------------------------------------------------------- +0.2 Complexity index (+0.2 atomic)
}
const fileContent = fs.readFileSync(path, 'utf8'); // -------------------------------------------------------------------------------------------- +1.6 Complexity index (+0.6 atomic, +1 structural)
const cst = parse(fileContent) // ---------------------------------------------------------------------------------------------------------------- +1.4 Complexity index (+0.4 atomic, +1 structural)
let classDeclaration = cst.children.ordinaryCompilationUnit[0].children?.typeDeclaration?.[0]?.children?.classDeclaration?.[0]; // --------------- +1.2 Complexity index (+1.2 atomic)
let interfaceDeclaration = cst.children.ordinaryCompilationUnit[0].children?.typeDeclaration?.[0]?.children?.interfaceDeclaration?.[0]; // ------- +1.2 Complexity index (+1.2 atomic)
let ast: any = []; // ---------------------------------------------------------------------------------------------------------------------------- +0.3 Complexity index (+0.3 atomic)
if(classDeclaration) { // ------------------------------------------------------------------------------------------------------------------------ +1.2 Complexity index (+0.2 atomic, +1 structural)
ast = cstToAst(classDeclaration); // --------------------------------------------------------------------------------------------------------- +1.4 Complexity index (+0.4 atomic, +1 structural)
} else if(interfaceDeclaration) { // ------------------------------------------------------------------------------------------------------------- +1.2 Complexity index (+0.2 atomic, +1 structural)
ast = cstToAst(interfaceDeclaration); // ----------------------------------------------------------------------------------------------------- +1.4 Complexity index (+0.4 atomic, +1 structural)
}
return { // -------------------------------------------------------------------------------------------------------------------------------------- +0.1 Complexity index (+0.1 atomic)
name: getFilename(path), // ------------------------------------------------------------------------------------------------------------------ +1.3 Complexity index (+0.3 atomic, +1 structural)
text: fileContent, // ------------------------------------------------------------------------------------------------------------------------ +0.2 Complexity index (+0.2 atomic)
astNode: { // -------------------------------------------------------------------------------------------------------------------------------- +0.1 Complexity index (+0.1 atomic)
kind: 'SourceFile', // ------------------------------------------------------------------------------------------------------------------- +0.2 Complexity index (+0.2 atomic)
start: 0, // ----------------------------------------------------------------------------------------------------------------------------- +0.2 Complexity index (+0.2 atomic)
pos: 0, // ------------------------------------------------------------------------------------------------------------------------------- +0.2 Complexity index (+0.2 atomic)
end: fileContent.length, // -------------------------------------------------------------------------------------------------------------- +0.3 Complexity index (+0.3 atomic)
children: [ // --------------------------------------------------------------------------------------------------------------------------- +0.1 Complexity index (+0.1 atomic)
ast, // ------------------------------------------------------------------------------------------------------------------------------ +0.1 Complexity index (+0.1 atomic)
{
"end": fileContent.length, // ---------------------------------------------------------------------------------------------------- +0.3 Complexity index (+0.3 atomic)
"kind": "EndOfFileToken", // ----------------------------------------------------------------------------------------------------- +0.2 Complexity index (+0.2 atomic)
"pos": fileContent.length, // ---------------------------------------------------------------------------------------------------- +0.3 Complexity index (+0.3 atomic)
"start": fileContent.length // --------------------------------------------------------------------------------------------------- +0.3 Complexity index (+0.3 atomic)
}
]
}
};
}
|