From bed67f2bf412db957e3109ccc3ac7c04e9ecb5f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Fr=C3=B6wis?= Date: Sun, 12 Mar 2017 12:46:22 +0100 Subject: [PATCH] Categories for static analysis modules --- src/app/staticanalysis/modules/categories.js | 4 +++ src/app/staticanalysis/modules/gasCosts.js | 2 ++ src/app/staticanalysis/modules/txOrigin.js | 2 ++ src/app/staticanalysis/staticAnalysisView.js | 38 ++++++++++++++++++-- src/app/utils.js | 13 ++++++- test/index.js | 1 + test/util-test.js | 26 ++++++++++++++ 7 files changed, 82 insertions(+), 4 deletions(-) create mode 100644 src/app/staticanalysis/modules/categories.js create mode 100644 test/util-test.js diff --git a/src/app/staticanalysis/modules/categories.js b/src/app/staticanalysis/modules/categories.js new file mode 100644 index 0000000000..380f54b534 --- /dev/null +++ b/src/app/staticanalysis/modules/categories.js @@ -0,0 +1,4 @@ +module.exports = { + SECURITY: {displayName: 'Security', id: 'SEC'}, + GAS: {displayName: 'Gas & Economy', id: 'GAS'} +} diff --git a/src/app/staticanalysis/modules/gasCosts.js b/src/app/staticanalysis/modules/gasCosts.js index 3247cc7fc0..974d0da6d3 100644 --- a/src/app/staticanalysis/modules/gasCosts.js +++ b/src/app/staticanalysis/modules/gasCosts.js @@ -1,5 +1,6 @@ var name = 'gas costs' var desc = 'Warn if the gas requiremets of functions are too high.' +var categories = require('./categories') function gasCosts () { } @@ -46,5 +47,6 @@ gasCosts.prototype.report = function (compilationResults) { module.exports = { name: name, description: desc, + category: categories.GAS, Module: gasCosts } diff --git a/src/app/staticanalysis/modules/txOrigin.js b/src/app/staticanalysis/modules/txOrigin.js index 45f3af8bf3..bd82d01abc 100644 --- a/src/app/staticanalysis/modules/txOrigin.js +++ b/src/app/staticanalysis/modules/txOrigin.js @@ -1,5 +1,6 @@ var name = 'tx origin' var desc = 'warn if tx.origin is used' +var categories = require('./categories') function txOrigin () { this.txOriginNodes = [] @@ -29,5 +30,6 @@ txOrigin.prototype.report = function () { module.exports = { name: name, description: desc, + category: categories.SECURITY, Module: txOrigin } diff --git a/src/app/staticanalysis/staticAnalysisView.js b/src/app/staticanalysis/staticAnalysisView.js index bd0c8e3621..b6fc961d09 100644 --- a/src/app/staticanalysis/staticAnalysisView.js +++ b/src/app/staticanalysis/staticAnalysisView.js @@ -2,6 +2,7 @@ var StaticAnalysisRunner = require('./staticAnalysisRunner.js') var yo = require('yo-yo') var $ = require('jquery') +var utils = require('../utils') function staticAnalysisView (appAPI, compilerEvent) { this.view = null @@ -26,7 +27,7 @@ staticAnalysisView.prototype.render = function () { var self = this var view = yo`
Static Analysis - +
${this.modulesView}
@@ -94,7 +95,38 @@ staticAnalysisView.prototype.run = function () { module.exports = staticAnalysisView function renderModules (modules) { - return modules.map(function (item, i) { - return yo`` + var groupedModules = utils.groupBy(preProcessModules(modules), 'categoryId') + return Object.keys(groupedModules).map((categoryId, i) => { + var category = groupedModules[categoryId] + var entriesDom = category.map((item, i) => { + return yo`` + }) + return yo`
+ + ${entriesDom} +
` }) } + +function preProcessModules (arr) { + return arr.map((item, i) => { + item['_index'] = i + item.categoryDisplayName = item.category.displayName + item.categoryId = item.category.id + return item + }) +} + diff --git a/src/app/utils.js b/src/app/utils.js index dce59963d2..7b12714603 100644 --- a/src/app/utils.js +++ b/src/app/utils.js @@ -4,6 +4,17 @@ function errortype (message) { return message.match(/^(.*:[0-9]*:[0-9]* )?Warning: /) ? 'warning' : 'error' } +function groupBy (arr, key) { + return arr.reduce((sum, item) => { + const groupByVal = item[key] + var groupedItems = sum[groupByVal] || [] + groupedItems.push(item) + sum[groupByVal] = groupedItems + return sum + }, {}) +} + module.exports = { - errortype: errortype + errortype: errortype, + groupBy: groupBy } diff --git a/test/index.js b/test/index.js index 84d8a5c68f..e89e2caba9 100644 --- a/test/index.js +++ b/test/index.js @@ -3,3 +3,4 @@ require('./compiler-test') require('./gist-handler-test') require('./query-params-test') +require('./util-test') diff --git a/test/util-test.js b/test/util-test.js new file mode 100644 index 0000000000..591a8566c8 --- /dev/null +++ b/test/util-test.js @@ -0,0 +1,26 @@ +var test = require('tape') + +var utils = require('../babelify-src/app/utils') + +test('util.groupBy on valid input', function (t) { + t.plan(1) + + var result = utils.groupBy([ + {category: 'GAS', name: 'a'}, + {category: 'SEC', name: 'b'}, + {category: 'GAS', name: 'c'} + + ], 'category') + + var expectedResult = { + 'GAS': [ + {category: 'GAS', name: 'a'}, + {category: 'GAS', name: 'c'} + ], + 'SEC': [ + {category: 'SEC', name: 'b'} + ] + } + + t.deepEqual(result, expectedResult) +})