Skip to content

Commit

Permalink
Merge branch 'typhonjs-node-escomplex-master'
Browse files Browse the repository at this point in the history
* typhonjs-node-escomplex-master:
  locked typhonjs-escomplex to 0.0.9; I think we're good to go!
  locked typhonjs-escomplex to 0.0.7 and updated src / tests
  locked typhonjs-escomplex to 0.0.6 as 0.0.7 will have a small change.
  Added ES6 tests and automatic detection of ES6 source for jshint.
  Update to support typhonjs-escomplex
  • Loading branch information
Jarrod Overson committed Aug 9, 2016
2 parents 80bb4bd + 7d7868d commit a944bb9
Show file tree
Hide file tree
Showing 18 changed files with 244 additions and 186 deletions.
6 changes: 3 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
tmp
node_module
node_modules
.idea
.DS_Store
npm-debug.log
node_modules
*.sass-cache
tmp/
reports/
2 changes: 1 addition & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ module.exports = function(grunt) {
'-q',
'-dtmp',
'-ttest report',
'test/fixtures/a.js','test/fixtures/b.js','test/fixtures/empty.js'
'test/fixtures/a.js','test/fixtures/b.js','test/fixtures/c-es6.js','test/fixtures/d-es6.js','test/fixtures/empty.js'
]
},
function(err, result, code){
Expand Down
10 changes: 5 additions & 5 deletions lib/assets/scripts/plato-file.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,19 @@ $(function(){

var popovers = cm.operation(function(){
var queuedPopovers = [];
__report.complexity.functions.forEach(function(fn,i){
__report.complexity.methods.forEach(function(fn,i){
byComplexity.push({
label : fn.name,
value : fn.complexity.cyclomatic
value : fn.cyclomatic
});
bySloc.push({
label : fn.name,
value : fn.complexity.sloc.physical,
value : fn.sloc.physical,
formatter: function (x) { return x + " lines"; }
});

var name = fn.name === '<anonymous>' ? 'function\\s*\\([^)]*\\)' : fn.name;
var line = fn.line - 1;
var line = fn.lineStart - 1;
var className = 'plato-mark-fn-' + i;
var gutter = {
gutterId : 'plato-gutter-complexity',
Expand All @@ -71,7 +71,7 @@ $(function(){
var origScroll = [window.pageXOffset,window.pageYOffset];
window.location.hash = '#plato-mark-fn-' + i;
window.scrollTo(origScroll[0],origScroll[1]);
var line = __report.complexity.functions[i].line;
var line = __report.complexity.methods[i].lineStart;
var coords = cm.charCoords({line : line, ch : 0});
$('body,html').animate({scrollTop : coords.top -50},250);
};
Expand Down
8 changes: 4 additions & 4 deletions lib/assets/scripts/plato-overview.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,16 +100,16 @@ $(function(){
reports.forEach(function(report){

// @todo shouldn't need this, 'auto [num]' doesn't seem to work : https://github.com/oesmith/morris.js/issues/201
sloc.ymax = Math.max(sloc.ymax, report.complexity.aggregate.complexity.sloc.physical);
bugs.ymax = Math.max(bugs.ymax, report.complexity.aggregate.complexity.halstead.bugs.toFixed(2));
sloc.ymax = Math.max(sloc.ymax, report.complexity.methodAggregate.sloc.physical);
bugs.ymax = Math.max(bugs.ymax, report.complexity.methodAggregate.halstead.bugs.toFixed(2));


sloc.data.push({
value : report.complexity.aggregate.complexity.sloc.physical,
value : report.complexity.methodAggregate.sloc.physical,
label : report.info.fileShort
});
bugs.data.push({
value : report.complexity.aggregate.complexity.halstead.bugs.toFixed(2),
value : report.complexity.methodAggregate.halstead.bugs.toFixed(2),
label : report.info.fileShort
});
maintainability.data.push({
Expand Down
12 changes: 6 additions & 6 deletions lib/models/FileHistory.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ FileHistory.prototype.addReport = function(report, date) {
date = date || report.date || new Date().toUTCString();
this.push({
date : date,
sloc : report.complexity.aggregate.complexity.sloc.physical,
lloc : report.complexity.aggregate.complexity.sloc.logical,
functions : report.complexity.functions.length,
deliveredBugs : report.complexity.aggregate.complexity.halstead.bugs,
sloc : report.complexity.methodAggregate.sloc.physical,
lloc : report.complexity.methodAggregate.sloc.logical,
functions : report.complexity.methods.length,
deliveredBugs : report.complexity.methodAggregate.halstead.bugs,
difficulty: report.complexity.methodAggregate.halstead.difficulty,
maintainability: report.complexity.maintainability,
lintErrors : (report.jshint && report.jshint.messages.length) || [],
difficulty: report.complexity.aggregate.complexity.halstead.difficulty
lintErrors : (report.jshint && report.jshint.messages.length) || []
});
return this;
};
Expand Down
11 changes: 6 additions & 5 deletions lib/plato.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ exports.inspect = function(files, outputDir, options, done) {

var flags = {
complexity : {
commonjs : true,
logicalor : true,
switchcase : true,
forin : false,
Expand Down Expand Up @@ -184,20 +185,20 @@ exports.getOverviewReport = function (reports) {
total : {
jshint: 0,
sloc : 0,
maintainability : 0,
maintainability : 0
},
average : {
sloc : 0,
maintainability : 0,
maintainability : 0
}
};

reports.forEach(function(report) {
// clone objects so we don't have to worry about side effects
summary.total.sloc += report.complexity.aggregate.complexity.sloc.physical;
summary.total.sloc += report.complexity.methodAggregate.sloc.physical;
summary.total.maintainability += report.complexity.maintainability;

var aggregate = _.cloneDeep(report.complexity.aggregate);
var methodAggregate = _.cloneDeep(report.complexity.methodAggregate);
var reportItem = {};
reportItem.info = report.info;
if (report.jshint) {
Expand All @@ -208,7 +209,7 @@ exports.getOverviewReport = function (reports) {
}
if (report.complexity) {
reportItem.complexity = {
aggregate : aggregate,
methodAggregate : methodAggregate,
module : report.complexity.module,
module_safe : report.complexity.module_safe,
maintainability : _.cloneDeep(report.complexity.maintainability)
Expand Down
27 changes: 10 additions & 17 deletions lib/reporters/complexity/index.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,21 @@
'use strict';

var escomplex = require('escomplex'),
var escomplex = require('typhonjs-escomplex'),
_ = require('lodash');

exports.process = function(source, options, reportInfo) {
var report = escomplex.analyse(source, options);

var report = escomplex.analyzeModule(source, options);
// Make the short filename easily accessible
report.module = reportInfo.fileShort;

// Munge the new `escomplex` format to match the older format of
// `complexity-report`
report.aggregate.complexity = {
cyclomatic : report.aggregate.cyclomatic,
sloc : report.aggregate.sloc,
halstead : report.aggregate.halstead
};

if (_.isArray(report.functions)) {
_.each(report.functions, function (func) {
func.complexity = {
cyclomatic : func.cyclomatic,
sloc : func.sloc,
halstead : func.halstead
};
// `typhonjs-escomplex` stores class methods inside `classes` entries. For now this is a hack to add class methods
// to the end of module methods. Plato needs to be updated to support classes.
if (_.isArray(report.methods) && _.isArray(report.classes)) {
_.each(report.classes, function (clazz) {
_.each(clazz.methods, function (method) {
report.methods.push(method);
});
});
}

Expand Down
21 changes: 19 additions & 2 deletions lib/reporters/jshint/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@
var JSHINT = require("jshint").JSHINT;
var jsHintCli = require("jshint/src/cli.js");

// Provides a regexp to test for ES6 / ES Modules. If the pass tests then esversion is set to 6 if not already specified.
var esmRegex = /(^\s*|[}\);\n]\s*)(import\s*(['"]|(\*\s+as\s+)?[^"'\(\)\n;]+\s*from\s*['"]|\{)|export\s+\*\s+from\s+["']|export\s* (\{|default|function|class|var|const|let|async\s+function))/;

exports.process = function (source, options/*, reportInfo */) {
if (options == null || Object.getOwnPropertyNames(options).length === 0) {
options = { options : {}, globals : {}};
var jsHintOptions = jsHintCli.getConfig(source);

delete jsHintOptions.dirname;
if (jsHintOptions != null && Object.getOwnPropertyNames(jsHintOptions).length > 0) {
if (jsHintOptions.globals) {
Expand All @@ -17,9 +21,22 @@ exports.process = function (source, options/*, reportInfo */) {
}
}

// Detect if source is an ES Module.
if (esmRegex.test(source)) {
// Make sure `options.options` is defined
if (typeof options.options !== 'object') {
options.options = {};
}

// If esversion is not already specified then set `esversion` to 6.
if (typeof options.options.esversion !== 'number') {
options.options.esversion = 6;
}
}

var results = lint(source, options.options, options.globals);
var report = generateReport(results);
return report;

return generateReport(results);
};

function generateReport(data) {
Expand Down
4 changes: 2 additions & 2 deletions lib/templates/display.html
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ <h2 class="reportTitle">Worst Maintainability Scores</h2>
<div class="reportBlock locList">
<h2 class="reportTitle">Largest Files</h2>
<ul class="list-unstyled">
<% _.each(_.take(_.sortBy(report.reports, function (report) { return -1 * report.complexity.aggregate.complexity.sloc.physical }), 5), function(report, i) { %>
<% _.each(_.take(_.sortBy(report.reports, function (report) { return -1 * report.complexity.methodAggregate.sloc.physical }), 5), function(report, i) { %>
<li>
<strong><%= report.complexity.aggregate.complexity.sloc.physical %> lines</strong>
<strong><%= report.complexity.methodAggregate.sloc.physical %> lines</strong>
<small><%= report.info.fileShort %></small>
</li>
<% }); %>
Expand Down
14 changes: 7 additions & 7 deletions lib/templates/file.html
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ <h2 class="header">Maintainability <a href="http://blogs.msdn.com/b/codeanalysis
</div>
<div class="col-md-6">
<h2 class="header">Lines of code <i class="icon icon-info-sign" rel="popover" data-placement="top" data-trigger="hover" data-content="Source Lines of Code / Logical Lines of Code" data-original-title="SLOC/LSLOC" data-container="body"></i></h2>
<p class="stat"><%= report.complexity.aggregate.complexity.sloc.physical %></p>
<p class="stat"><%= report.complexity.methodAggregate.sloc.physical %></p>
</div>
</div>
<div class="row historical">
Expand All @@ -62,11 +62,11 @@ <h2 class="header">Lines of code <i class="icon icon-info-sign" rel="popover" da
<div class="row">
<div class="col-md-6">
<h2 class="header">Difficulty <a href="http://en.wikipedia.org/wiki/Halstead_complexity_measures"><i class="icon icon-info-sign" rel="popover" data-placement="top" data-trigger="hover" data-content="The difficulty measure is related to the difficulty of the program to write or understand." data-original-title="Difficulty" data-container="body"></i></a></h2>
<p class="stat"><%= report.complexity.aggregate.complexity.halstead.difficulty.toFixed(2) %></p>
<p class="stat"><%= report.complexity.methodAggregate.halstead.difficulty.toFixed(2) %></p>
</div>
<div class="col-md-6">
<h2 class="header">Estimated Errors <a href="http://en.wikipedia.org/wiki/Halstead_complexity_measures"><i class="icon icon-info-sign" rel="popover" data-placement="top" data-trigger="hover" data-content="Halstead's delivered bugs is an estimate for the number of errors in the implementation." data-original-title="Delivered Bugs" data-container="body"></i></a></h2>
<p class="stat"><%= report.complexity.aggregate.complexity.halstead.bugs.toFixed(2) %></p>
<p class="stat"><%= report.complexity.methodAggregate.halstead.bugs.toFixed(2) %></p>
</div>
</div>
</div>
Expand Down Expand Up @@ -101,10 +101,10 @@ <h3 class="chart-header">By SLOC <i class="icon icon-info-sign" rel="popover" d

<script type="text/html" id="complexity-popover-template">
<div class="complexity-notice">
Complexity : {{ complexity.cyclomatic }} <br>
Length : {{ complexity.halstead.length }} <br>
Difficulty : {{ complexity.halstead.difficulty.toFixed(2) }} <br>
Est # bugs : {{ complexity.halstead.bugs.toFixed(2) }}<br>
Complexity : {{ cyclomatic }} <br>
Length : {{ halstead.length }} <br>
Difficulty : {{ halstead.difficulty.toFixed(2) }} <br>
Est # bugs : {{ halstead.bugs.toFixed(2) }}<br>
</div>
</script>

Expand Down
6 changes: 3 additions & 3 deletions lib/templates/overview.html
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,9 @@ <h1 class="col-md-12">Files</h1>
<span class="col-md-4 file"><a class="file-link" href="./<%= item.info.link %>"><%= item.info.fileShort %></a></span>
<span class="col-md-8 file-chart js-file-chart"
data-lint="<%= item.jshint && item.jshint.messages %>"
data-sloc="<%= item.complexity.aggregate.complexity.sloc.physical %>"
data-bugs="<%= item.complexity.aggregate.complexity.halstead.bugs.toFixed(2) %>"
data-complexity="<%= item.complexity.aggregate.complexity.cyclomatic%>"
data-sloc="<%= item.complexity.methodAggregate.sloc.physical %>"
data-bugs="<%= item.complexity.methodAggregate.halstead.bugs.toFixed(2) %>"
data-complexity="<%= item.complexity.methodAggregate.cyclomatic%>"
></span>
</div>
</li>
Expand Down
6 changes: 2 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,12 @@
"analyze"
],
"dependencies": {
"escomplex": "2.0.0-alpha",
"typhonjs-escomplex": "0.0.9",
"fs-extra": "~0.30.0",
"glob": "~7.0.5",
"jshint": "~2.9.2",
"lodash": "~4.13.1",
"posix-getopt": "~1.2.0",
"complexity-report": "2.0.0-alpha",
"eslint": "~3.0.1",
"esprima": "~2.7.2"
"eslint": "~3.0.1"
}
}
5 changes: 3 additions & 2 deletions test/casper-overview.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ casper.start('./tmp/index.html', function() {
this.test.assertSelectorHasText('.jumbotron h1', 'test report');
this.test.assertEvalEquals(function() {
return document.querySelectorAll('.file-list li').length;
}, 2, 'Has appropriate number list items in the file list');
}, 4, 'Has appropriate number list items in the file list');
this.test.assertEval(function() {
return document.querySelectorAll('.chart').length === document.querySelectorAll('.chart svg').length;
}, 'Every summary chart has an svg drawn');
Expand All @@ -22,7 +22,8 @@ casper.start('./tmp/index.html', function() {
});

casper.then(function () {
this.click('#chart_sloc svg');
var chart = this.getElementInfo('#chart_sloc svg');
this.mouse.click(chart.x + 1, chart.y + 1);
});

casper.waitForSelectorTextChange('.jumbotron h1', function() {
Expand Down
21 changes: 21 additions & 0 deletions test/fixtures/c-es6.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

const a = 1;

class C
{
static classMethodA(arg) {
return arg;
}

static classMethodB(arg) {
return arg;
}
}

function moduleMethod(arg) {
return arg;
}

const b = moduleMethod(0) + C.classMethodB(1) + C.classMethodA(2);

export default C;
24 changes: 24 additions & 0 deletions test/fixtures/d-es6.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

const a = 1;

class D
{
static classMethodA(arg) {
return arg;
}

static classMethodB(arg) {
return arg;
}
}

function moduleMethod(arg) {
return arg;
}

const b = moduleMethod(0) + D.classMethodB(1) + D.classMethodA(2);

let c = b + moduleMethod(0) + D.classMethodA(a) * D.classMethodB(1);
c = null;

export default D;
Loading

0 comments on commit a944bb9

Please sign in to comment.