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

Fixed: ignore cache when eslint rules have changed #151

Merged
merged 2 commits into from
Feb 22, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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))
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()