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

Commit

Permalink
Fixed: ignore cache when eslint rules have changed (#151)
Browse files Browse the repository at this point in the history
* Fixed: ignore cache when eslint rules have changed

* Update cache test with new test runner
  • Loading branch information
peternewnham authored and MoOx committed Feb 22, 2017
1 parent 40567da commit c667541
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 3 deletions.
21 changes: 18 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ var objectHash = require("object-hash")
var os = require("os")

var engines = {}
var rules = {}
var cache = null
var cachePath = null

Expand All @@ -29,25 +30,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))
Expand Down
48 changes: 48 additions & 0 deletions test/cache.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
var test = require("ava")
var webpack = require("webpack")
var conf = require("./utils/conf")
var fs = require("fs")

var cacheFilePath = "./node_modules/.cache/eslint-loader/data.json"

test.cb("eslint-loader can cache results", function(t) {
t.plan(2)
webpack(conf(
{
entry: "./test/fixtures/cache.js",
},
{
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.after.always("teardown", function() {
fs.unlinkSync(cacheFilePath)
})
7 changes: 7 additions & 0 deletions test/fixtures/cache.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
"use strict"

function cacheIt() {
return "cache"
}

cacheIt()

0 comments on commit c667541

Please sign in to comment.