minimatch 7.4.4 → 8.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/cjs/index.js CHANGED
@@ -3,13 +3,14 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.unescape = exports.escape = exports.Minimatch = exports.match = exports.makeRe = exports.braceExpand = exports.defaults = exports.filter = exports.GLOBSTAR = exports.sep = exports.minimatch = void 0;
6
+ exports.unescape = exports.escape = exports.AST = exports.Minimatch = exports.match = exports.makeRe = exports.braceExpand = exports.defaults = exports.filter = exports.GLOBSTAR = exports.sep = exports.minimatch = void 0;
7
7
  const brace_expansion_1 = __importDefault(require("brace-expansion"));
8
- const brace_expressions_js_1 = require("./brace-expressions.js");
8
+ const assert_valid_pattern_js_1 = require("./assert-valid-pattern.js");
9
+ const ast_js_1 = require("./ast.js");
9
10
  const escape_js_1 = require("./escape.js");
10
11
  const unescape_js_1 = require("./unescape.js");
11
12
  const minimatch = (p, pattern, options = {}) => {
12
- assertValidPattern(pattern);
13
+ (0, assert_valid_pattern_js_1.assertValidPattern)(pattern);
13
14
  // shortcut: comments match nothing.
14
15
  if (!options.nocomment && pattern.charAt(0) === '#') {
15
16
  return false;
@@ -85,13 +86,6 @@ exports.sep = defaultPlatform === 'win32' ? path.win32.sep : path.posix.sep;
85
86
  exports.minimatch.sep = exports.sep;
86
87
  exports.GLOBSTAR = Symbol('globstar **');
87
88
  exports.minimatch.GLOBSTAR = exports.GLOBSTAR;
88
- const plTypes = {
89
- '!': { open: '(?:(?!(?:', close: '))[^/]*?)' },
90
- '?': { open: '(?:', close: ')?' },
91
- '+': { open: '(?:', close: ')+' },
92
- '*': { open: '(?:', close: ')*' },
93
- '@': { open: '(?:', close: ')' },
94
- };
95
89
  // any single thing other than /
96
90
  // don't need to escape / when using new RegExp()
97
91
  const qmark = '[^/]';
@@ -104,15 +98,6 @@ const twoStarDot = '(?:(?!(?:\\/|^)(?:\\.{1,2})($|\\/)).)*?';
104
98
  // not a ^ or / followed by a dot,
105
99
  // followed by anything, any number of times.
106
100
  const twoStarNoDot = '(?:(?!(?:\\/|^)\\.).)*?';
107
- // "abc" -> { a:true, b:true, c:true }
108
- const charSet = (s) => s.split('').reduce((set, c) => {
109
- set[c] = true;
110
- return set;
111
- }, {});
112
- // characters that need to be escaped in RegExp.
113
- const reSpecials = charSet('().*{}+?[]^$\\!');
114
- // characters that indicate we have to add the pattern start
115
- const addPatternStartSet = charSet('[.(');
116
101
  const filter = (pattern, options = {}) => (p) => (0, exports.minimatch)(p, pattern, options);
117
102
  exports.filter = filter;
118
103
  exports.minimatch.filter = exports.filter;
@@ -132,6 +117,16 @@ const defaults = (def) => {
132
117
  return orig.defaults(ext(def, options)).Minimatch;
133
118
  }
134
119
  },
120
+ AST: class AST extends orig.AST {
121
+ /* c8 ignore start */
122
+ constructor(type, parent, options = {}) {
123
+ super(type, parent, ext(def, options));
124
+ }
125
+ /* c8 ignore stop */
126
+ static fromGlob(pattern, options = {}) {
127
+ return orig.AST.fromGlob(pattern, ext(def, options));
128
+ }
129
+ },
135
130
  unescape: (s, options = {}) => orig.unescape(s, ext(def, options)),
136
131
  escape: (s, options = {}) => orig.escape(s, ext(def, options)),
137
132
  filter: (pattern, options = {}) => orig.filter(pattern, ext(def, options)),
@@ -156,7 +151,7 @@ exports.minimatch.defaults = exports.defaults;
156
151
  // a{2..}b -> a{2..}b
157
152
  // a{b}c -> a{b}c
158
153
  const braceExpand = (pattern, options = {}) => {
159
- assertValidPattern(pattern);
154
+ (0, assert_valid_pattern_js_1.assertValidPattern)(pattern);
160
155
  // Thanks to Yeting Li <https://github.com/yetingli> for
161
156
  // improving this regexp to avoid a ReDOS vulnerability.
162
157
  if (options.nobrace || !/\{(?:(?!\{).)*\}/.test(pattern)) {
@@ -167,15 +162,6 @@ const braceExpand = (pattern, options = {}) => {
167
162
  };
168
163
  exports.braceExpand = braceExpand;
169
164
  exports.minimatch.braceExpand = exports.braceExpand;
170
- const MAX_PATTERN_LENGTH = 1024 * 64;
171
- const assertValidPattern = (pattern) => {
172
- if (typeof pattern !== 'string') {
173
- throw new TypeError('invalid pattern');
174
- }
175
- if (pattern.length > MAX_PATTERN_LENGTH) {
176
- throw new TypeError('pattern is too long');
177
- }
178
- };
179
165
  // parse a component of the expanded set.
180
166
  // At this point, no pattern may contain "/" in it
181
167
  // so we're going to return a 2d array, where each entry is the full
@@ -201,7 +187,6 @@ const match = (list, pattern, options = {}) => {
201
187
  exports.match = match;
202
188
  exports.minimatch.match = exports.match;
203
189
  // replace stuff like \* with *
204
- const globUnescape = (s) => s.replace(/\\(.)/g, '$1');
205
190
  const globMagic = /[?*]|[+@!]\(.*?\)|\[|\]/;
206
191
  const regExpEscape = (s) => s.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&');
207
192
  class Minimatch {
@@ -223,7 +208,7 @@ class Minimatch {
223
208
  windowsNoMagicRoot;
224
209
  regexp;
225
210
  constructor(pattern, options = {}) {
226
- assertValidPattern(pattern);
211
+ (0, assert_valid_pattern_js_1.assertValidPattern)(pattern);
227
212
  options = options || {};
228
213
  this.options = options;
229
214
  this.pattern = pattern;
@@ -814,7 +799,7 @@ class Minimatch {
814
799
  return (0, exports.braceExpand)(this.pattern, this.options);
815
800
  }
816
801
  parse(pattern) {
817
- assertValidPattern(pattern);
802
+ (0, assert_valid_pattern_js_1.assertValidPattern)(pattern);
818
803
  const options = this.options;
819
804
  // shortcuts
820
805
  if (pattern === '**')
@@ -852,293 +837,8 @@ class Minimatch {
852
837
  else if ((m = pattern.match(dotStarRE))) {
853
838
  fastTest = dotStarTest;
854
839
  }
855
- let re = '';
856
- let hasMagic = false;
857
- let escaping = false;
858
- // ? => one single character
859
- const patternListStack = [];
860
- const negativeLists = [];
861
- let stateChar = false;
862
- let uflag = false;
863
- let pl;
864
- // . and .. never match anything that doesn't start with .,
865
- // even when options.dot is set. However, if the pattern
866
- // starts with ., then traversal patterns can match.
867
- let dotTravAllowed = pattern.charAt(0) === '.';
868
- let dotFileAllowed = options.dot || dotTravAllowed;
869
- const patternStart = () => dotTravAllowed
870
- ? ''
871
- : dotFileAllowed
872
- ? '(?!(?:^|\\/)\\.{1,2}(?:$|\\/))'
873
- : '(?!\\.)';
874
- const subPatternStart = (p) => p.charAt(0) === '.'
875
- ? ''
876
- : options.dot
877
- ? '(?!(?:^|\\/)\\.{1,2}(?:$|\\/))'
878
- : '(?!\\.)';
879
- const clearStateChar = () => {
880
- if (stateChar) {
881
- // we had some state-tracking character
882
- // that wasn't consumed by this pass.
883
- switch (stateChar) {
884
- case '*':
885
- re += star;
886
- hasMagic = true;
887
- break;
888
- case '?':
889
- re += qmark;
890
- hasMagic = true;
891
- break;
892
- default:
893
- re += '\\' + stateChar;
894
- break;
895
- }
896
- this.debug('clearStateChar %j %j', stateChar, re);
897
- stateChar = false;
898
- }
899
- };
900
- for (let i = 0, c; i < pattern.length && (c = pattern.charAt(i)); i++) {
901
- this.debug('%s\t%s %s %j', pattern, i, re, c);
902
- // skip over any that are escaped.
903
- if (escaping) {
904
- // completely not allowed, even escaped.
905
- // should be impossible.
906
- /* c8 ignore start */
907
- if (c === '/') {
908
- return false;
909
- }
910
- /* c8 ignore stop */
911
- if (reSpecials[c]) {
912
- re += '\\';
913
- }
914
- re += c;
915
- escaping = false;
916
- continue;
917
- }
918
- switch (c) {
919
- // Should already be path-split by now.
920
- /* c8 ignore start */
921
- case '/': {
922
- return false;
923
- }
924
- /* c8 ignore stop */
925
- case '\\':
926
- clearStateChar();
927
- escaping = true;
928
- continue;
929
- // the various stateChar values
930
- // for the "extglob" stuff.
931
- case '?':
932
- case '*':
933
- case '+':
934
- case '@':
935
- case '!':
936
- this.debug('%s\t%s %s %j <-- stateChar', pattern, i, re, c);
937
- // if we already have a stateChar, then it means
938
- // that there was something like ** or +? in there.
939
- // Handle the stateChar, then proceed with this one.
940
- this.debug('call clearStateChar %j', stateChar);
941
- clearStateChar();
942
- stateChar = c;
943
- // if extglob is disabled, then +(asdf|foo) isn't a thing.
944
- // just clear the statechar *now*, rather than even diving into
945
- // the patternList stuff.
946
- if (options.noext)
947
- clearStateChar();
948
- continue;
949
- case '(': {
950
- if (!stateChar) {
951
- re += '\\(';
952
- continue;
953
- }
954
- const plEntry = {
955
- type: stateChar,
956
- start: i - 1,
957
- reStart: re.length,
958
- open: plTypes[stateChar].open,
959
- close: plTypes[stateChar].close,
960
- };
961
- this.debug(this.pattern, '\t', plEntry);
962
- patternListStack.push(plEntry);
963
- // negation is (?:(?!(?:js)(?:<rest>))[^/]*)
964
- re += plEntry.open;
965
- // next entry starts with a dot maybe?
966
- if (plEntry.start === 0 && plEntry.type !== '!') {
967
- dotTravAllowed = true;
968
- re += subPatternStart(pattern.slice(i + 1));
969
- }
970
- this.debug('plType %j %j', stateChar, re);
971
- stateChar = false;
972
- continue;
973
- }
974
- case ')': {
975
- const plEntry = patternListStack[patternListStack.length - 1];
976
- if (!plEntry) {
977
- re += '\\)';
978
- continue;
979
- }
980
- patternListStack.pop();
981
- // closing an extglob
982
- clearStateChar();
983
- hasMagic = true;
984
- pl = plEntry;
985
- // negation is (?:(?!js)[^/]*)
986
- // The others are (?:<pattern>)<type>
987
- re += pl.close;
988
- if (pl.type === '!') {
989
- negativeLists.push(Object.assign(pl, { reEnd: re.length }));
990
- }
991
- continue;
992
- }
993
- case '|': {
994
- const plEntry = patternListStack[patternListStack.length - 1];
995
- if (!plEntry) {
996
- re += '\\|';
997
- continue;
998
- }
999
- clearStateChar();
1000
- re += '|';
1001
- // next subpattern can start with a dot?
1002
- if (plEntry.start === 0 && plEntry.type !== '!') {
1003
- dotTravAllowed = true;
1004
- re += subPatternStart(pattern.slice(i + 1));
1005
- }
1006
- continue;
1007
- }
1008
- // these are mostly the same in regexp and glob
1009
- case '[':
1010
- // swallow any state-tracking char before the [
1011
- clearStateChar();
1012
- const [src, needUflag, consumed, magic] = (0, brace_expressions_js_1.parseClass)(pattern, i);
1013
- if (consumed) {
1014
- re += src;
1015
- uflag = uflag || needUflag;
1016
- i += consumed - 1;
1017
- hasMagic = hasMagic || magic;
1018
- }
1019
- else {
1020
- re += '\\[';
1021
- }
1022
- continue;
1023
- case ']':
1024
- re += '\\' + c;
1025
- continue;
1026
- default:
1027
- // swallow any state char that wasn't consumed
1028
- clearStateChar();
1029
- re += regExpEscape(c);
1030
- break;
1031
- } // switch
1032
- } // for
1033
- // handle the case where we had a +( thing at the *end*
1034
- // of the pattern.
1035
- // each pattern list stack adds 3 chars, and we need to go through
1036
- // and escape any | chars that were passed through as-is for the regexp.
1037
- // Go through and escape them, taking care not to double-escape any
1038
- // | chars that were already escaped.
1039
- for (pl = patternListStack.pop(); pl; pl = patternListStack.pop()) {
1040
- let tail;
1041
- tail = re.slice(pl.reStart + pl.open.length);
1042
- this.debug(this.pattern, 'setting tail', re, pl);
1043
- // maybe some even number of \, then maybe 1 \, followed by a |
1044
- tail = tail.replace(/((?:\\{2}){0,64})(\\?)\|/g, (_, $1, $2) => {
1045
- if (!$2) {
1046
- // the | isn't already escaped, so escape it.
1047
- $2 = '\\';
1048
- // should already be done
1049
- /* c8 ignore start */
1050
- }
1051
- /* c8 ignore stop */
1052
- // need to escape all those slashes *again*, without escaping the
1053
- // one that we need for escaping the | character. As it works out,
1054
- // escaping an even number of slashes can be done by simply repeating
1055
- // it exactly after itself. That's why this trick works.
1056
- //
1057
- // I am sorry that you have to see this.
1058
- return $1 + $1 + $2 + '|';
1059
- });
1060
- this.debug('tail=%j\n %s', tail, tail, pl, re);
1061
- const t = pl.type === '*' ? star : pl.type === '?' ? qmark : '\\' + pl.type;
1062
- hasMagic = true;
1063
- re = re.slice(0, pl.reStart) + t + '\\(' + tail;
1064
- }
1065
- // handle trailing things that only matter at the very end.
1066
- clearStateChar();
1067
- if (escaping) {
1068
- // trailing \\
1069
- re += '\\\\';
1070
- }
1071
- // only need to apply the nodot start if the re starts with
1072
- // something that could conceivably capture a dot
1073
- const addPatternStart = addPatternStartSet[re.charAt(0)];
1074
- // Hack to work around lack of negative lookbehind in JS
1075
- // A pattern like: *.!(x).!(y|z) needs to ensure that a name
1076
- // like 'a.xyz.yz' doesn't match. So, the first negative
1077
- // lookahead, has to look ALL the way ahead, to the end of
1078
- // the pattern.
1079
- for (let n = negativeLists.length - 1; n > -1; n--) {
1080
- const nl = negativeLists[n];
1081
- const nlBefore = re.slice(0, nl.reStart);
1082
- const nlFirst = re.slice(nl.reStart, nl.reEnd - 8);
1083
- let nlAfter = re.slice(nl.reEnd);
1084
- const nlLast = re.slice(nl.reEnd - 8, nl.reEnd) + nlAfter;
1085
- // Handle nested stuff like *(*.js|!(*.json)), where open parens
1086
- // mean that we should *not* include the ) in the bit that is considered
1087
- // "after" the negated section.
1088
- const closeParensBefore = nlBefore.split(')').length;
1089
- const openParensBefore = nlBefore.split('(').length - closeParensBefore;
1090
- let cleanAfter = nlAfter;
1091
- for (let i = 0; i < openParensBefore; i++) {
1092
- cleanAfter = cleanAfter.replace(/\)[+*?]?/, '');
1093
- }
1094
- nlAfter = cleanAfter;
1095
- const dollar = nlAfter === '' ? '(?:$|\\/)' : '';
1096
- re = nlBefore + nlFirst + nlAfter + dollar + nlLast;
1097
- }
1098
- // if the re is not "" at this point, then we need to make sure
1099
- // it doesn't match against an empty path part.
1100
- // Otherwise a/* will match a/, which it should not.
1101
- if (re !== '' && hasMagic) {
1102
- re = '(?=.)' + re;
1103
- }
1104
- if (addPatternStart) {
1105
- re = patternStart() + re;
1106
- }
1107
- // if it's nocase, and the lcase/uppercase don't match, it's magic
1108
- if (options.nocase && !hasMagic && !options.nocaseMagicOnly) {
1109
- hasMagic = pattern.toUpperCase() !== pattern.toLowerCase();
1110
- }
1111
- // skip the regexp for non-magical patterns
1112
- // unescape anything in it, though, so that it'll be
1113
- // an exact match against a file etc.
1114
- if (!hasMagic) {
1115
- return globUnescape(re);
1116
- }
1117
- const flags = (options.nocase ? 'i' : '') + (uflag ? 'u' : '');
1118
- try {
1119
- const ext = fastTest
1120
- ? {
1121
- _glob: pattern,
1122
- _src: re,
1123
- test: fastTest,
1124
- }
1125
- : {
1126
- _glob: pattern,
1127
- _src: re,
1128
- };
1129
- return Object.assign(new RegExp('^' + re + '$', flags), ext);
1130
- /* c8 ignore start */
1131
- }
1132
- catch (er) {
1133
- // should be impossible
1134
- // If it was an invalid regular expression, then it can't match
1135
- // anything. This trick looks for a character after the end of
1136
- // the string, which is of course impossible, except in multi-line
1137
- // mode, but it's not a /m regex.
1138
- this.debug('invalid regexp', er);
1139
- return new RegExp('$.');
1140
- }
1141
- /* c8 ignore stop */
840
+ const re = ast_js_1.AST.fromGlob(pattern, this.options).toMMPattern();
841
+ return fastTest ? Object.assign(re, { test: fastTest }) : re;
1142
842
  }
1143
843
  makeRe() {
1144
844
  if (this.regexp || this.regexp === false)
@@ -1160,7 +860,7 @@ class Minimatch {
1160
860
  : options.dot
1161
861
  ? twoStarDot
1162
862
  : twoStarNoDot;
1163
- const flags = options.nocase ? 'i' : '';
863
+ const flags = new Set(options.nocase ? ['i'] : []);
1164
864
  // regexpify non-globstar patterns
1165
865
  // if ** is only item, then we just do one twoStar
1166
866
  // if ** is first, and there are more, prepend (\/|twoStar\/)? to next
@@ -1169,11 +869,17 @@ class Minimatch {
1169
869
  // then filter out GLOBSTAR symbols
1170
870
  let re = set
1171
871
  .map(pattern => {
1172
- const pp = pattern.map(p => typeof p === 'string'
1173
- ? regExpEscape(p)
1174
- : p === exports.GLOBSTAR
1175
- ? exports.GLOBSTAR
1176
- : p._src);
872
+ const pp = pattern.map(p => {
873
+ if (p instanceof RegExp) {
874
+ for (const f of p.flags.split(''))
875
+ flags.add(f);
876
+ }
877
+ return typeof p === 'string'
878
+ ? regExpEscape(p)
879
+ : p === exports.GLOBSTAR
880
+ ? exports.GLOBSTAR
881
+ : p._src;
882
+ });
1177
883
  pp.forEach((p, i) => {
1178
884
  const next = pp[i + 1];
1179
885
  const prev = pp[i - 1];
@@ -1199,14 +905,17 @@ class Minimatch {
1199
905
  return pp.filter(p => p !== exports.GLOBSTAR).join('/');
1200
906
  })
1201
907
  .join('|');
908
+ // need to wrap in parens if we had more than one thing with |,
909
+ // otherwise only the first will be anchored to ^ and the last to $
910
+ const [open, close] = set.length > 1 ? ['(?:', ')'] : ['', ''];
1202
911
  // must match entire pattern
1203
912
  // ending in a * or ** will make it less strict.
1204
- re = '^(?:' + re + ')$';
913
+ re = '^' + open + re + close + '$';
1205
914
  // can match anything, as long as it's not this.
1206
915
  if (this.negate)
1207
- re = '^(?!' + re + ').*$';
916
+ re = '^(?!' + re + ').+$';
1208
917
  try {
1209
- this.regexp = new RegExp(re, flags);
918
+ this.regexp = new RegExp(re, [...flags].join(''));
1210
919
  /* c8 ignore start */
1211
920
  }
1212
921
  catch (ex) {
@@ -1293,11 +1002,14 @@ class Minimatch {
1293
1002
  }
1294
1003
  exports.Minimatch = Minimatch;
1295
1004
  /* c8 ignore start */
1005
+ var ast_js_2 = require("./ast.js");
1006
+ Object.defineProperty(exports, "AST", { enumerable: true, get: function () { return ast_js_2.AST; } });
1296
1007
  var escape_js_2 = require("./escape.js");
1297
1008
  Object.defineProperty(exports, "escape", { enumerable: true, get: function () { return escape_js_2.escape; } });
1298
1009
  var unescape_js_2 = require("./unescape.js");
1299
1010
  Object.defineProperty(exports, "unescape", { enumerable: true, get: function () { return unescape_js_2.unescape; } });
1300
1011
  /* c8 ignore stop */
1012
+ exports.minimatch.AST = ast_js_1.AST;
1301
1013
  exports.minimatch.Minimatch = Minimatch;
1302
1014
  exports.minimatch.escape = escape_js_1.escape;
1303
1015
  exports.minimatch.unescape = unescape_js_1.unescape;