From 5dce527ec2caf3e3ebae272370bfaf9756f9acd1 Mon Sep 17 00:00:00 2001 From: Kenton Jacobsen Date: Mon, 29 Jan 2018 10:59:49 -0500 Subject: [PATCH 1/4] Chore: Apply memoization to config creation within glob utils --- lib/util/glob-util.js | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/lib/util/glob-util.js b/lib/util/glob-util.js index 6a1f150a599..6698e51d307 100644 --- a/lib/util/glob-util.js +++ b/lib/util/glob-util.js @@ -8,7 +8,8 @@ // Requirements //------------------------------------------------------------------------------ -const fs = require("fs"), +const _ = require("lodash"), + fs = require("fs"), path = require("path"), GlobSync = require("./glob"), @@ -88,6 +89,18 @@ function resolveFileGlobPatterns(patterns, options) { return patterns.filter(p => p.length).map(processPathExtensions); } +const dotfilesPattern = /(?:(?:^\.)|(?:[/\\]\.))[^/\\.].*/; + +const memoGetIgnorePaths = _.memoize( + options => + new IgnoredPaths(options) +); + +const memoGetNewConfig = _.memoize( + (options, dotfiles) => + Object.assign({}, options, { dotfiles }) +); + /** * Build a list of absolute filesnames on which ESLint will act. * Ignored files are excluded from the results, as are duplicates. @@ -151,15 +164,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 = memoGetIgnorePaths(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 = memoGetNewConfig(options, globIncludesDotfiles); + } - const ignoredPaths = new IgnoredPaths(Object.assign({}, options, { dotfiles: options.dotfiles || globIncludesDotfiles })); + const ignoredPaths = memoGetIgnorePaths(newOptions); const shouldIgnore = ignoredPaths.getIgnoredFoldersGlobChecker(); const globOptions = { nodir: true, From fa7b5cd565c1148a0bb236ef7ad5fba860221847 Mon Sep 17 00:00:00 2001 From: Kenton Jacobsen Date: Mon, 5 Feb 2018 10:52:04 -0500 Subject: [PATCH 2/4] Code Review updates --- lib/util/glob-util.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/util/glob-util.js b/lib/util/glob-util.js index 6698e51d307..36bd559c865 100644 --- a/lib/util/glob-util.js +++ b/lib/util/glob-util.js @@ -8,7 +8,7 @@ // Requirements //------------------------------------------------------------------------------ -const _ = require("lodash"), +const lodash = require("lodash"), fs = require("fs"), path = require("path"), GlobSync = require("./glob"), @@ -91,12 +91,12 @@ function resolveFileGlobPatterns(patterns, options) { const dotfilesPattern = /(?:(?:^\.)|(?:[/\\]\.))[^/\\.].*/; -const memoGetIgnorePaths = _.memoize( +const getIgnorePaths = lodash.memoize( options => new IgnoredPaths(options) ); -const memoGetNewConfig = _.memoize( +const getNewConfig = lodash.memoize( (options, dotfiles) => Object.assign({}, options, { dotfiles }) ); @@ -164,7 +164,7 @@ function listFilesToProcess(globPatterns, options) { const file = path.resolve(cwd, pattern); if (fs.existsSync(file) && fs.statSync(file).isFile()) { - const ignoredPaths = memoGetIgnorePaths(options); + const ignoredPaths = getIgnorePaths(options); addFile(fs.realpathSync(file), true, ignoredPaths); } else { @@ -174,10 +174,10 @@ function listFilesToProcess(globPatterns, options) { let newOptions = options; if (!options.dotfiles) { - newOptions = memoGetNewConfig(options, globIncludesDotfiles); + newOptions = getNewConfig(options, globIncludesDotfiles); } - const ignoredPaths = memoGetIgnorePaths(newOptions); + const ignoredPaths = getIgnorePaths(newOptions); const shouldIgnore = ignoredPaths.getIgnoredFoldersGlobChecker(); const globOptions = { nodir: true, From 8b61b81ba7acef6730fd202408a098d1c97669c3 Mon Sep 17 00:00:00 2001 From: Kenton Jacobsen Date: Tue, 6 Feb 2018 10:15:03 -0500 Subject: [PATCH 3/4] Safer memoization --- lib/util/glob-util.js | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/lib/util/glob-util.js b/lib/util/glob-util.js index 36bd559c865..fa0b865f427 100644 --- a/lib/util/glob-util.js +++ b/lib/util/glob-util.js @@ -91,16 +91,6 @@ function resolveFileGlobPatterns(patterns, options) { const dotfilesPattern = /(?:(?:^\.)|(?:[/\\]\.))[^/\\.].*/; -const getIgnorePaths = lodash.memoize( - options => - new IgnoredPaths(options) -); - -const getNewConfig = lodash.memoize( - (options, dotfiles) => - Object.assign({}, options, { dotfiles }) -); - /** * Build a list of absolute filesnames on which ESLint will act. * Ignored files are excluded from the results, as are duplicates. @@ -120,6 +110,13 @@ function listFilesToProcess(globPatterns, options) { const cwd = (options && options.cwd) || process.cwd(); + const getIgnorePaths = lodash.memoize( + options => + new IgnoredPaths(options) + ); + + const getNewConfig = (options, dotfiles) => Object.assign({}, options, { dotfiles }); + /** * Executes the linter on a file defined by the `filename`. Skips * unsupported file extensions and any files that are already linted. From f7a43bb297e18937fa68d63517c66fb24bc5558c Mon Sep 17 00:00:00 2001 From: Kenton Jacobsen Date: Tue, 6 Feb 2018 12:45:43 -0500 Subject: [PATCH 4/4] Safer memoization --- lib/util/glob-util.js | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/lib/util/glob-util.js b/lib/util/glob-util.js index fa0b865f427..e4b78270a47 100644 --- a/lib/util/glob-util.js +++ b/lib/util/glob-util.js @@ -111,12 +111,10 @@ function listFilesToProcess(globPatterns, options) { const cwd = (options && options.cwd) || process.cwd(); const getIgnorePaths = lodash.memoize( - options => - new IgnoredPaths(options) + optionsObj => + new IgnoredPaths(optionsObj) ); - const getNewConfig = (options, dotfiles) => Object.assign({}, options, { dotfiles }); - /** * Executes the linter on a file defined by the `filename`. Skips * unsupported file extensions and any files that are already linted. @@ -171,7 +169,7 @@ function listFilesToProcess(globPatterns, options) { let newOptions = options; if (!options.dotfiles) { - newOptions = getNewConfig(options, globIncludesDotfiles); + newOptions = Object.assign({}, options, { dotfiles: globIncludesDotfiles }); } const ignoredPaths = getIgnorePaths(newOptions);