From 26a2daab311c8c59942c52f436d380a195db2bd4 Mon Sep 17 00:00:00 2001 From: Victor Hom Date: Mon, 12 Jun 2017 22:27:24 -0400 Subject: [PATCH] Chore: Cache fs reads in ignored-paths (fixes #8363) (#8706) --- lib/ignored-paths.js | 40 +++++++++++++++++++++++++------------- tests/lib/ignored-paths.js | 24 +++++++++++++++++++++++ 2 files changed, 50 insertions(+), 14 deletions(-) diff --git a/lib/ignored-paths.js b/lib/ignored-paths.js index 0d9152495ec..d2d89bd9895 100644 --- a/lib/ignored-paths.js +++ b/lib/ignored-paths.js @@ -37,7 +37,6 @@ const DEFAULT_OPTIONS = { cwd: process.cwd() }; - //------------------------------------------------------------------------------ // Helpers //------------------------------------------------------------------------------ @@ -80,6 +79,7 @@ class IgnoredPaths { */ constructor(options) { options = mergeDefaultOptions(options); + this.cache = {}; /** * add pattern to node-ignore instance @@ -91,17 +91,6 @@ class IgnoredPaths { return ig.addPattern(pattern); } - /** - * add ignore file to node-ignore instance - * @param {Object} ig, instance of node-ignore - * @param {string} filepath, file to add to ig - * @returns {array} raw ignore rules - */ - function addIgnoreFile(ig, filepath) { - ig.ignoreFiles.push(filepath); - return ig.add(fs.readFileSync(filepath, "utf8")); - } - this.defaultPatterns = [].concat(DEFAULT_IGNORE_DIRS, options.patterns || []); this.baseDir = options.cwd; @@ -155,8 +144,8 @@ class IgnoredPaths { if (ignorePath) { debug(`Adding ${ignorePath}`); this.baseDir = path.dirname(path.resolve(options.cwd, ignorePath)); - addIgnoreFile(this.ig.custom, ignorePath); - addIgnoreFile(this.ig.default, ignorePath); + this.addIgnoreFile(this.ig.custom, ignorePath); + this.addIgnoreFile(this.ig.default, ignorePath); } if (options.ignorePattern) { @@ -168,6 +157,29 @@ class IgnoredPaths { this.options = options; } + /** + * read ignore filepath + * @param {string} filePath, file to add to ig + * @returns {array} raw ignore rules + */ + readIgnoreFile(filePath) { + if (typeof this.cache[filePath] === "undefined") { + this.cache[filePath] = fs.readFileSync(filePath, "utf8"); + } + return this.cache[filePath]; + } + + /** + * add ignore file to node-ignore instance + * @param {Object} ig, instance of node-ignore + * @param {string} filePath, file to add to ig + * @returns {array} raw ignore rules + */ + addIgnoreFile(ig, filePath) { + ig.ignoreFiles.push(filePath); + return ig.add(this.readIgnoreFile(filePath)); + } + /** * Determine whether a file path is included in the default or custom ignore patterns * @param {string} filepath Path to check diff --git a/tests/lib/ignored-paths.js b/tests/lib/ignored-paths.js index 07d96672d0b..1631d37f41c 100644 --- a/tests/lib/ignored-paths.js +++ b/tests/lib/ignored-paths.js @@ -164,6 +164,30 @@ describe("IgnoredPaths", () => { ) ); }); + + + }); + + describe("caching file reads", () => { + + let readFileSyncCount; + + before(() => { + readFileSyncCount = sinon.spy(fs, "readFileSync"); + }); + + after(() => { + readFileSyncCount.restore(); + }); + + it("should cache readFileSync on same file paths", () => { + const ignoreFilePath = getFixturePath(".eslintignore"); + const ignoredPaths = new IgnoredPaths({ ignore: true, cwd: getFixturePath() }); + + ignoredPaths.readIgnoreFile(ignoreFilePath); + assert.isTrue(ignoredPaths.contains(ignoreFilePath)); + sinon.assert.calledOnce(readFileSyncCount); + }); }); describe("initialization with ignorePattern", () => {