Skip to content

Commit

Permalink
[EVOL]: add code duplication, circular dependencies and exclude analysis
Browse files Browse the repository at this point in the history
  • Loading branch information
helabenkhalfallah committed Jun 2, 2024
1 parent ec0cb69 commit fe960d0
Show file tree
Hide file tree
Showing 11 changed files with 1,129 additions and 193 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -132,4 +132,5 @@ dist
#project files
MyAwesomeReport.html
*.iml
*.idea
*.idea
target/**
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
"fs-extra": "=8.1.0",
"globals": "=15.3.0",
"globby": "=10.0.1",
"jscpd": "=4.0.4",
"jsinspect": "^0.12.7",
"lodash": "=4.17.21",
"madge": "=7.0.0",
"typhonjs-escomplex": "=0.1.0",
Expand Down
650 changes: 650 additions & 0 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

67 changes: 63 additions & 4 deletions src/commons/AuditUtils.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
import crypto from 'crypto';
import glob from 'globby';
import unixify from 'unixify';
import lodash from 'lodash';
import AppLogger from './AppLogger.js';

/**
* Checks if a file is of an accepted type.
* @param {string} fileName - The name of the file.
Expand Down Expand Up @@ -46,18 +52,71 @@ const isExcludedFile = (filePath) => (
filePath?.toLowerCase()?.endsWith('.d.ts') ||
filePath?.toLowerCase()?.endsWith('.config.js') ||
filePath?.toLowerCase()?.endsWith('.config.ts') ||
filePath?.toLowerCase()?.includes('/types/') ||
filePath?.toLowerCase()?.includes('type') ||
filePath?.toLowerCase()?.includes('index') ||
filePath?.toLowerCase()?.includes('dico') ||
false
);

/**
* The AuditUtils object.
* @typedef {Object} AuditUtils
* @property {function} isAcceptedFileType - Checks if a file is of an accepted type.
* @property {function} isExcludedFile - Checks if a file is excluded.
* Converts a pattern to a file.
* @param {string} pattern - The pattern to convert.
* @returns {Array} - Returns an array containing the files.
*/
const patternToFile = (pattern) => glob.sync(unixify(pattern));

/**
* Retrieves all files from a specified directory.
*
* @param {string} srcDir - The source directory to retrieve files from.
* @returns {Array|null} An array of files from the source directory, or null if the source directory is not specified or an error occurs.
*/
const getFiles = (srcDir) => {
try{
AppLogger.info(`[AuditUtils - parseFiles] srcDir: ${srcDir}`);

if (!srcDir || !srcDir.length) {
return null;
}

const files = lodash
.chain([
srcDir,
])
.map(patternToFile)
.flatten()
.value();

AppLogger.info(`[AuditUtils - parseFiles] files: ${files?.length}`);

return files;
}catch (error) {
AppLogger.info(`[AuditUtils - parseFiles] error: ${error.message}`);
return null;
}
};

/**
* Generate Hash for value string
* @param {string} value - Value to hashify
* @returns {string | null}
*/
const generateHash = (value) => {
if(!value){
return null;
}
return crypto
.createHash('md5')
.update(JSON.stringify(value))
.digest('hex');
};

const AuditUtils = {
isAcceptedFileType,
isExcludedFile,
getFiles,
generateHash,
};

export default AuditUtils;
51 changes: 20 additions & 31 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { parseArgs } from 'node:util';
import AppLogger from './commons/AppLogger.js';
import CodeComplexityAuditor from './kernel/complexity/CodeComplexityAuditor.js';
import CodeCouplingAuditor from './kernel/coupling/CodeCouplingAuditor.js';
import CodeDuplicationAuditor from './kernel/duplication/CodeDuplicationAuditor.js';
import CodeComplexityUtils from './kernel/complexity/CodeComplexityUtils.js';
import CodeCouplingUtils from './kernel/coupling/CodeCouplingUtils.js';

Expand Down Expand Up @@ -57,36 +58,6 @@ const codeComplexityAnalysisResult = await CodeComplexityAuditor.startAudit(
}
);

/**
* Starts the code coupling audit.
* https://github.com/pahen/madge?tab=readme-ov-file#configuration
* @type {Object}
*/
const codeCouplingAnalysisResult = await CodeCouplingAuditor.startAudit(srcDir, {
'fileExtensions': [
'ts',
'tsx',
'js',
'jsx'
],
excludeRegExp: [
'.*node_modules/.*',
'.*dist/.*',
'.*__mocks__/.*',
'.*husky/.*',
'.*husky/.*',
'.*vscode/.*',
'.*idea/.*',
'.*gitlab/.*',
'.*github/.*',
'.*eslint.*',
'.*jest.*',
'.*test.*',
'.*next.config.*',
'.*.d.ts.*',
]
});

/**
* Writes the audit result to files.
*/
Expand All @@ -99,6 +70,16 @@ CodeComplexityUtils
codeComplexityAnalysisResult,
});

/**
* Starts the code coupling audit.
* https://github.com/pahen/madge?tab=readme-ov-file#configuration
* @type {Object}
*/
const codeCouplingAnalysisResult = await CodeCouplingAuditor.startAudit(srcDir);

/**
* Writes the audit result to files.
*/
CodeCouplingUtils
.writeCodeCouplingAuditToFile({
codeCouplingOptions: {
Expand All @@ -108,4 +89,12 @@ CodeCouplingUtils
codeCouplingAnalysisResult,
});


/**
* Starts the code duplication audit.
* @type {Object}
*/
CodeDuplicationAuditor.startAudit(
srcDir,
outputDir,
format
);
10 changes: 3 additions & 7 deletions src/kernel/complexity/CodeComplexityAuditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const {
} = AuditUtils;

const {
inspect,
inspectDirectory,
} = CodeComplexityUtils;

/**
Expand Down Expand Up @@ -155,7 +155,7 @@ const startAudit = async (directory, options) => {
const {
summary,
files,
} = inspect(
} = inspectDirectory(
{
srcDir: directory,
options,
Expand All @@ -176,11 +176,7 @@ const startAudit = async (directory, options) => {
const fileName = item.file;
return (
isAcceptedFileType(fileName) &&
!isExcludedFile(fileName) &&
!fileName?.toLowerCase()?.includes('/types/') &&
!fileName?.toLowerCase()?.includes('type') &&
!fileName?.toLowerCase()?.includes('index') &&
!fileName?.toLowerCase()?.includes('dico')
!isExcludedFile(fileName)
);
})
.sort((a, b) => a.fileMaintainability > b.fileMaintainability ? 1 : -1);
Expand Down
5 changes: 4 additions & 1 deletion src/kernel/complexity/CodeComplexityConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,10 @@ const formatCodeComplexityHtmlReport = (summary, helpMessages, reportsByFile) =>
<h2 class="card-title text-body-secondary mt-1">Global Status</h2>
<p class="card-text text-start mt-4">
<span class="text-primary-emphasis">
Source lines of code (SLOC): <strong>${average?.sloc || 0}</strong>
Total number of lines in the source code (physical SLOC): <strong>${average?.psloc || 0}</strong>
</span>
<span class="text-primary-emphasis">
Number of lines that will be executed (logical SLOC): <strong>${average?.lsloc || 0}</strong>
</span>
<br />
<span class="text-primary-emphasis">
Expand Down
Loading

0 comments on commit fe960d0

Please sign in to comment.