From 4dbcc046c8b82d2c053d761f870041b4091575da Mon Sep 17 00:00:00 2001 From: Peter Newnham Date: Mon, 6 Feb 2017 16:11:08 +0000 Subject: [PATCH 1/2] Fixed: ignore cache when eslint rules have changed --- index.js | 21 +++++++++++++--- test/cache.js | 56 ++++++++++++++++++++++++++++++++++++++++++ test/fixtures/cache.js | 7 ++++++ 3 files changed, 81 insertions(+), 3 deletions(-) create mode 100644 test/cache.js create mode 100644 test/fixtures/cache.js diff --git a/index.js b/index.js index 7e1c260..87c6a2f 100644 --- a/index.js +++ b/index.js @@ -7,6 +7,7 @@ var findCacheDir = require("find-cache-dir") var objectHash = require("object-hash") var engines = {} +var rules = {} var cache = null var cachePath = null @@ -28,25 +29,39 @@ function lint(input, config, webpack) { resourcePath = resourcePath.substr(cwd.length + 1) } + // get engine + var configHash = objectHash(config) + var engine = engines[configHash] + var rulesHash = rules[configHash] + var res // If cache is enable and the data are the same as in the cache, just // use them if (config.cache) { + // just get rules hash once per engine for performance reasons + if (!rulesHash) { + rulesHash = objectHash(engine.getConfigForFile(resourcePath)) + rules[configHash] = rulesHash + } var inputMD5 = crypto.createHash("md5").update(input).digest("hex") - if (cache[resourcePath] && cache[resourcePath].hash === inputMD5) { + if ( + cache[resourcePath] && + cache[resourcePath].hash === inputMD5 && + cache[resourcePath].rules === rulesHash + ) { res = cache[resourcePath].res } } // Re-lint the text if the cache off or miss if (!res) { - var configHash = objectHash(config) - res = engines[configHash].executeOnText(input, resourcePath, true) + res = engine.executeOnText(input, resourcePath, true) // Save new results in the cache if (config.cache) { cache[resourcePath] = { hash: inputMD5, + rules: rulesHash, res: res, } fs.writeFileSync(cachePath, JSON.stringify(cache)) diff --git a/test/cache.js b/test/cache.js new file mode 100644 index 0000000..2f45258 --- /dev/null +++ b/test/cache.js @@ -0,0 +1,56 @@ +var test = require("tape") +var webpack = require("webpack") +var assign = require("object-assign") +var conf = require("./utils/conf") +var fs = require("fs") + +var cacheFilePath = "./node_modules/.cache/eslint-loader/data.json" + +test("eslint-loader can cache results", function(t) { + + // delete the require cache for eslint-loader otherwise any previously run + // tests will have initialised the cache as false and prevent this test + // from creating the cache file + delete require.cache[require.resolve("../index.js")] + + webpack(assign({}, + conf, + { + entry: "./test/fixtures/cache.js", + eslint: { + cache: true, + }, + } + ), + function(err) { + if (err) { + throw err + } + + fs.readFile(cacheFilePath, "utf8", function(err, contents) { + if (err) { + t.fail("expected cache file to have been created") + } + else { + t.pass("cache file has been created") + + var contentsJson = JSON.parse(contents) + t.deepEqual( + Object.keys(contentsJson["test/fixtures/cache.js"]), + ["hash", "rules", "res"], + "cache values have been set for the linted file" + ) + } + + t.end() + + }) + + }) +}) + +// delete the cache file once tests have completed +test("teardown", function(t) { + fs.unlinkSync(cacheFilePath) + t.end() +}) \ No newline at end of file diff --git a/test/fixtures/cache.js b/test/fixtures/cache.js new file mode 100644 index 0000000..48bffcc --- /dev/null +++ b/test/fixtures/cache.js @@ -0,0 +1,7 @@ +"use strict" + +function cacheIt() { + return "cache" +} + +cacheIt() From 5a22bb0c157598320efc59a7ded7e0430db339d6 Mon Sep 17 00:00:00 2001 From: Peter Newnham Date: Wed, 22 Feb 2017 09:16:13 +0000 Subject: [PATCH 2/2] Update cache test with new test runner --- test/cache.js | 24 ++++++++---------------- 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/test/cache.js b/test/cache.js index 2f45258..241f011 100644 --- a/test/cache.js +++ b/test/cache.js @@ -1,25 +1,18 @@ -var test = require("tape") +var test = require("ava") var webpack = require("webpack") -var assign = require("object-assign") var conf = require("./utils/conf") var fs = require("fs") var cacheFilePath = "./node_modules/.cache/eslint-loader/data.json" -test("eslint-loader can cache results", function(t) { - - // delete the require cache for eslint-loader otherwise any previously run - // tests will have initialised the cache as false and prevent this test - // from creating the cache file - delete require.cache[require.resolve("../index.js")] - - webpack(assign({}, - conf, +test.cb("eslint-loader can cache results", function(t) { + t.plan(2) + webpack(conf( { entry: "./test/fixtures/cache.js", - eslint: { - cache: true, - }, + }, + { + cache: true, } ), function(err) { @@ -50,7 +43,6 @@ test("eslint-loader can cache results", function(t) { }) // delete the cache file once tests have completed -test("teardown", function(t) { +test.after.always("teardown", function() { fs.unlinkSync(cacheFilePath) - t.end() }) \ No newline at end of file