minimatch
Advanced tools
Comparing version 7.3.0 to 7.4.0
@@ -1,1 +0,7 @@ | ||
export declare const parseClass: (glob: string, position: number) => [string, boolean, number]; | ||
export type ParseClassResult = [ | ||
src: string, | ||
uFlag: boolean, | ||
consumed: number, | ||
hasMagic: boolean | ||
]; | ||
export declare const parseClass: (glob: string, position: number) => ParseClassResult; |
@@ -24,8 +24,8 @@ "use strict"; | ||
// only need to escape a few things inside of brace expressions | ||
const regExpEscape = (s) => s.replace(/[[\]\\-]/g, '\\$&'); | ||
const rangesToString = (ranges) => { | ||
return (ranges | ||
// .map(r => r.replace(/[[\]]/g, '\\$&').replace(/^-/, '\\-')) | ||
.join('')); | ||
}; | ||
// escapes: [ \ ] - | ||
const braceEscape = (s) => s.replace(/[[\]\\-]/g, '\\$&'); | ||
// escape all regexp magic characters | ||
const regexpEscape = (s) => s.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&'); | ||
// everything has already been escaped, we just have to join | ||
const rangesToString = (ranges) => ranges.join(''); | ||
// takes a glob string at a posix brace expression, and returns | ||
@@ -79,3 +79,3 @@ // an equivalent regular expression source, and boolean indicating | ||
if (rangeStart) { | ||
return ['$.', false, glob.length - pos]; | ||
return ['$.', false, glob.length - pos, true]; | ||
} | ||
@@ -98,6 +98,6 @@ i += cls.length; | ||
if (c > rangeStart) { | ||
ranges.push(regExpEscape(rangeStart) + '-' + regExpEscape(c)); | ||
ranges.push(braceEscape(rangeStart) + '-' + braceEscape(c)); | ||
} | ||
else if (c === rangeStart) { | ||
ranges.push(regExpEscape(c)); | ||
ranges.push(braceEscape(c)); | ||
} | ||
@@ -111,3 +111,3 @@ rangeStart = ''; | ||
if (glob.startsWith('-]', i + 1)) { | ||
ranges.push(regExpEscape(c + '-')); | ||
ranges.push(braceEscape(c + '-')); | ||
i += 2; | ||
@@ -122,3 +122,3 @@ continue; | ||
// not the start of a range, just a single character | ||
ranges.push(regExpEscape(c)); | ||
ranges.push(braceEscape(c)); | ||
i++; | ||
@@ -129,3 +129,3 @@ } | ||
// but might still be valid as a literal match. | ||
return ['', false, 0]; | ||
return ['', false, 0, false]; | ||
} | ||
@@ -135,4 +135,15 @@ // if we got no ranges and no negates, then we have a range that | ||
if (!ranges.length && !negs.length) { | ||
return ['$.', false, glob.length - pos]; | ||
return ['$.', false, glob.length - pos, true]; | ||
} | ||
// if we got one positive range, and it's a single character, then that's | ||
// not actually a magic pattern, it's just that one literal character. | ||
// we should not treat that as "magic", we should just return the literal | ||
// character. [_] is a perfectly valid way to escape glob magic chars. | ||
if (negs.length === 0 && | ||
ranges.length === 1 && | ||
/^\\?.$/.test(ranges[0]) && | ||
!negate) { | ||
const r = ranges[0].length === 2 ? ranges[0].slice(-1) : ranges[0]; | ||
return [regexpEscape(r), false, endPos - pos, false]; | ||
} | ||
const sranges = '[' + (negate ? '^' : '') + rangesToString(ranges) + ']'; | ||
@@ -145,5 +156,5 @@ const snegs = '[' + (negate ? '' : '^') + rangesToString(negs) + ']'; | ||
: snegs; | ||
return [comb, uflag, endPos - pos]; | ||
return [comb, uflag, endPos - pos, true]; | ||
}; | ||
exports.parseClass = parseClass; | ||
//# sourceMappingURL=brace-expressions.js.map |
@@ -11,2 +11,4 @@ declare const _default: { | ||
Minimatch: typeof import("./index.js").Minimatch; | ||
escape: (s: string, { windowsPathsNoEscape, }?: Pick<import("./index.js").MinimatchOptions, "windowsPathsNoEscape">) => string; | ||
unescape: (s: string, { windowsPathsNoEscape, }?: Pick<import("./index.js").MinimatchOptions, "windowsPathsNoEscape">) => string; | ||
} & { | ||
@@ -23,2 +25,4 @@ default: { | ||
Minimatch: typeof import("./index.js").Minimatch; | ||
escape: (s: string, { windowsPathsNoEscape, }?: Pick<import("./index.js").MinimatchOptions, "windowsPathsNoEscape">) => string; | ||
unescape: (s: string, { windowsPathsNoEscape, }?: Pick<import("./index.js").MinimatchOptions, "windowsPathsNoEscape">) => string; | ||
}; | ||
@@ -35,4 +39,6 @@ minimatch: { | ||
Minimatch: typeof import("./index.js").Minimatch; | ||
escape: (s: string, { windowsPathsNoEscape, }?: Pick<import("./index.js").MinimatchOptions, "windowsPathsNoEscape">) => string; | ||
unescape: (s: string, { windowsPathsNoEscape, }?: Pick<import("./index.js").MinimatchOptions, "windowsPathsNoEscape">) => string; | ||
}; | ||
}; | ||
export = _default; |
@@ -15,2 +15,3 @@ export interface MinimatchOptions { | ||
nocaseMagicOnly?: boolean; | ||
magicalBraces?: boolean; | ||
matchBase?: boolean; | ||
@@ -33,2 +34,4 @@ flipNegate?: boolean; | ||
Minimatch: typeof Minimatch; | ||
escape: (s: string, { windowsPathsNoEscape, }?: Pick<MinimatchOptions, "windowsPathsNoEscape">) => string; | ||
unescape: (s: string, { windowsPathsNoEscape, }?: Pick<MinimatchOptions, "windowsPathsNoEscape">) => string; | ||
}; | ||
@@ -70,2 +73,3 @@ export default minimatch; | ||
constructor(pattern: string, options?: MinimatchOptions); | ||
hasMagic(): boolean; | ||
debug(..._: any[]): void; | ||
@@ -89,1 +93,3 @@ make(): void; | ||
} | ||
export { escape } from './escape.js'; | ||
export { unescape } from './unescape.js'; |
@@ -6,5 +6,7 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.Minimatch = exports.match = exports.makeRe = exports.braceExpand = exports.defaults = exports.filter = exports.GLOBSTAR = exports.sep = exports.minimatch = void 0; | ||
exports.unescape = exports.escape = exports.Minimatch = exports.match = exports.makeRe = exports.braceExpand = exports.defaults = exports.filter = exports.GLOBSTAR = exports.sep = exports.minimatch = void 0; | ||
const brace_expansion_1 = __importDefault(require("brace-expansion")); | ||
const brace_expressions_js_1 = require("./brace-expressions.js"); | ||
const escape_js_1 = require("./escape.js"); | ||
const unescape_js_1 = require("./unescape.js"); | ||
const minimatch = (p, pattern, options = {}) => { | ||
@@ -134,2 +136,4 @@ assertValidPattern(pattern); | ||
}, | ||
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)), | ||
@@ -253,2 +257,14 @@ defaults: (options) => orig.defaults(ext(def, options)), | ||
} | ||
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(..._) { } | ||
@@ -999,3 +1015,3 @@ make() { | ||
clearStateChar(); | ||
const [src, needUflag, consumed] = (0, brace_expressions_js_1.parseClass)(pattern, i); | ||
const [src, needUflag, consumed, magic] = (0, brace_expressions_js_1.parseClass)(pattern, i); | ||
if (consumed) { | ||
@@ -1005,3 +1021,3 @@ re += src; | ||
i += consumed - 1; | ||
hasMagic = true; | ||
hasMagic = hasMagic || magic; | ||
} | ||
@@ -1104,3 +1120,3 @@ else { | ||
if (!hasMagic) { | ||
return globUnescape(pattern); | ||
return globUnescape(re); | ||
} | ||
@@ -1285,3 +1301,11 @@ const flags = (options.nocase ? 'i' : '') + (uflag ? 'u' : ''); | ||
exports.Minimatch = Minimatch; | ||
/* c8 ignore start */ | ||
var escape_js_2 = require("./escape.js"); | ||
Object.defineProperty(exports, "escape", { enumerable: true, get: function () { return escape_js_2.escape; } }); | ||
var unescape_js_2 = require("./unescape.js"); | ||
Object.defineProperty(exports, "unescape", { enumerable: true, get: function () { return unescape_js_2.unescape; } }); | ||
/* c8 ignore stop */ | ||
exports.minimatch.Minimatch = Minimatch; | ||
exports.minimatch.escape = escape_js_1.escape; | ||
exports.minimatch.unescape = unescape_js_1.unescape; | ||
//# sourceMappingURL=index.js.map |
@@ -1,1 +0,7 @@ | ||
export declare const parseClass: (glob: string, position: number) => [string, boolean, number]; | ||
export type ParseClassResult = [ | ||
src: string, | ||
uFlag: boolean, | ||
consumed: number, | ||
hasMagic: boolean | ||
]; | ||
export declare const parseClass: (glob: string, position: number) => ParseClassResult; |
@@ -21,8 +21,8 @@ // translate the various posix character classes into unicode properties | ||
// only need to escape a few things inside of brace expressions | ||
const regExpEscape = (s) => s.replace(/[[\]\\-]/g, '\\$&'); | ||
const rangesToString = (ranges) => { | ||
return (ranges | ||
// .map(r => r.replace(/[[\]]/g, '\\$&').replace(/^-/, '\\-')) | ||
.join('')); | ||
}; | ||
// escapes: [ \ ] - | ||
const braceEscape = (s) => s.replace(/[[\]\\-]/g, '\\$&'); | ||
// escape all regexp magic characters | ||
const regexpEscape = (s) => s.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&'); | ||
// everything has already been escaped, we just have to join | ||
const rangesToString = (ranges) => ranges.join(''); | ||
// takes a glob string at a posix brace expression, and returns | ||
@@ -76,3 +76,3 @@ // an equivalent regular expression source, and boolean indicating | ||
if (rangeStart) { | ||
return ['$.', false, glob.length - pos]; | ||
return ['$.', false, glob.length - pos, true]; | ||
} | ||
@@ -95,6 +95,6 @@ i += cls.length; | ||
if (c > rangeStart) { | ||
ranges.push(regExpEscape(rangeStart) + '-' + regExpEscape(c)); | ||
ranges.push(braceEscape(rangeStart) + '-' + braceEscape(c)); | ||
} | ||
else if (c === rangeStart) { | ||
ranges.push(regExpEscape(c)); | ||
ranges.push(braceEscape(c)); | ||
} | ||
@@ -108,3 +108,3 @@ rangeStart = ''; | ||
if (glob.startsWith('-]', i + 1)) { | ||
ranges.push(regExpEscape(c + '-')); | ||
ranges.push(braceEscape(c + '-')); | ||
i += 2; | ||
@@ -119,3 +119,3 @@ continue; | ||
// not the start of a range, just a single character | ||
ranges.push(regExpEscape(c)); | ||
ranges.push(braceEscape(c)); | ||
i++; | ||
@@ -126,3 +126,3 @@ } | ||
// but might still be valid as a literal match. | ||
return ['', false, 0]; | ||
return ['', false, 0, false]; | ||
} | ||
@@ -132,4 +132,15 @@ // if we got no ranges and no negates, then we have a range that | ||
if (!ranges.length && !negs.length) { | ||
return ['$.', false, glob.length - pos]; | ||
return ['$.', false, glob.length - pos, true]; | ||
} | ||
// if we got one positive range, and it's a single character, then that's | ||
// not actually a magic pattern, it's just that one literal character. | ||
// we should not treat that as "magic", we should just return the literal | ||
// character. [_] is a perfectly valid way to escape glob magic chars. | ||
if (negs.length === 0 && | ||
ranges.length === 1 && | ||
/^\\?.$/.test(ranges[0]) && | ||
!negate) { | ||
const r = ranges[0].length === 2 ? ranges[0].slice(-1) : ranges[0]; | ||
return [regexpEscape(r), false, endPos - pos, false]; | ||
} | ||
const sranges = '[' + (negate ? '^' : '') + rangesToString(ranges) + ']'; | ||
@@ -142,4 +153,4 @@ const snegs = '[' + (negate ? '' : '^') + rangesToString(negs) + ']'; | ||
: snegs; | ||
return [comb, uflag, endPos - pos]; | ||
return [comb, uflag, endPos - pos, true]; | ||
}; | ||
//# sourceMappingURL=brace-expressions.js.map |
@@ -15,2 +15,3 @@ export interface MinimatchOptions { | ||
nocaseMagicOnly?: boolean; | ||
magicalBraces?: boolean; | ||
matchBase?: boolean; | ||
@@ -33,2 +34,4 @@ flipNegate?: boolean; | ||
Minimatch: typeof Minimatch; | ||
escape: (s: string, { windowsPathsNoEscape, }?: Pick<MinimatchOptions, "windowsPathsNoEscape">) => string; | ||
unescape: (s: string, { windowsPathsNoEscape, }?: Pick<MinimatchOptions, "windowsPathsNoEscape">) => string; | ||
}; | ||
@@ -70,2 +73,3 @@ export default minimatch; | ||
constructor(pattern: string, options?: MinimatchOptions); | ||
hasMagic(): boolean; | ||
debug(..._: any[]): void; | ||
@@ -89,1 +93,3 @@ make(): void; | ||
} | ||
export { escape } from './escape.js'; | ||
export { unescape } from './unescape.js'; |
import expand from 'brace-expansion'; | ||
import { parseClass } from './brace-expressions.js'; | ||
import { escape } from './escape.js'; | ||
import { unescape } from './unescape.js'; | ||
export const minimatch = (p, pattern, options = {}) => { | ||
@@ -125,2 +127,4 @@ assertValidPattern(pattern); | ||
}, | ||
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)), | ||
@@ -240,2 +244,14 @@ defaults: (options) => orig.defaults(ext(def, options)), | ||
} | ||
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(..._) { } | ||
@@ -986,3 +1002,3 @@ make() { | ||
clearStateChar(); | ||
const [src, needUflag, consumed] = parseClass(pattern, i); | ||
const [src, needUflag, consumed, magic] = parseClass(pattern, i); | ||
if (consumed) { | ||
@@ -992,3 +1008,3 @@ re += src; | ||
i += consumed - 1; | ||
hasMagic = true; | ||
hasMagic = hasMagic || magic; | ||
} | ||
@@ -1091,3 +1107,3 @@ else { | ||
if (!hasMagic) { | ||
return globUnescape(pattern); | ||
return globUnescape(re); | ||
} | ||
@@ -1271,3 +1287,9 @@ const flags = (options.nocase ? 'i' : '') + (uflag ? 'u' : ''); | ||
} | ||
/* c8 ignore start */ | ||
export { escape } from './escape.js'; | ||
export { unescape } from './unescape.js'; | ||
/* c8 ignore stop */ | ||
minimatch.Minimatch = Minimatch; | ||
minimatch.escape = escape; | ||
minimatch.unescape = unescape; | ||
//# sourceMappingURL=index.js.map |
@@ -5,3 +5,3 @@ { | ||
"description": "a glob matcher in javascript", | ||
"version": "7.3.0", | ||
"version": "7.4.0", | ||
"repository": { | ||
@@ -8,0 +8,0 @@ "type": "git", |
@@ -125,3 +125,3 @@ # minimatch | ||
- `makeRe` Generate the `regexp` member if necessary, and return it. | ||
- `makeRe()` Generate the `regexp` member if necessary, and return it. | ||
Will return `false` if the pattern is invalid. | ||
@@ -134,3 +134,18 @@ - `match(fname)` Return true if the filename matches the pattern, or | ||
used by a glob-walker that needs to avoid excessive filesystem calls. | ||
- `hasMagic()` Returns true if the parsed pattern contains any | ||
magic characters. Returns false if all comparator parts are | ||
string literals. If the `magicalBraces` option is set on the | ||
constructor, then it will consider brace expansions which are | ||
not otherwise magical to be magic. If not set, then a pattern | ||
like `a{b,c}d` will return `false`, because neither `abd` nor | ||
`acd` contain any special glob characters. | ||
This does **not** mean that the pattern string can be used as a | ||
literal filename, as it may contain magic glob characters that | ||
are escaped. For example, the pattern `\\*` or `[*]` would not | ||
be considered to have magic, as the matching portion parses to | ||
the literal string `'*'` and would match a path named `'*'`, | ||
not `'\\*'` or `'[*]'`. The `minimatch.unescape()` method may | ||
be used to remove escape characters. | ||
All other methods are internal, and will be called as necessary. | ||
@@ -155,2 +170,30 @@ | ||
### minimatch.escape(pattern, options = {}) | ||
Escape all magic characters in a glob pattern, so that it will | ||
only ever match literal strings | ||
If the `windowsPathsNoEscape` option is used, then characters are | ||
escaped by wrapping in `[]`, because a magic character wrapped in | ||
a character class can only be satisfied by that exact character. | ||
Slashes (and backslashes in `windowsPathsNoEscape` mode) cannot | ||
be escaped or unescaped. | ||
### minimatch.unescape(pattern, options = {}) | ||
Un-escape a glob string that may contain some escaped characters. | ||
If the `windowsPathsNoEscape` option is used, then square-brace | ||
escapes are removed, but not backslash escapes. For example, it | ||
will turn the string `'[*]'` into `*`, but it will not turn | ||
`'\\*'` into `'*'`, becuase `\` is a path separator in | ||
`windowsPathsNoEscape` mode. | ||
When `windowsPathsNoEscape` is not set, then both brace escapes | ||
and backslash escapes are removed. | ||
Slashes (and backslashes in `windowsPathsNoEscape` mode) cannot | ||
be escaped or unescaped. | ||
### minimatch.match(list, pattern, options) | ||
@@ -218,2 +261,12 @@ | ||
### magicalBraces | ||
This only affects the results of the `Minimatch.hasMagic` method. | ||
If the pattern contains brace expansions, such as `a{b,c}d`, but | ||
no other magic characters, then the `Minipass.hasMagic()` method | ||
will return `false` by default. When this option set, it will | ||
return `true` for brace expansion as well as other magic glob | ||
characters. | ||
### matchBase | ||
@@ -220,0 +273,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
247996
32
3266
460