cspell-glob 6.28.0 → 6.29.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/{GlobMatcher.d.ts → cjs/GlobMatcher.d.ts} +1 -1
- package/dist/{GlobMatcher.js → cjs/GlobMatcher.js} +4 -4
- package/dist/{globHelper.d.ts → cjs/globHelper.d.ts} +1 -1
- package/dist/cjs/index.d.ts +4 -0
- package/dist/{index.js → cjs/index.js} +9 -9
- package/dist/esm/GlobMatcher.d.mts +94 -0
- package/dist/esm/GlobMatcher.mjs +101 -0
- package/dist/esm/GlobMatcherTypes.d.mts +55 -0
- package/dist/esm/GlobMatcherTypes.mjs +2 -0
- package/dist/esm/globHelper.d.mts +73 -0
- package/dist/esm/globHelper.mjs +257 -0
- package/dist/esm/index.d.mts +4 -0
- package/dist/esm/index.mjs +3 -0
- package/package.json +24 -12
- package/dist/index.d.ts +0 -4
- /package/dist/{GlobMatcherTypes.d.ts → cjs/GlobMatcherTypes.d.ts} +0 -0
- /package/dist/{GlobMatcherTypes.js → cjs/GlobMatcherTypes.js} +0 -0
- /package/dist/{globHelper.js → cjs/globHelper.js} +0 -0
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { GlobMatch, GlobPattern, GlobPatternNormalized, GlobPatternWithRoot, PathInterface } from './GlobMatcherTypes';
|
|
1
|
+
import type { GlobMatch, GlobPattern, GlobPatternNormalized, GlobPatternWithRoot, PathInterface } from './GlobMatcherTypes.js';
|
|
2
2
|
type Optional<T> = {
|
|
3
3
|
[P in keyof T]?: T[P] | undefined;
|
|
4
4
|
};
|
|
@@ -29,7 +29,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
29
29
|
exports.GlobMatcher = void 0;
|
|
30
30
|
const micromatch_1 = __importDefault(require("micromatch"));
|
|
31
31
|
const Path = __importStar(require("path"));
|
|
32
|
-
const
|
|
32
|
+
const globHelper_js_1 = require("./globHelper.js");
|
|
33
33
|
class GlobMatcher {
|
|
34
34
|
constructor(patterns, rootOrOptions, _nodePath) {
|
|
35
35
|
_nodePath = _nodePath ?? Path;
|
|
@@ -45,9 +45,9 @@ class GlobMatcher {
|
|
|
45
45
|
: typeof patterns === 'string'
|
|
46
46
|
? patterns.split(/\r?\n/g)
|
|
47
47
|
: [patterns];
|
|
48
|
-
const globPatterns = (0,
|
|
48
|
+
const globPatterns = (0, globHelper_js_1.normalizeGlobPatterns)(patterns, this.options);
|
|
49
49
|
this.patternsNormalizedToRoot = globPatterns
|
|
50
|
-
.map((g) => (0,
|
|
50
|
+
.map((g) => (0, globHelper_js_1.normalizeGlobToRoot)(g, normalizedRoot, nodePath))
|
|
51
51
|
// Only keep globs that do not match the root when using exclude mode.
|
|
52
52
|
.filter((g) => nodePath.relative(g.root, normalizedRoot) === '');
|
|
53
53
|
this.patterns = globPatterns;
|
|
@@ -108,7 +108,7 @@ function buildMatcherFn(patterns, options) {
|
|
|
108
108
|
const pattern = rule.pattern;
|
|
109
109
|
const root = pattern.root;
|
|
110
110
|
const isRelPat = !pattern.isGlobalPattern;
|
|
111
|
-
if (isRelPat && !(0,
|
|
111
|
+
if (isRelPat && !(0, globHelper_js_1.doesRootContainPath)(root, filename, path)) {
|
|
112
112
|
continue;
|
|
113
113
|
}
|
|
114
114
|
const relName = isRelPat ? path.relative(root, filename) : filename;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { GlobPattern, GlobPatternNormalized, GlobPatternWithOptionalRoot, GlobPatternWithRoot, PathInterface } from './GlobMatcherTypes';
|
|
1
|
+
import type { GlobPattern, GlobPatternNormalized, GlobPatternWithOptionalRoot, GlobPatternWithRoot, PathInterface } from './GlobMatcherTypes.js';
|
|
2
2
|
/**
|
|
3
3
|
* This function tries its best to determine if `fileOrGlob` is a path to a file or a glob pattern.
|
|
4
4
|
* @param fileOrGlob - file (with absolute path) or glob.
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export { fileOrGlobToGlob, isGlobPatternNormalized, isGlobPatternWithOptionalRoot, isGlobPatternWithRoot, normalizeGlobPatterns, NormalizeOptions, } from './globHelper.js';
|
|
2
|
+
export { GlobMatcher, GlobMatchOptions } from './GlobMatcher.js';
|
|
3
|
+
export * from './GlobMatcherTypes.js';
|
|
4
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -15,13 +15,13 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
17
|
exports.GlobMatcher = exports.normalizeGlobPatterns = exports.isGlobPatternWithRoot = exports.isGlobPatternWithOptionalRoot = exports.isGlobPatternNormalized = exports.fileOrGlobToGlob = void 0;
|
|
18
|
-
var
|
|
19
|
-
Object.defineProperty(exports, "fileOrGlobToGlob", { enumerable: true, get: function () { return
|
|
20
|
-
Object.defineProperty(exports, "isGlobPatternNormalized", { enumerable: true, get: function () { return
|
|
21
|
-
Object.defineProperty(exports, "isGlobPatternWithOptionalRoot", { enumerable: true, get: function () { return
|
|
22
|
-
Object.defineProperty(exports, "isGlobPatternWithRoot", { enumerable: true, get: function () { return
|
|
23
|
-
Object.defineProperty(exports, "normalizeGlobPatterns", { enumerable: true, get: function () { return
|
|
24
|
-
var
|
|
25
|
-
Object.defineProperty(exports, "GlobMatcher", { enumerable: true, get: function () { return
|
|
26
|
-
__exportStar(require("./GlobMatcherTypes"), exports);
|
|
18
|
+
var globHelper_js_1 = require("./globHelper.js");
|
|
19
|
+
Object.defineProperty(exports, "fileOrGlobToGlob", { enumerable: true, get: function () { return globHelper_js_1.fileOrGlobToGlob; } });
|
|
20
|
+
Object.defineProperty(exports, "isGlobPatternNormalized", { enumerable: true, get: function () { return globHelper_js_1.isGlobPatternNormalized; } });
|
|
21
|
+
Object.defineProperty(exports, "isGlobPatternWithOptionalRoot", { enumerable: true, get: function () { return globHelper_js_1.isGlobPatternWithOptionalRoot; } });
|
|
22
|
+
Object.defineProperty(exports, "isGlobPatternWithRoot", { enumerable: true, get: function () { return globHelper_js_1.isGlobPatternWithRoot; } });
|
|
23
|
+
Object.defineProperty(exports, "normalizeGlobPatterns", { enumerable: true, get: function () { return globHelper_js_1.normalizeGlobPatterns; } });
|
|
24
|
+
var GlobMatcher_js_1 = require("./GlobMatcher.js");
|
|
25
|
+
Object.defineProperty(exports, "GlobMatcher", { enumerable: true, get: function () { return GlobMatcher_js_1.GlobMatcher; } });
|
|
26
|
+
__exportStar(require("./GlobMatcherTypes.js"), exports);
|
|
27
27
|
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import type { GlobMatch, GlobPattern, GlobPatternNormalized, GlobPatternWithRoot, PathInterface } from './GlobMatcherTypes.mjs';
|
|
2
|
+
type Optional<T> = {
|
|
3
|
+
[P in keyof T]?: T[P] | undefined;
|
|
4
|
+
};
|
|
5
|
+
export type GlobMatchOptions = Optional<NormalizedGlobMatchOptions>;
|
|
6
|
+
export type MatcherMode = 'exclude' | 'include';
|
|
7
|
+
interface NormalizedGlobMatchOptions {
|
|
8
|
+
/**
|
|
9
|
+
* The matcher has two modes (`include` or `exclude`) that impact how globs behave.
|
|
10
|
+
*
|
|
11
|
+
* `include` - designed for searching for file. By default it matches a sub-set of file.
|
|
12
|
+
* In include mode, the globs need to be more explicit to match.
|
|
13
|
+
* - `dot` is by default false.
|
|
14
|
+
* - `nested` is by default false.
|
|
15
|
+
*
|
|
16
|
+
* `exclude` - designed to emulate `.gitignore`. By default it matches a larger range of files.
|
|
17
|
+
* - `dot` is by default true.
|
|
18
|
+
* - `nested` is by default true.
|
|
19
|
+
*
|
|
20
|
+
* @default: 'exclude'
|
|
21
|
+
*/
|
|
22
|
+
mode: MatcherMode;
|
|
23
|
+
/**
|
|
24
|
+
* The default directory from which a glob is relative.
|
|
25
|
+
* Any globs that are not relative to the root will ignored.
|
|
26
|
+
* @default: process.cwd()
|
|
27
|
+
*/
|
|
28
|
+
root: string;
|
|
29
|
+
/**
|
|
30
|
+
* The directory to use as the current working directory.
|
|
31
|
+
* @default: process.cwd();
|
|
32
|
+
*/
|
|
33
|
+
cwd: string;
|
|
34
|
+
/**
|
|
35
|
+
* Allows matching against directories with a leading `.`.
|
|
36
|
+
*
|
|
37
|
+
* @default: mode == 'exclude'
|
|
38
|
+
*/
|
|
39
|
+
dot: boolean;
|
|
40
|
+
/**
|
|
41
|
+
* Allows matching against nested directories or files without needing to add `**`
|
|
42
|
+
*
|
|
43
|
+
* @default: mode == 'exclude'
|
|
44
|
+
*/
|
|
45
|
+
nested: boolean;
|
|
46
|
+
/**
|
|
47
|
+
* Mostly used for testing purposes. It allows explicitly specifying `path.win32` or `path.posix`.
|
|
48
|
+
*
|
|
49
|
+
* @default: require('path')
|
|
50
|
+
*/
|
|
51
|
+
nodePath: PathInterface;
|
|
52
|
+
/**
|
|
53
|
+
* Disable brace matching, so that `{a,b}` and `{1..3}` would be treated as literal characters.
|
|
54
|
+
*
|
|
55
|
+
* @default false
|
|
56
|
+
*/
|
|
57
|
+
nobrace?: boolean | undefined;
|
|
58
|
+
}
|
|
59
|
+
export declare class GlobMatcher {
|
|
60
|
+
/**
|
|
61
|
+
* @param filename full path of file to match against.
|
|
62
|
+
* @returns a GlobMatch - information about the match.
|
|
63
|
+
*/
|
|
64
|
+
readonly matchEx: (filename: string) => GlobMatch;
|
|
65
|
+
readonly path: PathInterface;
|
|
66
|
+
readonly patterns: GlobPatternWithRoot[];
|
|
67
|
+
readonly patternsNormalizedToRoot: GlobPatternNormalized[];
|
|
68
|
+
readonly root: string;
|
|
69
|
+
readonly dot: boolean;
|
|
70
|
+
readonly options: NormalizedGlobMatchOptions;
|
|
71
|
+
/**
|
|
72
|
+
* Construct a `.gitignore` emulator
|
|
73
|
+
* @param patterns - the contents of a `.gitignore` style file or an array of individual glob rules.
|
|
74
|
+
* @param root - the working directory
|
|
75
|
+
*/
|
|
76
|
+
constructor(patterns: GlobPattern | GlobPattern[], root?: string, nodePath?: PathInterface);
|
|
77
|
+
/**
|
|
78
|
+
* Construct a `.gitignore` emulator
|
|
79
|
+
* @param patterns - the contents of a `.gitignore` style file or an array of individual glob rules.
|
|
80
|
+
* @param options - to set the root and other options
|
|
81
|
+
*/
|
|
82
|
+
constructor(patterns: GlobPattern | GlobPattern[], options?: GlobMatchOptions);
|
|
83
|
+
constructor(patterns: GlobPattern | GlobPattern[], rootOrOptions?: string | GlobMatchOptions);
|
|
84
|
+
/**
|
|
85
|
+
* Check to see if a filename matches any of the globs.
|
|
86
|
+
* If filename is relative, it is considered relative to the root.
|
|
87
|
+
* If filename is absolute and contained within the root, it will be made relative before being tested for a glob match.
|
|
88
|
+
* If filename is absolute and not contained within the root, it will be tested as is.
|
|
89
|
+
* @param filename full path of the file to check.
|
|
90
|
+
*/
|
|
91
|
+
match(filename: string): boolean;
|
|
92
|
+
}
|
|
93
|
+
export {};
|
|
94
|
+
//# sourceMappingURL=GlobMatcher.d.mts.map
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import mm from 'micromatch';
|
|
2
|
+
import * as Path from 'path';
|
|
3
|
+
import { doesRootContainPath, normalizeGlobPatterns, normalizeGlobToRoot } from './globHelper.mjs';
|
|
4
|
+
export class GlobMatcher {
|
|
5
|
+
constructor(patterns, rootOrOptions, _nodePath) {
|
|
6
|
+
_nodePath = _nodePath ?? Path;
|
|
7
|
+
const options = typeof rootOrOptions === 'string' ? { root: rootOrOptions } : rootOrOptions ?? {};
|
|
8
|
+
const { mode = 'exclude' } = options;
|
|
9
|
+
const isExcludeMode = mode !== 'include';
|
|
10
|
+
_nodePath = options.nodePath ?? _nodePath;
|
|
11
|
+
const { root = _nodePath.resolve(), dot = isExcludeMode, nodePath = _nodePath, nested = isExcludeMode, cwd = process.cwd(), nobrace, } = options;
|
|
12
|
+
const normalizedRoot = nodePath.resolve(nodePath.normalize(root));
|
|
13
|
+
this.options = { root: normalizedRoot, dot, nodePath, nested, mode, nobrace, cwd };
|
|
14
|
+
patterns = Array.isArray(patterns)
|
|
15
|
+
? patterns
|
|
16
|
+
: typeof patterns === 'string'
|
|
17
|
+
? patterns.split(/\r?\n/g)
|
|
18
|
+
: [patterns];
|
|
19
|
+
const globPatterns = normalizeGlobPatterns(patterns, this.options);
|
|
20
|
+
this.patternsNormalizedToRoot = globPatterns
|
|
21
|
+
.map((g) => normalizeGlobToRoot(g, normalizedRoot, nodePath))
|
|
22
|
+
// Only keep globs that do not match the root when using exclude mode.
|
|
23
|
+
.filter((g) => nodePath.relative(g.root, normalizedRoot) === '');
|
|
24
|
+
this.patterns = globPatterns;
|
|
25
|
+
this.root = normalizedRoot;
|
|
26
|
+
this.path = nodePath;
|
|
27
|
+
this.dot = dot;
|
|
28
|
+
this.matchEx = buildMatcherFn(this.patterns, this.options);
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Check to see if a filename matches any of the globs.
|
|
32
|
+
* If filename is relative, it is considered relative to the root.
|
|
33
|
+
* If filename is absolute and contained within the root, it will be made relative before being tested for a glob match.
|
|
34
|
+
* If filename is absolute and not contained within the root, it will be tested as is.
|
|
35
|
+
* @param filename full path of the file to check.
|
|
36
|
+
*/
|
|
37
|
+
match(filename) {
|
|
38
|
+
return this.matchEx(filename).matched;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* This function attempts to emulate .gitignore functionality as much as possible.
|
|
43
|
+
*
|
|
44
|
+
* The resulting matcher function: (filename: string) => GlobMatch
|
|
45
|
+
*
|
|
46
|
+
* If filename is relative, it is considered relative to the root.
|
|
47
|
+
* If filename is absolute and contained within the root, it will be made relative before being tested for a glob match.
|
|
48
|
+
* If filename is absolute and not contained within the root, it will return a GlobMatchNoRule.
|
|
49
|
+
*
|
|
50
|
+
* @param patterns - the contents of a .gitignore style file or an array of individual glob rules.
|
|
51
|
+
* @param options - defines root and other options
|
|
52
|
+
* @returns a function given a filename returns true if it matches.
|
|
53
|
+
*/
|
|
54
|
+
function buildMatcherFn(patterns, options) {
|
|
55
|
+
const { nodePath: path, dot, nobrace } = options;
|
|
56
|
+
const makeReOptions = { dot, nobrace };
|
|
57
|
+
const rules = patterns
|
|
58
|
+
.map((pattern, index) => ({ pattern, index }))
|
|
59
|
+
.filter((r) => !!r.pattern.glob)
|
|
60
|
+
.filter((r) => !r.pattern.glob.startsWith('#'))
|
|
61
|
+
.map(({ pattern, index }) => {
|
|
62
|
+
const matchNeg = pattern.glob.match(/^!/);
|
|
63
|
+
const glob = pattern.glob.replace(/^!/, '');
|
|
64
|
+
const isNeg = (matchNeg && matchNeg[0].length & 1 && true) || false;
|
|
65
|
+
const reg = mm.makeRe(glob, makeReOptions);
|
|
66
|
+
const fn = (filename) => {
|
|
67
|
+
const match = filename.match(reg);
|
|
68
|
+
return !!match;
|
|
69
|
+
};
|
|
70
|
+
return { pattern, index, isNeg, fn, reg };
|
|
71
|
+
});
|
|
72
|
+
const negRules = rules.filter((r) => r.isNeg);
|
|
73
|
+
const posRules = rules.filter((r) => !r.isNeg);
|
|
74
|
+
const fn = (filename) => {
|
|
75
|
+
filename = path.resolve(path.normalize(filename));
|
|
76
|
+
function testRules(rules, matched) {
|
|
77
|
+
for (const rule of rules) {
|
|
78
|
+
const pattern = rule.pattern;
|
|
79
|
+
const root = pattern.root;
|
|
80
|
+
const isRelPat = !pattern.isGlobalPattern;
|
|
81
|
+
if (isRelPat && !doesRootContainPath(root, filename, path)) {
|
|
82
|
+
continue;
|
|
83
|
+
}
|
|
84
|
+
const relName = isRelPat ? path.relative(root, filename) : filename;
|
|
85
|
+
const fname = path.sep === '\\' ? relName.replace(/\\/g, '/') : relName;
|
|
86
|
+
if (rule.fn(fname)) {
|
|
87
|
+
return {
|
|
88
|
+
matched,
|
|
89
|
+
glob: pattern.glob,
|
|
90
|
+
root,
|
|
91
|
+
pattern,
|
|
92
|
+
index: rule.index,
|
|
93
|
+
isNeg: rule.isNeg,
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
return testRules(negRules, false) || testRules(posRules, true) || { matched: false };
|
|
99
|
+
};
|
|
100
|
+
return fn;
|
|
101
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
export interface PathInterface {
|
|
2
|
+
normalize(p: string): string;
|
|
3
|
+
join(...paths: string[]): string;
|
|
4
|
+
resolve(...paths: string[]): string;
|
|
5
|
+
relative(from: string, to: string): string;
|
|
6
|
+
isAbsolute(p: string): boolean;
|
|
7
|
+
sep: string;
|
|
8
|
+
}
|
|
9
|
+
export type GlobMatch = GlobMatchRule | GlobMatchNoRule;
|
|
10
|
+
export interface GlobMatchRule {
|
|
11
|
+
matched: boolean;
|
|
12
|
+
glob: string;
|
|
13
|
+
root: string;
|
|
14
|
+
pattern: GlobPatternWithRoot;
|
|
15
|
+
index: number;
|
|
16
|
+
isNeg: boolean;
|
|
17
|
+
}
|
|
18
|
+
export interface GlobMatchNoRule {
|
|
19
|
+
matched: false;
|
|
20
|
+
}
|
|
21
|
+
export type GlobPattern = SimpleGlobPattern | GlobPatternWithRoot | GlobPatternWithOptionalRoot;
|
|
22
|
+
export type SimpleGlobPattern = string;
|
|
23
|
+
export interface GlobPatternWithOptionalRoot {
|
|
24
|
+
/**
|
|
25
|
+
* a glob pattern
|
|
26
|
+
*/
|
|
27
|
+
glob: string;
|
|
28
|
+
/**
|
|
29
|
+
* The root from which the glob pattern is relative.
|
|
30
|
+
* @default: options.root
|
|
31
|
+
*/
|
|
32
|
+
root?: string | undefined;
|
|
33
|
+
/**
|
|
34
|
+
* Optional value useful for tracing which file a glob pattern was defined in.
|
|
35
|
+
*/
|
|
36
|
+
source?: string | undefined;
|
|
37
|
+
/**
|
|
38
|
+
* Optional line number in the source
|
|
39
|
+
*/
|
|
40
|
+
line?: number | undefined;
|
|
41
|
+
}
|
|
42
|
+
export interface GlobPatternWithRoot extends GlobPatternWithOptionalRoot {
|
|
43
|
+
root: string;
|
|
44
|
+
/**
|
|
45
|
+
* Global patterns do not need to be relative to the root.
|
|
46
|
+
*/
|
|
47
|
+
isGlobalPattern: boolean;
|
|
48
|
+
}
|
|
49
|
+
export interface GlobPatternNormalized extends GlobPatternWithRoot {
|
|
50
|
+
/** the original glob pattern before it was normalized */
|
|
51
|
+
rawGlob: string;
|
|
52
|
+
/** the original root */
|
|
53
|
+
rawRoot: string | undefined;
|
|
54
|
+
}
|
|
55
|
+
//# sourceMappingURL=GlobMatcherTypes.d.mts.map
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import type { GlobPattern, GlobPatternNormalized, GlobPatternWithOptionalRoot, GlobPatternWithRoot, PathInterface } from './GlobMatcherTypes.mjs';
|
|
2
|
+
/**
|
|
3
|
+
* This function tries its best to determine if `fileOrGlob` is a path to a file or a glob pattern.
|
|
4
|
+
* @param fileOrGlob - file (with absolute path) or glob.
|
|
5
|
+
* @param root - absolute path to the directory that will be considered the root when testing the glob pattern.
|
|
6
|
+
* @param path - optional node path methods - used for testing
|
|
7
|
+
*/
|
|
8
|
+
export declare function fileOrGlobToGlob(fileOrGlob: string | GlobPattern, root: string, path?: PathInterface): GlobPatternWithRoot;
|
|
9
|
+
/**
|
|
10
|
+
* Decide if a childPath is contained within a root or at the same level.
|
|
11
|
+
* @param root - absolute path
|
|
12
|
+
* @param childPath - absolute path
|
|
13
|
+
*/
|
|
14
|
+
export declare function doesRootContainPath(root: string, child: string, path: PathInterface): boolean;
|
|
15
|
+
export declare function isGlobPatternWithOptionalRoot(g: GlobPattern): g is GlobPatternWithOptionalRoot;
|
|
16
|
+
export declare function isGlobPatternWithRoot(g: GlobPatternWithRoot | GlobPatternWithOptionalRoot): g is GlobPatternWithRoot;
|
|
17
|
+
export declare function isGlobPatternNormalized(g: GlobPattern | GlobPatternNormalized): g is GlobPatternNormalized;
|
|
18
|
+
export interface NormalizeOptions {
|
|
19
|
+
/**
|
|
20
|
+
* Indicates that the glob should be modified to match nested patterns.
|
|
21
|
+
*
|
|
22
|
+
* Example: `node_modules` becomes `**/node_modules/**`, `**/node_modules`, and `node_modules/**`
|
|
23
|
+
*/
|
|
24
|
+
nested: boolean;
|
|
25
|
+
/**
|
|
26
|
+
* This is the root to use for the glob if the glob does not already contain one.
|
|
27
|
+
*/
|
|
28
|
+
root: string;
|
|
29
|
+
/**
|
|
30
|
+
* This is the replacement for `${cwd}` in either the root or in the glob.
|
|
31
|
+
*/
|
|
32
|
+
cwd?: string;
|
|
33
|
+
/**
|
|
34
|
+
* Optional path interface for working with paths.
|
|
35
|
+
*/
|
|
36
|
+
nodePath?: PathInterface;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
*
|
|
40
|
+
* @param patterns - glob patterns to normalize.
|
|
41
|
+
* @param options - Normalization options.
|
|
42
|
+
*/
|
|
43
|
+
export declare function normalizeGlobPatterns(patterns: GlobPattern[], options: NormalizeOptions): GlobPatternNormalized[];
|
|
44
|
+
export declare function normalizeGlobPattern(g: GlobPattern, options: NormalizeOptions): GlobPatternNormalized[];
|
|
45
|
+
/**
|
|
46
|
+
* Try to adjust the root of a glob to match a new root. If it is not possible, the original glob is returned.
|
|
47
|
+
* Note: this does NOT generate absolutely correct glob patterns. The results are intended to be used as a
|
|
48
|
+
* first pass only filter. Followed by testing against the original glob/root pair.
|
|
49
|
+
* @param glob - glob to map
|
|
50
|
+
* @param root - new root to use if possible
|
|
51
|
+
* @param path - Node Path modules to use (testing only)
|
|
52
|
+
*/
|
|
53
|
+
export declare function normalizeGlobToRoot<Glob extends GlobPatternWithRoot>(glob: Glob, root: string, path: PathInterface): Glob;
|
|
54
|
+
/**
|
|
55
|
+
* Rebase a glob string to a new prefix
|
|
56
|
+
* @param glob - glob string
|
|
57
|
+
* @param rebaseTo - glob prefix
|
|
58
|
+
*/
|
|
59
|
+
declare function rebaseGlob(glob: string, rebaseTo: string): string | undefined;
|
|
60
|
+
/**
|
|
61
|
+
* Trims any trailing spaces, tabs, line-feeds, new-lines, and comments
|
|
62
|
+
* @param glob - glob string
|
|
63
|
+
* @returns trimmed glob
|
|
64
|
+
*/
|
|
65
|
+
declare function trimGlob(glob: string): string;
|
|
66
|
+
declare function isGlobalGlob(glob: string): boolean;
|
|
67
|
+
export declare const __testing__: {
|
|
68
|
+
rebaseGlob: typeof rebaseGlob;
|
|
69
|
+
trimGlob: typeof trimGlob;
|
|
70
|
+
isGlobalGlob: typeof isGlobalGlob;
|
|
71
|
+
};
|
|
72
|
+
export {};
|
|
73
|
+
//# sourceMappingURL=globHelper.d.mts.map
|
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
/* eslint-disable no-irregular-whitespace */
|
|
2
|
+
import * as Path from 'path';
|
|
3
|
+
const { posix } = Path;
|
|
4
|
+
const relRegExp = /^\.[\\/]/;
|
|
5
|
+
/** test for glob patterns starting with `**` */
|
|
6
|
+
const isGlobalPatternRegExp = /^!*[*]{2}/;
|
|
7
|
+
/**
|
|
8
|
+
* This function tries its best to determine if `fileOrGlob` is a path to a file or a glob pattern.
|
|
9
|
+
* @param fileOrGlob - file (with absolute path) or glob.
|
|
10
|
+
* @param root - absolute path to the directory that will be considered the root when testing the glob pattern.
|
|
11
|
+
* @param path - optional node path methods - used for testing
|
|
12
|
+
*/
|
|
13
|
+
export function fileOrGlobToGlob(fileOrGlob, root, path = Path) {
|
|
14
|
+
const pathToGlob = path.sep === '\\' ? (p) => p.replace(/\\/g, '/') : (p) => p;
|
|
15
|
+
const isGlobalPattern = false;
|
|
16
|
+
if (isGlobPatternWithOptionalRoot(fileOrGlob)) {
|
|
17
|
+
const useRoot = fileOrGlob.root ?? root;
|
|
18
|
+
const isGlobalPattern = isGlobPatternWithRoot(fileOrGlob)
|
|
19
|
+
? fileOrGlob.isGlobalPattern
|
|
20
|
+
: isGlobalGlob(fileOrGlob.glob);
|
|
21
|
+
return { ...fileOrGlob, root: useRoot, isGlobalPattern };
|
|
22
|
+
}
|
|
23
|
+
if (doesRootContainPath(root, fileOrGlob, path) || relRegExp.test(fileOrGlob)) {
|
|
24
|
+
const rel = path.relative(root, path.resolve(root, fileOrGlob));
|
|
25
|
+
return { glob: pathToGlob(rel), root, isGlobalPattern };
|
|
26
|
+
}
|
|
27
|
+
return { glob: pathToGlob(fileOrGlob), root, isGlobalPattern };
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Decide if a childPath is contained within a root or at the same level.
|
|
31
|
+
* @param root - absolute path
|
|
32
|
+
* @param childPath - absolute path
|
|
33
|
+
*/
|
|
34
|
+
export function doesRootContainPath(root, child, path) {
|
|
35
|
+
if (child.startsWith(root))
|
|
36
|
+
return true;
|
|
37
|
+
const rel = path.relative(root, child);
|
|
38
|
+
return !rel || (rel !== child && !rel.startsWith('..') && !path.isAbsolute(rel));
|
|
39
|
+
}
|
|
40
|
+
export function isGlobPatternWithOptionalRoot(g) {
|
|
41
|
+
return typeof g !== 'string' && typeof g.glob === 'string';
|
|
42
|
+
}
|
|
43
|
+
export function isGlobPatternWithRoot(g) {
|
|
44
|
+
return typeof g.root === 'string' && 'isGlobalPattern' in g;
|
|
45
|
+
}
|
|
46
|
+
export function isGlobPatternNormalized(g) {
|
|
47
|
+
if (!isGlobPatternWithOptionalRoot(g))
|
|
48
|
+
return false;
|
|
49
|
+
if (!isGlobPatternWithRoot(g))
|
|
50
|
+
return false;
|
|
51
|
+
const gr = g;
|
|
52
|
+
return 'rawGlob' in gr && 'rawRoot' in gr && typeof gr.rawGlob === 'string';
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* @param pattern glob pattern
|
|
56
|
+
* @param nested when true add `**/<glob>/**`
|
|
57
|
+
* @returns the set of matching globs.
|
|
58
|
+
*/
|
|
59
|
+
function normalizePattern(pattern, nested) {
|
|
60
|
+
pattern = pattern.replace(/^(!!)+/, '');
|
|
61
|
+
const isNeg = pattern.startsWith('!');
|
|
62
|
+
const prefix = isNeg ? '!' : '';
|
|
63
|
+
pattern = isNeg ? pattern.slice(1) : pattern;
|
|
64
|
+
const patterns = nested ? normalizePatternNested(pattern) : normalizePatternGeneral(pattern);
|
|
65
|
+
return patterns.map((p) => prefix + p);
|
|
66
|
+
}
|
|
67
|
+
function normalizePatternNested(pattern) {
|
|
68
|
+
// no slashes will match files names or folders
|
|
69
|
+
if (!pattern.includes('/')) {
|
|
70
|
+
if (pattern === '**')
|
|
71
|
+
return ['**'];
|
|
72
|
+
return ['**/' + pattern, '**/' + pattern + '/**'];
|
|
73
|
+
}
|
|
74
|
+
const hasLeadingSlash = pattern.startsWith('/');
|
|
75
|
+
pattern = hasLeadingSlash ? pattern.slice(1) : pattern;
|
|
76
|
+
if (pattern.endsWith('/')) {
|
|
77
|
+
// legacy behavior, if it only has a trailing slash, allow matching against a nested directory.
|
|
78
|
+
return hasLeadingSlash || pattern.slice(0, -1).includes('/') ? [pattern + '**/*'] : ['**/' + pattern + '**/*'];
|
|
79
|
+
}
|
|
80
|
+
if (pattern.endsWith('**')) {
|
|
81
|
+
return [pattern];
|
|
82
|
+
}
|
|
83
|
+
return [pattern, pattern + '/**'];
|
|
84
|
+
}
|
|
85
|
+
function normalizePatternGeneral(pattern) {
|
|
86
|
+
pattern = pattern.startsWith('/') ? pattern.slice(1) : pattern;
|
|
87
|
+
pattern = pattern.endsWith('/') ? pattern + '**/*' : pattern;
|
|
88
|
+
return [pattern];
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
*
|
|
92
|
+
* @param patterns - glob patterns to normalize.
|
|
93
|
+
* @param options - Normalization options.
|
|
94
|
+
*/
|
|
95
|
+
export function normalizeGlobPatterns(patterns, options) {
|
|
96
|
+
function* normalize() {
|
|
97
|
+
for (const glob of patterns) {
|
|
98
|
+
if (isGlobPatternNormalized(glob)) {
|
|
99
|
+
yield glob;
|
|
100
|
+
continue;
|
|
101
|
+
}
|
|
102
|
+
yield* normalizeGlobPattern(glob, options);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
return [...normalize()];
|
|
106
|
+
}
|
|
107
|
+
export function normalizeGlobPattern(g, options) {
|
|
108
|
+
const { root, nodePath: path = Path, nested, cwd = Path.resolve() } = options;
|
|
109
|
+
g = !isGlobPatternWithOptionalRoot(g) ? { glob: g } : g;
|
|
110
|
+
const gr = { ...g, root: g.root ?? root };
|
|
111
|
+
const rawRoot = gr.root;
|
|
112
|
+
const rawGlob = g.glob;
|
|
113
|
+
gr.glob = gr.glob.trim(); // trimGlob(g.glob);
|
|
114
|
+
if (gr.glob.startsWith('${cwd}')) {
|
|
115
|
+
gr.glob = gr.glob.replace('${cwd}', '');
|
|
116
|
+
gr.root = '${cwd}';
|
|
117
|
+
}
|
|
118
|
+
if (gr.root.startsWith('${cwd}')) {
|
|
119
|
+
gr.root = path.resolve(gr.root.replace('${cwd}', cwd));
|
|
120
|
+
}
|
|
121
|
+
const isGlobalPattern = isGlobalGlob(gr.glob);
|
|
122
|
+
gr.root = path.resolve(root, path.normalize(gr.root));
|
|
123
|
+
const globs = normalizePattern(gr.glob, nested);
|
|
124
|
+
return globs.map((glob) => ({ ...gr, glob, rawGlob, rawRoot, isGlobalPattern }));
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Try to adjust the root of a glob to match a new root. If it is not possible, the original glob is returned.
|
|
128
|
+
* Note: this does NOT generate absolutely correct glob patterns. The results are intended to be used as a
|
|
129
|
+
* first pass only filter. Followed by testing against the original glob/root pair.
|
|
130
|
+
* @param glob - glob to map
|
|
131
|
+
* @param root - new root to use if possible
|
|
132
|
+
* @param path - Node Path modules to use (testing only)
|
|
133
|
+
*/
|
|
134
|
+
export function normalizeGlobToRoot(glob, root, path) {
|
|
135
|
+
function relToGlob(relativePath) {
|
|
136
|
+
return path.sep === '\\' ? relativePath.replace(/\\/g, '/') : relativePath;
|
|
137
|
+
}
|
|
138
|
+
if (glob.root === root) {
|
|
139
|
+
return glob;
|
|
140
|
+
}
|
|
141
|
+
const relFromRootToGlob = path.relative(root, glob.root);
|
|
142
|
+
if (!relFromRootToGlob) {
|
|
143
|
+
return glob;
|
|
144
|
+
}
|
|
145
|
+
if (glob.isGlobalPattern) {
|
|
146
|
+
return { ...glob, root };
|
|
147
|
+
}
|
|
148
|
+
const relFromGlobToRoot = path.relative(glob.root, root);
|
|
149
|
+
const globIsUnderRoot = relFromRootToGlob[0] !== '.' && !path.isAbsolute(relFromRootToGlob);
|
|
150
|
+
const rootIsUnderGlob = relFromGlobToRoot[0] !== '.' && !path.isAbsolute(relFromGlobToRoot);
|
|
151
|
+
// Root and Glob are not in the same part of the directory tree.
|
|
152
|
+
if (!globIsUnderRoot && !rootIsUnderGlob) {
|
|
153
|
+
return glob;
|
|
154
|
+
}
|
|
155
|
+
const isNeg = glob.glob.startsWith('!');
|
|
156
|
+
const g = isNeg ? glob.glob.slice(1) : glob.glob;
|
|
157
|
+
const prefix = isNeg ? '!' : '';
|
|
158
|
+
// prefix with root
|
|
159
|
+
if (globIsUnderRoot) {
|
|
160
|
+
const relGlob = relToGlob(relFromRootToGlob);
|
|
161
|
+
return {
|
|
162
|
+
...glob,
|
|
163
|
+
glob: prefix + posix.join(relGlob, g),
|
|
164
|
+
root,
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
// The root is under the glob root
|
|
168
|
+
// The more difficult case, the glob is higher than the root
|
|
169
|
+
// A best effort is made, but does not do advanced matching.
|
|
170
|
+
const relGlob = relToGlob(relFromGlobToRoot) + '/';
|
|
171
|
+
const rebasedGlob = rebaseGlob(g, relGlob);
|
|
172
|
+
return rebasedGlob ? { ...glob, glob: prefix + rebasedGlob, root } : glob;
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Rebase a glob string to a new prefix
|
|
176
|
+
* @param glob - glob string
|
|
177
|
+
* @param rebaseTo - glob prefix
|
|
178
|
+
*/
|
|
179
|
+
function rebaseGlob(glob, rebaseTo) {
|
|
180
|
+
if (!rebaseTo || rebaseTo === '/')
|
|
181
|
+
return glob;
|
|
182
|
+
if (glob.startsWith('**'))
|
|
183
|
+
return glob;
|
|
184
|
+
rebaseTo = rebaseTo.endsWith('/') ? rebaseTo : rebaseTo + '/';
|
|
185
|
+
if (glob.startsWith(rebaseTo)) {
|
|
186
|
+
return glob.slice(rebaseTo.length);
|
|
187
|
+
}
|
|
188
|
+
const relParts = rebaseTo.split('/');
|
|
189
|
+
const globParts = glob.split('/');
|
|
190
|
+
for (let i = 0; i < relParts.length && i < globParts.length; ++i) {
|
|
191
|
+
const relSeg = relParts[i];
|
|
192
|
+
const globSeg = globParts[i];
|
|
193
|
+
// the empty segment due to the end relGlob / allows for us to test against an empty segment.
|
|
194
|
+
if (!relSeg || globSeg === '**') {
|
|
195
|
+
return globParts.slice(i).join('/');
|
|
196
|
+
}
|
|
197
|
+
if (relSeg !== globSeg && globSeg !== '*') {
|
|
198
|
+
break;
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
return undefined;
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
204
|
+
* Trims any trailing spaces, tabs, line-feeds, new-lines, and comments
|
|
205
|
+
* @param glob - glob string
|
|
206
|
+
* @returns trimmed glob
|
|
207
|
+
*/
|
|
208
|
+
function trimGlob(glob) {
|
|
209
|
+
glob = glob.replace(/(?<!\\)#.*/g, '');
|
|
210
|
+
glob = trimGlobLeft(glob);
|
|
211
|
+
glob = trimGlobRight(glob);
|
|
212
|
+
return glob;
|
|
213
|
+
}
|
|
214
|
+
const spaces = {
|
|
215
|
+
' ': true,
|
|
216
|
+
'\t': true,
|
|
217
|
+
'\n': true,
|
|
218
|
+
'\r': true,
|
|
219
|
+
};
|
|
220
|
+
/**
|
|
221
|
+
* Trim any trailing spaces, tabs, line-feeds, or new-lines
|
|
222
|
+
* Handles a trailing \<space>
|
|
223
|
+
* @param glob - glob string
|
|
224
|
+
* @returns glob string with space to the right removed.
|
|
225
|
+
*/
|
|
226
|
+
function trimGlobRight(glob) {
|
|
227
|
+
const lenMin1 = glob.length - 1;
|
|
228
|
+
let i = lenMin1;
|
|
229
|
+
while (i >= 0 && glob[i] in spaces) {
|
|
230
|
+
--i;
|
|
231
|
+
}
|
|
232
|
+
if (glob[i] === '\\' && i < lenMin1) {
|
|
233
|
+
++i;
|
|
234
|
+
}
|
|
235
|
+
++i;
|
|
236
|
+
return i ? glob.slice(0, i) : '';
|
|
237
|
+
}
|
|
238
|
+
/**
|
|
239
|
+
* Trim any leading spaces, tabs, line-feeds, or new-lines
|
|
240
|
+
* @param glob - any string
|
|
241
|
+
* @returns string with leading spaces removed.
|
|
242
|
+
*/
|
|
243
|
+
function trimGlobLeft(glob) {
|
|
244
|
+
let i = 0;
|
|
245
|
+
while (i < glob.length && glob[i] in spaces) {
|
|
246
|
+
++i;
|
|
247
|
+
}
|
|
248
|
+
return glob.slice(i);
|
|
249
|
+
}
|
|
250
|
+
function isGlobalGlob(glob) {
|
|
251
|
+
return isGlobalPatternRegExp.test(glob);
|
|
252
|
+
}
|
|
253
|
+
export const __testing__ = {
|
|
254
|
+
rebaseGlob,
|
|
255
|
+
trimGlob,
|
|
256
|
+
isGlobalGlob,
|
|
257
|
+
};
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export { fileOrGlobToGlob, isGlobPatternNormalized, isGlobPatternWithOptionalRoot, isGlobPatternWithRoot, normalizeGlobPatterns, NormalizeOptions, } from './globHelper.mjs';
|
|
2
|
+
export { GlobMatcher, GlobMatchOptions } from './GlobMatcher.mjs';
|
|
3
|
+
export * from './GlobMatcherTypes.mjs';
|
|
4
|
+
//# sourceMappingURL=index.d.mts.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cspell-glob",
|
|
3
|
-
"version": "6.
|
|
3
|
+
"version": "6.29.0",
|
|
4
4
|
"description": "Glob matcher for cspell",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"cspell",
|
|
@@ -9,27 +9,39 @@
|
|
|
9
9
|
"author": "Jason Dent <jason@streetsidesoftware.nl>",
|
|
10
10
|
"homepage": "https://github.com/streetsidesoftware/cspell/tree/main/packages/cspell-glob#readme",
|
|
11
11
|
"license": "MIT",
|
|
12
|
-
"
|
|
13
|
-
"
|
|
14
|
-
|
|
12
|
+
"type": "commonjs",
|
|
13
|
+
"main": "dist/cjs/index.js",
|
|
14
|
+
"types": "dist/cjs/index.d.ts",
|
|
15
|
+
"module": "dist/esm/index.mjs",
|
|
16
|
+
"exports": {
|
|
17
|
+
".": {
|
|
18
|
+
"import": "./dist/esm/index.mjs",
|
|
19
|
+
"require": "./dist/cjs/index.js"
|
|
20
|
+
}
|
|
15
21
|
},
|
|
16
|
-
"typings": "dist/index.d.ts",
|
|
17
22
|
"files": [
|
|
23
|
+
"bin.mjs",
|
|
18
24
|
"dist",
|
|
25
|
+
"!dist/esm/**/*.js",
|
|
26
|
+
"!dist/esm/**/*.ts",
|
|
19
27
|
"!**/*.tsbuildInfo",
|
|
20
28
|
"!**/__mocks__",
|
|
29
|
+
"!**/test/**",
|
|
21
30
|
"!**/*.test.*",
|
|
22
31
|
"!**/*.spec.*",
|
|
23
32
|
"!**/*.map"
|
|
24
33
|
],
|
|
25
34
|
"scripts": {
|
|
26
|
-
"build": "tsc -p .",
|
|
27
|
-
"watch": "tsc -p . -w",
|
|
28
35
|
"clean": "shx rm -rf dist coverage .tsbuildinfo",
|
|
36
|
+
"build": "tsc -b . && ts2mjs dist/esm",
|
|
37
|
+
"build:esm": "tsc -p tsconfig.esm.json",
|
|
29
38
|
"clean-build": "pnpm run clean && pnpm run build",
|
|
30
|
-
"coverage": "
|
|
31
|
-
"
|
|
32
|
-
"
|
|
39
|
+
"coverage": "pnpm coverage:vitest && pnpm coverage:fix",
|
|
40
|
+
"coverage:vitest": "vitest run --coverage",
|
|
41
|
+
"coverage:fix": "nyc report --temp-dir \"$(pwd)/coverage\" --reporter lcov --report-dir \"$(pwd)/coverage\" --cwd ../..",
|
|
42
|
+
"test:watch": "vitest",
|
|
43
|
+
"test": "vitest run",
|
|
44
|
+
"watch": "tsc -b . -w"
|
|
33
45
|
},
|
|
34
46
|
"repository": {
|
|
35
47
|
"type": "git",
|
|
@@ -46,7 +58,7 @@
|
|
|
46
58
|
},
|
|
47
59
|
"devDependencies": {
|
|
48
60
|
"@types/micromatch": "^4.0.2",
|
|
49
|
-
"jest": "^29.
|
|
61
|
+
"jest": "^29.5.0"
|
|
50
62
|
},
|
|
51
|
-
"gitHead": "
|
|
63
|
+
"gitHead": "2eabb1c47c12c2a42eb95d30329be6f544ee2ffc"
|
|
52
64
|
}
|
package/dist/index.d.ts
DELETED
|
@@ -1,4 +0,0 @@
|
|
|
1
|
-
export { fileOrGlobToGlob, isGlobPatternNormalized, isGlobPatternWithOptionalRoot, isGlobPatternWithRoot, normalizeGlobPatterns, NormalizeOptions, } from './globHelper';
|
|
2
|
-
export { GlobMatcher, GlobMatchOptions } from './GlobMatcher';
|
|
3
|
-
export * from './GlobMatcherTypes';
|
|
4
|
-
//# sourceMappingURL=index.d.ts.map
|
|
File without changes
|
|
File without changes
|
|
File without changes
|