Skip to content

Commit

Permalink
Add option to disable file sanitization
Browse files Browse the repository at this point in the history
  • Loading branch information
pointlessone committed Aug 3, 2017
1 parent 723c000 commit 06da8ba
Show file tree
Hide file tree
Showing 5 changed files with 211 additions and 50 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,20 @@ eslint:
- .html
```

#### `sanitize_batch`

By default, this engine will skip files that appear to be minified (average line
length over 100). This feature can be disabled to include all files for
analysis.

```yaml
eslint:
enabled: true
config:
sanitize_batch: false
```


### Need help?

For help with ESLint, [check out their documentation][eslint-docs].
Expand Down
100 changes: 100 additions & 0 deletions integration/eslint_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ const sinon = require("sinon");
const expect = require("chai").expect;

const ESLint = require('../lib/eslint');
const fs = require("fs");
const path = require("path")
const temp = require('temp');

describe("eslint integration", function() {
let consoleMock = {};
Expand Down Expand Up @@ -50,6 +53,103 @@ describe("eslint integration", function() {
});
});

describe("sanitization", function() {
function withMinifiedSource(config, cb, done) {
temp.mkdir("code", function(err, directory) {
if (err) { throw err; }

process.chdir(directory);

const eslintConfigPath = path.join(directory, ".eslintrc.json");
fs.writeFile(eslintConfigPath, "{}", function(err) {
if (err) { throw err; }

const sourcePath = path.join(directory, "index.js");
fs.writeFile(sourcePath,
[...Array(13).keys()].map(()=>{ return "void(0);"; }).join(""), // a long string of voids
function(err) {
if (err) { throw err; }

const configPath = path.join(directory, "config.json");
fs.writeFile(
configPath,
JSON.stringify({
"enabled": true,
"config": config,
"include_paths": [sourcePath]
}),
function(err) {
if (err) { throw err; }

cb(directory);

done();
}
);
}
);
});
});
}

const BatchSanitizer = require("../lib/batch_sanitizer");
const CLIEngine = require('../lib/eslint-patch')().CLIEngine;

beforeEach(() => {
sinon.spy(BatchSanitizer.prototype, "sanitizedFiles");
sinon.spy(CLIEngine.prototype, "executeOnFiles");
});
afterEach(() => {
BatchSanitizer.prototype.sanitizedFiles.restore();
CLIEngine.prototype.executeOnFiles.restore();
});

it("is performed by default", function(done) {
this.timeout(5000);

withMinifiedSource(
{},
function(dir) {
ESLint.run(consoleMock, { dir: dir, configPath: `${dir}/config.json`});

expect(BatchSanitizer.prototype.sanitizedFiles.callCount).to.eql(1);
expect(CLIEngine.prototype.executeOnFiles.firstCall.args).to.eql([[]]);
},
done
);
});

it("is performed by when explicitly specified", function(done) {
this.timeout(5000);

withMinifiedSource(
{ sanitize_batch: true },
function(dir) {
ESLint.run(consoleMock, { dir: dir, configPath: `${dir}/config.json`});

expect(BatchSanitizer.prototype.sanitizedFiles.callCount).to.eql(1);
expect(CLIEngine.prototype.executeOnFiles.firstCall.args).to.eql([[]]);
},
done
);
});

it("is can be disabled", function(done) {
this.timeout(5000);

withMinifiedSource(
{ sanitize_batch: false },
function(dir) {
ESLint.run(consoleMock, { dir: dir, configPath: `${dir}/config.json`});

expect(BatchSanitizer.prototype.sanitizedFiles.callCount).to.eql(0);
expect(CLIEngine.prototype.executeOnFiles.firstCall.args).to.eql([[`${dir}/index.js`]]);
},
done
);
});
});

describe("output", function() {
it("is not messed up", function() {
this.timeout(5000);
Expand Down
72 changes: 42 additions & 30 deletions lib/engine_config.js
Original file line number Diff line number Diff line change
@@ -1,48 +1,60 @@
var fs = require("fs");

function EngineConfig(path) {
this.engineJSON = JSON.parse(fs.readFileSync(path));
};

EngineConfig.prototype.includePaths = function() {
return this.engineJSON.include_paths;
};

// cc-yaml currently ends up passing bools as stringy equivalents
function _coerceBool(val) {
if (typeof(val) === "string") {
return val === "true";
} else {
return !!val;
}
};
}

function UserEngineConfig(json) {
this.json = json;
};
class UserEngineConfig {
constructor(json) {
this.json = json;
}

EngineConfig.prototype.userConfig = function() {
return new UserEngineConfig(this.engineJSON.config || {});
};
get configPath() {
return this.json.config;
}

UserEngineConfig.prototype.configPath = function() {
return this.json.config;
};
get extensions() {
return this.json.extensions;
}

UserEngineConfig.prototype.extensions = function() {
return this.json.extensions;
};
get debug() {
return _coerceBool(this.json.debug);
}

UserEngineConfig.prototype.debug = function() {
return _coerceBool(this.json.debug);
};
get ignorePath() {
return this.json.ignore_path;
}

UserEngineConfig.prototype.ignorePath = function() {
return this.json.ignore_path;
};
get ignoreWarnings() {
return _coerceBool(this.json.ignore_warnings);
}

UserEngineConfig.prototype.ignoreWarnings = function() {
return _coerceBool(this.json.ignore_warnings);
};
get sanitizeBatch() {
if (this.json.hasOwnProperty("sanitize_batch")) {
return _coerceBool(this.json.sanitize_batch);
} else {
return true;
}
}
}

class EngineConfig {
constructor(path) {
this.engineJSON = JSON.parse(fs.readFileSync(path));
}

get includePaths() {
return this.engineJSON.include_paths;
}

get userConfig() {
return new UserEngineConfig(this.engineJSON.config || {});
}
}

module.exports = EngineConfig;
33 changes: 18 additions & 15 deletions lib/eslint.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ function run(console, runOptions) {
var cli; // instantiation delayed until after options are (potentially) modified
var debug = false;
var ignoreWarnings = false;
var sanitizeBatch = true;
var ESLINT_WARNING_SEVERITY = 1;

// a wrapper for emitting perf timing
Expand Down Expand Up @@ -151,21 +152,22 @@ function run(console, runOptions) {
}

function overrideOptions(userConfig) {
if (userConfig.configPath()) {
options.configFile = codeDir + "/" + userConfig.configPath();
if (userConfig.configPath) {
options.configFile = codeDir + "/" + userConfig.configPath;
options.useEslintrc = false;
}

if (userConfig.extensions()) {
options.extensions = userConfig.extensions();
if (userConfig.extensions) {
options.extensions = userConfig.extensions;
}

if (userConfig.ignorePath()) {
options.ignorePath = userConfig.ignorePath();
if (userConfig.ignorePath) {
options.ignorePath = userConfig.ignorePath;
}

ignoreWarnings = userConfig.ignoreWarnings();
debug = userConfig.debug();
ignoreWarnings = userConfig.ignoreWarnings;
debug = userConfig.debug;
sanitizeBatch = userConfig.sanitizeBatch;
}

// No explicit includes, let's try with everything
Expand All @@ -175,11 +177,11 @@ function run(console, runOptions) {
if (fs.existsSync(configPath)) {
var engineConfig = new EngineConfig(configPath);

if (engineConfig.includePaths()) {
buildFileList = inclusionBasedFileListBuilder(engineConfig.includePaths());
if (engineConfig.includePaths) {
buildFileList = inclusionBasedFileListBuilder(engineConfig.includePaths);
}

overrideOptions(engineConfig.userConfig());
overrideOptions(engineConfig.userConfig);
}

cli = new CLIEngine(options);
Expand All @@ -193,19 +195,20 @@ function run(console, runOptions) {
var batchNum = 0
, batchSize = 10
, batchFiles
, batchReport
, sanitizedBatchFiles;
, batchReport;

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

if (debug) {
console.error("Analyzing: " + batchFiles);
}

runWithTiming("analyze-batch-" + batchNum, function() {
batchReport = cli.executeOnFiles(sanitizedBatchFiles);
batchReport = cli.executeOnFiles(batchFiles);
});
runWithTiming("report-batch" + batchNum, function() {
batchReport.results.forEach(function(result) {
Expand Down
42 changes: 37 additions & 5 deletions test/engine_config_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,31 +27,63 @@ describe("EngineConfig", function() {
describe("ignoreWarnings", function() {
it("is false by default", function(done) {
withConfig({}, done, function(engine_config) {
expect(engine_config.userConfig().ignoreWarnings()).to.eq(false);
expect(engine_config.userConfig.ignoreWarnings).to.eq(false);
});
});

it("is false when specified", function(done) {
withConfig({ config: { ignore_warnings: false } }, done, function(engine_config) {
expect(engine_config.userConfig().ignoreWarnings()).to.eq(false);
expect(engine_config.userConfig.ignoreWarnings).to.eq(false);
});
});

it("is false when specified as string", function(done) {
withConfig({ config: { ignore_warnings: "false" } }, done, function(engine_config) {
expect(engine_config.userConfig().ignoreWarnings()).to.eq(false);
expect(engine_config.userConfig.ignoreWarnings).to.eq(false);
});
});

it("is true when specified", function(done) {
withConfig({ config: { ignore_warnings: true } }, done, function(engine_config) {
expect(engine_config.userConfig().ignoreWarnings()).to.eq(true);
expect(engine_config.userConfig.ignoreWarnings).to.eq(true);
});
});

it("is true when specified as string", function(done) {
withConfig({ config: { ignore_warnings: "true" } }, done, function(engine_config) {
expect(engine_config.userConfig().ignoreWarnings()).to.eq(true);
expect(engine_config.userConfig.ignoreWarnings).to.eq(true);
});
});
});

describe("sanitizeBatch", function() {
it("is true by default", function(done) {
withConfig({}, done, function(engine_config) {
expect(engine_config.userConfig.sanitizeBatch).to.eq(true);
});
});

it("is false when specified", function(done) {
withConfig({ config: { sanitize_batch: false } }, done, function(engine_config) {
expect(engine_config.userConfig.sanitizeBatch).to.eq(false);
});
});

it("is false when specified as string", function(done) {
withConfig({ config: { sanitize_batch: "false" } }, done, function(engine_config) {
expect(engine_config.userConfig.sanitizeBatch).to.eq(false);
});
});

it("is true when specified", function(done) {
withConfig({ config: { sanitize_batch: true } }, done, function(engine_config) {
expect(engine_config.userConfig.sanitizeBatch).to.eq(true);
});
});

it("is true when specified as string", function(done) {
withConfig({ config: { sanitize_batch: "true" } }, done, function(engine_config) {
expect(engine_config.userConfig.sanitizeBatch).to.eq(true);
});
});
});
Expand Down

0 comments on commit 06da8ba

Please sign in to comment.