-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #115 from codeclimate/abh-wf-skip-minified-eslint
Heuristically filter out minified files
- Loading branch information
Showing
5 changed files
with
387 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.