From 70f22f3f545402811068cee1fae5035be3dc4600 Mon Sep 17 00:00:00 2001 From: Kenton Jacobsen Date: Fri, 16 Feb 2018 16:39:12 -0500 Subject: [PATCH] Chore: Apply memoization to config creation within glob utils (#9944) * Chore: Apply memoization to config creation within glob utils * Code Review updates * Safer memoization * Safer memoization --- lib/util/glob-util.js | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/lib/util/glob-util.js b/lib/util/glob-util.js index 6a1f150a599..e4b78270a47 100644 --- a/lib/util/glob-util.js +++ b/lib/util/glob-util.js @@ -8,7 +8,8 @@ // Requirements //------------------------------------------------------------------------------ -const fs = require("fs"), +const lodash = require("lodash"), + fs = require("fs"), path = require("path"), GlobSync = require("./glob"), @@ -88,6 +89,8 @@ function resolveFileGlobPatterns(patterns, options) { return patterns.filter(p => p.length).map(processPathExtensions); } +const dotfilesPattern = /(?:(?:^\.)|(?:[/\\]\.))[^/\\.].*/; + /** * Build a list of absolute filesnames on which ESLint will act. * Ignored files are excluded from the results, as are duplicates. @@ -107,6 +110,11 @@ function listFilesToProcess(globPatterns, options) { const cwd = (options && options.cwd) || process.cwd(); + const getIgnorePaths = lodash.memoize( + optionsObj => + new IgnoredPaths(optionsObj) + ); + /** * Executes the linter on a file defined by the `filename`. Skips * unsupported file extensions and any files that are already linted. @@ -151,15 +159,20 @@ function listFilesToProcess(globPatterns, options) { const file = path.resolve(cwd, pattern); if (fs.existsSync(file) && fs.statSync(file).isFile()) { - const ignoredPaths = new IgnoredPaths(options); + const ignoredPaths = getIgnorePaths(options); addFile(fs.realpathSync(file), true, ignoredPaths); } else { // regex to find .hidden or /.hidden patterns, but not ./relative or ../relative - const globIncludesDotfiles = /(?:(?:^\.)|(?:[/\\]\.))[^/\\.].*/.test(pattern); + const globIncludesDotfiles = dotfilesPattern.test(pattern); + let newOptions = options; + + if (!options.dotfiles) { + newOptions = Object.assign({}, options, { dotfiles: globIncludesDotfiles }); + } - const ignoredPaths = new IgnoredPaths(Object.assign({}, options, { dotfiles: options.dotfiles || globIncludesDotfiles })); + const ignoredPaths = getIgnorePaths(newOptions); const shouldIgnore = ignoredPaths.getIgnoredFoldersGlobChecker(); const globOptions = { nodir: true,