Skip to content

Commit

Permalink
Fix: CircularJSON dependency warning
Browse files Browse the repository at this point in the history
  • Loading branch information
Teamop committed Feb 3, 2019
1 parent faf3c4e commit 047548d
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 19 deletions.
6 changes: 4 additions & 2 deletions lib/cli-engine.js
Expand Up @@ -581,6 +581,7 @@ class CLIEngine {
const startTime = Date.now();
const fileList = globUtils.listFilesToProcess(patterns, options);
const allUsedRules = new Set();
let cacheLintResults = true;
const results = fileList.map(fileInfo => {
if (fileInfo.ignored) {
return createIgnoreResult(fileInfo.filename, options.cwd);
Expand All @@ -596,10 +597,11 @@ class CLIEngine {
debug(`Reprocessing cached file to allow autofix: ${fileInfo.filename}`);
} else {
debug(`Skipping file since it hasn't changed: ${fileInfo.filename}`);

cacheLintResults = false;
return cachedLintResults;
}
}

}

// if there's a cache, populate it
Expand All @@ -618,7 +620,7 @@ class CLIEngine {
return result;
});

if (options.cache) {
if (options.cache && cacheLintResults) {
results.forEach(result => {

/*
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -47,7 +47,7 @@
"espree": "^5.0.0",
"esquery": "^1.0.1",
"esutils": "^2.0.2",
"file-entry-cache": "^2.0.0",
"file-entry-cache": "^4.0.0",
"functional-red-black-tree": "^1.0.1",
"glob": "^7.1.2",
"globals": "^11.7.0",
Expand Down
7 changes: 5 additions & 2 deletions tests/bin/eslint.js
Expand Up @@ -258,15 +258,18 @@ describe("bin/eslint.js", () => {
it("updates the cache file when the source file is modified", () => {
const initialCacheContent = fs.readFileSync(CACHE_PATH, "utf8");

// Update the file to change its mtime
fs.writeFileSync(SOURCE_PATH, fs.readFileSync(SOURCE_PATH, "utf8"));
// Update the file to add one blank space
fs.writeFileSync(SOURCE_PATH, fs.readFileSync(SOURCE_PATH, "utf8").concat(" "));

const child = runESLint(ARGS_WITH_CACHE);

return assertExitCode(child, 0).then(() => {
const newCacheContent = fs.readFileSync(CACHE_PATH, "utf8");

assert.notStrictEqual(initialCacheContent, newCacheContent, "Cache file should change after source is modified");

// restore the file
fs.writeFileSync(SOURCE_PATH, fs.readFileSync(SOURCE_PATH, "utf8").slice(0, -1));
});
});
it("deletes the cache file when run without the --cache argument", () => {
Expand Down
38 changes: 24 additions & 14 deletions tests/lib/cli-engine.js
Expand Up @@ -21,6 +21,8 @@ const assert = require("chai").assert,

const proxyquire = require("proxyquire").noCallThru().noPreserveCache();

const fCache = require("file-entry-cache");

//------------------------------------------------------------------------------
// Tests
//------------------------------------------------------------------------------
Expand Down Expand Up @@ -2301,14 +2303,17 @@ describe("CLIEngine", () => {
});

// create a new spy
spy = sandbox.spy(fs, "readFileSync");
// eslint-disable-next-line no-underscore-dangle
spy = sandbox.spy(engine._lintResultCache, "reconcile");
// eslint-disable-next-line no-underscore-dangle
const spy1 = sandbox.spy(engine._lintResultCache, "setCachedLintResults");

const cachedResult = engine.executeOnFiles([file]);

assert.deepStrictEqual(result, cachedResult, "the result is the same regardless of using cache or not");

// assert the file was not processed because the cache was used
assert.isFalse(spy.calledWith(file), "the file was not loaded because it used the cache");
assert.isFalse(spy.called && spy1.called, "the file was not loaded because it used the cache");
});

it("should remember the files from a previous run and do not operate on then if not changed", () => {
Expand Down Expand Up @@ -2375,11 +2380,12 @@ describe("CLIEngine", () => {

assert.isTrue(shell.test("-f", cacheFile), "the cache for eslint was created");

const cache = JSON.parse(fs.readFileSync(cacheFile));
const fileCache = fCache.createFromFile(cacheFile);
const { cache } = fileCache;

assert.isTrue(typeof cache[goodFile] === "object", "the entry for the good file is in the cache");
assert.isTrue(typeof cache.getKey(goodFile) === "object", "the entry for the good file is in the cache");

assert.isTrue(typeof cache[badFile] === "object", "the entry for the bad file is in the cache");
assert.isTrue(typeof cache.getKey(badFile) === "object", "the entry for the bad file is in the cache");

const cachedResult = engine.executeOnFiles([badFile, goodFile]);

Expand Down Expand Up @@ -2412,9 +2418,10 @@ describe("CLIEngine", () => {

engine.executeOnFiles([badFile, goodFile, toBeDeletedFile]);

let cache = JSON.parse(fs.readFileSync(cacheFile));
const fileCache = fCache.createFromFile(cacheFile);
let { cache } = fileCache;

assert.isTrue(typeof cache[toBeDeletedFile] === "object", "the entry for the file to be deleted is in the cache");
assert.isTrue(typeof cache.getKey(toBeDeletedFile) === "object", "the entry for the file to be deleted is in the cache");

// delete the file from the file system
fs.unlinkSync(toBeDeletedFile);
Expand Down Expand Up @@ -2456,9 +2463,10 @@ describe("CLIEngine", () => {

engine.executeOnFiles([badFile, goodFile, testFile2]);

let cache = JSON.parse(fs.readFileSync(cacheFile));
let fileCache = fCache.createFromFile(cacheFile);
let { cache } = fileCache;

assert.isTrue(typeof cache[testFile2] === "object", "the entry for the test-file2 is in the cache");
assert.isTrue(typeof cache.getKey(testFile2) === "object", "the entry for the test-file2 is in the cache");

/*
* we pass a different set of files minus test-file2
Expand All @@ -2467,9 +2475,10 @@ describe("CLIEngine", () => {
*/
engine.executeOnFiles([badFile, goodFile]);

cache = JSON.parse(fs.readFileSync(cacheFile));
fileCache = fCache.createFromFile(cacheFile);
cache = fileCache.cache;

assert.isTrue(typeof cache[testFile2] === "object", "the entry for the test-file2 is in the cache");
assert.isTrue(typeof cache.getKey(testFile2) === "object", "the entry for the test-file2 is in the cache");
});

it("should not delete cache when executing on text", () => {
Expand Down Expand Up @@ -2590,11 +2599,12 @@ describe("CLIEngine", () => {

assert.isTrue(shell.test("-f", customCacheFile), "the cache for eslint was created");

const cache = JSON.parse(fs.readFileSync(customCacheFile));
const fileCache = fCache.createFromFile(customCacheFile);
const { cache } = fileCache;

assert.isTrue(typeof cache[goodFile] === "object", "the entry for the good file is in the cache");
assert.isTrue(typeof cache.getKey(goodFile) === "object", "the entry for the good file is in the cache");

assert.isTrue(typeof cache[badFile] === "object", "the entry for the bad file is in the cache");
assert.isTrue(typeof cache.getKey(badFile) === "object", "the entry for the bad file is in the cache");

const cachedResult = engine.executeOnFiles([badFile, goodFile]);

Expand Down

0 comments on commit 047548d

Please sign in to comment.