From d58c9c7dd13db15eeed1992e47e026bd9fdef508 Mon Sep 17 00:00:00 2001 From: HDuck <1ntranc3d@gmail.com> Date: Sun, 23 Jun 2019 22:17:04 +0300 Subject: [PATCH 1/6] add eslint ver.6+ tests support --- package.json | 4 ++-- test/formatter-multiple-entries.js | 11 +++++++++-- test/formatter-write.js | 8 +++++++- test/mock/eslint/lib/cli-engine/formatters/stylish.js | 3 +++ test/mock/eslint/package.json | 4 ++++ 5 files changed, 25 insertions(+), 5 deletions(-) create mode 100644 test/mock/eslint/lib/cli-engine/formatters/stylish.js create mode 100644 test/mock/eslint/package.json diff --git a/package.json b/package.json index 1fed1e2..b1e7ff5 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": "5.1.0 - 6", "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..a0ea74a 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 eslintVersion = require("eslint/package.json").version; var conf = require("./utils/conf"); @@ -10,6 +11,12 @@ test.cb( "eslint-loader can be configured to write multiple eslint result files", function(t) { var outputFilename = "outputReport-[name].txt"; + + var formattersPath = "eslint/lib/formatters"; + if (eslintVersion >= "6.0.0") { + formattersPath = "eslint/lib/cli-engine/formatters"; + } + var config = conf( { entry: [ @@ -19,7 +26,7 @@ test.cb( ] }, { - formatter: require("eslint/lib/formatters/checkstyle"), + formatter: require(formattersPath + "/checkstyle"), outputReport: { filePath: outputFilename } @@ -27,7 +34,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..e8048ef 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 eslintVersion = require("eslint/package.json").version; var conf = require("./utils/conf"); @@ -11,13 +12,18 @@ test.cb( function(t) { t.plan(2); + var formattersPath = "eslint/lib/formatters"; + if (eslintVersion >= "6.0.0") { + formattersPath = "eslint/lib/cli-engine/formatters"; + } + var outputFilename = "outputReport.txt"; var config = conf( { entry: "./test/fixtures/error.js" }, { - formatter: require("eslint/lib/formatters/checkstyle"), + formatter: require(formattersPath + "/checkstyle"), outputReport: { filePath: outputFilename } 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" +} From 25e199308662bcf806d8e6914f69fc40b8770eeb Mon Sep 17 00:00:00 2001 From: HDuck <1ntranc3d@gmail.com> Date: Sun, 23 Jun 2019 22:18:03 +0300 Subject: [PATCH 2/6] fix formatters path issue --- index.js | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index 8b2a103..2c6ee09 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" }, @@ -189,14 +196,20 @@ module.exports = function(input, map) { } } if (config.formatter == null || typeof config.formatter !== "function") { + var defaultFormatterPath = "/lib/formatters/stylish"; + + if (eslintVersion >= "6.0.0") { + defaultFormatterPath = "/lib/cli-engine/formatters/stylish"; + } + if (userEslintPath) { try { - config.formatter = require(userEslintPath + "/lib/formatters/stylish"); + config.formatter = require(userEslintPath + defaultFormatterPath); } catch (e) { - config.formatter = require("eslint/lib/formatters/stylish"); + config.formatter = require("eslint" + defaultFormatterPath); } } else { - config.formatter = require("eslint/lib/formatters/stylish"); + config.formatter = require("eslint" + defaultFormatterPath); } } From 63e83f4596b16b4f5d1bdd3d03a9dfcc266ad898 Mon Sep 17 00:00:00 2001 From: HDuck <1ntranc3d@gmail.com> Date: Wed, 26 Jun 2019 22:46:53 +0300 Subject: [PATCH 3/6] add tests CLIEngine.getFormatter --- test/formatter-multiple-entries.js | 9 +--- test/formatter-write.js | 9 +--- test/mock/eslint/index.js | 71 ++++++++++++++++++++---------- 3 files changed, 51 insertions(+), 38 deletions(-) diff --git a/test/formatter-multiple-entries.js b/test/formatter-multiple-entries.js index a0ea74a..f0902d8 100644 --- a/test/formatter-multiple-entries.js +++ b/test/formatter-multiple-entries.js @@ -3,7 +3,7 @@ var path = require("path"); var test = require("ava"); var webpack = require("webpack"); -var eslintVersion = require("eslint/package.json").version; +var CLIEngine = require("eslint").CLIEngine; var conf = require("./utils/conf"); @@ -12,11 +12,6 @@ test.cb( function(t) { var outputFilename = "outputReport-[name].txt"; - var formattersPath = "eslint/lib/formatters"; - if (eslintVersion >= "6.0.0") { - formattersPath = "eslint/lib/cli-engine/formatters"; - } - var config = conf( { entry: [ @@ -26,7 +21,7 @@ test.cb( ] }, { - formatter: require(formattersPath + "/checkstyle"), + formatter: CLIEngine.getFormatter("checkstyle"), outputReport: { filePath: outputFilename } diff --git a/test/formatter-write.js b/test/formatter-write.js index e8048ef..d7850c1 100644 --- a/test/formatter-write.js +++ b/test/formatter-write.js @@ -3,7 +3,7 @@ var fs = require("fs"); var test = require("ava"); var webpack = require("webpack"); -var eslintVersion = require("eslint/package.json").version; +var CLIEngine = require("eslint").CLIEngine; var conf = require("./utils/conf"); @@ -12,18 +12,13 @@ test.cb( function(t) { t.plan(2); - var formattersPath = "eslint/lib/formatters"; - if (eslintVersion >= "6.0.0") { - formattersPath = "eslint/lib/cli-engine/formatters"; - } - var outputFilename = "outputReport.txt"; var config = conf( { entry: "./test/fixtures/error.js" }, { - formatter: require(formattersPath + "/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 +}; From a81b9505ead17db57d0bfb2c619e5e7529c1877d Mon Sep 17 00:00:00 2001 From: HDuck <1ntranc3d@gmail.com> Date: Wed, 26 Jun 2019 22:49:15 +0300 Subject: [PATCH 4/6] add formatters from CLIEngine --- index.js | 24 ++++++------------------ 1 file changed, 6 insertions(+), 18 deletions(-) diff --git a/index.js b/index.js index 2c6ee09..bf16935 100644 --- a/index.js +++ b/index.js @@ -195,23 +195,6 @@ module.exports = function(input, map) { // ignored } } - if (config.formatter == null || typeof config.formatter !== "function") { - var defaultFormatterPath = "/lib/formatters/stylish"; - - if (eslintVersion >= "6.0.0") { - defaultFormatterPath = "/lib/cli-engine/formatters/stylish"; - } - - if (userEslintPath) { - try { - config.formatter = require(userEslintPath + defaultFormatterPath); - } catch (e) { - config.formatter = require("eslint" + defaultFormatterPath); - } - } else { - config.formatter = require("eslint" + defaultFormatterPath); - } - } var cacheDirectory = config.cache; var cacheIdentifier = config.cacheIdentifier; @@ -220,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; @@ -236,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(); From 6da5b5721440c33336eaa235622629d7e300b5ce Mon Sep 17 00:00:00 2001 From: HDuck <1ntranc3d@gmail.com> Date: Wed, 26 Jun 2019 22:56:58 +0300 Subject: [PATCH 5/6] remove Node.js 6 support --- .travis.yml | 1 - package.json | 3 +++ 2 files changed, 3 insertions(+), 1 deletion(-) 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/package.json b/package.json index b1e7ff5..6f8d379 100644 --- a/package.json +++ b/package.json @@ -57,5 +57,8 @@ "test/*.js" ], "verbose": true + }, + "engines": { + "node": ">6" } } From 52627eaf85b739a83c9207085b4fb74b7d269cae Mon Sep 17 00:00:00 2001 From: HDuck <1ntranc3d@gmail.com> Date: Thu, 27 Jun 2019 21:36:07 +0300 Subject: [PATCH 6/6] pr review fix --- package.json | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/package.json b/package.json index 6f8d379..639be94 100644 --- a/package.json +++ b/package.json @@ -28,7 +28,7 @@ }, "devDependencies": { "ava": "^0.17.0", - "eslint": "5.1.0 - 6", + "eslint": "^6.0.1", "eslint-config-i-am-meticulous": "^11.0.0", "eslint-friendly-formatter": "^2.0.4", "husky": "^0.14.3", @@ -57,8 +57,5 @@ "test/*.js" ], "verbose": true - }, - "engines": { - "node": ">6" } }