From b58ae2e6d6bd4662b549ca5c0472943055a74df8 Mon Sep 17 00:00:00 2001 From: Gyandeep Singh Date: Wed, 21 Jun 2017 17:13:31 -0500 Subject: [PATCH] Chore: Only instantiate fileEntryCache when cache flage set (perf) (#8763) --- lib/cli-engine.js | 27 ++++++++++++++++----------- tests/lib/cli-engine.js | 2 ++ 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/lib/cli-engine.js b/lib/cli-engine.js index ebca4cb819a..8d5ab065fe5 100644 --- a/lib/cli-engine.js +++ b/lib/cli-engine.js @@ -398,15 +398,17 @@ class CLIEngine { this.options = options; this.linter = new Linter(); - const cacheFile = getCacheFile(this.options.cacheLocation || this.options.cacheFile, this.options.cwd); - - /** - * Cache used to avoid operating on files that haven't changed since the - * last successful execution (e.g., file passed linting with no errors and - * no warnings). - * @type {Object} - */ - this._fileCache = fileEntryCache.create(cacheFile); + if (options.cache) { + const cacheFile = getCacheFile(this.options.cacheLocation || this.options.cacheFile, this.options.cwd); + + /** + * Cache used to avoid operating on files that haven't changed since the + * last successful execution (e.g., file passed linting with no errors and + * no warnings). + * @type {Object} + */ + this._fileCache = fileEntryCache.create(cacheFile); + } // load in additional rules if (this.options.rulePaths) { @@ -495,6 +497,11 @@ class CLIEngine { fileCache = this._fileCache, configHelper = this.config; let prevConfig; // the previous configuration used + const cacheFile = getCacheFile(this.options.cacheLocation || this.options.cacheFile, this.options.cwd); + + if (!options.cache && fs.existsSync(cacheFile)) { + fs.unlinkSync(cacheFile); + } /** * Calculates the hash of the config file used to validate a given file @@ -570,8 +577,6 @@ class CLIEngine { // move to the next file return; } - } else { - fileCache.destroy(); } debug(`Processing ${filename}`); diff --git a/tests/lib/cli-engine.js b/tests/lib/cli-engine.js index f0f43e6e128..fcc80b876bb 100644 --- a/tests/lib/cli-engine.js +++ b/tests/lib/cli-engine.js @@ -1324,6 +1324,8 @@ describe("CLIEngine", () => { fakeFS.realpathSync = function() { throw new Error("this error should not happen"); }; + fakeFS.existsSync = fs.existsSync; + fakeFS.unlinkSync = fs.unlinkSync; engine = new LocalCLIEngine({ ignorePattern: "tests"