escomplex-plugin-metrics-module
Advanced tools
Comparing version 0.0.12 to 0.0.13
@@ -1,2 +0,2 @@ | ||
'use strict'; | ||
"use strict"; | ||
@@ -9,8 +9,2 @@ Object.defineProperty(exports, "__esModule", { | ||
var _ObjectUtil = require('typhonjs-escomplex-commons/dist/utils/ObjectUtil'); | ||
var _ObjectUtil2 = _interopRequireDefault(_ObjectUtil); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
@@ -30,3 +24,3 @@ | ||
_createClass(ModuleMetricCalculate, null, [{ | ||
key: 'calculate', | ||
key: "calculate", | ||
@@ -38,20 +32,10 @@ /** | ||
* @param {ModuleReport} moduleReport - The ModuleReport being processed. | ||
* @param {object} settings - Settings for module processing. | ||
* | ||
* @private | ||
*/ | ||
value: function calculate(moduleReport, settings) { | ||
var moduleMethodCount = moduleReport.methods.length; | ||
var moduleMethodAverages = moduleReport.methodAverage; | ||
var moduleMethodAverageKeys = _ObjectUtil2.default.getAccessorList(moduleMethodAverages); | ||
value: function calculate(moduleReport) { | ||
// Handle module methods. | ||
moduleReport.methods.forEach(function (methodReport) { | ||
moduleMethodAverageKeys.forEach(function (averageKey) { | ||
ModuleMetricCalculate.calculateCyclomaticDensity(methodReport); | ||
ModuleMetricCalculate.calculateHalsteadMetrics(methodReport.halstead); | ||
var targetValue = _ObjectUtil2.default.safeAccess(methodReport, averageKey, 0); | ||
_ObjectUtil2.default.safeSet(moduleMethodAverages, averageKey, targetValue, 'add'); | ||
}); | ||
ModuleMetricCalculate.calculateCyclomaticDensity(methodReport); | ||
ModuleMetricCalculate.calculateHalsteadMetrics(methodReport.halstead); | ||
}); | ||
@@ -61,7 +45,2 @@ | ||
moduleReport.classes.forEach(function (classReport) { | ||
var classMethodAverages = classReport.methodAverage; | ||
var classMethodCount = classReport.methods.length; | ||
moduleMethodCount += classMethodCount; | ||
// Process all class methods. | ||
@@ -71,9 +50,2 @@ classReport.methods.forEach(function (methodReport) { | ||
ModuleMetricCalculate.calculateHalsteadMetrics(methodReport.halstead); | ||
moduleMethodAverageKeys.forEach(function (averageKey) { | ||
var targetValue = _ObjectUtil2.default.safeAccess(methodReport, averageKey, 0); | ||
_ObjectUtil2.default.safeSet(moduleMethodAverages, averageKey, targetValue, 'add'); | ||
_ObjectUtil2.default.safeSet(classMethodAverages, averageKey, targetValue, 'add'); | ||
}); | ||
}); | ||
@@ -83,20 +55,2 @@ | ||
ModuleMetricCalculate.calculateHalsteadMetrics(classReport.aggregateMethodReport.halstead); | ||
// If there are no class methods use the class aggregate MethodReport. | ||
if (classMethodCount === 0) { | ||
// Sane handling of classes that contain no methods. | ||
moduleMethodAverageKeys.forEach(function (averageKey) { | ||
var targetValue = _ObjectUtil2.default.safeAccess(classReport.aggregateMethodReport, averageKey, 0); | ||
_ObjectUtil2.default.safeSet(classMethodAverages, averageKey, targetValue, 'add'); | ||
}); | ||
classMethodCount = 1; | ||
} | ||
moduleMethodAverageKeys.forEach(function (averageKey) { | ||
_ObjectUtil2.default.safeSet(classMethodAverages, averageKey, classMethodCount, 'div'); | ||
}); | ||
ModuleMetricCalculate.calculateMaintainabilityIndex(classReport, settings, classMethodAverages.cyclomatic, classMethodAverages.halstead.effort, classMethodAverages.sloc.logical); | ||
}); | ||
@@ -106,21 +60,2 @@ | ||
ModuleMetricCalculate.calculateHalsteadMetrics(moduleReport.aggregateMethodReport.halstead); | ||
// If there are no module methods use the module aggregate MethodReport. | ||
if (moduleMethodCount === 0) { | ||
// Sane handling of classes that contain no methods. | ||
moduleMethodAverageKeys.forEach(function (averageKey) { | ||
var targetValue = _ObjectUtil2.default.safeAccess(moduleReport.aggregateMethodReport, averageKey, 0); | ||
_ObjectUtil2.default.safeSet(moduleMethodAverages, averageKey, targetValue, 'add'); | ||
}); | ||
// Sane handling of modules that contain no methods. | ||
moduleMethodCount = 1; | ||
} | ||
moduleMethodAverageKeys.forEach(function (averageKey) { | ||
_ObjectUtil2.default.safeSet(moduleMethodAverages, averageKey, moduleMethodCount, 'div'); | ||
}); | ||
ModuleMetricCalculate.calculateMaintainabilityIndex(moduleReport, settings, moduleMethodAverages.cyclomatic, moduleMethodAverages.halstead.effort, moduleMethodAverages.sloc.logical); | ||
} | ||
@@ -139,3 +74,3 @@ | ||
}, { | ||
key: 'calculateCyclomaticDensity', | ||
key: "calculateCyclomaticDensity", | ||
value: function calculateCyclomaticDensity(report) { | ||
@@ -159,3 +94,3 @@ report.cyclomaticDensity = report.sloc.logical === 0 ? 0 : report.cyclomatic / report.sloc.logical * 100; | ||
}, { | ||
key: 'calculateHalsteadMetrics', | ||
key: "calculateHalsteadMetrics", | ||
value: function calculateHalsteadMetrics(halstead) { | ||
@@ -176,45 +111,2 @@ halstead.length = halstead.operators.total + halstead.operands.total; | ||
} | ||
/** | ||
* Designed in 1991 by Paul Oman and Jack Hagemeister at the University of Idaho, this metric is calculated at the | ||
* whole program or module level from averages of the other 3 metrics, using the following formula: | ||
* ``` | ||
* 171 - | ||
* (3.42 * ln(mean effort)) - | ||
* (0.23 * ln(mean cyclomatic complexity)) - | ||
* (16.2 * ln(mean logical LOC)) | ||
* ``` | ||
* Values are on a logarithmic scale ranging from negative infinity up to 171, with greater numbers indicating a | ||
* higher level of maintainability. In their original paper, Oman and Hagemeister identified 65 as the threshold | ||
* value below which a program should be considered difficult to maintain. | ||
* | ||
* @param {ClassReport|ModuleReport} report - A ClassReport or ModuleReport to perform calculations on. | ||
* @param {object} settings - Settings for module processing. | ||
* @param {number} averageCyclomatic - Average cyclomatic metric across a ClassReport / ModuleReport. | ||
* @param {number} averageEffort - Average Halstead effort across a ClassReport / ModuleReport. | ||
* @param {number} averageLoc - Average SLOC metric across a ClassReport / ModuleReport. | ||
* | ||
* @private | ||
*/ | ||
}, { | ||
key: 'calculateMaintainabilityIndex', | ||
value: function calculateMaintainabilityIndex(report, settings, averageCyclomatic, averageEffort, averageLoc) { | ||
/* istanbul ignore if */ | ||
if (averageCyclomatic === 0) { | ||
throw new Error('Encountered function with cyclomatic complexity zero!'); | ||
} | ||
report.maintainability = 171 - 3.42 * Math.log(averageEffort) - 0.23 * Math.log(averageCyclomatic) - 16.2 * Math.log(averageLoc); | ||
/* istanbul ignore if */ | ||
if (report.maintainability > 171) { | ||
report.maintainability = 171; | ||
} | ||
/* istanbul ignore if */ | ||
if (settings.newmi) { | ||
report.maintainability = Math.max(0, report.maintainability * 100 / 171); | ||
} | ||
} | ||
}]); | ||
@@ -226,2 +118,2 @@ | ||
exports.default = ModuleMetricCalculate; | ||
module.exports = exports['default']; | ||
module.exports = exports["default"]; |
@@ -11,2 +11,6 @@ 'use strict'; | ||
var _ModuleMetricAverage = require('./ModuleMetricAverage'); | ||
var _ModuleMetricAverage2 = _interopRequireDefault(_ModuleMetricAverage); | ||
var _ModuleMetricCalculate = require('./ModuleMetricCalculate'); | ||
@@ -16,6 +20,10 @@ | ||
var _ModuleMetricControl = require('./ModuleMetricControl'); | ||
var _ModuleMetricPostAverage = require('./ModuleMetricPostAverage'); | ||
var _ModuleMetricControl2 = _interopRequireDefault(_ModuleMetricControl); | ||
var _ModuleMetricPostAverage2 = _interopRequireDefault(_ModuleMetricPostAverage); | ||
var _ModuleMetricProcess = require('./ModuleMetricProcess'); | ||
var _ModuleMetricProcess2 = _interopRequireDefault(_ModuleMetricProcess); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
@@ -56,3 +64,3 @@ | ||
* During AST traversal when a node is entered it is processed immediately if the node type corresponds to a | ||
* loaded trait syntax. | ||
* loaded trait syntax. This is the main metric capture and processing location. | ||
* | ||
@@ -73,3 +81,3 @@ * @param {object} ev - escomplex plugin event data. | ||
if ((typeof syntax === 'undefined' ? 'undefined' : _typeof(syntax)) === 'object') { | ||
_ModuleMetricControl2.default.processSyntax(moduleReport, scopeControl, syntax, node, parent); | ||
_ModuleMetricProcess2.default.processSyntax(moduleReport, scopeControl, syntax, node, parent); | ||
} | ||
@@ -79,3 +87,3 @@ } | ||
/** | ||
* Performs final calculations based on collected report data. | ||
* Performs average calculations based on collected report data. | ||
* | ||
@@ -86,9 +94,9 @@ * @param {object} ev - escomplex plugin event data. | ||
}, { | ||
key: 'onModuleEnd', | ||
value: function onModuleEnd(ev) { | ||
_ModuleMetricCalculate2.default.calculate(ev.data.moduleReport, ev.data.settings); | ||
key: 'onModuleAverage', | ||
value: function onModuleAverage(ev) { | ||
_ModuleMetricAverage2.default.calculate(ev.data.moduleReport); | ||
} | ||
/** | ||
* A new module report scope has been created. Update any associated metrics regarding the new scope. | ||
* Performs initial calculations based on collected report data. | ||
* | ||
@@ -99,10 +107,30 @@ * @param {object} ev - escomplex plugin event data. | ||
}, { | ||
key: 'onScopeCreated', | ||
value: function onScopeCreated(ev) { | ||
var moduleReport = ev.data.moduleReport; | ||
var scopeControl = ev.data.scopeControl; | ||
var newScope = ev.data.newScope; | ||
key: 'onModuleCalculate', | ||
value: function onModuleCalculate(ev) { | ||
_ModuleMetricCalculate2.default.calculate(ev.data.moduleReport); | ||
} | ||
_ModuleMetricControl2.default.createScope(moduleReport, scopeControl, newScope); | ||
/** | ||
* Performs any calculations that depend on averaged data. This is where the maintainability index is calculated. | ||
* | ||
* @param {object} ev - escomplex plugin event data. | ||
*/ | ||
}, { | ||
key: 'onModulePostAverage', | ||
value: function onModulePostAverage(ev) { | ||
_ModuleMetricPostAverage2.default.calculate(ev.data.moduleReport, ev.data.settings); | ||
} | ||
/** | ||
* A new module report scope has been created. Update any associated metrics processing regarding the new scope. | ||
* | ||
* @param {object} ev - escomplex plugin event data. | ||
*/ | ||
}, { | ||
key: 'onModuleScopeCreated', | ||
value: function onModuleScopeCreated(ev) { | ||
_ModuleMetricProcess2.default.createScope(ev.data.moduleReport, ev.data.scopeControl, ev.data.newScope); | ||
} | ||
}]); | ||
@@ -109,0 +137,0 @@ |
{ | ||
"name": "escomplex-plugin-metrics-module", | ||
"version": "0.0.12", | ||
"version": "0.0.13", | ||
"homepage": "https://github.com/typhonjs-node-escomplex/escomplex-plugin-metrics-module/", | ||
@@ -26,3 +26,3 @@ "description": "Provides the core module metric / report generation plugin for typhonjs-escomplex module processing.", | ||
"devDependencies": { | ||
"escomplex-plugin-syntax-babylon": "^0.0.12", | ||
"escomplex-plugin-syntax-babylon": "^0.0.13", | ||
"typhonjs-ast-walker": "^0.1.0", | ||
@@ -29,0 +29,0 @@ "typhonjs-config-eslint": "^0.4.0", |
@@ -1,3 +0,1 @@ | ||
import ObjectUtil from 'typhonjs-escomplex-commons/src/utils/ObjectUtil'; | ||
/** | ||
@@ -16,23 +14,12 @@ * Provides a typhonjs-escomplex-module / ESComplexModule plugin which gathers and calculates all default metrics. | ||
* @param {ModuleReport} moduleReport - The ModuleReport being processed. | ||
* @param {object} settings - Settings for module processing. | ||
* | ||
* @private | ||
*/ | ||
static calculate(moduleReport, settings) | ||
static calculate(moduleReport) | ||
{ | ||
let moduleMethodCount = moduleReport.methods.length; | ||
const moduleMethodAverages = moduleReport.methodAverage; | ||
const moduleMethodAverageKeys = ObjectUtil.getAccessorList(moduleMethodAverages); | ||
// Handle module methods. | ||
moduleReport.methods.forEach((methodReport) => | ||
{ | ||
moduleMethodAverageKeys.forEach((averageKey) => | ||
{ | ||
ModuleMetricCalculate.calculateCyclomaticDensity(methodReport); | ||
ModuleMetricCalculate.calculateHalsteadMetrics(methodReport.halstead); | ||
const targetValue = ObjectUtil.safeAccess(methodReport, averageKey, 0); | ||
ObjectUtil.safeSet(moduleMethodAverages, averageKey, targetValue, 'add'); | ||
}); | ||
ModuleMetricCalculate.calculateCyclomaticDensity(methodReport); | ||
ModuleMetricCalculate.calculateHalsteadMetrics(methodReport.halstead); | ||
}); | ||
@@ -43,7 +30,2 @@ | ||
{ | ||
const classMethodAverages = classReport.methodAverage; | ||
let classMethodCount = classReport.methods.length; | ||
moduleMethodCount += classMethodCount; | ||
// Process all class methods. | ||
@@ -54,10 +36,2 @@ classReport.methods.forEach((methodReport) => | ||
ModuleMetricCalculate.calculateHalsteadMetrics(methodReport.halstead); | ||
moduleMethodAverageKeys.forEach((averageKey) => | ||
{ | ||
const targetValue = ObjectUtil.safeAccess(methodReport, averageKey, 0); | ||
ObjectUtil.safeSet(moduleMethodAverages, averageKey, targetValue, 'add'); | ||
ObjectUtil.safeSet(classMethodAverages, averageKey, targetValue, 'add'); | ||
}); | ||
}); | ||
@@ -67,24 +41,2 @@ | ||
ModuleMetricCalculate.calculateHalsteadMetrics(classReport.aggregateMethodReport.halstead); | ||
// If there are no class methods use the class aggregate MethodReport. | ||
if (classMethodCount === 0) | ||
{ | ||
// Sane handling of classes that contain no methods. | ||
moduleMethodAverageKeys.forEach((averageKey) => | ||
{ | ||
const targetValue = ObjectUtil.safeAccess(classReport.aggregateMethodReport, averageKey, 0); | ||
ObjectUtil.safeSet(classMethodAverages, averageKey, targetValue, 'add'); | ||
}); | ||
classMethodCount = 1; | ||
} | ||
moduleMethodAverageKeys.forEach((averageKey) => | ||
{ | ||
ObjectUtil.safeSet(classMethodAverages, averageKey, classMethodCount, 'div'); | ||
}); | ||
ModuleMetricCalculate.calculateMaintainabilityIndex(classReport, settings, classMethodAverages.cyclomatic, | ||
classMethodAverages.halstead.effort, classMethodAverages.sloc.logical); | ||
}); | ||
@@ -94,25 +46,2 @@ | ||
ModuleMetricCalculate.calculateHalsteadMetrics(moduleReport.aggregateMethodReport.halstead); | ||
// If there are no module methods use the module aggregate MethodReport. | ||
if (moduleMethodCount === 0) | ||
{ | ||
// Sane handling of classes that contain no methods. | ||
moduleMethodAverageKeys.forEach((averageKey) => | ||
{ | ||
const targetValue = ObjectUtil.safeAccess(moduleReport.aggregateMethodReport, averageKey, 0); | ||
ObjectUtil.safeSet(moduleMethodAverages, averageKey, targetValue, 'add'); | ||
}); | ||
// Sane handling of modules that contain no methods. | ||
moduleMethodCount = 1; | ||
} | ||
moduleMethodAverageKeys.forEach((averageKey) => | ||
{ | ||
ObjectUtil.safeSet(moduleMethodAverages, averageKey, moduleMethodCount, 'div'); | ||
}); | ||
ModuleMetricCalculate.calculateMaintainabilityIndex(moduleReport, settings, moduleMethodAverages.cyclomatic, | ||
moduleMethodAverages.halstead.effort, moduleMethodAverages.sloc.logical); | ||
} | ||
@@ -166,41 +95,2 @@ | ||
} | ||
/** | ||
* Designed in 1991 by Paul Oman and Jack Hagemeister at the University of Idaho, this metric is calculated at the | ||
* whole program or module level from averages of the other 3 metrics, using the following formula: | ||
* ``` | ||
* 171 - | ||
* (3.42 * ln(mean effort)) - | ||
* (0.23 * ln(mean cyclomatic complexity)) - | ||
* (16.2 * ln(mean logical LOC)) | ||
* ``` | ||
* Values are on a logarithmic scale ranging from negative infinity up to 171, with greater numbers indicating a | ||
* higher level of maintainability. In their original paper, Oman and Hagemeister identified 65 as the threshold | ||
* value below which a program should be considered difficult to maintain. | ||
* | ||
* @param {ClassReport|ModuleReport} report - A ClassReport or ModuleReport to perform calculations on. | ||
* @param {object} settings - Settings for module processing. | ||
* @param {number} averageCyclomatic - Average cyclomatic metric across a ClassReport / ModuleReport. | ||
* @param {number} averageEffort - Average Halstead effort across a ClassReport / ModuleReport. | ||
* @param {number} averageLoc - Average SLOC metric across a ClassReport / ModuleReport. | ||
* | ||
* @private | ||
*/ | ||
static calculateMaintainabilityIndex(report, settings, averageCyclomatic, averageEffort, averageLoc) | ||
{ | ||
/* istanbul ignore if */ | ||
if (averageCyclomatic === 0) { throw new Error('Encountered function with cyclomatic complexity zero!'); } | ||
report.maintainability = | ||
171 | ||
- (3.42 * Math.log(averageEffort)) | ||
- (0.23 * Math.log(averageCyclomatic)) | ||
- (16.2 * Math.log(averageLoc)); | ||
/* istanbul ignore if */ | ||
if (report.maintainability > 171) { report.maintainability = 171; } | ||
/* istanbul ignore if */ | ||
if (settings.newmi) { report.maintainability = Math.max(0, (report.maintainability * 100) / 171); } | ||
} | ||
} |
@@ -1,3 +0,5 @@ | ||
import ModuleMetricCalculate from './ModuleMetricCalculate'; | ||
import ModuleMetricControl from './ModuleMetricControl'; | ||
import ModuleMetricAverage from './ModuleMetricAverage'; | ||
import ModuleMetricCalculate from './ModuleMetricCalculate'; | ||
import ModuleMetricPostAverage from './ModuleMetricPostAverage'; | ||
import ModuleMetricProcess from './ModuleMetricProcess'; | ||
@@ -30,3 +32,3 @@ /** | ||
* During AST traversal when a node is entered it is processed immediately if the node type corresponds to a | ||
* loaded trait syntax. | ||
* loaded trait syntax. This is the main metric capture and processing location. | ||
* | ||
@@ -46,3 +48,3 @@ * @param {object} ev - escomplex plugin event data. | ||
{ | ||
ModuleMetricControl.processSyntax(moduleReport, scopeControl, syntax, node, parent); | ||
ModuleMetricProcess.processSyntax(moduleReport, scopeControl, syntax, node, parent); | ||
} | ||
@@ -52,24 +54,40 @@ } | ||
/** | ||
* Performs final calculations based on collected report data. | ||
* Performs average calculations based on collected report data. | ||
* | ||
* @param {object} ev - escomplex plugin event data. | ||
*/ | ||
onModuleEnd(ev) | ||
onModuleAverage(ev) | ||
{ | ||
ModuleMetricCalculate.calculate(ev.data.moduleReport, ev.data.settings); | ||
ModuleMetricAverage.calculate(ev.data.moduleReport); | ||
} | ||
/** | ||
* A new module report scope has been created. Update any associated metrics regarding the new scope. | ||
* Performs initial calculations based on collected report data. | ||
* | ||
* @param {object} ev - escomplex plugin event data. | ||
*/ | ||
onScopeCreated(ev) | ||
onModuleCalculate(ev) | ||
{ | ||
const moduleReport = ev.data.moduleReport; | ||
const scopeControl = ev.data.scopeControl; | ||
const newScope = ev.data.newScope; | ||
ModuleMetricCalculate.calculate(ev.data.moduleReport); | ||
} | ||
ModuleMetricControl.createScope(moduleReport, scopeControl, newScope); | ||
/** | ||
* Performs any calculations that depend on averaged data. This is where the maintainability index is calculated. | ||
* | ||
* @param {object} ev - escomplex plugin event data. | ||
*/ | ||
onModulePostAverage(ev) | ||
{ | ||
ModuleMetricPostAverage.calculate(ev.data.moduleReport, ev.data.settings); | ||
} | ||
/** | ||
* A new module report scope has been created. Update any associated metrics processing regarding the new scope. | ||
* | ||
* @param {object} ev - escomplex plugin event data. | ||
*/ | ||
onModuleScopeCreated(ev) | ||
{ | ||
ModuleMetricProcess.createScope(ev.data.moduleReport, ev.data.scopeControl, ev.data.newScope); | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
73945
16
1058
1