From b6c3938aae61c0390ac8d941af50cf36df14cfc3 Mon Sep 17 00:00:00 2001 From: Anatoliy <1ntranc3d@gmail.com> Date: Fri, 28 Jun 2019 14:10:47 +0300 Subject: [PATCH] feat: add eslint 6 support (#275) --- .travis.yml | 1 - index.js | 27 +++---- package.json | 4 +- test/formatter-multiple-entries.js | 6 +- test/formatter-write.js | 3 +- test/mock/eslint/index.js | 71 ++++++++++++------- .../lib/cli-engine/formatters/stylish.js | 3 + test/mock/eslint/package.json | 4 ++ 8 files changed, 76 insertions(+), 43 deletions(-) create mode 100644 test/mock/eslint/lib/cli-engine/formatters/stylish.js create mode 100644 test/mock/eslint/package.json diff --git a/.travis.yml b/.travis.yml index 66c753f..6625452 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,7 +3,6 @@ language: node_js node_js: - 10 - 8 - - 6 env: - WEBPACK_VERSION=4 diff --git a/index.js b/index.js index 8b2a103..bf16935 100644 --- a/index.js +++ b/index.js @@ -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" }, @@ -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; @@ -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; @@ -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(); diff --git a/package.json b/package.json index 1fed1e2..639be94 100644 --- a/package.json +++ b/package.json @@ -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": { @@ -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", diff --git a/test/formatter-multiple-entries.js b/test/formatter-multiple-entries.js index db2a1d9..f0902d8 100644 --- a/test/formatter-multiple-entries.js +++ b/test/formatter-multiple-entries.js @@ -3,6 +3,7 @@ var path = require("path"); var test = require("ava"); var webpack = require("webpack"); +var CLIEngine = require("eslint").CLIEngine; var conf = require("./utils/conf"); @@ -10,6 +11,7 @@ test.cb( "eslint-loader can be configured to write multiple eslint result files", function(t) { var outputFilename = "outputReport-[name].txt"; + var config = conf( { entry: [ @@ -19,7 +21,7 @@ test.cb( ] }, { - formatter: require("eslint/lib/formatters/checkstyle"), + formatter: CLIEngine.getFormatter("checkstyle"), outputReport: { filePath: outputFilename } @@ -27,7 +29,7 @@ test.cb( ); /* 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) { diff --git a/test/formatter-write.js b/test/formatter-write.js index 9e96f74..d7850c1 100644 --- a/test/formatter-write.js +++ b/test/formatter-write.js @@ -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"); @@ -17,7 +18,7 @@ test.cb( entry: "./test/fixtures/error.js" }, { - formatter: require("eslint/lib/formatters/checkstyle"), + formatter: CLIEngine.getFormatter("checkstyle"), outputReport: { filePath: outputFilename } diff --git a/test/mock/eslint/index.js b/test/mock/eslint/index.js index 53bc525..a2dcd84 100644 --- a/test/mock/eslint/index.js +++ b/test/mock/eslint/index.js @@ -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 +}; diff --git a/test/mock/eslint/lib/cli-engine/formatters/stylish.js b/test/mock/eslint/lib/cli-engine/formatters/stylish.js new file mode 100644 index 0000000..bccccf2 --- /dev/null +++ b/test/mock/eslint/lib/cli-engine/formatters/stylish.js @@ -0,0 +1,3 @@ +module.exports = function(result) { + return JSON.stringify(result); +}; diff --git a/test/mock/eslint/package.json b/test/mock/eslint/package.json new file mode 100644 index 0000000..946f6e8 --- /dev/null +++ b/test/mock/eslint/package.json @@ -0,0 +1,4 @@ +{ + "name": "eslint", + "version": "5.16.0" +}