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

Commit

Permalink
feat: add eslint 6 support (#275)
Browse files Browse the repository at this point in the history
  • Loading branch information
HDuck authored and evilebottnawi committed Jun 28, 2019
1 parent 02c1e76 commit b6c3938
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 43 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ language: node_js
node_js:
- 10
- 8
- 6

env:
- WEBPACK_VERSION=4
Expand Down
27 changes: 14 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,19 @@ module.exports = function(input, map) {

var userEslintPath = userOptions.eslintPath;

var eslintPkgPath = "eslint/package.json";
if (userEslintPath) {
eslintPkgPath = userEslintPath + "/package.json";
}

var eslintVersion = require(eslintPkgPath).version;

var config = assign(
// loader defaults
{
cacheIdentifier: JSON.stringify({
"eslint-loader": pkg.version,
eslint: require(userEslintPath || "eslint").version
eslint: eslintVersion
}),
eslintPath: "eslint"
},
Expand All @@ -188,17 +195,6 @@ module.exports = function(input, map) {
// ignored
}
}
if (config.formatter == null || typeof config.formatter !== "function") {
if (userEslintPath) {
try {
config.formatter = require(userEslintPath + "/lib/formatters/stylish");
} catch (e) {
config.formatter = require("eslint/lib/formatters/stylish");
}
} else {
config.formatter = require("eslint/lib/formatters/stylish");
}
}

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

// Create the engine only once per config
var configHash = objectHash(config);

if (!engines[configHash]) {
var eslint = require(config.eslintPath);
engines[configHash] = new eslint.CLIEngine(config);
}

var engine = engines[configHash];
if (config.formatter == null || typeof config.formatter !== "function") {
config.formatter = engine.getFormatter("stylish");
}

webpack.cacheable();

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

var engine = engines[configHash];
// return early if cached
if (config.cache) {
var callback = webpack.async();
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"index.js"
],
"peerDependencies": {
"eslint": ">=1.6.0 <6.0.0",
"eslint": ">=1.6.0 <7.0.0",
"webpack": ">=2.0.0 <5.0.0"
},
"dependencies": {
Expand All @@ -28,7 +28,7 @@
},
"devDependencies": {
"ava": "^0.17.0",
"eslint": "^5.1.0",
"eslint": "^6.0.1",
"eslint-config-i-am-meticulous": "^11.0.0",
"eslint-friendly-formatter": "^2.0.4",
"husky": "^0.14.3",
Expand Down
6 changes: 4 additions & 2 deletions test/formatter-multiple-entries.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ var path = require("path");

var test = require("ava");
var webpack = require("webpack");
var CLIEngine = require("eslint").CLIEngine;

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

test.cb(
"eslint-loader can be configured to write multiple eslint result files",
function(t) {
var outputFilename = "outputReport-[name].txt";

var config = conf(
{
entry: [
Expand All @@ -19,15 +21,15 @@ test.cb(
]
},
{
formatter: require("eslint/lib/formatters/checkstyle"),
formatter: CLIEngine.getFormatter("checkstyle"),
outputReport: {
filePath: outputFilename
}
}
);

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

webpack(config, function(err, stats) {
Expand Down
3 changes: 2 additions & 1 deletion test/formatter-write.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ var fs = require("fs");

var test = require("ava");
var webpack = require("webpack");
var CLIEngine = require("eslint").CLIEngine;

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

Expand All @@ -17,7 +18,7 @@ test.cb(
entry: "./test/fixtures/error.js"
},
{
formatter: require("eslint/lib/formatters/checkstyle"),
formatter: CLIEngine.getFormatter("checkstyle"),
outputReport: {
filePath: outputFilename
}
Expand Down
71 changes: 47 additions & 24 deletions test/mock/eslint/index.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,56 @@
function CLIEngine() {

}
function CLIEngine() {}

CLIEngine.prototype.executeOnText = function() {
return {
results: [{
filePath: "",
messages: [{
ruleId: "no-undef",
severity: 2,
message: "Fake error",
line: 1,
column: 11,
nodeType: "Identifier",
source: "var foo = stuff",
}],
errorCount: 2,
warningCount: 0,
fixableErrorCount: 0,
fixableWarningCount: 0,
source: "",
}],
results: [
{
filePath: "",
messages: [
{
ruleId: "no-undef",
severity: 2,
message: "Fake error",
line: 1,
column: 11,
nodeType: "Identifier",
source: "var foo = stuff"
}
],
errorCount: 2,
warningCount: 0,
fixableErrorCount: 0,
fixableWarningCount: 0,
source: ""
}
],
errorCount: 2,
warningCount: 0,
fixableErrorCount: 0,
fixableWarningCount: 0,
fixableWarningCount: 0
};
};

CLIEngine.prototype.getFormatter = function(format) {
const resolvedFormatName = format || "stylish";

if (typeof resolvedFormatName !== "string") {
return null;
}

const eslintVersion = require("./package.json").version;
const formatterPath =
eslintVersion >= "6.0.0"
? "./lib/cli-engine/formatters/stylish"
: "./lib/formatters/stylish";

try {
return require(formatterPath);
} catch (ex) {
ex.message = `There was a problem loading formatter: ${formatterPath}\nError: ${ex.message}`;
throw ex;
}
}
};

module.exports = {
CLIEngine: CLIEngine,
}
CLIEngine: CLIEngine
};
3 changes: 3 additions & 0 deletions test/mock/eslint/lib/cli-engine/formatters/stylish.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = function(result) {
return JSON.stringify(result);
};
4 changes: 4 additions & 0 deletions test/mock/eslint/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "eslint",
"version": "5.16.0"
}

0 comments on commit b6c3938

Please sign in to comment.