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 Jan 24, 2019
1 parent faf3c4e commit 0ff5471
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 12 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
21 changes: 14 additions & 7 deletions tests/lib/cli-engine.js
Expand Up @@ -21,6 +21,10 @@ const assert = require("chai").assert,

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

// Need use 'flatted' to parse the cache content as the 'file-entry-cache' use it to create cache
// eslint-disable-next-line node/no-extraneous-require
const flatted = require("flatted");

//------------------------------------------------------------------------------
// Tests
//------------------------------------------------------------------------------
Expand Down Expand Up @@ -2301,14 +2305,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,7 +2382,7 @@ describe("CLIEngine", () => {

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

const cache = JSON.parse(fs.readFileSync(cacheFile));
const cache = flatted.parse(fs.readFileSync(cacheFile));

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

Expand Down Expand Up @@ -2412,7 +2419,7 @@ describe("CLIEngine", () => {

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

let cache = JSON.parse(fs.readFileSync(cacheFile));
let cache = flatted.parse(fs.readFileSync(cacheFile));

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

Expand Down Expand Up @@ -2456,7 +2463,7 @@ describe("CLIEngine", () => {

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

let cache = JSON.parse(fs.readFileSync(cacheFile));
let cache = flatted.parse(fs.readFileSync(cacheFile));

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

Expand All @@ -2467,7 +2474,7 @@ describe("CLIEngine", () => {
*/
engine.executeOnFiles([badFile, goodFile]);

cache = JSON.parse(fs.readFileSync(cacheFile));
cache = flatted.parse(fs.readFileSync(cacheFile));

assert.isTrue(typeof cache[testFile2] === "object", "the entry for the test-file2 is in the cache");
});
Expand Down Expand Up @@ -2590,7 +2597,7 @@ describe("CLIEngine", () => {

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

const cache = JSON.parse(fs.readFileSync(customCacheFile));
const cache = flatted.parse(fs.readFileSync(customCacheFile));

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

Expand Down

0 comments on commit 0ff5471

Please sign in to comment.