Skip to content

Commit

Permalink
Merge pull request #115 from codeclimate/abh-wf-skip-minified-eslint
Browse files Browse the repository at this point in the history
Heuristically filter out minified files
  • Loading branch information
Ashley Baldwin-Hunter committed Aug 11, 2016
2 parents eddac7d + 6b4307d commit 40884b0
Show file tree
Hide file tree
Showing 5 changed files with 387 additions and 2 deletions.
7 changes: 5 additions & 2 deletions bin/eslint.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ var glob = require("glob");
var options = { extensions: [".js"], ignore: true, reset: false, useEslintrc: true };
var cli; // instantiation delayed until after options are (potentially) modified
var debug = false;
var BatchSanitizer = require("../lib/batch_sanitizer");
var checks = require("../lib/checks");
var validateConfig = require("../lib/validate_config");
var computeFingerprint = require("../lib/compute_fingerprint");
Expand Down Expand Up @@ -181,17 +182,19 @@ function analyzeFiles() {
var batchNum = 0
, batchSize = 10
, batchFiles
, batchReport;
, batchReport
, sanitizedBatchFiles;

while(analysisFiles.length > 0) {
batchFiles = analysisFiles.splice(0, batchSize);
sanitizedBatchFiles = (new BatchSanitizer(batchFiles)).sanitizedFiles();

if (debug) {
process.stderr.write("Analyzing: " + batchFiles + "\n");
}

runWithTiming("analyze-batch-" + batchNum, function() {
batchReport = cli.executeOnFiles(batchFiles);
batchReport = cli.executeOnFiles(sanitizedBatchFiles);
});
runWithTiming("report-batch" + batchNum, function() {
batchReport.results.forEach(function(result) {
Expand Down
41 changes: 41 additions & 0 deletions lib/batch_sanitizer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
var fs = require("fs");

var MINIFIED_AVG_LINE_LENGTH_CUTOFF = 100;

function BatchSanitizer(files, stderr) {
this.files = files;
this.stderr = stderr || process.stderr;
}

BatchSanitizer.prototype.sanitizedFiles = function() {
var sanitizedFiles = [];

for(var i = 0; i < this.files.length; i++) {
if (this.isMinified(this.files[i])) {
this.stderr.write("WARN: Skipping " + this.files[i] + ": it appears to be minified\n");
} else {
sanitizedFiles.push(this.files[i]);
}
}

return sanitizedFiles;
};

BatchSanitizer.prototype.isMinified = function(path) {
var buf = fs.readFileSync(path)
, newline = "\n".charCodeAt(0)
, lineCount = 0
, charsSeen = 0;

for(var i = 0; i < buf.length; i++) {
if (buf[i] === newline) {
lineCount++;
} else {
charsSeen++;
}
}

return (charsSeen / lineCount) >= MINIFIED_AVG_LINE_LENGTH_CUTOFF;
};

module.exports = BatchSanitizer;
17 changes: 17 additions & 0 deletions test/batch_sanitizer_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
var BatchSanitizer = require("../lib/batch_sanitizer")
, expect = require("chai").expect;

describe("BatchSanitizer module", function() {
describe(".sanitizedFiles()", function() {
it("filters out files that appear minified", function() {
var stderr = { contents: "", write: function(str) { this.contents += str; } }
, sanitizer = new BatchSanitizer(
["./test/fixtures/minified_batch/minified.js", "./test/fixtures/minified_batch/unminified.js"],
stderr
);

expect(sanitizer.sanitizedFiles()).to.eql(["./test/fixtures/minified_batch/unminified.js"]);
expect(stderr.contents).to.match(/WARN: Skipping/);
});
});
});

0 comments on commit 40884b0

Please sign in to comment.