Skip to content

Commit

Permalink
lib: move glob from path to internal/fs/glob
Browse files Browse the repository at this point in the history
  • Loading branch information
pmarchini committed Dec 11, 2024
1 parent 4e2bdf6 commit a33a817
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 41 deletions.
16 changes: 16 additions & 0 deletions lib/internal/fs/glob.js
Original file line number Diff line number Diff line change
Expand Up @@ -650,7 +650,23 @@ class Glob {
}
}

function glob(path, pattern, windows) {
validateString(path, 'path');
validateString(pattern, 'pattern');
return lazyMinimatch().minimatch(path, pattern, {
kEmptyObject,
nocase: isMacOS || isWindows,
windowsPathsNoEscape: true,
nonegate: true,
nocomment: true,
optimizationLevel: 2,
platform: windows ? 'win32' : 'posix',
nocaseMagicOnly: true,
});
}

module.exports = {
__proto__: null,
Glob,
glob,
};
23 changes: 3 additions & 20 deletions lib/internal/test_runner/coverage.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const {
readFileSync,
rmSync,
} = require('fs');
const { setupCoverageHooks, isWindows, isMacOS } = require('internal/util');
const { setupCoverageHooks } = require('internal/util');
const { tmpdir } = require('os');
const { join, resolve, relative } = require('path');
const { fileURLToPath } = require('internal/url');
Expand All @@ -36,31 +36,14 @@ const {
ERR_SOURCE_MAP_MISSING_SOURCE,
},
} = require('internal/errors');
const { glob } = require('internal/fs/glob');

const kCoverageFileRegex = /^coverage-(\d+)-(\d{13})-(\d+)\.json$/;
const kIgnoreRegex = /\/\* node:coverage ignore next (?<count>\d+ )?\*\//;
const kLineEndingRegex = /\r?\n$/u;
const kLineSplitRegex = /(?<=\r?\n)/u;
const kStatusRegex = /\/\* node:coverage (?<status>enable|disable) \*\//;

let minimatch;
function lazyMinimatch() {
minimatch ??= require('internal/deps/minimatch/index');
return minimatch;
}

function glob(path, pattern, windows) {
return lazyMinimatch().minimatch(path, pattern, {
__proto__: null,
nocase: isMacOS || isWindows,
windowsPathsNoEscape: true,
nonegate: true,
nocomment: true,
optimizationLevel: 2,
platform: windows ? 'win32' : 'posix',
nocaseMagicOnly: true,
});
}

class CoverageLine {
constructor(line, startOffset, src, length = src?.length) {
const newlineLength = src == null ? 0 :
Expand Down
27 changes: 6 additions & 21 deletions lib/path.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,12 @@ const {
} = require('internal/validators');

const {
getLazy,
emitExperimentalWarning,
isWindows,
isMacOS,
getLazy,
} = require('internal/util');

const lazyMinimatch = getLazy(() => require('internal/deps/minimatch/index'));
const lazyGlob = getLazy(() => require('internal/fs/glob')).glob;

function isPathSeparator(code) {
return code === CHAR_FORWARD_SLASH || code === CHAR_BACKWARD_SLASH;
Expand Down Expand Up @@ -164,22 +163,6 @@ function _format(sep, pathObject) {
return dir === pathObject.root ? `${dir}${base}` : `${dir}${sep}${base}`;
}

function glob(path, pattern, windows) {
emitExperimentalWarning('glob');
validateString(path, 'path');
validateString(pattern, 'pattern');
return lazyMinimatch().minimatch(path, pattern, {
__proto__: null,
nocase: isMacOS || isWindows,
windowsPathsNoEscape: true,
nonegate: true,
nocomment: true,
optimizationLevel: 2,
platform: windows ? 'win32' : 'posix',
nocaseMagicOnly: true,
});
}

const win32 = {
/**
* path.resolve([from ...], to)
Expand Down Expand Up @@ -1140,7 +1123,8 @@ const win32 = {
},

matchesGlob(path, pattern) {
return glob(path, pattern, true);
emitExperimentalWarning('glob');
return lazyGlob()(path, pattern, true);
},

sep: '\\',
Expand Down Expand Up @@ -1616,7 +1600,8 @@ const posix = {
},

matchesGlob(path, pattern) {
return glob(path, pattern, false);
emitExperimentalWarning('glob');
return lazyGlob()(path, pattern, false);
},

sep: '/',
Expand Down

0 comments on commit a33a817

Please sign in to comment.