|
import expand from 'brace-expansion'; |
|
import { assertValidPattern } from './assert-valid-pattern.js'; |
|
import { AST } from './ast.js'; |
|
import { escape } from './escape.js'; |
|
import { unescape } from './unescape.js'; |
|
export const minimatch = (p, pattern, options = {}) => { |
|
assertValidPattern(pattern); |
|
|
|
if (!options.nocomment && pattern.charAt(0) === '#') { |
|
return false; |
|
} |
|
return new Minimatch(pattern, options).match(p); |
|
}; |
|
|
|
const starDotExtRE = /^\*+([^+@!?\*\[\(]*)$/; |
|
const starDotExtTest = (ext) => (f) => !f.startsWith('.') && f.endsWith(ext); |
|
const starDotExtTestDot = (ext) => (f) => f.endsWith(ext); |
|
const starDotExtTestNocase = (ext) => { |
|
ext = ext.toLowerCase(); |
|
return (f) => !f.startsWith('.') && f.toLowerCase().endsWith(ext); |
|
}; |
|
const starDotExtTestNocaseDot = (ext) => { |
|
ext = ext.toLowerCase(); |
|
return (f) => f.toLowerCase().endsWith(ext); |
|
}; |
|
const starDotStarRE = /^\*+\.\*+$/; |
|
const starDotStarTest = (f) => !f.startsWith('.') && f.includes('.'); |
|
const starDotStarTestDot = (f) => f !== '.' && f !== '..' && f.includes('.'); |
|
const dotStarRE = /^\.\*+$/; |
|
const dotStarTest = (f) => f !== '.' && f !== '..' && f.startsWith('.'); |
|
const starRE = /^\*+$/; |
|
const starTest = (f) => f.length !== 0 && !f.startsWith('.'); |
|
const starTestDot = (f) => f.length !== 0 && f !== '.' && f !== '..'; |
|
const qmarksRE = /^\?+([^+@!?\*\[\(]*)?$/; |
|
const qmarksTestNocase = ([$0, ext = '']) => { |
|
const noext = qmarksTestNoExt([$0]); |
|
if (!ext) |
|
return noext; |
|
ext = ext.toLowerCase(); |
|
return (f) => noext(f) && f.toLowerCase().endsWith(ext); |
|
}; |
|
const qmarksTestNocaseDot = ([$0, ext = '']) => { |
|
const noext = qmarksTestNoExtDot([$0]); |
|
if (!ext) |
|
return noext; |
|
ext = ext.toLowerCase(); |
|
return (f) => noext(f) && f.toLowerCase().endsWith(ext); |
|
}; |
|
const qmarksTestDot = ([$0, ext = '']) => { |
|
const noext = qmarksTestNoExtDot([$0]); |
|
return !ext ? noext : (f) => noext(f) && f.endsWith(ext); |
|
}; |
|
const qmarksTest = ([$0, ext = '']) => { |
|
const noext = qmarksTestNoExt([$0]); |
|
return !ext ? noext : (f) => noext(f) && f.endsWith(ext); |
|
}; |
|
const qmarksTestNoExt = ([$0]) => { |
|
const len = $0.length; |
|
return (f) => f.length === len && !f.startsWith('.'); |
|
}; |
|
const qmarksTestNoExtDot = ([$0]) => { |
|
const len = $0.length; |
|
return (f) => f.length === len && f !== '.' && f !== '..'; |
|
}; |
|
|
|
const defaultPlatform = (typeof process === 'object' && process |
|
? (typeof process.env === 'object' && |
|
process.env && |
|
process.env.__MINIMATCH_TESTING_PLATFORM__) || |
|
process.platform |
|
: 'posix'); |
|
const path = { |
|
win32: { sep: '\\' }, |
|
posix: { sep: '/' }, |
|
}; |
|
|
|
export const sep = defaultPlatform === 'win32' ? path.win32.sep : path.posix.sep; |
|
minimatch.sep = sep; |
|
export const GLOBSTAR = Symbol('globstar **'); |
|
minimatch.GLOBSTAR = GLOBSTAR; |
|
|
|
|
|
const qmark = '[^/]'; |
|
|
|
const star = qmark + '*?'; |
|
|
|
|
|
|
|
const twoStarDot = '(?:(?!(?:\\/|^)(?:\\.{1,2})($|\\/)).)*?'; |
|
|
|
|
|
const twoStarNoDot = '(?:(?!(?:\\/|^)\\.).)*?'; |
|
export const filter = (pattern, options = {}) => (p) => minimatch(p, pattern, options); |
|
minimatch.filter = filter; |
|
const ext = (a, b = {}) => Object.assign({}, a, b); |
|
export const defaults = (def) => { |
|
if (!def || typeof def !== 'object' || !Object.keys(def).length) { |
|
return minimatch; |
|
} |
|
const orig = minimatch; |
|
const m = (p, pattern, options = {}) => orig(p, pattern, ext(def, options)); |
|
return Object.assign(m, { |
|
Minimatch: class Minimatch extends orig.Minimatch { |
|
constructor(pattern, options = {}) { |
|
super(pattern, ext(def, options)); |
|
} |
|
static defaults(options) { |
|
return orig.defaults(ext(def, options)).Minimatch; |
|
} |
|
}, |
|
AST: class AST extends orig.AST { |
|
|
|
constructor(type, parent, options = {}) { |
|
super(type, parent, ext(def, options)); |
|
} |
|
|
|
static fromGlob(pattern, options = {}) { |
|
return orig.AST.fromGlob(pattern, ext(def, options)); |
|
} |
|
}, |
|
unescape: (s, options = {}) => orig.unescape(s, ext(def, options)), |
|
escape: (s, options = {}) => orig.escape(s, ext(def, options)), |
|
filter: (pattern, options = {}) => orig.filter(pattern, ext(def, options)), |
|
defaults: (options) => orig.defaults(ext(def, options)), |
|
makeRe: (pattern, options = {}) => orig.makeRe(pattern, ext(def, options)), |
|
braceExpand: (pattern, options = {}) => orig.braceExpand(pattern, ext(def, options)), |
|
match: (list, pattern, options = {}) => orig.match(list, pattern, ext(def, options)), |
|
sep: orig.sep, |
|
GLOBSTAR: GLOBSTAR, |
|
}); |
|
}; |
|
minimatch.defaults = defaults; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export const braceExpand = (pattern, options = {}) => { |
|
assertValidPattern(pattern); |
|
|
|
|
|
if (options.nobrace || !/\{(?:(?!\{).)*\}/.test(pattern)) { |
|
|
|
return [pattern]; |
|
} |
|
return expand(pattern); |
|
}; |
|
minimatch.braceExpand = braceExpand; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export const makeRe = (pattern, options = {}) => new Minimatch(pattern, options).makeRe(); |
|
minimatch.makeRe = makeRe; |
|
export const match = (list, pattern, options = {}) => { |
|
const mm = new Minimatch(pattern, options); |
|
list = list.filter(f => mm.match(f)); |
|
if (mm.options.nonull && !list.length) { |
|
list.push(pattern); |
|
} |
|
return list; |
|
}; |
|
minimatch.match = match; |
|
|
|
const globMagic = /[?*]|[+@!]\(.*?\)|\[|\]/; |
|
const regExpEscape = (s) => s.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&'); |
|
export class Minimatch { |
|
options; |
|
set; |
|
pattern; |
|
windowsPathsNoEscape; |
|
nonegate; |
|
negate; |
|
comment; |
|
empty; |
|
preserveMultipleSlashes; |
|
partial; |
|
globSet; |
|
globParts; |
|
nocase; |
|
isWindows; |
|
platform; |
|
windowsNoMagicRoot; |
|
regexp; |
|
constructor(pattern, options = {}) { |
|
assertValidPattern(pattern); |
|
options = options || {}; |
|
this.options = options; |
|
this.pattern = pattern; |
|
this.platform = options.platform || defaultPlatform; |
|
this.isWindows = this.platform === 'win32'; |
|
this.windowsPathsNoEscape = |
|
!!options.windowsPathsNoEscape || options.allowWindowsEscape === false; |
|
if (this.windowsPathsNoEscape) { |
|
this.pattern = this.pattern.replace(/\\/g, '/'); |
|
} |
|
this.preserveMultipleSlashes = !!options.preserveMultipleSlashes; |
|
this.regexp = null; |
|
this.negate = false; |
|
this.nonegate = !!options.nonegate; |
|
this.comment = false; |
|
this.empty = false; |
|
this.partial = !!options.partial; |
|
this.nocase = !!this.options.nocase; |
|
this.windowsNoMagicRoot = |
|
options.windowsNoMagicRoot !== undefined |
|
? options.windowsNoMagicRoot |
|
: !!(this.isWindows && this.nocase); |
|
this.globSet = []; |
|
this.globParts = []; |
|
this.set = []; |
|
|
|
this.make(); |
|
} |
|
hasMagic() { |
|
if (this.options.magicalBraces && this.set.length > 1) { |
|
return true; |
|
} |
|
for (const pattern of this.set) { |
|
for (const part of pattern) { |
|
if (typeof part !== 'string') |
|
return true; |
|
} |
|
} |
|
return false; |
|
} |
|
debug(..._) { } |
|
make() { |
|
const pattern = this.pattern; |
|
const options = this.options; |
|
|
|
if (!options.nocomment && pattern.charAt(0) === '#') { |
|
this.comment = true; |
|
return; |
|
} |
|
if (!pattern) { |
|
this.empty = true; |
|
return; |
|
} |
|
|
|
this.parseNegate(); |
|
|
|
this.globSet = [...new Set(this.braceExpand())]; |
|
if (options.debug) { |
|
this.debug = (...args) => console.error(...args); |
|
} |
|
this.debug(this.pattern, this.globSet); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const rawGlobParts = this.globSet.map(s => this.slashSplit(s)); |
|
this.globParts = this.preprocess(rawGlobParts); |
|
this.debug(this.pattern, this.globParts); |
|
|
|
let set = this.globParts.map((s, _, __) => { |
|
if (this.isWindows && this.windowsNoMagicRoot) { |
|
|
|
const isUNC = s[0] === '' && |
|
s[1] === '' && |
|
(s[2] === '?' || !globMagic.test(s[2])) && |
|
!globMagic.test(s[3]); |
|
const isDrive = /^[a-z]:/i.test(s[0]); |
|
if (isUNC) { |
|
return [...s.slice(0, 4), ...s.slice(4).map(ss => this.parse(ss))]; |
|
} |
|
else if (isDrive) { |
|
return [s[0], ...s.slice(1).map(ss => this.parse(ss))]; |
|
} |
|
} |
|
return s.map(ss => this.parse(ss)); |
|
}); |
|
this.debug(this.pattern, set); |
|
|
|
this.set = set.filter(s => s.indexOf(false) === -1); |
|
|
|
if (this.isWindows) { |
|
for (let i = 0; i < this.set.length; i++) { |
|
const p = this.set[i]; |
|
if (p[0] === '' && |
|
p[1] === '' && |
|
this.globParts[i][2] === '?' && |
|
typeof p[3] === 'string' && |
|
/^[a-z]:$/i.test(p[3])) { |
|
p[2] = '?'; |
|
} |
|
} |
|
} |
|
this.debug(this.pattern, this.set); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
preprocess(globParts) { |
|
|
|
if (this.options.noglobstar) { |
|
for (let i = 0; i < globParts.length; i++) { |
|
for (let j = 0; j < globParts[i].length; j++) { |
|
if (globParts[i][j] === '**') { |
|
globParts[i][j] = '*'; |
|
} |
|
} |
|
} |
|
} |
|
const { optimizationLevel = 1 } = this.options; |
|
if (optimizationLevel >= 2) { |
|
|
|
globParts = this.firstPhasePreProcess(globParts); |
|
globParts = this.secondPhasePreProcess(globParts); |
|
} |
|
else if (optimizationLevel >= 1) { |
|
|
|
globParts = this.levelOneOptimize(globParts); |
|
} |
|
else { |
|
globParts = this.adjascentGlobstarOptimize(globParts); |
|
} |
|
return globParts; |
|
} |
|
|
|
adjascentGlobstarOptimize(globParts) { |
|
return globParts.map(parts => { |
|
let gs = -1; |
|
while (-1 !== (gs = parts.indexOf('**', gs + 1))) { |
|
let i = gs; |
|
while (parts[i + 1] === '**') { |
|
i++; |
|
} |
|
if (i !== gs) { |
|
parts.splice(gs, i - gs); |
|
} |
|
} |
|
return parts; |
|
}); |
|
} |
|
|
|
levelOneOptimize(globParts) { |
|
return globParts.map(parts => { |
|
parts = parts.reduce((set, part) => { |
|
const prev = set[set.length - 1]; |
|
if (part === '**' && prev === '**') { |
|
return set; |
|
} |
|
if (part === '..') { |
|
if (prev && prev !== '..' && prev !== '.' && prev !== '**') { |
|
set.pop(); |
|
return set; |
|
} |
|
} |
|
set.push(part); |
|
return set; |
|
}, []); |
|
return parts.length === 0 ? [''] : parts; |
|
}); |
|
} |
|
levelTwoFileOptimize(parts) { |
|
if (!Array.isArray(parts)) { |
|
parts = this.slashSplit(parts); |
|
} |
|
let didSomething = false; |
|
do { |
|
didSomething = false; |
|
|
|
if (!this.preserveMultipleSlashes) { |
|
for (let i = 1; i < parts.length - 1; i++) { |
|
const p = parts[i]; |
|
|
|
if (i === 1 && p === '' && parts[0] === '') |
|
continue; |
|
if (p === '.' || p === '') { |
|
didSomething = true; |
|
parts.splice(i, 1); |
|
i--; |
|
} |
|
} |
|
if (parts[0] === '.' && |
|
parts.length === 2 && |
|
(parts[1] === '.' || parts[1] === '')) { |
|
didSomething = true; |
|
parts.pop(); |
|
} |
|
} |
|
|
|
let dd = 0; |
|
while (-1 !== (dd = parts.indexOf('..', dd + 1))) { |
|
const p = parts[dd - 1]; |
|
if (p && p !== '.' && p !== '..' && p !== '**') { |
|
didSomething = true; |
|
parts.splice(dd - 1, 2); |
|
dd -= 2; |
|
} |
|
} |
|
} while (didSomething); |
|
return parts.length === 0 ? [''] : parts; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
firstPhasePreProcess(globParts) { |
|
let didSomething = false; |
|
do { |
|
didSomething = false; |
|
|
|
for (let parts of globParts) { |
|
let gs = -1; |
|
while (-1 !== (gs = parts.indexOf('**', gs + 1))) { |
|
let gss = gs; |
|
while (parts[gss + 1] === '**') { |
|
|
|
gss++; |
|
} |
|
|
|
|
|
if (gss > gs) { |
|
parts.splice(gs + 1, gss - gs); |
|
} |
|
let next = parts[gs + 1]; |
|
const p = parts[gs + 2]; |
|
const p2 = parts[gs + 3]; |
|
if (next !== '..') |
|
continue; |
|
if (!p || |
|
p === '.' || |
|
p === '..' || |
|
!p2 || |
|
p2 === '.' || |
|
p2 === '..') { |
|
continue; |
|
} |
|
didSomething = true; |
|
|
|
parts.splice(gs, 1); |
|
const other = parts.slice(0); |
|
other[gs] = '**'; |
|
globParts.push(other); |
|
gs--; |
|
} |
|
|
|
if (!this.preserveMultipleSlashes) { |
|
for (let i = 1; i < parts.length - 1; i++) { |
|
const p = parts[i]; |
|
|
|
if (i === 1 && p === '' && parts[0] === '') |
|
continue; |
|
if (p === '.' || p === '') { |
|
didSomething = true; |
|
parts.splice(i, 1); |
|
i--; |
|
} |
|
} |
|
if (parts[0] === '.' && |
|
parts.length === 2 && |
|
(parts[1] === '.' || parts[1] === '')) { |
|
didSomething = true; |
|
parts.pop(); |
|
} |
|
} |
|
|
|
let dd = 0; |
|
while (-1 !== (dd = parts.indexOf('..', dd + 1))) { |
|
const p = parts[dd - 1]; |
|
if (p && p !== '.' && p !== '..' && p !== '**') { |
|
didSomething = true; |
|
const needDot = dd === 1 && parts[dd + 1] === '**'; |
|
const splin = needDot ? ['.'] : []; |
|
parts.splice(dd - 1, 2, ...splin); |
|
if (parts.length === 0) |
|
parts.push(''); |
|
dd -= 2; |
|
} |
|
} |
|
} |
|
} while (didSomething); |
|
return globParts; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
secondPhasePreProcess(globParts) { |
|
for (let i = 0; i < globParts.length - 1; i++) { |
|
for (let j = i + 1; j < globParts.length; j++) { |
|
const matched = this.partsMatch(globParts[i], globParts[j], !this.preserveMultipleSlashes); |
|
if (!matched) |
|
continue; |
|
globParts[i] = matched; |
|
globParts[j] = []; |
|
} |
|
} |
|
return globParts.filter(gs => gs.length); |
|
} |
|
partsMatch(a, b, emptyGSMatch = false) { |
|
let ai = 0; |
|
let bi = 0; |
|
let result = []; |
|
let which = ''; |
|
while (ai < a.length && bi < b.length) { |
|
if (a[ai] === b[bi]) { |
|
result.push(which === 'b' ? b[bi] : a[ai]); |
|
ai++; |
|
bi++; |
|
} |
|
else if (emptyGSMatch && a[ai] === '**' && b[bi] === a[ai + 1]) { |
|
result.push(a[ai]); |
|
ai++; |
|
} |
|
else if (emptyGSMatch && b[bi] === '**' && a[ai] === b[bi + 1]) { |
|
result.push(b[bi]); |
|
bi++; |
|
} |
|
else if (a[ai] === '*' && |
|
b[bi] && |
|
(this.options.dot || !b[bi].startsWith('.')) && |
|
b[bi] !== '**') { |
|
if (which === 'b') |
|
return false; |
|
which = 'a'; |
|
result.push(a[ai]); |
|
ai++; |
|
bi++; |
|
} |
|
else if (b[bi] === '*' && |
|
a[ai] && |
|
(this.options.dot || !a[ai].startsWith('.')) && |
|
a[ai] !== '**') { |
|
if (which === 'a') |
|
return false; |
|
which = 'b'; |
|
result.push(b[bi]); |
|
ai++; |
|
bi++; |
|
} |
|
else { |
|
return false; |
|
} |
|
} |
|
|
|
|
|
return a.length === b.length && result; |
|
} |
|
parseNegate() { |
|
if (this.nonegate) |
|
return; |
|
const pattern = this.pattern; |
|
let negate = false; |
|
let negateOffset = 0; |
|
for (let i = 0; i < pattern.length && pattern.charAt(i) === '!'; i++) { |
|
negate = !negate; |
|
negateOffset++; |
|
} |
|
if (negateOffset) |
|
this.pattern = pattern.slice(negateOffset); |
|
this.negate = negate; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
matchOne(file, pattern, partial = false) { |
|
const options = this.options; |
|
|
|
|
|
|
|
if (this.isWindows) { |
|
const fileDrive = typeof file[0] === 'string' && /^[a-z]:$/i.test(file[0]); |
|
const fileUNC = !fileDrive && |
|
file[0] === '' && |
|
file[1] === '' && |
|
file[2] === '?' && |
|
/^[a-z]:$/i.test(file[3]); |
|
const patternDrive = typeof pattern[0] === 'string' && /^[a-z]:$/i.test(pattern[0]); |
|
const patternUNC = !patternDrive && |
|
pattern[0] === '' && |
|
pattern[1] === '' && |
|
pattern[2] === '?' && |
|
typeof pattern[3] === 'string' && |
|
/^[a-z]:$/i.test(pattern[3]); |
|
const fdi = fileUNC ? 3 : fileDrive ? 0 : undefined; |
|
const pdi = patternUNC ? 3 : patternDrive ? 0 : undefined; |
|
if (typeof fdi === 'number' && typeof pdi === 'number') { |
|
const [fd, pd] = [file[fdi], pattern[pdi]]; |
|
if (fd.toLowerCase() === pd.toLowerCase()) { |
|
pattern[pdi] = fd; |
|
if (pdi > fdi) { |
|
pattern = pattern.slice(pdi); |
|
} |
|
else if (fdi > pdi) { |
|
file = file.slice(fdi); |
|
} |
|
} |
|
} |
|
} |
|
|
|
|
|
const { optimizationLevel = 1 } = this.options; |
|
if (optimizationLevel >= 2) { |
|
file = this.levelTwoFileOptimize(file); |
|
} |
|
this.debug('matchOne', this, { file, pattern }); |
|
this.debug('matchOne', file.length, pattern.length); |
|
for (var fi = 0, pi = 0, fl = file.length, pl = pattern.length; fi < fl && pi < pl; fi++, pi++) { |
|
this.debug('matchOne loop'); |
|
var p = pattern[pi]; |
|
var f = file[fi]; |
|
this.debug(pattern, p, f); |
|
|
|
|
|
|
|
if (p === false) { |
|
return false; |
|
} |
|
|
|
if (p === GLOBSTAR) { |
|
this.debug('GLOBSTAR', [pattern, p, f]); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var fr = fi; |
|
var pr = pi + 1; |
|
if (pr === pl) { |
|
this.debug('** at the end'); |
|
|
|
|
|
|
|
|
|
|
|
|
|
for (; fi < fl; fi++) { |
|
if (file[fi] === '.' || |
|
file[fi] === '..' || |
|
(!options.dot && file[fi].charAt(0) === '.')) |
|
return false; |
|
} |
|
return true; |
|
} |
|
|
|
while (fr < fl) { |
|
var swallowee = file[fr]; |
|
this.debug('\nglobstar while', file, fr, pattern, pr, swallowee); |
|
|
|
if (this.matchOne(file.slice(fr), pattern.slice(pr), partial)) { |
|
this.debug('globstar found match!', fr, fl, swallowee); |
|
|
|
return true; |
|
} |
|
else { |
|
|
|
|
|
if (swallowee === '.' || |
|
swallowee === '..' || |
|
(!options.dot && swallowee.charAt(0) === '.')) { |
|
this.debug('dot detected!', file, fr, pattern, pr); |
|
break; |
|
} |
|
|
|
this.debug('globstar swallow a segment, and continue'); |
|
fr++; |
|
} |
|
} |
|
|
|
|
|
|
|
if (partial) { |
|
|
|
this.debug('\n>>> no match, partial?', file, fr, pattern, pr); |
|
if (fr === fl) { |
|
return true; |
|
} |
|
} |
|
|
|
return false; |
|
} |
|
|
|
|
|
|
|
let hit; |
|
if (typeof p === 'string') { |
|
hit = f === p; |
|
this.debug('string match', p, f, hit); |
|
} |
|
else { |
|
hit = p.test(f); |
|
this.debug('pattern match', p, f, hit); |
|
} |
|
if (!hit) |
|
return false; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (fi === fl && pi === pl) { |
|
|
|
|
|
return true; |
|
} |
|
else if (fi === fl) { |
|
|
|
|
|
|
|
return partial; |
|
} |
|
else if (pi === pl) { |
|
|
|
|
|
|
|
|
|
return fi === fl - 1 && file[fi] === ''; |
|
|
|
} |
|
else { |
|
|
|
throw new Error('wtf?'); |
|
} |
|
|
|
} |
|
braceExpand() { |
|
return braceExpand(this.pattern, this.options); |
|
} |
|
parse(pattern) { |
|
assertValidPattern(pattern); |
|
const options = this.options; |
|
|
|
if (pattern === '**') |
|
return GLOBSTAR; |
|
if (pattern === '') |
|
return ''; |
|
|
|
|
|
let m; |
|
let fastTest = null; |
|
if ((m = pattern.match(starRE))) { |
|
fastTest = options.dot ? starTestDot : starTest; |
|
} |
|
else if ((m = pattern.match(starDotExtRE))) { |
|
fastTest = (options.nocase |
|
? options.dot |
|
? starDotExtTestNocaseDot |
|
: starDotExtTestNocase |
|
: options.dot |
|
? starDotExtTestDot |
|
: starDotExtTest)(m[1]); |
|
} |
|
else if ((m = pattern.match(qmarksRE))) { |
|
fastTest = (options.nocase |
|
? options.dot |
|
? qmarksTestNocaseDot |
|
: qmarksTestNocase |
|
: options.dot |
|
? qmarksTestDot |
|
: qmarksTest)(m); |
|
} |
|
else if ((m = pattern.match(starDotStarRE))) { |
|
fastTest = options.dot ? starDotStarTestDot : starDotStarTest; |
|
} |
|
else if ((m = pattern.match(dotStarRE))) { |
|
fastTest = dotStarTest; |
|
} |
|
const re = AST.fromGlob(pattern, this.options).toMMPattern(); |
|
return fastTest ? Object.assign(re, { test: fastTest }) : re; |
|
} |
|
makeRe() { |
|
if (this.regexp || this.regexp === false) |
|
return this.regexp; |
|
|
|
|
|
|
|
|
|
|
|
|
|
const set = this.set; |
|
if (!set.length) { |
|
this.regexp = false; |
|
return this.regexp; |
|
} |
|
const options = this.options; |
|
const twoStar = options.noglobstar |
|
? star |
|
: options.dot |
|
? twoStarDot |
|
: twoStarNoDot; |
|
const flags = new Set(options.nocase ? ['i'] : []); |
|
|
|
|
|
|
|
|
|
|
|
|
|
let re = set |
|
.map(pattern => { |
|
const pp = pattern.map(p => { |
|
if (p instanceof RegExp) { |
|
for (const f of p.flags.split('')) |
|
flags.add(f); |
|
} |
|
return typeof p === 'string' |
|
? regExpEscape(p) |
|
: p === GLOBSTAR |
|
? GLOBSTAR |
|
: p._src; |
|
}); |
|
pp.forEach((p, i) => { |
|
const next = pp[i + 1]; |
|
const prev = pp[i - 1]; |
|
if (p !== GLOBSTAR || prev === GLOBSTAR) { |
|
return; |
|
} |
|
if (prev === undefined) { |
|
if (next !== undefined && next !== GLOBSTAR) { |
|
pp[i + 1] = '(?:\\/|' + twoStar + '\\/)?' + next; |
|
} |
|
else { |
|
pp[i] = twoStar; |
|
} |
|
} |
|
else if (next === undefined) { |
|
pp[i - 1] = prev + '(?:\\/|' + twoStar + ')?'; |
|
} |
|
else if (next !== GLOBSTAR) { |
|
pp[i - 1] = prev + '(?:\\/|\\/' + twoStar + '\\/)' + next; |
|
pp[i + 1] = GLOBSTAR; |
|
} |
|
}); |
|
return pp.filter(p => p !== GLOBSTAR).join('/'); |
|
}) |
|
.join('|'); |
|
|
|
|
|
const [open, close] = set.length > 1 ? ['(?:', ')'] : ['', '']; |
|
|
|
|
|
re = '^' + open + re + close + '$'; |
|
|
|
if (this.negate) |
|
re = '^(?!' + re + ').+$'; |
|
try { |
|
this.regexp = new RegExp(re, [...flags].join('')); |
|
|
|
} |
|
catch (ex) { |
|
|
|
this.regexp = false; |
|
} |
|
|
|
return this.regexp; |
|
} |
|
slashSplit(p) { |
|
|
|
|
|
|
|
|
|
if (this.preserveMultipleSlashes) { |
|
return p.split('/'); |
|
} |
|
else if (this.isWindows && /^\/\/[^\/]+/.test(p)) { |
|
|
|
return ['', ...p.split(/\/+/)]; |
|
} |
|
else { |
|
return p.split(/\/+/); |
|
} |
|
} |
|
match(f, partial = this.partial) { |
|
this.debug('match', f, this.pattern); |
|
|
|
|
|
if (this.comment) { |
|
return false; |
|
} |
|
if (this.empty) { |
|
return f === ''; |
|
} |
|
if (f === '/' && partial) { |
|
return true; |
|
} |
|
const options = this.options; |
|
|
|
if (this.isWindows) { |
|
f = f.split('\\').join('/'); |
|
} |
|
|
|
const ff = this.slashSplit(f); |
|
this.debug(this.pattern, 'split', ff); |
|
|
|
|
|
|
|
|
|
const set = this.set; |
|
this.debug(this.pattern, 'set', set); |
|
|
|
let filename = ff[ff.length - 1]; |
|
if (!filename) { |
|
for (let i = ff.length - 2; !filename && i >= 0; i--) { |
|
filename = ff[i]; |
|
} |
|
} |
|
for (let i = 0; i < set.length; i++) { |
|
const pattern = set[i]; |
|
let file = ff; |
|
if (options.matchBase && pattern.length === 1) { |
|
file = [filename]; |
|
} |
|
const hit = this.matchOne(file, pattern, partial); |
|
if (hit) { |
|
if (options.flipNegate) { |
|
return true; |
|
} |
|
return !this.negate; |
|
} |
|
} |
|
|
|
|
|
if (options.flipNegate) { |
|
return false; |
|
} |
|
return this.negate; |
|
} |
|
static defaults(def) { |
|
return minimatch.defaults(def).Minimatch; |
|
} |
|
} |
|
|
|
export { AST } from './ast.js'; |
|
export { escape } from './escape.js'; |
|
export { unescape } from './unescape.js'; |
|
|
|
minimatch.AST = AST; |
|
minimatch.Minimatch = Minimatch; |
|
minimatch.escape = escape; |
|
minimatch.unescape = unescape; |
|
|