Skip to content

Commit 06da8ba

Browse files
committed
Add option to disable file sanitization
1 parent 723c000 commit 06da8ba

File tree

5 files changed

+211
-50
lines changed

5 files changed

+211
-50
lines changed

README.md

+14
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,20 @@ eslint:
5656
- .html
5757
```
5858

59+
#### `sanitize_batch`
60+
61+
By default, this engine will skip files that appear to be minified (average line
62+
length over 100). This feature can be disabled to include all files for
63+
analysis.
64+
65+
```yaml
66+
eslint:
67+
enabled: true
68+
config:
69+
sanitize_batch: false
70+
```
71+
72+
5973
### Need help?
6074

6175
For help with ESLint, [check out their documentation][eslint-docs].

integration/eslint_test.js

+100
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ const sinon = require("sinon");
22
const expect = require("chai").expect;
33

44
const ESLint = require('../lib/eslint');
5+
const fs = require("fs");
6+
const path = require("path")
7+
const temp = require('temp');
58

69
describe("eslint integration", function() {
710
let consoleMock = {};
@@ -50,6 +53,103 @@ describe("eslint integration", function() {
5053
});
5154
});
5255

56+
describe("sanitization", function() {
57+
function withMinifiedSource(config, cb, done) {
58+
temp.mkdir("code", function(err, directory) {
59+
if (err) { throw err; }
60+
61+
process.chdir(directory);
62+
63+
const eslintConfigPath = path.join(directory, ".eslintrc.json");
64+
fs.writeFile(eslintConfigPath, "{}", function(err) {
65+
if (err) { throw err; }
66+
67+
const sourcePath = path.join(directory, "index.js");
68+
fs.writeFile(sourcePath,
69+
[...Array(13).keys()].map(()=>{ return "void(0);"; }).join(""), // a long string of voids
70+
function(err) {
71+
if (err) { throw err; }
72+
73+
const configPath = path.join(directory, "config.json");
74+
fs.writeFile(
75+
configPath,
76+
JSON.stringify({
77+
"enabled": true,
78+
"config": config,
79+
"include_paths": [sourcePath]
80+
}),
81+
function(err) {
82+
if (err) { throw err; }
83+
84+
cb(directory);
85+
86+
done();
87+
}
88+
);
89+
}
90+
);
91+
});
92+
});
93+
}
94+
95+
const BatchSanitizer = require("../lib/batch_sanitizer");
96+
const CLIEngine = require('../lib/eslint-patch')().CLIEngine;
97+
98+
beforeEach(() => {
99+
sinon.spy(BatchSanitizer.prototype, "sanitizedFiles");
100+
sinon.spy(CLIEngine.prototype, "executeOnFiles");
101+
});
102+
afterEach(() => {
103+
BatchSanitizer.prototype.sanitizedFiles.restore();
104+
CLIEngine.prototype.executeOnFiles.restore();
105+
});
106+
107+
it("is performed by default", function(done) {
108+
this.timeout(5000);
109+
110+
withMinifiedSource(
111+
{},
112+
function(dir) {
113+
ESLint.run(consoleMock, { dir: dir, configPath: `${dir}/config.json`});
114+
115+
expect(BatchSanitizer.prototype.sanitizedFiles.callCount).to.eql(1);
116+
expect(CLIEngine.prototype.executeOnFiles.firstCall.args).to.eql([[]]);
117+
},
118+
done
119+
);
120+
});
121+
122+
it("is performed by when explicitly specified", function(done) {
123+
this.timeout(5000);
124+
125+
withMinifiedSource(
126+
{ sanitize_batch: true },
127+
function(dir) {
128+
ESLint.run(consoleMock, { dir: dir, configPath: `${dir}/config.json`});
129+
130+
expect(BatchSanitizer.prototype.sanitizedFiles.callCount).to.eql(1);
131+
expect(CLIEngine.prototype.executeOnFiles.firstCall.args).to.eql([[]]);
132+
},
133+
done
134+
);
135+
});
136+
137+
it("is can be disabled", function(done) {
138+
this.timeout(5000);
139+
140+
withMinifiedSource(
141+
{ sanitize_batch: false },
142+
function(dir) {
143+
ESLint.run(consoleMock, { dir: dir, configPath: `${dir}/config.json`});
144+
145+
expect(BatchSanitizer.prototype.sanitizedFiles.callCount).to.eql(0);
146+
expect(CLIEngine.prototype.executeOnFiles.firstCall.args).to.eql([[`${dir}/index.js`]]);
147+
},
148+
done
149+
);
150+
});
151+
});
152+
53153
describe("output", function() {
54154
it("is not messed up", function() {
55155
this.timeout(5000);

lib/engine_config.js

+42-30
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,60 @@
11
var fs = require("fs");
22

3-
function EngineConfig(path) {
4-
this.engineJSON = JSON.parse(fs.readFileSync(path));
5-
};
6-
7-
EngineConfig.prototype.includePaths = function() {
8-
return this.engineJSON.include_paths;
9-
};
10-
113
// cc-yaml currently ends up passing bools as stringy equivalents
124
function _coerceBool(val) {
135
if (typeof(val) === "string") {
146
return val === "true";
157
} else {
168
return !!val;
179
}
18-
};
10+
}
1911

20-
function UserEngineConfig(json) {
21-
this.json = json;
22-
};
12+
class UserEngineConfig {
13+
constructor(json) {
14+
this.json = json;
15+
}
2316

24-
EngineConfig.prototype.userConfig = function() {
25-
return new UserEngineConfig(this.engineJSON.config || {});
26-
};
17+
get configPath() {
18+
return this.json.config;
19+
}
2720

28-
UserEngineConfig.prototype.configPath = function() {
29-
return this.json.config;
30-
};
21+
get extensions() {
22+
return this.json.extensions;
23+
}
3124

32-
UserEngineConfig.prototype.extensions = function() {
33-
return this.json.extensions;
34-
};
25+
get debug() {
26+
return _coerceBool(this.json.debug);
27+
}
3528

36-
UserEngineConfig.prototype.debug = function() {
37-
return _coerceBool(this.json.debug);
38-
};
29+
get ignorePath() {
30+
return this.json.ignore_path;
31+
}
3932

40-
UserEngineConfig.prototype.ignorePath = function() {
41-
return this.json.ignore_path;
42-
};
33+
get ignoreWarnings() {
34+
return _coerceBool(this.json.ignore_warnings);
35+
}
4336

44-
UserEngineConfig.prototype.ignoreWarnings = function() {
45-
return _coerceBool(this.json.ignore_warnings);
46-
};
37+
get sanitizeBatch() {
38+
if (this.json.hasOwnProperty("sanitize_batch")) {
39+
return _coerceBool(this.json.sanitize_batch);
40+
} else {
41+
return true;
42+
}
43+
}
44+
}
45+
46+
class EngineConfig {
47+
constructor(path) {
48+
this.engineJSON = JSON.parse(fs.readFileSync(path));
49+
}
50+
51+
get includePaths() {
52+
return this.engineJSON.include_paths;
53+
}
54+
55+
get userConfig() {
56+
return new UserEngineConfig(this.engineJSON.config || {});
57+
}
58+
}
4759

4860
module.exports = EngineConfig;

lib/eslint.js

+18-15
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ function run(console, runOptions) {
2727
var cli; // instantiation delayed until after options are (potentially) modified
2828
var debug = false;
2929
var ignoreWarnings = false;
30+
var sanitizeBatch = true;
3031
var ESLINT_WARNING_SEVERITY = 1;
3132

3233
// a wrapper for emitting perf timing
@@ -151,21 +152,22 @@ function run(console, runOptions) {
151152
}
152153

153154
function overrideOptions(userConfig) {
154-
if (userConfig.configPath()) {
155-
options.configFile = codeDir + "/" + userConfig.configPath();
155+
if (userConfig.configPath) {
156+
options.configFile = codeDir + "/" + userConfig.configPath;
156157
options.useEslintrc = false;
157158
}
158159

159-
if (userConfig.extensions()) {
160-
options.extensions = userConfig.extensions();
160+
if (userConfig.extensions) {
161+
options.extensions = userConfig.extensions;
161162
}
162163

163-
if (userConfig.ignorePath()) {
164-
options.ignorePath = userConfig.ignorePath();
164+
if (userConfig.ignorePath) {
165+
options.ignorePath = userConfig.ignorePath;
165166
}
166167

167-
ignoreWarnings = userConfig.ignoreWarnings();
168-
debug = userConfig.debug();
168+
ignoreWarnings = userConfig.ignoreWarnings;
169+
debug = userConfig.debug;
170+
sanitizeBatch = userConfig.sanitizeBatch;
169171
}
170172

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

178-
if (engineConfig.includePaths()) {
179-
buildFileList = inclusionBasedFileListBuilder(engineConfig.includePaths());
180+
if (engineConfig.includePaths) {
181+
buildFileList = inclusionBasedFileListBuilder(engineConfig.includePaths);
180182
}
181183

182-
overrideOptions(engineConfig.userConfig());
184+
overrideOptions(engineConfig.userConfig);
183185
}
184186

185187
cli = new CLIEngine(options);
@@ -193,19 +195,20 @@ function run(console, runOptions) {
193195
var batchNum = 0
194196
, batchSize = 10
195197
, batchFiles
196-
, batchReport
197-
, sanitizedBatchFiles;
198+
, batchReport;
198199

199200
while(analysisFiles.length > 0) {
200201
batchFiles = analysisFiles.splice(0, batchSize);
201-
sanitizedBatchFiles = (new BatchSanitizer(batchFiles)).sanitizedFiles();
202+
if (sanitizeBatch) {
203+
batchFiles = (new BatchSanitizer(batchFiles)).sanitizedFiles();
204+
}
202205

203206
if (debug) {
204207
console.error("Analyzing: " + batchFiles);
205208
}
206209

207210
runWithTiming("analyze-batch-" + batchNum, function() {
208-
batchReport = cli.executeOnFiles(sanitizedBatchFiles);
211+
batchReport = cli.executeOnFiles(batchFiles);
209212
});
210213
runWithTiming("report-batch" + batchNum, function() {
211214
batchReport.results.forEach(function(result) {

test/engine_config_test.js

+37-5
Original file line numberDiff line numberDiff line change
@@ -27,31 +27,63 @@ describe("EngineConfig", function() {
2727
describe("ignoreWarnings", function() {
2828
it("is false by default", function(done) {
2929
withConfig({}, done, function(engine_config) {
30-
expect(engine_config.userConfig().ignoreWarnings()).to.eq(false);
30+
expect(engine_config.userConfig.ignoreWarnings).to.eq(false);
3131
});
3232
});
3333

3434
it("is false when specified", function(done) {
3535
withConfig({ config: { ignore_warnings: false } }, done, function(engine_config) {
36-
expect(engine_config.userConfig().ignoreWarnings()).to.eq(false);
36+
expect(engine_config.userConfig.ignoreWarnings).to.eq(false);
3737
});
3838
});
3939

4040
it("is false when specified as string", function(done) {
4141
withConfig({ config: { ignore_warnings: "false" } }, done, function(engine_config) {
42-
expect(engine_config.userConfig().ignoreWarnings()).to.eq(false);
42+
expect(engine_config.userConfig.ignoreWarnings).to.eq(false);
4343
});
4444
});
4545

4646
it("is true when specified", function(done) {
4747
withConfig({ config: { ignore_warnings: true } }, done, function(engine_config) {
48-
expect(engine_config.userConfig().ignoreWarnings()).to.eq(true);
48+
expect(engine_config.userConfig.ignoreWarnings).to.eq(true);
4949
});
5050
});
5151

5252
it("is true when specified as string", function(done) {
5353
withConfig({ config: { ignore_warnings: "true" } }, done, function(engine_config) {
54-
expect(engine_config.userConfig().ignoreWarnings()).to.eq(true);
54+
expect(engine_config.userConfig.ignoreWarnings).to.eq(true);
55+
});
56+
});
57+
});
58+
59+
describe("sanitizeBatch", function() {
60+
it("is true by default", function(done) {
61+
withConfig({}, done, function(engine_config) {
62+
expect(engine_config.userConfig.sanitizeBatch).to.eq(true);
63+
});
64+
});
65+
66+
it("is false when specified", function(done) {
67+
withConfig({ config: { sanitize_batch: false } }, done, function(engine_config) {
68+
expect(engine_config.userConfig.sanitizeBatch).to.eq(false);
69+
});
70+
});
71+
72+
it("is false when specified as string", function(done) {
73+
withConfig({ config: { sanitize_batch: "false" } }, done, function(engine_config) {
74+
expect(engine_config.userConfig.sanitizeBatch).to.eq(false);
75+
});
76+
});
77+
78+
it("is true when specified", function(done) {
79+
withConfig({ config: { sanitize_batch: true } }, done, function(engine_config) {
80+
expect(engine_config.userConfig.sanitizeBatch).to.eq(true);
81+
});
82+
});
83+
84+
it("is true when specified as string", function(done) {
85+
withConfig({ config: { sanitize_batch: "true" } }, done, function(engine_config) {
86+
expect(engine_config.userConfig.sanitizeBatch).to.eq(true);
5587
});
5688
});
5789
});

0 commit comments

Comments
 (0)