Skip to content
This repository was archived by the owner on Sep 28, 2020. It is now read-only.

Commit b6c3938

Browse files
HDuckevilebottnawi
authored andcommitted
feat: add eslint 6 support (#275)
1 parent 02c1e76 commit b6c3938

File tree

8 files changed

+76
-43
lines changed

8 files changed

+76
-43
lines changed

Diff for: .travis.yml

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ language: node_js
33
node_js:
44
- 10
55
- 8
6-
- 6
76

87
env:
98
- WEBPACK_VERSION=4

Diff for: index.js

+14-13
Original file line numberDiff line numberDiff line change
@@ -162,12 +162,19 @@ module.exports = function(input, map) {
162162

163163
var userEslintPath = userOptions.eslintPath;
164164

165+
var eslintPkgPath = "eslint/package.json";
166+
if (userEslintPath) {
167+
eslintPkgPath = userEslintPath + "/package.json";
168+
}
169+
170+
var eslintVersion = require(eslintPkgPath).version;
171+
165172
var config = assign(
166173
// loader defaults
167174
{
168175
cacheIdentifier: JSON.stringify({
169176
"eslint-loader": pkg.version,
170-
eslint: require(userEslintPath || "eslint").version
177+
eslint: eslintVersion
171178
}),
172179
eslintPath: "eslint"
173180
},
@@ -188,17 +195,6 @@ module.exports = function(input, map) {
188195
// ignored
189196
}
190197
}
191-
if (config.formatter == null || typeof config.formatter !== "function") {
192-
if (userEslintPath) {
193-
try {
194-
config.formatter = require(userEslintPath + "/lib/formatters/stylish");
195-
} catch (e) {
196-
config.formatter = require("eslint/lib/formatters/stylish");
197-
}
198-
} else {
199-
config.formatter = require("eslint/lib/formatters/stylish");
200-
}
201-
}
202198

203199
var cacheDirectory = config.cache;
204200
var cacheIdentifier = config.cacheIdentifier;
@@ -207,11 +203,17 @@ module.exports = function(input, map) {
207203

208204
// Create the engine only once per config
209205
var configHash = objectHash(config);
206+
210207
if (!engines[configHash]) {
211208
var eslint = require(config.eslintPath);
212209
engines[configHash] = new eslint.CLIEngine(config);
213210
}
214211

212+
var engine = engines[configHash];
213+
if (config.formatter == null || typeof config.formatter !== "function") {
214+
config.formatter = engine.getFormatter("stylish");
215+
}
216+
215217
webpack.cacheable();
216218

217219
var resourcePath = webpack.resourcePath;
@@ -223,7 +225,6 @@ module.exports = function(input, map) {
223225
resourcePath = resourcePath.substr(cwd.length + 1);
224226
}
225227

226-
var engine = engines[configHash];
227228
// return early if cached
228229
if (config.cache) {
229230
var callback = webpack.async();

Diff for: package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"index.js"
1717
],
1818
"peerDependencies": {
19-
"eslint": ">=1.6.0 <6.0.0",
19+
"eslint": ">=1.6.0 <7.0.0",
2020
"webpack": ">=2.0.0 <5.0.0"
2121
},
2222
"dependencies": {
@@ -28,7 +28,7 @@
2828
},
2929
"devDependencies": {
3030
"ava": "^0.17.0",
31-
"eslint": "^5.1.0",
31+
"eslint": "^6.0.1",
3232
"eslint-config-i-am-meticulous": "^11.0.0",
3333
"eslint-friendly-formatter": "^2.0.4",
3434
"husky": "^0.14.3",

Diff for: test/formatter-multiple-entries.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@ var path = require("path");
33

44
var test = require("ava");
55
var webpack = require("webpack");
6+
var CLIEngine = require("eslint").CLIEngine;
67

78
var conf = require("./utils/conf");
89

910
test.cb(
1011
"eslint-loader can be configured to write multiple eslint result files",
1112
function(t) {
1213
var outputFilename = "outputReport-[name].txt";
14+
1315
var config = conf(
1416
{
1517
entry: [
@@ -19,15 +21,15 @@ test.cb(
1921
]
2022
},
2123
{
22-
formatter: require("eslint/lib/formatters/checkstyle"),
24+
formatter: CLIEngine.getFormatter("checkstyle"),
2325
outputReport: {
2426
filePath: outputFilename
2527
}
2628
}
2729
);
2830

2931
/* Plan for the success count. Failure cases are going to fail anyway so the
30-
* count being off for those cases doesn't matter. */
32+
* count being off for those cases doesn't matter. */
3133
t.plan(config.entry.length * 2);
3234

3335
webpack(config, function(err, stats) {

Diff for: test/formatter-write.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ var fs = require("fs");
33

44
var test = require("ava");
55
var webpack = require("webpack");
6+
var CLIEngine = require("eslint").CLIEngine;
67

78
var conf = require("./utils/conf");
89

@@ -17,7 +18,7 @@ test.cb(
1718
entry: "./test/fixtures/error.js"
1819
},
1920
{
20-
formatter: require("eslint/lib/formatters/checkstyle"),
21+
formatter: CLIEngine.getFormatter("checkstyle"),
2122
outputReport: {
2223
filePath: outputFilename
2324
}

Diff for: test/mock/eslint/index.js

+47-24
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,56 @@
1-
function CLIEngine() {
2-
3-
}
1+
function CLIEngine() {}
42

53
CLIEngine.prototype.executeOnText = function() {
64
return {
7-
results: [{
8-
filePath: "",
9-
messages: [{
10-
ruleId: "no-undef",
11-
severity: 2,
12-
message: "Fake error",
13-
line: 1,
14-
column: 11,
15-
nodeType: "Identifier",
16-
source: "var foo = stuff",
17-
}],
18-
errorCount: 2,
19-
warningCount: 0,
20-
fixableErrorCount: 0,
21-
fixableWarningCount: 0,
22-
source: "",
23-
}],
5+
results: [
6+
{
7+
filePath: "",
8+
messages: [
9+
{
10+
ruleId: "no-undef",
11+
severity: 2,
12+
message: "Fake error",
13+
line: 1,
14+
column: 11,
15+
nodeType: "Identifier",
16+
source: "var foo = stuff"
17+
}
18+
],
19+
errorCount: 2,
20+
warningCount: 0,
21+
fixableErrorCount: 0,
22+
fixableWarningCount: 0,
23+
source: ""
24+
}
25+
],
2426
errorCount: 2,
2527
warningCount: 0,
2628
fixableErrorCount: 0,
27-
fixableWarningCount: 0,
29+
fixableWarningCount: 0
30+
};
31+
};
32+
33+
CLIEngine.prototype.getFormatter = function(format) {
34+
const resolvedFormatName = format || "stylish";
35+
36+
if (typeof resolvedFormatName !== "string") {
37+
return null;
38+
}
39+
40+
const eslintVersion = require("./package.json").version;
41+
const formatterPath =
42+
eslintVersion >= "6.0.0"
43+
? "./lib/cli-engine/formatters/stylish"
44+
: "./lib/formatters/stylish";
45+
46+
try {
47+
return require(formatterPath);
48+
} catch (ex) {
49+
ex.message = `There was a problem loading formatter: ${formatterPath}\nError: ${ex.message}`;
50+
throw ex;
2851
}
29-
}
52+
};
3053

3154
module.exports = {
32-
CLIEngine: CLIEngine,
33-
}
55+
CLIEngine: CLIEngine
56+
};
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = function(result) {
2+
return JSON.stringify(result);
3+
};

Diff for: test/mock/eslint/package.json

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"name": "eslint",
3+
"version": "5.16.0"
4+
}

0 commit comments

Comments
 (0)