@herb-tools/linter 0.8.6 → 0.8.7

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/index.cjs CHANGED
@@ -1,6 +1,7 @@
1
1
  'use strict';
2
2
 
3
3
  var core = require('@herb-tools/core');
4
+ var picomatch = require('picomatch');
4
5
 
5
6
  class PrintContext {
6
7
  output = "";
@@ -671,2061 +672,6 @@ class ERBToRubyStringPrinter extends IdentityPrinter {
671
672
  }
672
673
  }
673
674
 
674
- const balanced = (a, b, str) => {
675
- const ma = a instanceof RegExp ? maybeMatch(a, str) : a;
676
- const mb = b instanceof RegExp ? maybeMatch(b, str) : b;
677
- const r = ma !== null && mb != null && range(ma, mb, str);
678
- return (r && {
679
- start: r[0],
680
- end: r[1],
681
- pre: str.slice(0, r[0]),
682
- body: str.slice(r[0] + ma.length, r[1]),
683
- post: str.slice(r[1] + mb.length),
684
- });
685
- };
686
- const maybeMatch = (reg, str) => {
687
- const m = str.match(reg);
688
- return m ? m[0] : null;
689
- };
690
- const range = (a, b, str) => {
691
- let begs, beg, left, right = undefined, result;
692
- let ai = str.indexOf(a);
693
- let bi = str.indexOf(b, ai + 1);
694
- let i = ai;
695
- if (ai >= 0 && bi > 0) {
696
- if (a === b) {
697
- return [ai, bi];
698
- }
699
- begs = [];
700
- left = str.length;
701
- while (i >= 0 && !result) {
702
- if (i === ai) {
703
- begs.push(i);
704
- ai = str.indexOf(a, i + 1);
705
- }
706
- else if (begs.length === 1) {
707
- const r = begs.pop();
708
- if (r !== undefined)
709
- result = [r, bi];
710
- }
711
- else {
712
- beg = begs.pop();
713
- if (beg !== undefined && beg < left) {
714
- left = beg;
715
- right = bi;
716
- }
717
- bi = str.indexOf(b, i + 1);
718
- }
719
- i = ai < bi && ai >= 0 ? ai : bi;
720
- }
721
- if (begs.length && right !== undefined) {
722
- result = [left, right];
723
- }
724
- }
725
- return result;
726
- };
727
-
728
- const escSlash = '\0SLASH' + Math.random() + '\0';
729
- const escOpen = '\0OPEN' + Math.random() + '\0';
730
- const escClose = '\0CLOSE' + Math.random() + '\0';
731
- const escComma = '\0COMMA' + Math.random() + '\0';
732
- const escPeriod = '\0PERIOD' + Math.random() + '\0';
733
- const escSlashPattern = new RegExp(escSlash, 'g');
734
- const escOpenPattern = new RegExp(escOpen, 'g');
735
- const escClosePattern = new RegExp(escClose, 'g');
736
- const escCommaPattern = new RegExp(escComma, 'g');
737
- const escPeriodPattern = new RegExp(escPeriod, 'g');
738
- const slashPattern = /\\\\/g;
739
- const openPattern = /\\{/g;
740
- const closePattern = /\\}/g;
741
- const commaPattern = /\\,/g;
742
- const periodPattern = /\\./g;
743
- function numeric(str) {
744
- return !isNaN(str) ? parseInt(str, 10) : str.charCodeAt(0);
745
- }
746
- function escapeBraces(str) {
747
- return str
748
- .replace(slashPattern, escSlash)
749
- .replace(openPattern, escOpen)
750
- .replace(closePattern, escClose)
751
- .replace(commaPattern, escComma)
752
- .replace(periodPattern, escPeriod);
753
- }
754
- function unescapeBraces(str) {
755
- return str
756
- .replace(escSlashPattern, '\\')
757
- .replace(escOpenPattern, '{')
758
- .replace(escClosePattern, '}')
759
- .replace(escCommaPattern, ',')
760
- .replace(escPeriodPattern, '.');
761
- }
762
- /**
763
- * Basically just str.split(","), but handling cases
764
- * where we have nested braced sections, which should be
765
- * treated as individual members, like {a,{b,c},d}
766
- */
767
- function parseCommaParts(str) {
768
- if (!str) {
769
- return [''];
770
- }
771
- const parts = [];
772
- const m = balanced('{', '}', str);
773
- if (!m) {
774
- return str.split(',');
775
- }
776
- const { pre, body, post } = m;
777
- const p = pre.split(',');
778
- p[p.length - 1] += '{' + body + '}';
779
- const postParts = parseCommaParts(post);
780
- if (post.length) {
781
- p[p.length - 1] += postParts.shift();
782
- p.push.apply(p, postParts);
783
- }
784
- parts.push.apply(parts, p);
785
- return parts;
786
- }
787
- function expand(str) {
788
- if (!str) {
789
- return [];
790
- }
791
- // I don't know why Bash 4.3 does this, but it does.
792
- // Anything starting with {} will have the first two bytes preserved
793
- // but *only* at the top level, so {},a}b will not expand to anything,
794
- // but a{},b}c will be expanded to [a}c,abc].
795
- // One could argue that this is a bug in Bash, but since the goal of
796
- // this module is to match Bash's rules, we escape a leading {}
797
- if (str.slice(0, 2) === '{}') {
798
- str = '\\{\\}' + str.slice(2);
799
- }
800
- return expand_(escapeBraces(str), true).map(unescapeBraces);
801
- }
802
- function embrace(str) {
803
- return '{' + str + '}';
804
- }
805
- function isPadded(el) {
806
- return /^-?0\d/.test(el);
807
- }
808
- function lte(i, y) {
809
- return i <= y;
810
- }
811
- function gte(i, y) {
812
- return i >= y;
813
- }
814
- function expand_(str, isTop) {
815
- /** @type {string[]} */
816
- const expansions = [];
817
- const m = balanced('{', '}', str);
818
- if (!m)
819
- return [str];
820
- // no need to expand pre, since it is guaranteed to be free of brace-sets
821
- const pre = m.pre;
822
- const post = m.post.length ? expand_(m.post, false) : [''];
823
- if (/\$$/.test(m.pre)) {
824
- for (let k = 0; k < post.length; k++) {
825
- const expansion = pre + '{' + m.body + '}' + post[k];
826
- expansions.push(expansion);
827
- }
828
- }
829
- else {
830
- const isNumericSequence = /^-?\d+\.\.-?\d+(?:\.\.-?\d+)?$/.test(m.body);
831
- const isAlphaSequence = /^[a-zA-Z]\.\.[a-zA-Z](?:\.\.-?\d+)?$/.test(m.body);
832
- const isSequence = isNumericSequence || isAlphaSequence;
833
- const isOptions = m.body.indexOf(',') >= 0;
834
- if (!isSequence && !isOptions) {
835
- // {a},b}
836
- if (m.post.match(/,(?!,).*\}/)) {
837
- str = m.pre + '{' + m.body + escClose + m.post;
838
- return expand_(str);
839
- }
840
- return [str];
841
- }
842
- let n;
843
- if (isSequence) {
844
- n = m.body.split(/\.\./);
845
- }
846
- else {
847
- n = parseCommaParts(m.body);
848
- if (n.length === 1 && n[0] !== undefined) {
849
- // x{{a,b}}y ==> x{a}y x{b}y
850
- n = expand_(n[0], false).map(embrace);
851
- //XXX is this necessary? Can't seem to hit it in tests.
852
- /* c8 ignore start */
853
- if (n.length === 1) {
854
- return post.map(p => m.pre + n[0] + p);
855
- }
856
- /* c8 ignore stop */
857
- }
858
- }
859
- // at this point, n is the parts, and we know it's not a comma set
860
- // with a single entry.
861
- let N;
862
- if (isSequence && n[0] !== undefined && n[1] !== undefined) {
863
- const x = numeric(n[0]);
864
- const y = numeric(n[1]);
865
- const width = Math.max(n[0].length, n[1].length);
866
- let incr = n.length === 3 && n[2] !== undefined ? Math.abs(numeric(n[2])) : 1;
867
- let test = lte;
868
- const reverse = y < x;
869
- if (reverse) {
870
- incr *= -1;
871
- test = gte;
872
- }
873
- const pad = n.some(isPadded);
874
- N = [];
875
- for (let i = x; test(i, y); i += incr) {
876
- let c;
877
- if (isAlphaSequence) {
878
- c = String.fromCharCode(i);
879
- if (c === '\\') {
880
- c = '';
881
- }
882
- }
883
- else {
884
- c = String(i);
885
- if (pad) {
886
- const need = width - c.length;
887
- if (need > 0) {
888
- const z = new Array(need + 1).join('0');
889
- if (i < 0) {
890
- c = '-' + z + c.slice(1);
891
- }
892
- else {
893
- c = z + c;
894
- }
895
- }
896
- }
897
- }
898
- N.push(c);
899
- }
900
- }
901
- else {
902
- N = [];
903
- for (let j = 0; j < n.length; j++) {
904
- N.push.apply(N, expand_(n[j], false));
905
- }
906
- }
907
- for (let j = 0; j < N.length; j++) {
908
- for (let k = 0; k < post.length; k++) {
909
- const expansion = pre + N[j] + post[k];
910
- if (!isTop || isSequence || expansion) {
911
- expansions.push(expansion);
912
- }
913
- }
914
- }
915
- }
916
- return expansions;
917
- }
918
-
919
- const MAX_PATTERN_LENGTH = 1024 * 64;
920
- const assertValidPattern = (pattern) => {
921
- if (typeof pattern !== 'string') {
922
- throw new TypeError('invalid pattern');
923
- }
924
- if (pattern.length > MAX_PATTERN_LENGTH) {
925
- throw new TypeError('pattern is too long');
926
- }
927
- };
928
-
929
- // translate the various posix character classes into unicode properties
930
- // this works across all unicode locales
931
- // { <posix class>: [<translation>, /u flag required, negated]
932
- const posixClasses = {
933
- '[:alnum:]': ['\\p{L}\\p{Nl}\\p{Nd}', true],
934
- '[:alpha:]': ['\\p{L}\\p{Nl}', true],
935
- '[:ascii:]': ['\\x' + '00-\\x' + '7f', false],
936
- '[:blank:]': ['\\p{Zs}\\t', true],
937
- '[:cntrl:]': ['\\p{Cc}', true],
938
- '[:digit:]': ['\\p{Nd}', true],
939
- '[:graph:]': ['\\p{Z}\\p{C}', true, true],
940
- '[:lower:]': ['\\p{Ll}', true],
941
- '[:print:]': ['\\p{C}', true],
942
- '[:punct:]': ['\\p{P}', true],
943
- '[:space:]': ['\\p{Z}\\t\\r\\n\\v\\f', true],
944
- '[:upper:]': ['\\p{Lu}', true],
945
- '[:word:]': ['\\p{L}\\p{Nl}\\p{Nd}\\p{Pc}', true],
946
- '[:xdigit:]': ['A-Fa-f0-9', false],
947
- };
948
- // only need to escape a few things inside of brace expressions
949
- // escapes: [ \ ] -
950
- const braceEscape = (s) => s.replace(/[[\]\\-]/g, '\\$&');
951
- // escape all regexp magic characters
952
- const regexpEscape = (s) => s.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&');
953
- // everything has already been escaped, we just have to join
954
- const rangesToString = (ranges) => ranges.join('');
955
- // takes a glob string at a posix brace expression, and returns
956
- // an equivalent regular expression source, and boolean indicating
957
- // whether the /u flag needs to be applied, and the number of chars
958
- // consumed to parse the character class.
959
- // This also removes out of order ranges, and returns ($.) if the
960
- // entire class just no good.
961
- const parseClass = (glob, position) => {
962
- const pos = position;
963
- /* c8 ignore start */
964
- if (glob.charAt(pos) !== '[') {
965
- throw new Error('not in a brace expression');
966
- }
967
- /* c8 ignore stop */
968
- const ranges = [];
969
- const negs = [];
970
- let i = pos + 1;
971
- let sawStart = false;
972
- let uflag = false;
973
- let escaping = false;
974
- let negate = false;
975
- let endPos = pos;
976
- let rangeStart = '';
977
- WHILE: while (i < glob.length) {
978
- const c = glob.charAt(i);
979
- if ((c === '!' || c === '^') && i === pos + 1) {
980
- negate = true;
981
- i++;
982
- continue;
983
- }
984
- if (c === ']' && sawStart && !escaping) {
985
- endPos = i + 1;
986
- break;
987
- }
988
- sawStart = true;
989
- if (c === '\\') {
990
- if (!escaping) {
991
- escaping = true;
992
- i++;
993
- continue;
994
- }
995
- // escaped \ char, fall through and treat like normal char
996
- }
997
- if (c === '[' && !escaping) {
998
- // either a posix class, a collation equivalent, or just a [
999
- for (const [cls, [unip, u, neg]] of Object.entries(posixClasses)) {
1000
- if (glob.startsWith(cls, i)) {
1001
- // invalid, [a-[] is fine, but not [a-[:alpha]]
1002
- if (rangeStart) {
1003
- return ['$.', false, glob.length - pos, true];
1004
- }
1005
- i += cls.length;
1006
- if (neg)
1007
- negs.push(unip);
1008
- else
1009
- ranges.push(unip);
1010
- uflag = uflag || u;
1011
- continue WHILE;
1012
- }
1013
- }
1014
- }
1015
- // now it's just a normal character, effectively
1016
- escaping = false;
1017
- if (rangeStart) {
1018
- // throw this range away if it's not valid, but others
1019
- // can still match.
1020
- if (c > rangeStart) {
1021
- ranges.push(braceEscape(rangeStart) + '-' + braceEscape(c));
1022
- }
1023
- else if (c === rangeStart) {
1024
- ranges.push(braceEscape(c));
1025
- }
1026
- rangeStart = '';
1027
- i++;
1028
- continue;
1029
- }
1030
- // now might be the start of a range.
1031
- // can be either c-d or c-] or c<more...>] or c] at this point
1032
- if (glob.startsWith('-]', i + 1)) {
1033
- ranges.push(braceEscape(c + '-'));
1034
- i += 2;
1035
- continue;
1036
- }
1037
- if (glob.startsWith('-', i + 1)) {
1038
- rangeStart = c;
1039
- i += 2;
1040
- continue;
1041
- }
1042
- // not the start of a range, just a single character
1043
- ranges.push(braceEscape(c));
1044
- i++;
1045
- }
1046
- if (endPos < i) {
1047
- // didn't see the end of the class, not a valid class,
1048
- // but might still be valid as a literal match.
1049
- return ['', false, 0, false];
1050
- }
1051
- // if we got no ranges and no negates, then we have a range that
1052
- // cannot possibly match anything, and that poisons the whole glob
1053
- if (!ranges.length && !negs.length) {
1054
- return ['$.', false, glob.length - pos, true];
1055
- }
1056
- // if we got one positive range, and it's a single character, then that's
1057
- // not actually a magic pattern, it's just that one literal character.
1058
- // we should not treat that as "magic", we should just return the literal
1059
- // character. [_] is a perfectly valid way to escape glob magic chars.
1060
- if (negs.length === 0 &&
1061
- ranges.length === 1 &&
1062
- /^\\?.$/.test(ranges[0]) &&
1063
- !negate) {
1064
- const r = ranges[0].length === 2 ? ranges[0].slice(-1) : ranges[0];
1065
- return [regexpEscape(r), false, endPos - pos, false];
1066
- }
1067
- const sranges = '[' + (negate ? '^' : '') + rangesToString(ranges) + ']';
1068
- const snegs = '[' + (negate ? '' : '^') + rangesToString(negs) + ']';
1069
- const comb = ranges.length && negs.length
1070
- ? '(' + sranges + '|' + snegs + ')'
1071
- : ranges.length
1072
- ? sranges
1073
- : snegs;
1074
- return [comb, uflag, endPos - pos, true];
1075
- };
1076
-
1077
- /**
1078
- * Un-escape a string that has been escaped with {@link escape}.
1079
- *
1080
- * If the {@link MinimatchOptions.windowsPathsNoEscape} option is used, then
1081
- * square-bracket escapes are removed, but not backslash escapes.
1082
- *
1083
- * For example, it will turn the string `'[*]'` into `*`, but it will not
1084
- * turn `'\\*'` into `'*'`, because `\` is a path separator in
1085
- * `windowsPathsNoEscape` mode.
1086
- *
1087
- * When `windowsPathsNoEscape` is not set, then both square-bracket escapes and
1088
- * backslash escapes are removed.
1089
- *
1090
- * Slashes (and backslashes in `windowsPathsNoEscape` mode) cannot be escaped
1091
- * or unescaped.
1092
- *
1093
- * When `magicalBraces` is not set, escapes of braces (`{` and `}`) will not be
1094
- * unescaped.
1095
- */
1096
- const unescape = (s, { windowsPathsNoEscape = false, magicalBraces = true, } = {}) => {
1097
- if (magicalBraces) {
1098
- return windowsPathsNoEscape
1099
- ? s.replace(/\[([^\/\\])\]/g, '$1')
1100
- : s
1101
- .replace(/((?!\\).|^)\[([^\/\\])\]/g, '$1$2')
1102
- .replace(/\\([^\/])/g, '$1');
1103
- }
1104
- return windowsPathsNoEscape
1105
- ? s.replace(/\[([^\/\\{}])\]/g, '$1')
1106
- : s
1107
- .replace(/((?!\\).|^)\[([^\/\\{}])\]/g, '$1$2')
1108
- .replace(/\\([^\/{}])/g, '$1');
1109
- };
1110
-
1111
- // parse a single path portion
1112
- const types = new Set(['!', '?', '+', '*', '@']);
1113
- const isExtglobType = (c) => types.has(c);
1114
- // Patterns that get prepended to bind to the start of either the
1115
- // entire string, or just a single path portion, to prevent dots
1116
- // and/or traversal patterns, when needed.
1117
- // Exts don't need the ^ or / bit, because the root binds that already.
1118
- const startNoTraversal = '(?!(?:^|/)\\.\\.?(?:$|/))';
1119
- const startNoDot = '(?!\\.)';
1120
- // characters that indicate a start of pattern needs the "no dots" bit,
1121
- // because a dot *might* be matched. ( is not in the list, because in
1122
- // the case of a child extglob, it will handle the prevention itself.
1123
- const addPatternStart = new Set(['[', '.']);
1124
- // cases where traversal is A-OK, no dot prevention needed
1125
- const justDots = new Set(['..', '.']);
1126
- const reSpecials = new Set('().*{}+?[]^$\\!');
1127
- const regExpEscape$1 = (s) => s.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&');
1128
- // any single thing other than /
1129
- const qmark$1 = '[^/]';
1130
- // * => any number of characters
1131
- const star$1 = qmark$1 + '*?';
1132
- // use + when we need to ensure that *something* matches, because the * is
1133
- // the only thing in the path portion.
1134
- const starNoEmpty = qmark$1 + '+?';
1135
- // remove the \ chars that we added if we end up doing a nonmagic compare
1136
- // const deslash = (s: string) => s.replace(/\\(.)/g, '$1')
1137
- class AST {
1138
- type;
1139
- #root;
1140
- #hasMagic;
1141
- #uflag = false;
1142
- #parts = [];
1143
- #parent;
1144
- #parentIndex;
1145
- #negs;
1146
- #filledNegs = false;
1147
- #options;
1148
- #toString;
1149
- // set to true if it's an extglob with no children
1150
- // (which really means one child of '')
1151
- #emptyExt = false;
1152
- constructor(type, parent, options = {}) {
1153
- this.type = type;
1154
- // extglobs are inherently magical
1155
- if (type)
1156
- this.#hasMagic = true;
1157
- this.#parent = parent;
1158
- this.#root = this.#parent ? this.#parent.#root : this;
1159
- this.#options = this.#root === this ? options : this.#root.#options;
1160
- this.#negs = this.#root === this ? [] : this.#root.#negs;
1161
- if (type === '!' && !this.#root.#filledNegs)
1162
- this.#negs.push(this);
1163
- this.#parentIndex = this.#parent ? this.#parent.#parts.length : 0;
1164
- }
1165
- get hasMagic() {
1166
- /* c8 ignore start */
1167
- if (this.#hasMagic !== undefined)
1168
- return this.#hasMagic;
1169
- /* c8 ignore stop */
1170
- for (const p of this.#parts) {
1171
- if (typeof p === 'string')
1172
- continue;
1173
- if (p.type || p.hasMagic)
1174
- return (this.#hasMagic = true);
1175
- }
1176
- // note: will be undefined until we generate the regexp src and find out
1177
- return this.#hasMagic;
1178
- }
1179
- // reconstructs the pattern
1180
- toString() {
1181
- if (this.#toString !== undefined)
1182
- return this.#toString;
1183
- if (!this.type) {
1184
- return (this.#toString = this.#parts.map(p => String(p)).join(''));
1185
- }
1186
- else {
1187
- return (this.#toString =
1188
- this.type + '(' + this.#parts.map(p => String(p)).join('|') + ')');
1189
- }
1190
- }
1191
- #fillNegs() {
1192
- /* c8 ignore start */
1193
- if (this !== this.#root)
1194
- throw new Error('should only call on root');
1195
- if (this.#filledNegs)
1196
- return this;
1197
- /* c8 ignore stop */
1198
- // call toString() once to fill this out
1199
- this.toString();
1200
- this.#filledNegs = true;
1201
- let n;
1202
- while ((n = this.#negs.pop())) {
1203
- if (n.type !== '!')
1204
- continue;
1205
- // walk up the tree, appending everthing that comes AFTER parentIndex
1206
- let p = n;
1207
- let pp = p.#parent;
1208
- while (pp) {
1209
- for (let i = p.#parentIndex + 1; !pp.type && i < pp.#parts.length; i++) {
1210
- for (const part of n.#parts) {
1211
- /* c8 ignore start */
1212
- if (typeof part === 'string') {
1213
- throw new Error('string part in extglob AST??');
1214
- }
1215
- /* c8 ignore stop */
1216
- part.copyIn(pp.#parts[i]);
1217
- }
1218
- }
1219
- p = pp;
1220
- pp = p.#parent;
1221
- }
1222
- }
1223
- return this;
1224
- }
1225
- push(...parts) {
1226
- for (const p of parts) {
1227
- if (p === '')
1228
- continue;
1229
- /* c8 ignore start */
1230
- if (typeof p !== 'string' && !(p instanceof AST && p.#parent === this)) {
1231
- throw new Error('invalid part: ' + p);
1232
- }
1233
- /* c8 ignore stop */
1234
- this.#parts.push(p);
1235
- }
1236
- }
1237
- toJSON() {
1238
- const ret = this.type === null
1239
- ? this.#parts.slice().map(p => (typeof p === 'string' ? p : p.toJSON()))
1240
- : [this.type, ...this.#parts.map(p => p.toJSON())];
1241
- if (this.isStart() && !this.type)
1242
- ret.unshift([]);
1243
- if (this.isEnd() &&
1244
- (this === this.#root ||
1245
- (this.#root.#filledNegs && this.#parent?.type === '!'))) {
1246
- ret.push({});
1247
- }
1248
- return ret;
1249
- }
1250
- isStart() {
1251
- if (this.#root === this)
1252
- return true;
1253
- // if (this.type) return !!this.#parent?.isStart()
1254
- if (!this.#parent?.isStart())
1255
- return false;
1256
- if (this.#parentIndex === 0)
1257
- return true;
1258
- // if everything AHEAD of this is a negation, then it's still the "start"
1259
- const p = this.#parent;
1260
- for (let i = 0; i < this.#parentIndex; i++) {
1261
- const pp = p.#parts[i];
1262
- if (!(pp instanceof AST && pp.type === '!')) {
1263
- return false;
1264
- }
1265
- }
1266
- return true;
1267
- }
1268
- isEnd() {
1269
- if (this.#root === this)
1270
- return true;
1271
- if (this.#parent?.type === '!')
1272
- return true;
1273
- if (!this.#parent?.isEnd())
1274
- return false;
1275
- if (!this.type)
1276
- return this.#parent?.isEnd();
1277
- // if not root, it'll always have a parent
1278
- /* c8 ignore start */
1279
- const pl = this.#parent ? this.#parent.#parts.length : 0;
1280
- /* c8 ignore stop */
1281
- return this.#parentIndex === pl - 1;
1282
- }
1283
- copyIn(part) {
1284
- if (typeof part === 'string')
1285
- this.push(part);
1286
- else
1287
- this.push(part.clone(this));
1288
- }
1289
- clone(parent) {
1290
- const c = new AST(this.type, parent);
1291
- for (const p of this.#parts) {
1292
- c.copyIn(p);
1293
- }
1294
- return c;
1295
- }
1296
- static #parseAST(str, ast, pos, opt) {
1297
- let escaping = false;
1298
- let inBrace = false;
1299
- let braceStart = -1;
1300
- let braceNeg = false;
1301
- if (ast.type === null) {
1302
- // outside of a extglob, append until we find a start
1303
- let i = pos;
1304
- let acc = '';
1305
- while (i < str.length) {
1306
- const c = str.charAt(i++);
1307
- // still accumulate escapes at this point, but we do ignore
1308
- // starts that are escaped
1309
- if (escaping || c === '\\') {
1310
- escaping = !escaping;
1311
- acc += c;
1312
- continue;
1313
- }
1314
- if (inBrace) {
1315
- if (i === braceStart + 1) {
1316
- if (c === '^' || c === '!') {
1317
- braceNeg = true;
1318
- }
1319
- }
1320
- else if (c === ']' && !(i === braceStart + 2 && braceNeg)) {
1321
- inBrace = false;
1322
- }
1323
- acc += c;
1324
- continue;
1325
- }
1326
- else if (c === '[') {
1327
- inBrace = true;
1328
- braceStart = i;
1329
- braceNeg = false;
1330
- acc += c;
1331
- continue;
1332
- }
1333
- if (!opt.noext && isExtglobType(c) && str.charAt(i) === '(') {
1334
- ast.push(acc);
1335
- acc = '';
1336
- const ext = new AST(c, ast);
1337
- i = AST.#parseAST(str, ext, i, opt);
1338
- ast.push(ext);
1339
- continue;
1340
- }
1341
- acc += c;
1342
- }
1343
- ast.push(acc);
1344
- return i;
1345
- }
1346
- // some kind of extglob, pos is at the (
1347
- // find the next | or )
1348
- let i = pos + 1;
1349
- let part = new AST(null, ast);
1350
- const parts = [];
1351
- let acc = '';
1352
- while (i < str.length) {
1353
- const c = str.charAt(i++);
1354
- // still accumulate escapes at this point, but we do ignore
1355
- // starts that are escaped
1356
- if (escaping || c === '\\') {
1357
- escaping = !escaping;
1358
- acc += c;
1359
- continue;
1360
- }
1361
- if (inBrace) {
1362
- if (i === braceStart + 1) {
1363
- if (c === '^' || c === '!') {
1364
- braceNeg = true;
1365
- }
1366
- }
1367
- else if (c === ']' && !(i === braceStart + 2 && braceNeg)) {
1368
- inBrace = false;
1369
- }
1370
- acc += c;
1371
- continue;
1372
- }
1373
- else if (c === '[') {
1374
- inBrace = true;
1375
- braceStart = i;
1376
- braceNeg = false;
1377
- acc += c;
1378
- continue;
1379
- }
1380
- if (isExtglobType(c) && str.charAt(i) === '(') {
1381
- part.push(acc);
1382
- acc = '';
1383
- const ext = new AST(c, part);
1384
- part.push(ext);
1385
- i = AST.#parseAST(str, ext, i, opt);
1386
- continue;
1387
- }
1388
- if (c === '|') {
1389
- part.push(acc);
1390
- acc = '';
1391
- parts.push(part);
1392
- part = new AST(null, ast);
1393
- continue;
1394
- }
1395
- if (c === ')') {
1396
- if (acc === '' && ast.#parts.length === 0) {
1397
- ast.#emptyExt = true;
1398
- }
1399
- part.push(acc);
1400
- acc = '';
1401
- ast.push(...parts, part);
1402
- return i;
1403
- }
1404
- acc += c;
1405
- }
1406
- // unfinished extglob
1407
- // if we got here, it was a malformed extglob! not an extglob, but
1408
- // maybe something else in there.
1409
- ast.type = null;
1410
- ast.#hasMagic = undefined;
1411
- ast.#parts = [str.substring(pos - 1)];
1412
- return i;
1413
- }
1414
- static fromGlob(pattern, options = {}) {
1415
- const ast = new AST(null, undefined, options);
1416
- AST.#parseAST(pattern, ast, 0, options);
1417
- return ast;
1418
- }
1419
- // returns the regular expression if there's magic, or the unescaped
1420
- // string if not.
1421
- toMMPattern() {
1422
- // should only be called on root
1423
- /* c8 ignore start */
1424
- if (this !== this.#root)
1425
- return this.#root.toMMPattern();
1426
- /* c8 ignore stop */
1427
- const glob = this.toString();
1428
- const [re, body, hasMagic, uflag] = this.toRegExpSource();
1429
- // if we're in nocase mode, and not nocaseMagicOnly, then we do
1430
- // still need a regular expression if we have to case-insensitively
1431
- // match capital/lowercase characters.
1432
- const anyMagic = hasMagic ||
1433
- this.#hasMagic ||
1434
- (this.#options.nocase &&
1435
- !this.#options.nocaseMagicOnly &&
1436
- glob.toUpperCase() !== glob.toLowerCase());
1437
- if (!anyMagic) {
1438
- return body;
1439
- }
1440
- const flags = (this.#options.nocase ? 'i' : '') + (uflag ? 'u' : '');
1441
- return Object.assign(new RegExp(`^${re}$`, flags), {
1442
- _src: re,
1443
- _glob: glob,
1444
- });
1445
- }
1446
- get options() {
1447
- return this.#options;
1448
- }
1449
- // returns the string match, the regexp source, whether there's magic
1450
- // in the regexp (so a regular expression is required) and whether or
1451
- // not the uflag is needed for the regular expression (for posix classes)
1452
- // TODO: instead of injecting the start/end at this point, just return
1453
- // the BODY of the regexp, along with the start/end portions suitable
1454
- // for binding the start/end in either a joined full-path makeRe context
1455
- // (where we bind to (^|/), or a standalone matchPart context (where
1456
- // we bind to ^, and not /). Otherwise slashes get duped!
1457
- //
1458
- // In part-matching mode, the start is:
1459
- // - if not isStart: nothing
1460
- // - if traversal possible, but not allowed: ^(?!\.\.?$)
1461
- // - if dots allowed or not possible: ^
1462
- // - if dots possible and not allowed: ^(?!\.)
1463
- // end is:
1464
- // - if not isEnd(): nothing
1465
- // - else: $
1466
- //
1467
- // In full-path matching mode, we put the slash at the START of the
1468
- // pattern, so start is:
1469
- // - if first pattern: same as part-matching mode
1470
- // - if not isStart(): nothing
1471
- // - if traversal possible, but not allowed: /(?!\.\.?(?:$|/))
1472
- // - if dots allowed or not possible: /
1473
- // - if dots possible and not allowed: /(?!\.)
1474
- // end is:
1475
- // - if last pattern, same as part-matching mode
1476
- // - else nothing
1477
- //
1478
- // Always put the (?:$|/) on negated tails, though, because that has to be
1479
- // there to bind the end of the negated pattern portion, and it's easier to
1480
- // just stick it in now rather than try to inject it later in the middle of
1481
- // the pattern.
1482
- //
1483
- // We can just always return the same end, and leave it up to the caller
1484
- // to know whether it's going to be used joined or in parts.
1485
- // And, if the start is adjusted slightly, can do the same there:
1486
- // - if not isStart: nothing
1487
- // - if traversal possible, but not allowed: (?:/|^)(?!\.\.?$)
1488
- // - if dots allowed or not possible: (?:/|^)
1489
- // - if dots possible and not allowed: (?:/|^)(?!\.)
1490
- //
1491
- // But it's better to have a simpler binding without a conditional, for
1492
- // performance, so probably better to return both start options.
1493
- //
1494
- // Then the caller just ignores the end if it's not the first pattern,
1495
- // and the start always gets applied.
1496
- //
1497
- // But that's always going to be $ if it's the ending pattern, or nothing,
1498
- // so the caller can just attach $ at the end of the pattern when building.
1499
- //
1500
- // So the todo is:
1501
- // - better detect what kind of start is needed
1502
- // - return both flavors of starting pattern
1503
- // - attach $ at the end of the pattern when creating the actual RegExp
1504
- //
1505
- // Ah, but wait, no, that all only applies to the root when the first pattern
1506
- // is not an extglob. If the first pattern IS an extglob, then we need all
1507
- // that dot prevention biz to live in the extglob portions, because eg
1508
- // +(*|.x*) can match .xy but not .yx.
1509
- //
1510
- // So, return the two flavors if it's #root and the first child is not an
1511
- // AST, otherwise leave it to the child AST to handle it, and there,
1512
- // use the (?:^|/) style of start binding.
1513
- //
1514
- // Even simplified further:
1515
- // - Since the start for a join is eg /(?!\.) and the start for a part
1516
- // is ^(?!\.), we can just prepend (?!\.) to the pattern (either root
1517
- // or start or whatever) and prepend ^ or / at the Regexp construction.
1518
- toRegExpSource(allowDot) {
1519
- const dot = allowDot ?? !!this.#options.dot;
1520
- if (this.#root === this)
1521
- this.#fillNegs();
1522
- if (!this.type) {
1523
- const noEmpty = this.isStart() &&
1524
- this.isEnd() &&
1525
- !this.#parts.some(s => typeof s !== 'string');
1526
- const src = this.#parts
1527
- .map(p => {
1528
- const [re, _, hasMagic, uflag] = typeof p === 'string'
1529
- ? AST.#parseGlob(p, this.#hasMagic, noEmpty)
1530
- : p.toRegExpSource(allowDot);
1531
- this.#hasMagic = this.#hasMagic || hasMagic;
1532
- this.#uflag = this.#uflag || uflag;
1533
- return re;
1534
- })
1535
- .join('');
1536
- let start = '';
1537
- if (this.isStart()) {
1538
- if (typeof this.#parts[0] === 'string') {
1539
- // this is the string that will match the start of the pattern,
1540
- // so we need to protect against dots and such.
1541
- // '.' and '..' cannot match unless the pattern is that exactly,
1542
- // even if it starts with . or dot:true is set.
1543
- const dotTravAllowed = this.#parts.length === 1 && justDots.has(this.#parts[0]);
1544
- if (!dotTravAllowed) {
1545
- const aps = addPatternStart;
1546
- // check if we have a possibility of matching . or ..,
1547
- // and prevent that.
1548
- const needNoTrav =
1549
- // dots are allowed, and the pattern starts with [ or .
1550
- (dot && aps.has(src.charAt(0))) ||
1551
- // the pattern starts with \., and then [ or .
1552
- (src.startsWith('\\.') && aps.has(src.charAt(2))) ||
1553
- // the pattern starts with \.\., and then [ or .
1554
- (src.startsWith('\\.\\.') && aps.has(src.charAt(4)));
1555
- // no need to prevent dots if it can't match a dot, or if a
1556
- // sub-pattern will be preventing it anyway.
1557
- const needNoDot = !dot && !allowDot && aps.has(src.charAt(0));
1558
- start = needNoTrav ? startNoTraversal : needNoDot ? startNoDot : '';
1559
- }
1560
- }
1561
- }
1562
- // append the "end of path portion" pattern to negation tails
1563
- let end = '';
1564
- if (this.isEnd() &&
1565
- this.#root.#filledNegs &&
1566
- this.#parent?.type === '!') {
1567
- end = '(?:$|\\/)';
1568
- }
1569
- const final = start + src + end;
1570
- return [
1571
- final,
1572
- unescape(src),
1573
- (this.#hasMagic = !!this.#hasMagic),
1574
- this.#uflag,
1575
- ];
1576
- }
1577
- // We need to calculate the body *twice* if it's a repeat pattern
1578
- // at the start, once in nodot mode, then again in dot mode, so a
1579
- // pattern like *(?) can match 'x.y'
1580
- const repeated = this.type === '*' || this.type === '+';
1581
- // some kind of extglob
1582
- const start = this.type === '!' ? '(?:(?!(?:' : '(?:';
1583
- let body = this.#partsToRegExp(dot);
1584
- if (this.isStart() && this.isEnd() && !body && this.type !== '!') {
1585
- // invalid extglob, has to at least be *something* present, if it's
1586
- // the entire path portion.
1587
- const s = this.toString();
1588
- this.#parts = [s];
1589
- this.type = null;
1590
- this.#hasMagic = undefined;
1591
- return [s, unescape(this.toString()), false, false];
1592
- }
1593
- // XXX abstract out this map method
1594
- let bodyDotAllowed = !repeated || allowDot || dot || !startNoDot
1595
- ? ''
1596
- : this.#partsToRegExp(true);
1597
- if (bodyDotAllowed === body) {
1598
- bodyDotAllowed = '';
1599
- }
1600
- if (bodyDotAllowed) {
1601
- body = `(?:${body})(?:${bodyDotAllowed})*?`;
1602
- }
1603
- // an empty !() is exactly equivalent to a starNoEmpty
1604
- let final = '';
1605
- if (this.type === '!' && this.#emptyExt) {
1606
- final = (this.isStart() && !dot ? startNoDot : '') + starNoEmpty;
1607
- }
1608
- else {
1609
- const close = this.type === '!'
1610
- ? // !() must match something,but !(x) can match ''
1611
- '))' +
1612
- (this.isStart() && !dot && !allowDot ? startNoDot : '') +
1613
- star$1 +
1614
- ')'
1615
- : this.type === '@'
1616
- ? ')'
1617
- : this.type === '?'
1618
- ? ')?'
1619
- : this.type === '+' && bodyDotAllowed
1620
- ? ')'
1621
- : this.type === '*' && bodyDotAllowed
1622
- ? `)?`
1623
- : `)${this.type}`;
1624
- final = start + body + close;
1625
- }
1626
- return [
1627
- final,
1628
- unescape(body),
1629
- (this.#hasMagic = !!this.#hasMagic),
1630
- this.#uflag,
1631
- ];
1632
- }
1633
- #partsToRegExp(dot) {
1634
- return this.#parts
1635
- .map(p => {
1636
- // extglob ASTs should only contain parent ASTs
1637
- /* c8 ignore start */
1638
- if (typeof p === 'string') {
1639
- throw new Error('string type in extglob ast??');
1640
- }
1641
- /* c8 ignore stop */
1642
- // can ignore hasMagic, because extglobs are already always magic
1643
- const [re, _, _hasMagic, uflag] = p.toRegExpSource(dot);
1644
- this.#uflag = this.#uflag || uflag;
1645
- return re;
1646
- })
1647
- .filter(p => !(this.isStart() && this.isEnd()) || !!p)
1648
- .join('|');
1649
- }
1650
- static #parseGlob(glob, hasMagic, noEmpty = false) {
1651
- let escaping = false;
1652
- let re = '';
1653
- let uflag = false;
1654
- for (let i = 0; i < glob.length; i++) {
1655
- const c = glob.charAt(i);
1656
- if (escaping) {
1657
- escaping = false;
1658
- re += (reSpecials.has(c) ? '\\' : '') + c;
1659
- continue;
1660
- }
1661
- if (c === '\\') {
1662
- if (i === glob.length - 1) {
1663
- re += '\\\\';
1664
- }
1665
- else {
1666
- escaping = true;
1667
- }
1668
- continue;
1669
- }
1670
- if (c === '[') {
1671
- const [src, needUflag, consumed, magic] = parseClass(glob, i);
1672
- if (consumed) {
1673
- re += src;
1674
- uflag = uflag || needUflag;
1675
- i += consumed - 1;
1676
- hasMagic = hasMagic || magic;
1677
- continue;
1678
- }
1679
- }
1680
- if (c === '*') {
1681
- re += noEmpty && glob === '*' ? starNoEmpty : star$1;
1682
- hasMagic = true;
1683
- continue;
1684
- }
1685
- if (c === '?') {
1686
- re += qmark$1;
1687
- hasMagic = true;
1688
- continue;
1689
- }
1690
- re += regExpEscape$1(c);
1691
- }
1692
- return [re, unescape(glob), !!hasMagic, uflag];
1693
- }
1694
- }
1695
-
1696
- /**
1697
- * Escape all magic characters in a glob pattern.
1698
- *
1699
- * If the {@link MinimatchOptions.windowsPathsNoEscape}
1700
- * option is used, then characters are escaped by wrapping in `[]`, because
1701
- * a magic character wrapped in a character class can only be satisfied by
1702
- * that exact character. In this mode, `\` is _not_ escaped, because it is
1703
- * not interpreted as a magic character, but instead as a path separator.
1704
- *
1705
- * If the {@link MinimatchOptions.magicalBraces} option is used,
1706
- * then braces (`{` and `}`) will be escaped.
1707
- */
1708
- const escape = (s, { windowsPathsNoEscape = false, magicalBraces = false, } = {}) => {
1709
- // don't need to escape +@! because we escape the parens
1710
- // that make those magic, and escaping ! as [!] isn't valid,
1711
- // because [!]] is a valid glob class meaning not ']'.
1712
- if (magicalBraces) {
1713
- return windowsPathsNoEscape
1714
- ? s.replace(/[?*()[\]{}]/g, '[$&]')
1715
- : s.replace(/[?*()[\]\\{}]/g, '\\$&');
1716
- }
1717
- return windowsPathsNoEscape
1718
- ? s.replace(/[?*()[\]]/g, '[$&]')
1719
- : s.replace(/[?*()[\]\\]/g, '\\$&');
1720
- };
1721
-
1722
- const minimatch = (p, pattern, options = {}) => {
1723
- assertValidPattern(pattern);
1724
- // shortcut: comments match nothing.
1725
- if (!options.nocomment && pattern.charAt(0) === '#') {
1726
- return false;
1727
- }
1728
- return new Minimatch(pattern, options).match(p);
1729
- };
1730
- // Optimized checking for the most common glob patterns.
1731
- const starDotExtRE = /^\*+([^+@!?\*\[\(]*)$/;
1732
- const starDotExtTest = (ext) => (f) => !f.startsWith('.') && f.endsWith(ext);
1733
- const starDotExtTestDot = (ext) => (f) => f.endsWith(ext);
1734
- const starDotExtTestNocase = (ext) => {
1735
- ext = ext.toLowerCase();
1736
- return (f) => !f.startsWith('.') && f.toLowerCase().endsWith(ext);
1737
- };
1738
- const starDotExtTestNocaseDot = (ext) => {
1739
- ext = ext.toLowerCase();
1740
- return (f) => f.toLowerCase().endsWith(ext);
1741
- };
1742
- const starDotStarRE = /^\*+\.\*+$/;
1743
- const starDotStarTest = (f) => !f.startsWith('.') && f.includes('.');
1744
- const starDotStarTestDot = (f) => f !== '.' && f !== '..' && f.includes('.');
1745
- const dotStarRE = /^\.\*+$/;
1746
- const dotStarTest = (f) => f !== '.' && f !== '..' && f.startsWith('.');
1747
- const starRE = /^\*+$/;
1748
- const starTest = (f) => f.length !== 0 && !f.startsWith('.');
1749
- const starTestDot = (f) => f.length !== 0 && f !== '.' && f !== '..';
1750
- const qmarksRE = /^\?+([^+@!?\*\[\(]*)?$/;
1751
- const qmarksTestNocase = ([$0, ext = '']) => {
1752
- const noext = qmarksTestNoExt([$0]);
1753
- if (!ext)
1754
- return noext;
1755
- ext = ext.toLowerCase();
1756
- return (f) => noext(f) && f.toLowerCase().endsWith(ext);
1757
- };
1758
- const qmarksTestNocaseDot = ([$0, ext = '']) => {
1759
- const noext = qmarksTestNoExtDot([$0]);
1760
- if (!ext)
1761
- return noext;
1762
- ext = ext.toLowerCase();
1763
- return (f) => noext(f) && f.toLowerCase().endsWith(ext);
1764
- };
1765
- const qmarksTestDot = ([$0, ext = '']) => {
1766
- const noext = qmarksTestNoExtDot([$0]);
1767
- return !ext ? noext : (f) => noext(f) && f.endsWith(ext);
1768
- };
1769
- const qmarksTest = ([$0, ext = '']) => {
1770
- const noext = qmarksTestNoExt([$0]);
1771
- return !ext ? noext : (f) => noext(f) && f.endsWith(ext);
1772
- };
1773
- const qmarksTestNoExt = ([$0]) => {
1774
- const len = $0.length;
1775
- return (f) => f.length === len && !f.startsWith('.');
1776
- };
1777
- const qmarksTestNoExtDot = ([$0]) => {
1778
- const len = $0.length;
1779
- return (f) => f.length === len && f !== '.' && f !== '..';
1780
- };
1781
- /* c8 ignore start */
1782
- const defaultPlatform = (typeof process === 'object' && process
1783
- ? (typeof process.env === 'object' &&
1784
- process.env &&
1785
- process.env.__MINIMATCH_TESTING_PLATFORM__) ||
1786
- process.platform
1787
- : 'posix');
1788
- const path = {
1789
- win32: { sep: '\\' },
1790
- posix: { sep: '/' },
1791
- };
1792
- /* c8 ignore stop */
1793
- const sep = defaultPlatform === 'win32' ? path.win32.sep : path.posix.sep;
1794
- minimatch.sep = sep;
1795
- const GLOBSTAR = Symbol('globstar **');
1796
- minimatch.GLOBSTAR = GLOBSTAR;
1797
- // any single thing other than /
1798
- // don't need to escape / when using new RegExp()
1799
- const qmark = '[^/]';
1800
- // * => any number of characters
1801
- const star = qmark + '*?';
1802
- // ** when dots are allowed. Anything goes, except .. and .
1803
- // not (^ or / followed by one or two dots followed by $ or /),
1804
- // followed by anything, any number of times.
1805
- const twoStarDot = '(?:(?!(?:\\/|^)(?:\\.{1,2})($|\\/)).)*?';
1806
- // not a ^ or / followed by a dot,
1807
- // followed by anything, any number of times.
1808
- const twoStarNoDot = '(?:(?!(?:\\/|^)\\.).)*?';
1809
- const filter = (pattern, options = {}) => (p) => minimatch(p, pattern, options);
1810
- minimatch.filter = filter;
1811
- const ext = (a, b = {}) => Object.assign({}, a, b);
1812
- const defaults = (def) => {
1813
- if (!def || typeof def !== 'object' || !Object.keys(def).length) {
1814
- return minimatch;
1815
- }
1816
- const orig = minimatch;
1817
- const m = (p, pattern, options = {}) => orig(p, pattern, ext(def, options));
1818
- return Object.assign(m, {
1819
- Minimatch: class Minimatch extends orig.Minimatch {
1820
- constructor(pattern, options = {}) {
1821
- super(pattern, ext(def, options));
1822
- }
1823
- static defaults(options) {
1824
- return orig.defaults(ext(def, options)).Minimatch;
1825
- }
1826
- },
1827
- AST: class AST extends orig.AST {
1828
- /* c8 ignore start */
1829
- constructor(type, parent, options = {}) {
1830
- super(type, parent, ext(def, options));
1831
- }
1832
- /* c8 ignore stop */
1833
- static fromGlob(pattern, options = {}) {
1834
- return orig.AST.fromGlob(pattern, ext(def, options));
1835
- }
1836
- },
1837
- unescape: (s, options = {}) => orig.unescape(s, ext(def, options)),
1838
- escape: (s, options = {}) => orig.escape(s, ext(def, options)),
1839
- filter: (pattern, options = {}) => orig.filter(pattern, ext(def, options)),
1840
- defaults: (options) => orig.defaults(ext(def, options)),
1841
- makeRe: (pattern, options = {}) => orig.makeRe(pattern, ext(def, options)),
1842
- braceExpand: (pattern, options = {}) => orig.braceExpand(pattern, ext(def, options)),
1843
- match: (list, pattern, options = {}) => orig.match(list, pattern, ext(def, options)),
1844
- sep: orig.sep,
1845
- GLOBSTAR: GLOBSTAR,
1846
- });
1847
- };
1848
- minimatch.defaults = defaults;
1849
- // Brace expansion:
1850
- // a{b,c}d -> abd acd
1851
- // a{b,}c -> abc ac
1852
- // a{0..3}d -> a0d a1d a2d a3d
1853
- // a{b,c{d,e}f}g -> abg acdfg acefg
1854
- // a{b,c}d{e,f}g -> abdeg acdeg abdeg abdfg
1855
- //
1856
- // Invalid sets are not expanded.
1857
- // a{2..}b -> a{2..}b
1858
- // a{b}c -> a{b}c
1859
- const braceExpand = (pattern, options = {}) => {
1860
- assertValidPattern(pattern);
1861
- // Thanks to Yeting Li <https://github.com/yetingli> for
1862
- // improving this regexp to avoid a ReDOS vulnerability.
1863
- if (options.nobrace || !/\{(?:(?!\{).)*\}/.test(pattern)) {
1864
- // shortcut. no need to expand.
1865
- return [pattern];
1866
- }
1867
- return expand(pattern);
1868
- };
1869
- minimatch.braceExpand = braceExpand;
1870
- // parse a component of the expanded set.
1871
- // At this point, no pattern may contain "/" in it
1872
- // so we're going to return a 2d array, where each entry is the full
1873
- // pattern, split on '/', and then turned into a regular expression.
1874
- // A regexp is made at the end which joins each array with an
1875
- // escaped /, and another full one which joins each regexp with |.
1876
- //
1877
- // Following the lead of Bash 4.1, note that "**" only has special meaning
1878
- // when it is the *only* thing in a path portion. Otherwise, any series
1879
- // of * is equivalent to a single *. Globstar behavior is enabled by
1880
- // default, and can be disabled by setting options.noglobstar.
1881
- const makeRe = (pattern, options = {}) => new Minimatch(pattern, options).makeRe();
1882
- minimatch.makeRe = makeRe;
1883
- const match = (list, pattern, options = {}) => {
1884
- const mm = new Minimatch(pattern, options);
1885
- list = list.filter(f => mm.match(f));
1886
- if (mm.options.nonull && !list.length) {
1887
- list.push(pattern);
1888
- }
1889
- return list;
1890
- };
1891
- minimatch.match = match;
1892
- // replace stuff like \* with *
1893
- const globMagic = /[?*]|[+@!]\(.*?\)|\[|\]/;
1894
- const regExpEscape = (s) => s.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&');
1895
- class Minimatch {
1896
- options;
1897
- set;
1898
- pattern;
1899
- windowsPathsNoEscape;
1900
- nonegate;
1901
- negate;
1902
- comment;
1903
- empty;
1904
- preserveMultipleSlashes;
1905
- partial;
1906
- globSet;
1907
- globParts;
1908
- nocase;
1909
- isWindows;
1910
- platform;
1911
- windowsNoMagicRoot;
1912
- regexp;
1913
- constructor(pattern, options = {}) {
1914
- assertValidPattern(pattern);
1915
- options = options || {};
1916
- this.options = options;
1917
- this.pattern = pattern;
1918
- this.platform = options.platform || defaultPlatform;
1919
- this.isWindows = this.platform === 'win32';
1920
- this.windowsPathsNoEscape =
1921
- !!options.windowsPathsNoEscape || options.allowWindowsEscape === false;
1922
- if (this.windowsPathsNoEscape) {
1923
- this.pattern = this.pattern.replace(/\\/g, '/');
1924
- }
1925
- this.preserveMultipleSlashes = !!options.preserveMultipleSlashes;
1926
- this.regexp = null;
1927
- this.negate = false;
1928
- this.nonegate = !!options.nonegate;
1929
- this.comment = false;
1930
- this.empty = false;
1931
- this.partial = !!options.partial;
1932
- this.nocase = !!this.options.nocase;
1933
- this.windowsNoMagicRoot =
1934
- options.windowsNoMagicRoot !== undefined
1935
- ? options.windowsNoMagicRoot
1936
- : !!(this.isWindows && this.nocase);
1937
- this.globSet = [];
1938
- this.globParts = [];
1939
- this.set = [];
1940
- // make the set of regexps etc.
1941
- this.make();
1942
- }
1943
- hasMagic() {
1944
- if (this.options.magicalBraces && this.set.length > 1) {
1945
- return true;
1946
- }
1947
- for (const pattern of this.set) {
1948
- for (const part of pattern) {
1949
- if (typeof part !== 'string')
1950
- return true;
1951
- }
1952
- }
1953
- return false;
1954
- }
1955
- debug(..._) { }
1956
- make() {
1957
- const pattern = this.pattern;
1958
- const options = this.options;
1959
- // empty patterns and comments match nothing.
1960
- if (!options.nocomment && pattern.charAt(0) === '#') {
1961
- this.comment = true;
1962
- return;
1963
- }
1964
- if (!pattern) {
1965
- this.empty = true;
1966
- return;
1967
- }
1968
- // step 1: figure out negation, etc.
1969
- this.parseNegate();
1970
- // step 2: expand braces
1971
- this.globSet = [...new Set(this.braceExpand())];
1972
- if (options.debug) {
1973
- this.debug = (...args) => console.error(...args);
1974
- }
1975
- this.debug(this.pattern, this.globSet);
1976
- // step 3: now we have a set, so turn each one into a series of
1977
- // path-portion matching patterns.
1978
- // These will be regexps, except in the case of "**", which is
1979
- // set to the GLOBSTAR object for globstar behavior,
1980
- // and will not contain any / characters
1981
- //
1982
- // First, we preprocess to make the glob pattern sets a bit simpler
1983
- // and deduped. There are some perf-killing patterns that can cause
1984
- // problems with a glob walk, but we can simplify them down a bit.
1985
- const rawGlobParts = this.globSet.map(s => this.slashSplit(s));
1986
- this.globParts = this.preprocess(rawGlobParts);
1987
- this.debug(this.pattern, this.globParts);
1988
- // glob --> regexps
1989
- let set = this.globParts.map((s, _, __) => {
1990
- if (this.isWindows && this.windowsNoMagicRoot) {
1991
- // check if it's a drive or unc path.
1992
- const isUNC = s[0] === '' &&
1993
- s[1] === '' &&
1994
- (s[2] === '?' || !globMagic.test(s[2])) &&
1995
- !globMagic.test(s[3]);
1996
- const isDrive = /^[a-z]:/i.test(s[0]);
1997
- if (isUNC) {
1998
- return [...s.slice(0, 4), ...s.slice(4).map(ss => this.parse(ss))];
1999
- }
2000
- else if (isDrive) {
2001
- return [s[0], ...s.slice(1).map(ss => this.parse(ss))];
2002
- }
2003
- }
2004
- return s.map(ss => this.parse(ss));
2005
- });
2006
- this.debug(this.pattern, set);
2007
- // filter out everything that didn't compile properly.
2008
- this.set = set.filter(s => s.indexOf(false) === -1);
2009
- // do not treat the ? in UNC paths as magic
2010
- if (this.isWindows) {
2011
- for (let i = 0; i < this.set.length; i++) {
2012
- const p = this.set[i];
2013
- if (p[0] === '' &&
2014
- p[1] === '' &&
2015
- this.globParts[i][2] === '?' &&
2016
- typeof p[3] === 'string' &&
2017
- /^[a-z]:$/i.test(p[3])) {
2018
- p[2] = '?';
2019
- }
2020
- }
2021
- }
2022
- this.debug(this.pattern, this.set);
2023
- }
2024
- // various transforms to equivalent pattern sets that are
2025
- // faster to process in a filesystem walk. The goal is to
2026
- // eliminate what we can, and push all ** patterns as far
2027
- // to the right as possible, even if it increases the number
2028
- // of patterns that we have to process.
2029
- preprocess(globParts) {
2030
- // if we're not in globstar mode, then turn all ** into *
2031
- if (this.options.noglobstar) {
2032
- for (let i = 0; i < globParts.length; i++) {
2033
- for (let j = 0; j < globParts[i].length; j++) {
2034
- if (globParts[i][j] === '**') {
2035
- globParts[i][j] = '*';
2036
- }
2037
- }
2038
- }
2039
- }
2040
- const { optimizationLevel = 1 } = this.options;
2041
- if (optimizationLevel >= 2) {
2042
- // aggressive optimization for the purpose of fs walking
2043
- globParts = this.firstPhasePreProcess(globParts);
2044
- globParts = this.secondPhasePreProcess(globParts);
2045
- }
2046
- else if (optimizationLevel >= 1) {
2047
- // just basic optimizations to remove some .. parts
2048
- globParts = this.levelOneOptimize(globParts);
2049
- }
2050
- else {
2051
- // just collapse multiple ** portions into one
2052
- globParts = this.adjascentGlobstarOptimize(globParts);
2053
- }
2054
- return globParts;
2055
- }
2056
- // just get rid of adjascent ** portions
2057
- adjascentGlobstarOptimize(globParts) {
2058
- return globParts.map(parts => {
2059
- let gs = -1;
2060
- while (-1 !== (gs = parts.indexOf('**', gs + 1))) {
2061
- let i = gs;
2062
- while (parts[i + 1] === '**') {
2063
- i++;
2064
- }
2065
- if (i !== gs) {
2066
- parts.splice(gs, i - gs);
2067
- }
2068
- }
2069
- return parts;
2070
- });
2071
- }
2072
- // get rid of adjascent ** and resolve .. portions
2073
- levelOneOptimize(globParts) {
2074
- return globParts.map(parts => {
2075
- parts = parts.reduce((set, part) => {
2076
- const prev = set[set.length - 1];
2077
- if (part === '**' && prev === '**') {
2078
- return set;
2079
- }
2080
- if (part === '..') {
2081
- if (prev && prev !== '..' && prev !== '.' && prev !== '**') {
2082
- set.pop();
2083
- return set;
2084
- }
2085
- }
2086
- set.push(part);
2087
- return set;
2088
- }, []);
2089
- return parts.length === 0 ? [''] : parts;
2090
- });
2091
- }
2092
- levelTwoFileOptimize(parts) {
2093
- if (!Array.isArray(parts)) {
2094
- parts = this.slashSplit(parts);
2095
- }
2096
- let didSomething = false;
2097
- do {
2098
- didSomething = false;
2099
- // <pre>/<e>/<rest> -> <pre>/<rest>
2100
- if (!this.preserveMultipleSlashes) {
2101
- for (let i = 1; i < parts.length - 1; i++) {
2102
- const p = parts[i];
2103
- // don't squeeze out UNC patterns
2104
- if (i === 1 && p === '' && parts[0] === '')
2105
- continue;
2106
- if (p === '.' || p === '') {
2107
- didSomething = true;
2108
- parts.splice(i, 1);
2109
- i--;
2110
- }
2111
- }
2112
- if (parts[0] === '.' &&
2113
- parts.length === 2 &&
2114
- (parts[1] === '.' || parts[1] === '')) {
2115
- didSomething = true;
2116
- parts.pop();
2117
- }
2118
- }
2119
- // <pre>/<p>/../<rest> -> <pre>/<rest>
2120
- let dd = 0;
2121
- while (-1 !== (dd = parts.indexOf('..', dd + 1))) {
2122
- const p = parts[dd - 1];
2123
- if (p && p !== '.' && p !== '..' && p !== '**') {
2124
- didSomething = true;
2125
- parts.splice(dd - 1, 2);
2126
- dd -= 2;
2127
- }
2128
- }
2129
- } while (didSomething);
2130
- return parts.length === 0 ? [''] : parts;
2131
- }
2132
- // First phase: single-pattern processing
2133
- // <pre> is 1 or more portions
2134
- // <rest> is 1 or more portions
2135
- // <p> is any portion other than ., .., '', or **
2136
- // <e> is . or ''
2137
- //
2138
- // **/.. is *brutal* for filesystem walking performance, because
2139
- // it effectively resets the recursive walk each time it occurs,
2140
- // and ** cannot be reduced out by a .. pattern part like a regexp
2141
- // or most strings (other than .., ., and '') can be.
2142
- //
2143
- // <pre>/**/../<p>/<p>/<rest> -> {<pre>/../<p>/<p>/<rest>,<pre>/**/<p>/<p>/<rest>}
2144
- // <pre>/<e>/<rest> -> <pre>/<rest>
2145
- // <pre>/<p>/../<rest> -> <pre>/<rest>
2146
- // **/**/<rest> -> **/<rest>
2147
- //
2148
- // **/*/<rest> -> */**/<rest> <== not valid because ** doesn't follow
2149
- // this WOULD be allowed if ** did follow symlinks, or * didn't
2150
- firstPhasePreProcess(globParts) {
2151
- let didSomething = false;
2152
- do {
2153
- didSomething = false;
2154
- // <pre>/**/../<p>/<p>/<rest> -> {<pre>/../<p>/<p>/<rest>,<pre>/**/<p>/<p>/<rest>}
2155
- for (let parts of globParts) {
2156
- let gs = -1;
2157
- while (-1 !== (gs = parts.indexOf('**', gs + 1))) {
2158
- let gss = gs;
2159
- while (parts[gss + 1] === '**') {
2160
- // <pre>/**/**/<rest> -> <pre>/**/<rest>
2161
- gss++;
2162
- }
2163
- // eg, if gs is 2 and gss is 4, that means we have 3 **
2164
- // parts, and can remove 2 of them.
2165
- if (gss > gs) {
2166
- parts.splice(gs + 1, gss - gs);
2167
- }
2168
- let next = parts[gs + 1];
2169
- const p = parts[gs + 2];
2170
- const p2 = parts[gs + 3];
2171
- if (next !== '..')
2172
- continue;
2173
- if (!p ||
2174
- p === '.' ||
2175
- p === '..' ||
2176
- !p2 ||
2177
- p2 === '.' ||
2178
- p2 === '..') {
2179
- continue;
2180
- }
2181
- didSomething = true;
2182
- // edit parts in place, and push the new one
2183
- parts.splice(gs, 1);
2184
- const other = parts.slice(0);
2185
- other[gs] = '**';
2186
- globParts.push(other);
2187
- gs--;
2188
- }
2189
- // <pre>/<e>/<rest> -> <pre>/<rest>
2190
- if (!this.preserveMultipleSlashes) {
2191
- for (let i = 1; i < parts.length - 1; i++) {
2192
- const p = parts[i];
2193
- // don't squeeze out UNC patterns
2194
- if (i === 1 && p === '' && parts[0] === '')
2195
- continue;
2196
- if (p === '.' || p === '') {
2197
- didSomething = true;
2198
- parts.splice(i, 1);
2199
- i--;
2200
- }
2201
- }
2202
- if (parts[0] === '.' &&
2203
- parts.length === 2 &&
2204
- (parts[1] === '.' || parts[1] === '')) {
2205
- didSomething = true;
2206
- parts.pop();
2207
- }
2208
- }
2209
- // <pre>/<p>/../<rest> -> <pre>/<rest>
2210
- let dd = 0;
2211
- while (-1 !== (dd = parts.indexOf('..', dd + 1))) {
2212
- const p = parts[dd - 1];
2213
- if (p && p !== '.' && p !== '..' && p !== '**') {
2214
- didSomething = true;
2215
- const needDot = dd === 1 && parts[dd + 1] === '**';
2216
- const splin = needDot ? ['.'] : [];
2217
- parts.splice(dd - 1, 2, ...splin);
2218
- if (parts.length === 0)
2219
- parts.push('');
2220
- dd -= 2;
2221
- }
2222
- }
2223
- }
2224
- } while (didSomething);
2225
- return globParts;
2226
- }
2227
- // second phase: multi-pattern dedupes
2228
- // {<pre>/*/<rest>,<pre>/<p>/<rest>} -> <pre>/*/<rest>
2229
- // {<pre>/<rest>,<pre>/<rest>} -> <pre>/<rest>
2230
- // {<pre>/**/<rest>,<pre>/<rest>} -> <pre>/**/<rest>
2231
- //
2232
- // {<pre>/**/<rest>,<pre>/**/<p>/<rest>} -> <pre>/**/<rest>
2233
- // ^-- not valid because ** doens't follow symlinks
2234
- secondPhasePreProcess(globParts) {
2235
- for (let i = 0; i < globParts.length - 1; i++) {
2236
- for (let j = i + 1; j < globParts.length; j++) {
2237
- const matched = this.partsMatch(globParts[i], globParts[j], !this.preserveMultipleSlashes);
2238
- if (matched) {
2239
- globParts[i] = [];
2240
- globParts[j] = matched;
2241
- break;
2242
- }
2243
- }
2244
- }
2245
- return globParts.filter(gs => gs.length);
2246
- }
2247
- partsMatch(a, b, emptyGSMatch = false) {
2248
- let ai = 0;
2249
- let bi = 0;
2250
- let result = [];
2251
- let which = '';
2252
- while (ai < a.length && bi < b.length) {
2253
- if (a[ai] === b[bi]) {
2254
- result.push(which === 'b' ? b[bi] : a[ai]);
2255
- ai++;
2256
- bi++;
2257
- }
2258
- else if (emptyGSMatch && a[ai] === '**' && b[bi] === a[ai + 1]) {
2259
- result.push(a[ai]);
2260
- ai++;
2261
- }
2262
- else if (emptyGSMatch && b[bi] === '**' && a[ai] === b[bi + 1]) {
2263
- result.push(b[bi]);
2264
- bi++;
2265
- }
2266
- else if (a[ai] === '*' &&
2267
- b[bi] &&
2268
- (this.options.dot || !b[bi].startsWith('.')) &&
2269
- b[bi] !== '**') {
2270
- if (which === 'b')
2271
- return false;
2272
- which = 'a';
2273
- result.push(a[ai]);
2274
- ai++;
2275
- bi++;
2276
- }
2277
- else if (b[bi] === '*' &&
2278
- a[ai] &&
2279
- (this.options.dot || !a[ai].startsWith('.')) &&
2280
- a[ai] !== '**') {
2281
- if (which === 'a')
2282
- return false;
2283
- which = 'b';
2284
- result.push(b[bi]);
2285
- ai++;
2286
- bi++;
2287
- }
2288
- else {
2289
- return false;
2290
- }
2291
- }
2292
- // if we fall out of the loop, it means they two are identical
2293
- // as long as their lengths match
2294
- return a.length === b.length && result;
2295
- }
2296
- parseNegate() {
2297
- if (this.nonegate)
2298
- return;
2299
- const pattern = this.pattern;
2300
- let negate = false;
2301
- let negateOffset = 0;
2302
- for (let i = 0; i < pattern.length && pattern.charAt(i) === '!'; i++) {
2303
- negate = !negate;
2304
- negateOffset++;
2305
- }
2306
- if (negateOffset)
2307
- this.pattern = pattern.slice(negateOffset);
2308
- this.negate = negate;
2309
- }
2310
- // set partial to true to test if, for example,
2311
- // "/a/b" matches the start of "/*/b/*/d"
2312
- // Partial means, if you run out of file before you run
2313
- // out of pattern, then that's fine, as long as all
2314
- // the parts match.
2315
- matchOne(file, pattern, partial = false) {
2316
- const options = this.options;
2317
- // UNC paths like //?/X:/... can match X:/... and vice versa
2318
- // Drive letters in absolute drive or unc paths are always compared
2319
- // case-insensitively.
2320
- if (this.isWindows) {
2321
- const fileDrive = typeof file[0] === 'string' && /^[a-z]:$/i.test(file[0]);
2322
- const fileUNC = !fileDrive &&
2323
- file[0] === '' &&
2324
- file[1] === '' &&
2325
- file[2] === '?' &&
2326
- /^[a-z]:$/i.test(file[3]);
2327
- const patternDrive = typeof pattern[0] === 'string' && /^[a-z]:$/i.test(pattern[0]);
2328
- const patternUNC = !patternDrive &&
2329
- pattern[0] === '' &&
2330
- pattern[1] === '' &&
2331
- pattern[2] === '?' &&
2332
- typeof pattern[3] === 'string' &&
2333
- /^[a-z]:$/i.test(pattern[3]);
2334
- const fdi = fileUNC ? 3 : fileDrive ? 0 : undefined;
2335
- const pdi = patternUNC ? 3 : patternDrive ? 0 : undefined;
2336
- if (typeof fdi === 'number' && typeof pdi === 'number') {
2337
- const [fd, pd] = [file[fdi], pattern[pdi]];
2338
- if (fd.toLowerCase() === pd.toLowerCase()) {
2339
- pattern[pdi] = fd;
2340
- if (pdi > fdi) {
2341
- pattern = pattern.slice(pdi);
2342
- }
2343
- else if (fdi > pdi) {
2344
- file = file.slice(fdi);
2345
- }
2346
- }
2347
- }
2348
- }
2349
- // resolve and reduce . and .. portions in the file as well.
2350
- // don't need to do the second phase, because it's only one string[]
2351
- const { optimizationLevel = 1 } = this.options;
2352
- if (optimizationLevel >= 2) {
2353
- file = this.levelTwoFileOptimize(file);
2354
- }
2355
- this.debug('matchOne', this, { file, pattern });
2356
- this.debug('matchOne', file.length, pattern.length);
2357
- for (var fi = 0, pi = 0, fl = file.length, pl = pattern.length; fi < fl && pi < pl; fi++, pi++) {
2358
- this.debug('matchOne loop');
2359
- var p = pattern[pi];
2360
- var f = file[fi];
2361
- this.debug(pattern, p, f);
2362
- // should be impossible.
2363
- // some invalid regexp stuff in the set.
2364
- /* c8 ignore start */
2365
- if (p === false) {
2366
- return false;
2367
- }
2368
- /* c8 ignore stop */
2369
- if (p === GLOBSTAR) {
2370
- this.debug('GLOBSTAR', [pattern, p, f]);
2371
- // "**"
2372
- // a/**/b/**/c would match the following:
2373
- // a/b/x/y/z/c
2374
- // a/x/y/z/b/c
2375
- // a/b/x/b/x/c
2376
- // a/b/c
2377
- // To do this, take the rest of the pattern after
2378
- // the **, and see if it would match the file remainder.
2379
- // If so, return success.
2380
- // If not, the ** "swallows" a segment, and try again.
2381
- // This is recursively awful.
2382
- //
2383
- // a/**/b/**/c matching a/b/x/y/z/c
2384
- // - a matches a
2385
- // - doublestar
2386
- // - matchOne(b/x/y/z/c, b/**/c)
2387
- // - b matches b
2388
- // - doublestar
2389
- // - matchOne(x/y/z/c, c) -> no
2390
- // - matchOne(y/z/c, c) -> no
2391
- // - matchOne(z/c, c) -> no
2392
- // - matchOne(c, c) yes, hit
2393
- var fr = fi;
2394
- var pr = pi + 1;
2395
- if (pr === pl) {
2396
- this.debug('** at the end');
2397
- // a ** at the end will just swallow the rest.
2398
- // We have found a match.
2399
- // however, it will not swallow /.x, unless
2400
- // options.dot is set.
2401
- // . and .. are *never* matched by **, for explosively
2402
- // exponential reasons.
2403
- for (; fi < fl; fi++) {
2404
- if (file[fi] === '.' ||
2405
- file[fi] === '..' ||
2406
- (!options.dot && file[fi].charAt(0) === '.'))
2407
- return false;
2408
- }
2409
- return true;
2410
- }
2411
- // ok, let's see if we can swallow whatever we can.
2412
- while (fr < fl) {
2413
- var swallowee = file[fr];
2414
- this.debug('\nglobstar while', file, fr, pattern, pr, swallowee);
2415
- // XXX remove this slice. Just pass the start index.
2416
- if (this.matchOne(file.slice(fr), pattern.slice(pr), partial)) {
2417
- this.debug('globstar found match!', fr, fl, swallowee);
2418
- // found a match.
2419
- return true;
2420
- }
2421
- else {
2422
- // can't swallow "." or ".." ever.
2423
- // can only swallow ".foo" when explicitly asked.
2424
- if (swallowee === '.' ||
2425
- swallowee === '..' ||
2426
- (!options.dot && swallowee.charAt(0) === '.')) {
2427
- this.debug('dot detected!', file, fr, pattern, pr);
2428
- break;
2429
- }
2430
- // ** swallows a segment, and continue.
2431
- this.debug('globstar swallow a segment, and continue');
2432
- fr++;
2433
- }
2434
- }
2435
- // no match was found.
2436
- // However, in partial mode, we can't say this is necessarily over.
2437
- /* c8 ignore start */
2438
- if (partial) {
2439
- // ran out of file
2440
- this.debug('\n>>> no match, partial?', file, fr, pattern, pr);
2441
- if (fr === fl) {
2442
- return true;
2443
- }
2444
- }
2445
- /* c8 ignore stop */
2446
- return false;
2447
- }
2448
- // something other than **
2449
- // non-magic patterns just have to match exactly
2450
- // patterns with magic have been turned into regexps.
2451
- let hit;
2452
- if (typeof p === 'string') {
2453
- hit = f === p;
2454
- this.debug('string match', p, f, hit);
2455
- }
2456
- else {
2457
- hit = p.test(f);
2458
- this.debug('pattern match', p, f, hit);
2459
- }
2460
- if (!hit)
2461
- return false;
2462
- }
2463
- // Note: ending in / means that we'll get a final ""
2464
- // at the end of the pattern. This can only match a
2465
- // corresponding "" at the end of the file.
2466
- // If the file ends in /, then it can only match a
2467
- // a pattern that ends in /, unless the pattern just
2468
- // doesn't have any more for it. But, a/b/ should *not*
2469
- // match "a/b/*", even though "" matches against the
2470
- // [^/]*? pattern, except in partial mode, where it might
2471
- // simply not be reached yet.
2472
- // However, a/b/ should still satisfy a/*
2473
- // now either we fell off the end of the pattern, or we're done.
2474
- if (fi === fl && pi === pl) {
2475
- // ran out of pattern and filename at the same time.
2476
- // an exact hit!
2477
- return true;
2478
- }
2479
- else if (fi === fl) {
2480
- // ran out of file, but still had pattern left.
2481
- // this is ok if we're doing the match as part of
2482
- // a glob fs traversal.
2483
- return partial;
2484
- }
2485
- else if (pi === pl) {
2486
- // ran out of pattern, still have file left.
2487
- // this is only acceptable if we're on the very last
2488
- // empty segment of a file with a trailing slash.
2489
- // a/* should match a/b/
2490
- return fi === fl - 1 && file[fi] === '';
2491
- /* c8 ignore start */
2492
- }
2493
- else {
2494
- // should be unreachable.
2495
- throw new Error('wtf?');
2496
- }
2497
- /* c8 ignore stop */
2498
- }
2499
- braceExpand() {
2500
- return braceExpand(this.pattern, this.options);
2501
- }
2502
- parse(pattern) {
2503
- assertValidPattern(pattern);
2504
- const options = this.options;
2505
- // shortcuts
2506
- if (pattern === '**')
2507
- return GLOBSTAR;
2508
- if (pattern === '')
2509
- return '';
2510
- // far and away, the most common glob pattern parts are
2511
- // *, *.*, and *.<ext> Add a fast check method for those.
2512
- let m;
2513
- let fastTest = null;
2514
- if ((m = pattern.match(starRE))) {
2515
- fastTest = options.dot ? starTestDot : starTest;
2516
- }
2517
- else if ((m = pattern.match(starDotExtRE))) {
2518
- fastTest = (options.nocase
2519
- ? options.dot
2520
- ? starDotExtTestNocaseDot
2521
- : starDotExtTestNocase
2522
- : options.dot
2523
- ? starDotExtTestDot
2524
- : starDotExtTest)(m[1]);
2525
- }
2526
- else if ((m = pattern.match(qmarksRE))) {
2527
- fastTest = (options.nocase
2528
- ? options.dot
2529
- ? qmarksTestNocaseDot
2530
- : qmarksTestNocase
2531
- : options.dot
2532
- ? qmarksTestDot
2533
- : qmarksTest)(m);
2534
- }
2535
- else if ((m = pattern.match(starDotStarRE))) {
2536
- fastTest = options.dot ? starDotStarTestDot : starDotStarTest;
2537
- }
2538
- else if ((m = pattern.match(dotStarRE))) {
2539
- fastTest = dotStarTest;
2540
- }
2541
- const re = AST.fromGlob(pattern, this.options).toMMPattern();
2542
- if (fastTest && typeof re === 'object') {
2543
- // Avoids overriding in frozen environments
2544
- Reflect.defineProperty(re, 'test', { value: fastTest });
2545
- }
2546
- return re;
2547
- }
2548
- makeRe() {
2549
- if (this.regexp || this.regexp === false)
2550
- return this.regexp;
2551
- // at this point, this.set is a 2d array of partial
2552
- // pattern strings, or "**".
2553
- //
2554
- // It's better to use .match(). This function shouldn't
2555
- // be used, really, but it's pretty convenient sometimes,
2556
- // when you just want to work with a regex.
2557
- const set = this.set;
2558
- if (!set.length) {
2559
- this.regexp = false;
2560
- return this.regexp;
2561
- }
2562
- const options = this.options;
2563
- const twoStar = options.noglobstar
2564
- ? star
2565
- : options.dot
2566
- ? twoStarDot
2567
- : twoStarNoDot;
2568
- const flags = new Set(options.nocase ? ['i'] : []);
2569
- // regexpify non-globstar patterns
2570
- // if ** is only item, then we just do one twoStar
2571
- // if ** is first, and there are more, prepend (\/|twoStar\/)? to next
2572
- // if ** is last, append (\/twoStar|) to previous
2573
- // if ** is in the middle, append (\/|\/twoStar\/) to previous
2574
- // then filter out GLOBSTAR symbols
2575
- let re = set
2576
- .map(pattern => {
2577
- const pp = pattern.map(p => {
2578
- if (p instanceof RegExp) {
2579
- for (const f of p.flags.split(''))
2580
- flags.add(f);
2581
- }
2582
- return typeof p === 'string'
2583
- ? regExpEscape(p)
2584
- : p === GLOBSTAR
2585
- ? GLOBSTAR
2586
- : p._src;
2587
- });
2588
- pp.forEach((p, i) => {
2589
- const next = pp[i + 1];
2590
- const prev = pp[i - 1];
2591
- if (p !== GLOBSTAR || prev === GLOBSTAR) {
2592
- return;
2593
- }
2594
- if (prev === undefined) {
2595
- if (next !== undefined && next !== GLOBSTAR) {
2596
- pp[i + 1] = '(?:\\/|' + twoStar + '\\/)?' + next;
2597
- }
2598
- else {
2599
- pp[i] = twoStar;
2600
- }
2601
- }
2602
- else if (next === undefined) {
2603
- pp[i - 1] = prev + '(?:\\/|\\/' + twoStar + ')?';
2604
- }
2605
- else if (next !== GLOBSTAR) {
2606
- pp[i - 1] = prev + '(?:\\/|\\/' + twoStar + '\\/)' + next;
2607
- pp[i + 1] = GLOBSTAR;
2608
- }
2609
- });
2610
- const filtered = pp.filter(p => p !== GLOBSTAR);
2611
- // For partial matches, we need to make the pattern match
2612
- // any prefix of the full path. We do this by generating
2613
- // alternative patterns that match progressively longer prefixes.
2614
- if (this.partial && filtered.length >= 1) {
2615
- const prefixes = [];
2616
- for (let i = 1; i <= filtered.length; i++) {
2617
- prefixes.push(filtered.slice(0, i).join('/'));
2618
- }
2619
- return '(?:' + prefixes.join('|') + ')';
2620
- }
2621
- return filtered.join('/');
2622
- })
2623
- .join('|');
2624
- // need to wrap in parens if we had more than one thing with |,
2625
- // otherwise only the first will be anchored to ^ and the last to $
2626
- const [open, close] = set.length > 1 ? ['(?:', ')'] : ['', ''];
2627
- // must match entire pattern
2628
- // ending in a * or ** will make it less strict.
2629
- re = '^' + open + re + close + '$';
2630
- // In partial mode, '/' should always match as it's a valid prefix for any pattern
2631
- if (this.partial) {
2632
- re = '^(?:\\/|' + open + re.slice(1, -1) + close + ')$';
2633
- }
2634
- // can match anything, as long as it's not this.
2635
- if (this.negate)
2636
- re = '^(?!' + re + ').+$';
2637
- try {
2638
- this.regexp = new RegExp(re, [...flags].join(''));
2639
- /* c8 ignore start */
2640
- }
2641
- catch (ex) {
2642
- // should be impossible
2643
- this.regexp = false;
2644
- }
2645
- /* c8 ignore stop */
2646
- return this.regexp;
2647
- }
2648
- slashSplit(p) {
2649
- // if p starts with // on windows, we preserve that
2650
- // so that UNC paths aren't broken. Otherwise, any number of
2651
- // / characters are coalesced into one, unless
2652
- // preserveMultipleSlashes is set to true.
2653
- if (this.preserveMultipleSlashes) {
2654
- return p.split('/');
2655
- }
2656
- else if (this.isWindows && /^\/\/[^\/]+/.test(p)) {
2657
- // add an extra '' for the one we lose
2658
- return ['', ...p.split(/\/+/)];
2659
- }
2660
- else {
2661
- return p.split(/\/+/);
2662
- }
2663
- }
2664
- match(f, partial = this.partial) {
2665
- this.debug('match', f, this.pattern);
2666
- // short-circuit in the case of busted things.
2667
- // comments, etc.
2668
- if (this.comment) {
2669
- return false;
2670
- }
2671
- if (this.empty) {
2672
- return f === '';
2673
- }
2674
- if (f === '/' && partial) {
2675
- return true;
2676
- }
2677
- const options = this.options;
2678
- // windows: need to use /, not \
2679
- if (this.isWindows) {
2680
- f = f.split('\\').join('/');
2681
- }
2682
- // treat the test path as a set of pathparts.
2683
- const ff = this.slashSplit(f);
2684
- this.debug(this.pattern, 'split', ff);
2685
- // just ONE of the pattern sets in this.set needs to match
2686
- // in order for it to be valid. If negating, then just one
2687
- // match means that we have failed.
2688
- // Either way, return on the first hit.
2689
- const set = this.set;
2690
- this.debug(this.pattern, 'set', set);
2691
- // Find the basename of the path by looking for the last non-empty segment
2692
- let filename = ff[ff.length - 1];
2693
- if (!filename) {
2694
- for (let i = ff.length - 2; !filename && i >= 0; i--) {
2695
- filename = ff[i];
2696
- }
2697
- }
2698
- for (let i = 0; i < set.length; i++) {
2699
- const pattern = set[i];
2700
- let file = ff;
2701
- if (options.matchBase && pattern.length === 1) {
2702
- file = [filename];
2703
- }
2704
- const hit = this.matchOne(file, pattern, partial);
2705
- if (hit) {
2706
- if (options.flipNegate) {
2707
- return true;
2708
- }
2709
- return !this.negate;
2710
- }
2711
- }
2712
- // didn't get any hits. this is success if it's a negative
2713
- // pattern, failure otherwise.
2714
- if (options.flipNegate) {
2715
- return false;
2716
- }
2717
- return this.negate;
2718
- }
2719
- static defaults(def) {
2720
- return minimatch.defaults(def).Minimatch;
2721
- }
2722
- }
2723
- /* c8 ignore stop */
2724
- minimatch.AST = AST;
2725
- minimatch.Minimatch = Minimatch;
2726
- minimatch.escape = escape;
2727
- minimatch.unescape = unescape;
2728
-
2729
675
  /**
2730
676
  * Default configuration for rules when defaultConfig is not specified.
2731
677
  * Custom rules can omit defaultConfig and will use these defaults.
@@ -5512,34 +3458,104 @@ class HTMLNoBlockInsideInlineRule extends ParserRule {
5512
3458
  }
5513
3459
  }
5514
3460
 
5515
- class NoDuplicateAttributesVisitor extends AttributeVisitorMixin {
5516
- attributeNames = new Map();
3461
+ class NoDuplicateAttributesVisitor extends ControlFlowTrackingVisitor {
3462
+ tagAttributes = new Set();
3463
+ currentBranchAttributes = new Set();
3464
+ controlFlowAttributes = new Set();
5517
3465
  visitHTMLOpenTagNode(node) {
5518
- this.attributeNames.clear();
3466
+ this.tagAttributes = new Set();
3467
+ this.currentBranchAttributes = new Set();
3468
+ this.controlFlowAttributes = new Set();
5519
3469
  super.visitHTMLOpenTagNode(node);
5520
- this.reportDuplicates();
5521
3470
  }
5522
- checkStaticAttributeStaticValue({ attributeName, attributeNode }) {
5523
- this.trackAttributeName(attributeName, attributeNode);
3471
+ visitHTMLAttributeNode(node) {
3472
+ this.checkAttribute(node);
5524
3473
  }
5525
- checkStaticAttributeDynamicValue({ attributeName, attributeNode }) {
5526
- this.trackAttributeName(attributeName, attributeNode);
3474
+ onEnterControlFlow(_controlFlowType, wasAlreadyInControlFlow) {
3475
+ const stateToRestore = {
3476
+ previousBranchAttributes: this.currentBranchAttributes,
3477
+ previousControlFlowAttributes: this.controlFlowAttributes,
3478
+ };
3479
+ this.currentBranchAttributes = new Set();
3480
+ if (!wasAlreadyInControlFlow) {
3481
+ this.controlFlowAttributes = new Set();
3482
+ }
3483
+ return stateToRestore;
5527
3484
  }
5528
- trackAttributeName(attributeName, attributeNode) {
5529
- if (!this.attributeNames.has(attributeName)) {
5530
- this.attributeNames.set(attributeName, []);
3485
+ onExitControlFlow(controlFlowType, wasAlreadyInControlFlow, stateToRestore) {
3486
+ if (controlFlowType === exports.ControlFlowType.CONDITIONAL && !wasAlreadyInControlFlow) {
3487
+ this.controlFlowAttributes.forEach((attr) => this.tagAttributes.add(attr));
5531
3488
  }
5532
- this.attributeNames.get(attributeName).push(attributeNode);
3489
+ this.currentBranchAttributes = stateToRestore.previousBranchAttributes;
3490
+ this.controlFlowAttributes = stateToRestore.previousControlFlowAttributes;
5533
3491
  }
5534
- reportDuplicates() {
5535
- for (const [attributeName, attributeNodes] of this.attributeNames) {
5536
- if (attributeNodes.length > 1) {
5537
- for (let i = 1; i < attributeNodes.length; i++) {
5538
- const attributeNode = attributeNodes[i];
5539
- this.addOffense(`Duplicate attribute \`${attributeName}\` found on tag. Remove the duplicate occurrence.`, attributeNode.name.location);
5540
- }
5541
- }
3492
+ onEnterBranch() {
3493
+ const stateToRestore = {
3494
+ previousBranchAttributes: this.currentBranchAttributes,
3495
+ };
3496
+ if (this.isInControlFlow) {
3497
+ this.currentBranchAttributes = new Set();
3498
+ }
3499
+ return stateToRestore;
3500
+ }
3501
+ onExitBranch(_stateToRestore) { }
3502
+ checkAttribute(attributeNode) {
3503
+ const identifier = getAttributeName(attributeNode);
3504
+ if (!identifier)
3505
+ return;
3506
+ this.processAttributeDuplicate(identifier, attributeNode);
3507
+ }
3508
+ processAttributeDuplicate(identifier, attributeNode) {
3509
+ if (!this.isInControlFlow) {
3510
+ this.handleHTMLAttribute(identifier, attributeNode);
3511
+ return;
3512
+ }
3513
+ if (this.currentControlFlowType === exports.ControlFlowType.LOOP) {
3514
+ this.handleLoopAttribute(identifier, attributeNode);
3515
+ }
3516
+ else {
3517
+ this.handleConditionalAttribute(identifier, attributeNode);
3518
+ }
3519
+ this.currentBranchAttributes.add(identifier);
3520
+ }
3521
+ handleHTMLAttribute(identifier, attributeNode) {
3522
+ if (this.tagAttributes.has(identifier)) {
3523
+ this.addDuplicateAttributeOffense(identifier, attributeNode.name.location);
3524
+ }
3525
+ this.tagAttributes.add(identifier);
3526
+ }
3527
+ handleLoopAttribute(identifier, attributeNode) {
3528
+ if (this.currentBranchAttributes.has(identifier)) {
3529
+ this.addSameLoopIterationOffense(identifier, attributeNode.name.location);
3530
+ return;
3531
+ }
3532
+ if (this.tagAttributes.has(identifier)) {
3533
+ this.addDuplicateAttributeOffense(identifier, attributeNode.name.location);
3534
+ return;
3535
+ }
3536
+ this.addLoopWillDuplicateOffense(identifier, attributeNode.name.location);
3537
+ }
3538
+ handleConditionalAttribute(identifier, attributeNode) {
3539
+ if (this.currentBranchAttributes.has(identifier)) {
3540
+ this.addSameBranchOffense(identifier, attributeNode.name.location);
3541
+ return;
3542
+ }
3543
+ if (this.tagAttributes.has(identifier)) {
3544
+ this.addDuplicateAttributeOffense(identifier, attributeNode.name.location);
5542
3545
  }
3546
+ this.controlFlowAttributes.add(identifier);
3547
+ }
3548
+ addDuplicateAttributeOffense(identifier, location) {
3549
+ this.addOffense(`Duplicate attribute \`${identifier}\`. Browsers only use the first occurrence and ignore duplicate attributes. Remove the duplicate or merge the values.`, location);
3550
+ }
3551
+ addSameLoopIterationOffense(identifier, location) {
3552
+ this.addOffense(`Duplicate attribute \`${identifier}\` in same loop iteration. Each iteration will produce an element with duplicate attributes. Remove one or merge the values.`, location);
3553
+ }
3554
+ addLoopWillDuplicateOffense(identifier, location) {
3555
+ this.addOffense(`Attribute \`${identifier}\` inside loop will appear multiple times on this element. Use a dynamic attribute name like \`${identifier}-<%= index %>\` or move the attribute outside the loop.`, location);
3556
+ }
3557
+ addSameBranchOffense(identifier, location) {
3558
+ this.addOffense(`Duplicate attribute \`${identifier}\` in same branch. This branch will produce an element with duplicate attributes. Remove one or merge the values.`, location);
5543
3559
  }
5544
3560
  }
5545
3561
  class HTMLNoDuplicateAttributesRule extends ParserRule {
@@ -6860,7 +4876,7 @@ class Linter {
6860
4876
  if (context?.fileName && !this.config?.linter?.rules?.[rule.name]?.exclude) {
6861
4877
  const defaultExclude = rule.defaultConfig?.exclude ?? DEFAULT_RULE_CONFIG.exclude;
6862
4878
  if (defaultExclude && defaultExclude.length > 0) {
6863
- const isExcluded = defaultExclude.some((pattern) => minimatch(context.fileName, pattern));
4879
+ const isExcluded = defaultExclude.some(pattern => picomatch.isMatch(context.fileName, pattern));
6864
4880
  if (isExcluded) {
6865
4881
  return [];
6866
4882
  }