cspell-glob 5.12.2 → 5.12.3

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.
@@ -41,6 +41,12 @@ interface NormalizedGlobMatchOptions {
41
41
  * @default: require('path')
42
42
  */
43
43
  nodePath: PathInterface;
44
+ /**
45
+ * Disable brace matching, so that `{a,b}` and `{1..3}` would be treated as literal characters.
46
+ *
47
+ * @default false
48
+ */
49
+ nobrace?: boolean;
44
50
  }
45
51
  export declare class GlobMatcher {
46
52
  /**
@@ -25,13 +25,15 @@ const Path = __importStar(require("path"));
25
25
  const globHelper_1 = require("./globHelper");
26
26
  class GlobMatcher {
27
27
  constructor(patterns, rootOrOptions, _nodePath) {
28
+ var _a;
28
29
  _nodePath = _nodePath !== null && _nodePath !== void 0 ? _nodePath : Path;
29
- const options = typeof rootOrOptions === 'string' ? { root: rootOrOptions, nodePath: _nodePath } : rootOrOptions !== null && rootOrOptions !== void 0 ? rootOrOptions : {};
30
+ const options = typeof rootOrOptions === 'string' ? { root: rootOrOptions } : rootOrOptions !== null && rootOrOptions !== void 0 ? rootOrOptions : {};
30
31
  const { mode = 'exclude' } = options;
31
32
  const isExcludeMode = mode !== 'include';
32
- const { root = _nodePath.resolve(), dot = isExcludeMode, nodePath = _nodePath, nested = isExcludeMode, } = options;
33
+ _nodePath = (_a = options.nodePath) !== null && _a !== void 0 ? _a : _nodePath;
34
+ const { root = _nodePath.resolve(), dot = isExcludeMode, nodePath = _nodePath, nested = isExcludeMode, nobrace, } = options;
33
35
  const normalizedRoot = nodePath.resolve(nodePath.normalize(root));
34
- this.options = { root: normalizedRoot, dot, nodePath, nested, mode };
36
+ this.options = { root: normalizedRoot, dot, nodePath, nested, mode, nobrace };
35
37
  patterns = Array.isArray(patterns)
36
38
  ? patterns
37
39
  : typeof patterns === 'string'
@@ -74,7 +76,8 @@ exports.GlobMatcher = GlobMatcher;
74
76
  * @returns a function given a filename returns true if it matches.
75
77
  */
76
78
  function buildMatcherFn(patterns, options) {
77
- const path = options.nodePath;
79
+ const { nodePath: path, dot, nobrace } = options;
80
+ const makeReOptions = { dot, nobrace };
78
81
  const rules = patterns
79
82
  .map((pattern, index) => ({ pattern, index }))
80
83
  .filter((r) => !!r.pattern.glob)
@@ -83,7 +86,7 @@ function buildMatcherFn(patterns, options) {
83
86
  const matchNeg = pattern.glob.match(/^!/);
84
87
  const glob = pattern.glob.replace(/^!/, '');
85
88
  const isNeg = (matchNeg && matchNeg[0].length & 1 && true) || false;
86
- const reg = mm.makeRe(glob, { dot: options.dot });
89
+ const reg = mm.makeRe(glob, makeReOptions);
87
90
  const fn = (filename) => {
88
91
  const match = filename.match(reg);
89
92
  return !!match;
@@ -34,6 +34,10 @@ export interface GlobPatternWithOptionalRoot {
34
34
  * Optional value useful for tracing which file a glob pattern was defined in.
35
35
  */
36
36
  source?: string;
37
+ /**
38
+ * Optional line number in the source
39
+ */
40
+ line?: number;
37
41
  }
38
42
  export interface GlobPatternWithRoot extends GlobPatternWithOptionalRoot {
39
43
  root: string;
@@ -14,6 +14,7 @@ export declare function fileOrGlobToGlob(fileOrGlob: string | GlobPattern, root:
14
14
  export declare function doesRootContainPath(root: string, child: string, path: PathInterface): boolean;
15
15
  export declare function isGlobPatternWithOptionalRoot(g: GlobPattern): g is GlobPatternWithOptionalRoot;
16
16
  export declare function isGlobPatternWithRoot(g: GlobPatternWithRoot | GlobPatternWithOptionalRoot): g is GlobPatternWithRoot;
17
+ export declare function isGlobPatternNormalized(g: GlobPattern | GlobPatternNormalized): g is GlobPatternNormalized;
17
18
  export interface NormalizeOptions {
18
19
  nested: boolean;
19
20
  root: string;
@@ -34,15 +35,21 @@ export declare function normalizeGlobPatterns(patterns: GlobPattern[], options:
34
35
  * @param path - Node Path modules to use (testing only)
35
36
  */
36
37
  export declare function normalizeGlobToRoot<Glob extends GlobPatternWithRoot>(glob: Glob, root: string, path: PathInterface): Glob;
37
- export declare function isGlobPatternNormalized(p: GlobPatternWithRoot | GlobPatternNormalized): p is GlobPatternNormalized;
38
38
  /**
39
39
  * Rebase a glob string to a new prefix
40
40
  * @param glob - glob string
41
41
  * @param rebaseTo - glob prefix
42
42
  */
43
43
  declare function rebaseGlob(glob: string, rebaseTo: string): string | undefined;
44
+ /**
45
+ * Trims any trailing spaces, tabs, line-feeds, new-lines, and comments
46
+ * @param glob - glob string
47
+ * @returns trimmed glob
48
+ */
49
+ declare function trimGlob(glob: string): string;
44
50
  export declare const __testing__: {
45
51
  rebaseGlob: typeof rebaseGlob;
52
+ trimGlob: typeof trimGlob;
46
53
  };
47
54
  export {};
48
55
  //# sourceMappingURL=globHelper.d.ts.map
@@ -19,7 +19,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
19
19
  return result;
20
20
  };
21
21
  Object.defineProperty(exports, "__esModule", { value: true });
22
- exports.__testing__ = exports.isGlobPatternNormalized = exports.normalizeGlobToRoot = exports.normalizeGlobPatterns = exports.isGlobPatternWithRoot = exports.isGlobPatternWithOptionalRoot = exports.doesRootContainPath = exports.fileOrGlobToGlob = void 0;
22
+ exports.__testing__ = exports.normalizeGlobToRoot = exports.normalizeGlobPatterns = exports.isGlobPatternNormalized = exports.isGlobPatternWithRoot = exports.isGlobPatternWithOptionalRoot = exports.doesRootContainPath = exports.fileOrGlobToGlob = void 0;
23
23
  const Path = __importStar(require("path"));
24
24
  const { posix } = Path;
25
25
  const relRegExp = /^\.[\\/]/;
@@ -59,16 +59,6 @@ function doesRootContainPath(root, child, path) {
59
59
  return !rel || (rel !== child && !rel.startsWith('..') && !path.isAbsolute(rel));
60
60
  }
61
61
  exports.doesRootContainPath = doesRootContainPath;
62
- const mutationsNestedOnly = [
63
- [/^[/]([^/]*)$/, '{$1,$1/**}'],
64
- [/^[^/#][^/]*$/, '**/{$&,$&/**}'],
65
- [/^[^/#][^/]*\/$/, '**/$&**/*'], // ending slash, should match any nested directory
66
- ];
67
- const mutationsGeneral = [
68
- [/^\//, ''],
69
- [/\/$/, '$&**/*'], // if it ends in a slash, make sure matches the folder
70
- ];
71
- const mutationsNested = mutationsNestedOnly.concat(mutationsGeneral);
72
62
  function isGlobPatternWithOptionalRoot(g) {
73
63
  return typeof g !== 'string' && typeof g.glob === 'string';
74
64
  }
@@ -77,13 +67,45 @@ function isGlobPatternWithRoot(g) {
77
67
  return !!g.root;
78
68
  }
79
69
  exports.isGlobPatternWithRoot = isGlobPatternWithRoot;
70
+ function isGlobPatternNormalized(g) {
71
+ if (!isGlobPatternWithOptionalRoot(g))
72
+ return false;
73
+ if (!isGlobPatternWithRoot(g))
74
+ return false;
75
+ const gr = g;
76
+ return 'rawGlob' in gr && 'rawRoot' in gr && typeof gr.rawGlob === 'string';
77
+ }
78
+ exports.isGlobPatternNormalized = isGlobPatternNormalized;
80
79
  function normalizePattern(pattern, nested) {
81
80
  pattern = pattern.replace(/^(!!)+/, '');
82
81
  const isNeg = pattern.startsWith('!');
82
+ const prefix = isNeg ? '!' : '';
83
83
  pattern = isNeg ? pattern.slice(1) : pattern;
84
- const mutations = nested ? mutationsNested : mutationsGeneral;
85
- pattern = mutations.reduce((p, [regex, replace]) => p.replace(regex, replace), pattern);
86
- return isNeg ? '!' + pattern : pattern;
84
+ const patterns = nested ? normalizePatternNested(pattern) : normalizePatternGeneral(pattern);
85
+ return patterns.map((p) => prefix + p);
86
+ }
87
+ function normalizePatternNested(pattern) {
88
+ // no slashes will match files names or folders
89
+ if (!pattern.includes('/')) {
90
+ if (pattern === '**')
91
+ return ['**'];
92
+ return ['**/' + pattern, '**/' + pattern + '/**'];
93
+ }
94
+ const hasLeadingSlash = pattern.startsWith('/');
95
+ pattern = hasLeadingSlash ? pattern.slice(1) : pattern;
96
+ if (pattern.endsWith('/')) {
97
+ // legacy behavior, if it only has a trailing slash, allow matching against a nested directory.
98
+ return hasLeadingSlash || pattern.slice(0, -1).includes('/') ? [pattern + '**/*'] : ['**/' + pattern + '**/*'];
99
+ }
100
+ if (pattern.endsWith('**')) {
101
+ return [pattern];
102
+ }
103
+ return [pattern, pattern + '/**'];
104
+ }
105
+ function normalizePatternGeneral(pattern) {
106
+ pattern = pattern.startsWith('/') ? pattern.slice(1) : pattern;
107
+ pattern = pattern.endsWith('/') ? pattern + '**/*' : pattern;
108
+ return [pattern];
87
109
  }
88
110
  /**
89
111
  *
@@ -91,25 +113,32 @@ function normalizePattern(pattern, nested) {
91
113
  * @param options - Normalization options.
92
114
  */
93
115
  function normalizeGlobPatterns(patterns, options) {
94
- return patterns.map((g) => normalizeGlobPatternWithRoot(g, options));
116
+ function* normalize() {
117
+ for (const glob of patterns) {
118
+ if (isGlobPatternNormalized(glob)) {
119
+ yield glob;
120
+ continue;
121
+ }
122
+ yield* normalizeGlobPatternWithRoot(glob, options);
123
+ }
124
+ }
125
+ return [...normalize()];
95
126
  }
96
127
  exports.normalizeGlobPatterns = normalizeGlobPatterns;
97
128
  function normalizeGlobPatternWithRoot(g, options) {
129
+ var _a;
98
130
  const { root, nodePath: path, nested } = options;
99
- g = !isGlobPatternWithOptionalRoot(g)
100
- ? {
101
- glob: g.trim(),
102
- root,
103
- }
104
- : g;
105
- const gr = isGlobPatternWithRoot(g) ? { ...g } : { ...g, root };
131
+ g = !isGlobPatternWithOptionalRoot(g) ? { glob: g } : g;
132
+ const gr = { ...g, root: (_a = g.root) !== null && _a !== void 0 ? _a : root };
133
+ const rawRoot = gr.root;
134
+ const rawGlob = g.glob;
135
+ gr.glob = gr.glob.trim(); // trimGlob(g.glob);
106
136
  if (gr.root.startsWith('${cwd}')) {
107
137
  gr.root = path.join(path.resolve(), gr.root.replace('${cwd}', ''));
108
138
  }
109
139
  gr.root = path.resolve(root, path.normalize(gr.root));
110
- gr.glob = normalizePattern(gr.glob, nested);
111
- const gn = { ...gr, rawGlob: g.glob, rawRoot: g.root };
112
- return gn;
140
+ const globs = normalizePattern(gr.glob, nested);
141
+ return globs.map((glob) => ({ ...gr, glob, rawGlob, rawRoot }));
113
142
  }
114
143
  /**
115
144
  * Try to adjust the root of a glob to match a new root. If it is not possible, the original glob is returned.
@@ -157,10 +186,6 @@ function normalizeGlobToRoot(glob, root, path) {
157
186
  return rebasedGlob ? { ...glob, glob: prefix + rebasedGlob, root } : glob;
158
187
  }
159
188
  exports.normalizeGlobToRoot = normalizeGlobToRoot;
160
- function isGlobPatternNormalized(p) {
161
- return p.rawGlob !== undefined;
162
- }
163
- exports.isGlobPatternNormalized = isGlobPatternNormalized;
164
189
  /**
165
190
  * Rebase a glob string to a new prefix
166
191
  * @param glob - glob string
@@ -190,7 +215,55 @@ function rebaseGlob(glob, rebaseTo) {
190
215
  }
191
216
  return undefined;
192
217
  }
218
+ /**
219
+ * Trims any trailing spaces, tabs, line-feeds, new-lines, and comments
220
+ * @param glob - glob string
221
+ * @returns trimmed glob
222
+ */
223
+ function trimGlob(glob) {
224
+ glob = glob.replace(/(?<!\\)#.*/g, '');
225
+ glob = trimGlobLeft(glob);
226
+ glob = trimGlobRight(glob);
227
+ return glob;
228
+ }
229
+ const spaces = {
230
+ ' ': true,
231
+ '\t': true,
232
+ '\n': true,
233
+ '\r': true,
234
+ };
235
+ /**
236
+ * Trim any trailing spaces, tabs, line-feeds, or new-lines
237
+ * Handles a trailing \<space>
238
+ * @param glob - glob string
239
+ * @returns glob string with space to the right removed.
240
+ */
241
+ function trimGlobRight(glob) {
242
+ const lenMin1 = glob.length - 1;
243
+ let i = lenMin1;
244
+ while (i >= 0 && glob[i] in spaces) {
245
+ --i;
246
+ }
247
+ if (glob[i] === '\\' && i < lenMin1) {
248
+ ++i;
249
+ }
250
+ ++i;
251
+ return i ? glob.slice(0, i) : '';
252
+ }
253
+ /**
254
+ * Trim any leading spaces, tabs, line-feeds, or new-lines
255
+ * @param glob - any string
256
+ * @returns string with leading spaces removed.
257
+ */
258
+ function trimGlobLeft(glob) {
259
+ let i = 0;
260
+ while (i < glob.length && glob[i] in spaces) {
261
+ ++i;
262
+ }
263
+ return glob.slice(i);
264
+ }
193
265
  exports.__testing__ = {
194
266
  rebaseGlob,
267
+ trimGlob,
195
268
  };
196
269
  //# sourceMappingURL=globHelper.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cspell-glob",
3
- "version": "5.12.2",
3
+ "version": "5.12.3",
4
4
  "description": "Glob matcher for cspell",
5
5
  "keywords": [
6
6
  "cspell",
@@ -47,8 +47,8 @@
47
47
  "devDependencies": {
48
48
  "@types/micromatch": "^4.0.2",
49
49
  "@types/node": "^16.10.3",
50
- "jest": "^27.2.4",
50
+ "jest": "^27.2.5",
51
51
  "rimraf": "^3.0.2"
52
52
  },
53
- "gitHead": "1418785bb9c3a29b794a6e9d447b5638f8eb9fbf"
53
+ "gitHead": "6c424aed933adb8d983ab09dd45042d63d7a5cd8"
54
54
  }