diff --git a/.jshintrc b/.jshintrc new file mode 100644 index 0000000..35ac972 --- /dev/null +++ b/.jshintrc @@ -0,0 +1,19 @@ +{ + "bitwise": true, + "camelcase": true, + "curly": true, + "eqeqeq": true, + "freeze": true, + "immed": true, + "indent": 4, + "latedef": "nofunc", + "newcap": true, + "noarg": true, + "noempty": true, + "nonbsp": true, + "nonew": true, + "undef": true, + "unused": true, + "maxdepth": 4, + "maxcomplexity": 6 +} diff --git a/README.md b/README.md index 0c8cb11..a476105 100644 --- a/README.md +++ b/README.md @@ -18,9 +18,6 @@ the copyright belongs to Spotify AB. * Make Travis validate the HTML and fail the build if the HTML is invalid. -* Make Travis run a Javascript code checker and fail the build if there -are warnings. - * Dump all input lines we failed to parse in red at the end so it's obvious if we need to add something. @@ -109,3 +106,6 @@ as the secondary sort key. This way similar stacks will end up closer to each other. * Sort thread names in each group alphabetically. + +* Make Travis run a Javascript code checker and fail the build if there +are warnings. diff --git a/analyze.js b/analyze.js index c72f7af..850b1bb 100644 --- a/analyze.js +++ b/analyze.js @@ -14,9 +14,12 @@ See the License for the specific language governing permissions and limitations under the License. */ +/* global document */ + var EMPTY_STACK = " \n"; -function analyze_textfield() { +// This method is called from HTML so we need to tell JSHint it's not unused +function analyzeTextfield() { // jshint ignore: line var text = document.getElementById("TEXTAREA").value; var analyzer = new Analyzer(text); @@ -27,14 +30,14 @@ function analyze_textfield() { // // Returns an object with two properties: // value = the first group of the extracted object -// shorter_string = the string with the full contents of the regex removed +// shorterString = the string with the full contents of the regex removed function _extract(regex, string) { var match = regex.exec(string); if (match === null) { - return {value: undefined, shorter_string: string}; + return {value: undefined, shorterString: string}; } - return {value: match[1], shorter_string: string.replace(regex, "")}; + return {value: match[1], shorterString: string.replace(regex, "")}; } function setOutputText(unescaped) { @@ -92,39 +95,39 @@ function Thread(line) { var match; match = _extract(/\[([0-9a-fx,]+)\]$/, line); this.dontKnow = match.value; - line = match.shorter_string; + line = match.shorterString; match = _extract(/ nid=([0-9a-fx,]+)/, line); this.nid = match.value; - line = match.shorter_string; + line = match.shorterString; match = _extract(/ tid=([0-9a-fx,]+)/, line); this.tid = match.value; - line = match.shorter_string; + line = match.shorterString; match = _extract(/ prio=([0-9]+)/, line); this.prio = match.value; - line = match.shorter_string; + line = match.shorterString; match = _extract(/ os_prio=([0-9a-fx,]+)/, line); - this.os_prio = match.value; - line = match.shorter_string; + this.osPrio = match.value; + line = match.shorterString; match = _extract(/ (daemon)/, line); this.daemon = (match.value !== undefined); - line = match.shorter_string; + line = match.shorterString; match = _extract(/ #([0-9]+)/, line); this.number = match.value; - line = match.shorter_string; + line = match.shorterString; match = _extract(/ group="(.*)"/, line); this.group = match.value; - line = match.shorter_string; + line = match.shorterString; match = _extract(/^"(.*)" /, line); this.name = match.value; - line = match.shorter_string; + line = match.shorterString; this.state = line.trim(); @@ -135,6 +138,27 @@ function Thread(line) { this._frames = []; } +function toStackWithHeadersString(stack, threads) { + var string = ''; + if (threads.length > 4) { + string += "" + threads.length + " threads with this stack:\n"; + } + + // Print thread headers for this stack in alphabetic order + var headers = []; + for (var k = 0; k < threads.length; k++) { + headers.push(threads[k].toHeaderString()); + } + headers.sort(); + for (var l = 0; l < headers.length; l++) { + string += headers[l] + '\n'; + } + + string += stack; + + return string; +} + // Create an analyzer object function Analyzer(text) { this._analyze = function(text) { @@ -142,7 +166,7 @@ function Analyzer(text) { var currentThread = null; for (var i = 0; i < lines.length; i++) { var line = lines[i]; - while (line.charAt(0) == '"' && line.indexOf('prio=') == -1) { + while (line.charAt(0) === '"' && line.indexOf('prio=') === -1) { // Multi line thread name i++; if (i >= lines.length) { @@ -181,22 +205,22 @@ function Analyzer(text) { stacks.push(stack); } stacks.sort(function(a, b) { - if (a == b) { + if (a === b) { return 0; } - var a_score = stacksToThreads[a].length; + var scoreA = stacksToThreads[a].length; if (a === EMPTY_STACK) { - a_score = -123456; + scoreA = -123456; } - var b_score = stacksToThreads[b].length; + var scoreB = stacksToThreads[b].length; if (b === EMPTY_STACK) { - b_score = -123456; + scoreB = -123456; } - if (b_score != a_score) { - return b_score - a_score; + if (scoreB !== scoreA) { + return scoreB - scoreA; } // Use stack contents as secondary sort key. This is @@ -216,27 +240,8 @@ function Analyzer(text) { asString += "" + this.threads.length + " threads found:\n"; for (var j = 0; j < stacks.length; j++) { var currentStack = stacks[j]; - - asString += '\n'; - var threads = stacksToThreads[currentStack]; - if (threads.length > 4) { - asString += "" + threads.length + " threads with this stack:\n"; - } - - // Print thread headers for this stack in alphabetic order - var headers = []; - for (var k = 0; k < threads.length; k++) { - var currentThread = threads[k]; - - headers.push(threads[k].toHeaderString()); - } - headers.sort(); - for (var l = 0; l < headers.length; l++) { - asString += headers[l] + '\n'; - } - - asString += currentStack; + asString += '\n' + toStackWithHeadersString(currentStack, threads); } return asString; diff --git a/gulpfile.js b/gulpfile.js index 3733fa9..5afab2b 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -14,9 +14,19 @@ See the License for the specific language governing permissions and limitations under the License. */ +/* global require */ + var gulp = require('gulp'); var qunit = require('gulp-qunit'); +var jshint = require('gulp-jshint'); -gulp.task('default', function() { +gulp.task('test', function() { return gulp.src('./test.html').pipe(qunit()); }); + +gulp.task('lint', function() { + return gulp.src('*.js') + .pipe(jshint()) + .pipe(jshint.reporter('default')) + .pipe(jshint.reporter('fail')); +}); diff --git a/index.html b/index.html index 1577499..6074b0c 100644 --- a/index.html +++ b/index.html @@ -29,7 +29,7 @@

Java Thread Dump Analyzer


- +