view-ignored 0.10.1 → 0.11.1

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.
Files changed (70) hide show
  1. package/README.md +116 -66
  2. package/out/browser.d.ts +1 -0
  3. package/out/browser.js +1 -0
  4. package/out/browser_scan.d.ts +1 -3
  5. package/out/browser_scan.js +11 -46
  6. package/out/browser_stream.d.ts +6 -1
  7. package/out/browser_stream.js +6 -1
  8. package/out/index.d.ts +1 -0
  9. package/out/index.js +1 -0
  10. package/out/patterns/extractor.d.ts +7 -7
  11. package/out/patterns/gitignore.d.ts +2 -2
  12. package/out/patterns/gitignore.js +14 -6
  13. package/out/patterns/ignores.d.ts +17 -8
  14. package/out/patterns/index.d.ts +1 -0
  15. package/out/patterns/index.js +1 -0
  16. package/out/patterns/init.d.ts +2 -2
  17. package/out/patterns/initState.d.ts +0 -7
  18. package/out/patterns/jsrjson.d.ts +2 -3
  19. package/out/patterns/jsrjson.js +25 -38
  20. package/out/patterns/matcherContext.d.ts +12 -9
  21. package/out/patterns/matcherContextPatch.js +153 -73
  22. package/out/patterns/matcherStream.d.ts +19 -59
  23. package/out/patterns/matcherStream.js +75 -25
  24. package/out/patterns/matcherStreamTypes.d.ts +65 -0
  25. package/out/patterns/matcherStreamTypes.js +1 -0
  26. package/out/patterns/packagejson.d.ts +2 -2
  27. package/out/patterns/packagejson.js +11 -14
  28. package/out/patterns/patternCompile.js +35 -37
  29. package/out/patterns/patternList.d.ts +6 -2
  30. package/out/patterns/patternList.js +8 -3
  31. package/out/patterns/resolveSources.d.ts +22 -5
  32. package/out/patterns/resolveSources.js +153 -97
  33. package/out/patterns/resource.d.ts +16 -0
  34. package/out/patterns/resource.js +1 -0
  35. package/out/patterns/rule.d.ts +59 -11
  36. package/out/patterns/rule.js +101 -64
  37. package/out/patterns/source.d.ts +9 -17
  38. package/out/scan.d.ts +11 -3
  39. package/out/scan.js +16 -4
  40. package/out/scanCb.d.ts +16 -0
  41. package/out/scanCb.js +73 -0
  42. package/out/scanParallel.d.ts +18 -0
  43. package/out/scanParallel.js +146 -0
  44. package/out/stream.d.ts +6 -1
  45. package/out/stream.js +7 -2
  46. package/out/targets/bun.js +43 -36
  47. package/out/targets/deno.js +25 -23
  48. package/out/targets/git.js +3 -3
  49. package/out/targets/jsr.js +25 -23
  50. package/out/targets/jsrManifest.d.ts +8 -7
  51. package/out/targets/jsrManifest.js +40 -9
  52. package/out/targets/npm.js +30 -23
  53. package/out/targets/npmManifest.d.ts +18 -46
  54. package/out/targets/npmManifest.js +96 -23
  55. package/out/targets/target.d.ts +8 -16
  56. package/out/targets/vsce.js +20 -27
  57. package/out/targets/vsceManifest.d.ts +7 -0
  58. package/out/targets/vsceManifest.js +18 -0
  59. package/out/targets/yarn.js +48 -39
  60. package/out/targets/yarnClassic.js +20 -18
  61. package/out/types.d.ts +8 -7
  62. package/out/unixify.d.ts +1 -1
  63. package/out/unixify.js +40 -21
  64. package/out/walk.d.ts +42 -4
  65. package/out/walk.js +146 -92
  66. package/package.json +42 -27
  67. package/out/getDepth.d.ts +0 -4
  68. package/out/getDepth.js +0 -21
  69. package/out/opendir.d.ts +0 -3
  70. package/out/opendir.js +0 -28
@@ -1,5 +1,5 @@
1
- import { dirname } from "node:path";
2
- import { join, base } from "../unixify.js";
1
+ import { join } from "../unixify.js";
2
+ import {} from "./extractor.js";
3
3
  import { patternListCompile } from "./patternList.js";
4
4
  /**
5
5
  * Compiles the {@link Rule} (forced).
@@ -10,128 +10,184 @@ import { patternListCompile } from "./patternList.js";
10
10
  *
11
11
  * @since 0.6.0
12
12
  */
13
- export function ruleCompile(signedPattern, options) {
14
- signedPattern.compiled = patternListCompile(signedPattern.pattern, options);
15
- return signedPattern;
13
+ export function ruleCompile(rule, options) {
14
+ rule.compiled = patternListCompile(rule.pattern, options);
15
+ return rule;
16
16
  }
17
17
  /**
18
- * Populates the {@link MatcherContext.external} map with {@link Source} objects.
19
- *
20
18
  * @since 0.6.0
21
19
  */
22
- export async function resolveSources(options) {
23
- const { fs, ctx, cwd, signal, target } = options;
20
+ export function resolveSources(options, cb) {
21
+ const { fs, external, cwd, signal, target, resource: parentResource } = options;
24
22
  let dir = options.dir;
25
- if (ctx.external.has(dir)) {
23
+ if (target.root === "." && dir !== ".") {
24
+ resolveSources({ ...options, dir: "." }, (err, res) => {
25
+ if (err)
26
+ return cb(err, null);
27
+ external.set(dir, res);
28
+ cb(null, res);
29
+ });
30
+ return;
31
+ }
32
+ let source = external.get(dir);
33
+ if (source !== undefined) {
34
+ cb(null, source);
26
35
  return;
27
36
  }
28
- let source;
29
37
  const noSourceDirList = [dir];
30
38
  if (dir !== ".") {
31
- dir = dirname(dir);
32
- // find source from an ancestor [dir < ... < cwd]
33
- while (true) {
34
- signal?.throwIfAborted();
35
- source = ctx.external.get(dir);
36
- if (source !== undefined) {
37
- // if cache is found populate descendants [cwd > ... > dir]
38
- for (const noSourceDir of noSourceDirList) {
39
- ctx.external.set(noSourceDir, source);
40
- }
39
+ const segments = dir.split("/");
40
+ for (let i = segments.length - 1; i >= 0; i--) {
41
+ if (signal?.aborted) {
42
+ cb(signal.reason, null);
41
43
  return;
42
44
  }
43
- noSourceDirList.push(dir);
44
- const parent = dirname(dir);
45
- if (dir === parent)
45
+ const d = segments.slice(0, i).join("/") || ".";
46
+ source = external.get(d);
47
+ if (source !== undefined) {
48
+ dir = d;
46
49
  break;
47
- dir = parent;
48
- continue;
50
+ }
51
+ noSourceDirList.push(d);
52
+ if (d === ".") {
53
+ dir = ".";
54
+ break;
55
+ }
49
56
  }
50
57
  }
51
- // else
52
58
  // find non-cwd source [root > cwd) and populate [cwd > ... > dir]
53
- const preCwdSegments = [];
54
- if (target.root.startsWith("/")) {
55
- let c = dirname(cwd);
56
- while (true) {
57
- signal?.throwIfAborted();
58
- preCwdSegments.push(c);
59
- if (c === target.root)
60
- break;
61
- const parent = dirname(c);
62
- c = parent;
59
+ if (target.root.charCodeAt(0) === 47) {
60
+ // "/"
61
+ const segments = cwd.split("/");
62
+ const preCwdSegments = [];
63
+ let current = "";
64
+ for (let i = 0, len = segments.length - 1; i < len; i++) {
65
+ current += segments[i] + "/";
66
+ const path = current.length > 1 ? current.slice(0, -1) : "/";
67
+ if (path.length >= target.root.length) {
68
+ preCwdSegments.push(path);
69
+ }
63
70
  }
64
- preCwdSegments.reverse();
65
- source = await findSourceForAbsoluteDirs(preCwdSegments, ctx, fs, target, signal);
66
- if (typeof source === "object") {
67
- for (const noSourceDir of noSourceDirList) {
68
- signal?.throwIfAborted();
69
- ctx.external.set(noSourceDir, source);
71
+ findSourceForAbsoluteDirsCb(preCwdSegments, fs, target, signal, (err, source) => {
72
+ if (err) {
73
+ cb(err, null);
74
+ return;
70
75
  }
76
+ const absPaths = Array.from({ length: noSourceDirList.length });
77
+ for (let i = 0, len = noSourceDirList.length; i < len; i++) {
78
+ absPaths[i] = join(cwd, noSourceDirList[i]);
79
+ }
80
+ findSourceForAbsoluteDirsCb(absPaths, fs, target, signal, (err, s) => {
81
+ if (err) {
82
+ cb(err, null);
83
+ return;
84
+ }
85
+ const finalSource = s || source || parentResource || null;
86
+ external.set(options.dir, finalSource);
87
+ cb(null, finalSource);
88
+ }, options.entries);
89
+ });
90
+ return;
91
+ }
92
+ const absPaths = Array.from({ length: noSourceDirList.length });
93
+ for (let i = 0, len = noSourceDirList.length; i < len; i++) {
94
+ absPaths[i] = join(cwd, noSourceDirList[i]);
95
+ }
96
+ findSourceForAbsoluteDirsCb(absPaths, fs, target, signal, (err, source) => {
97
+ if (err) {
98
+ cb(err, null);
71
99
  return;
72
100
  }
101
+ const finalSource = source || parentResource || null;
102
+ external.set(options.dir, finalSource);
103
+ cb(null, finalSource);
104
+ }, options.entries);
105
+ }
106
+ function findSourceForAbsoluteDirsCb(paths, fs, target, signal, cb, entries) {
107
+ if (signal?.aborted) {
108
+ cb(signal.reason, null);
109
+ return;
73
110
  }
74
- const absPaths = noSourceDirList.map((rel) => join(cwd, rel));
75
- source = await findSourceForAbsoluteDirs(absPaths, ctx, fs, target, signal);
76
- if (source !== undefined) {
77
- for (const noSourceDir of noSourceDirList) {
78
- signal?.throwIfAborted();
79
- ctx.external.set(noSourceDir, source);
111
+ const extractors = target.extractors;
112
+ const plen = paths.length;
113
+ const elen = extractors.length;
114
+ let i = 0;
115
+ let j = 0;
116
+ function next() {
117
+ if (i >= plen) {
118
+ cb(null, null);
119
+ return;
80
120
  }
81
- }
82
- }
83
- async function findSourceForAbsoluteDirs(paths, ctx, fs, target, signal) {
84
- for (const parent of paths) {
85
- for (const extractor of target.extractors) {
86
- signal?.throwIfAborted();
87
- const s = await tryExtractor(parent, fs, ctx, extractor);
88
- if (typeof s === "object" && s.error) {
89
- ctx.failed.push(s);
90
- return s;
121
+ const parent = paths[i];
122
+ const extractor = extractors[j];
123
+ j++;
124
+ if (j >= elen) {
125
+ i++;
126
+ j = 0;
127
+ }
128
+ if (entries && plen > 0 && parent === paths[0]) {
129
+ const epath = extractor.path;
130
+ const slashIdx = epath.indexOf("/");
131
+ const firstSegment = slashIdx === -1 ? epath : epath.substring(0, slashIdx);
132
+ let found = false;
133
+ for (let k = 0, len = entries.length; k < len; k++) {
134
+ if (entries[k].name === firstSegment) {
135
+ found = true;
136
+ break;
137
+ }
91
138
  }
92
- if (typeof s === "object") {
93
- return s;
139
+ if (!found) {
140
+ next();
141
+ return;
94
142
  }
95
143
  }
144
+ tryExtractorCb(parent, fs, extractor, (err, source) => {
145
+ if (err) {
146
+ cb(err, null);
147
+ return;
148
+ }
149
+ if (source !== null) {
150
+ cb(null, source);
151
+ return;
152
+ }
153
+ next();
154
+ });
96
155
  }
97
- return "none";
156
+ next();
98
157
  }
99
- async function tryExtractor(cwd, fs, ctx, extractor) {
100
- let abs = join(cwd, extractor.path);
101
- const name = base(extractor.path);
102
- const newSource = {
103
- name,
104
- path: extractor.path,
105
- inverted: false,
106
- pattern: [],
107
- };
108
- let buff;
109
- try {
110
- buff = await fs.promises.readFile(abs);
111
- }
112
- catch (err) {
113
- const error = err;
114
- if (error.code === "ENOENT") {
115
- return "none";
158
+ function tryExtractorCb(cwd, fs, extractor, cb) {
159
+ const abs = join(cwd, extractor.path);
160
+ fs.readFile(abs, (err, buff) => {
161
+ if (err) {
162
+ const error = err;
163
+ if (error.code === "ENOENT") {
164
+ cb(null, null);
165
+ return;
166
+ }
167
+ cb(null, {
168
+ error,
169
+ source: {
170
+ inverted: false,
171
+ path: extractor.path,
172
+ rules: [],
173
+ },
174
+ });
175
+ return;
116
176
  }
117
- newSource.error = error;
118
- return newSource;
119
- }
120
- try {
121
- const act = extractor.extract(newSource, buff, ctx);
122
- if (act === "none") {
123
- return act;
177
+ const newSource = {
178
+ inverted: false,
179
+ path: extractor.path,
180
+ rules: [],
181
+ };
182
+ const act = extractor.extract(newSource, buff);
183
+ if (act === null) {
184
+ cb(null, null);
185
+ return;
124
186
  }
125
- }
126
- catch (err) {
127
- if (err === "none") {
128
- return err;
187
+ if (act instanceof Error) {
188
+ cb(null, { error: act, source: newSource });
189
+ return;
129
190
  }
130
- newSource.error =
131
- err instanceof Error
132
- ? err
133
- : new Error("Unknown error during source extraction", { cause: err });
134
- return newSource;
135
- }
136
- return newSource;
191
+ cb(null, newSource);
192
+ });
137
193
  }
@@ -0,0 +1,16 @@
1
+ import type { Source } from "./source.js";
2
+ /**
3
+ * Represents missing source, existing source or invalid source.
4
+ *
5
+ * @since 0.11.0
6
+ */
7
+ export type Resource = Source | InvalidSource | null;
8
+ /**
9
+ * Represents a source with an associated error.
10
+ *
11
+ * @since 0.11.0
12
+ */
13
+ export type InvalidSource = {
14
+ source: Source;
15
+ error: Error;
16
+ };
@@ -0,0 +1 @@
1
+ export {};
@@ -48,7 +48,7 @@ export type MatchKind = RuleMatch["kind"];
48
48
  *
49
49
  * @since 0.9.1
50
50
  */
51
- export interface RuleMatchBase<K extends string> {
51
+ export interface RuleMatchBase<K extends string | number | symbol> {
52
52
  kind: K;
53
53
  ignored: boolean;
54
54
  }
@@ -57,7 +57,7 @@ export interface RuleMatchBase<K extends string> {
57
57
  *
58
58
  * @since 0.9.1
59
59
  */
60
- export interface RuleMatchBaseSource<K extends string> extends RuleMatchBase<K> {
60
+ export interface RuleMatchBaseSource<K extends string | number | symbol> extends RuleMatchBase<K> {
61
61
  source: Source;
62
62
  }
63
63
  /**
@@ -65,30 +65,72 @@ export interface RuleMatchBaseSource<K extends string> extends RuleMatchBase<K>
65
65
  *
66
66
  * @since 0.9.1
67
67
  */
68
- export interface RuleMatchBasePattern<K extends string> extends RuleMatchBase<K> {
68
+ export interface RuleMatchBasePattern<K extends string | number | symbol> extends RuleMatchBase<K> {
69
69
  pattern: string;
70
70
  }
71
71
  /**
72
72
  * @see {@link RuleMatch}
73
73
  *
74
- * @since 0.9.1
74
+ * @since 0.11.0
75
75
  */
76
- export interface RuleMatchBaseErrorPattern<K extends string> extends RuleMatchBasePattern<K> {
76
+ export interface RuleMatchBaseError<K extends string | number | symbol> extends RuleMatchBase<K> {
77
77
  error: Error;
78
78
  }
79
+ /**
80
+ * @see {@link RuleMatch}
81
+ *
82
+ * @since 0.11.0
83
+ */
84
+ export interface RuleMatchBaseInvalidSource<K extends string | number | symbol> extends RuleMatchBaseError<K>, RuleMatchBaseSource<K> {
85
+ }
86
+ /**
87
+ * @see {@link RuleMatch}
88
+ *
89
+ * @since 0.11.0
90
+ */
91
+ export interface RuleMatchBaseInvalidPattern<K extends string | number | symbol> extends RuleMatchBasePattern<K>, RuleMatchBaseError<K> {
92
+ }
93
+ /**
94
+ * @see {@link RuleMatch}
95
+ *
96
+ * @since 0.11.0
97
+ */
98
+ export interface RuleMatchBaseInvalidExternal<K extends string | number | symbol> extends RuleMatchBaseInvalidPattern<K>, RuleMatchBaseSource<K> {
99
+ }
79
100
  /**
80
101
  * @see {@link RuleMatch}
81
102
  *
82
103
  * @since 0.9.1
83
104
  */
84
- export interface RuleMatchBaseSourcePattern<K extends string> extends RuleMatchBasePattern<K>, RuleMatchBaseSource<K> {
105
+ export interface RuleMatchBaseExternal<K extends string | number | symbol> extends RuleMatchBasePattern<K>, RuleMatchBaseSource<K> {
106
+ }
107
+ /**
108
+ * The kind of a pattern match.
109
+ *
110
+ * @since 0.11.0
111
+ */
112
+ export declare const enum RuleMatchKind {
113
+ "none" = 0,
114
+ "missingSource" = 1,
115
+ "noMatch" = 2,
116
+ "invalidSource" = 3,
117
+ "invalidExternal" = 4,
118
+ "invalidInternal" = 5,
119
+ "external" = 6,
120
+ "internal" = 7
85
121
  }
86
122
  /**
87
123
  * @see {@link ruleTest}
88
124
  *
89
125
  * @since 0.6.0
90
126
  */
91
- export type RuleMatch = RuleMatchBase<"none" | "missing-source"> | RuleMatchBaseSource<"no-match" | "broken-source" | "invalid-pattern"> | RuleMatchBaseErrorPattern<"invalid-internal-pattern"> | RuleMatchBasePattern<"internal"> | RuleMatchBaseSourcePattern<"external">;
127
+ export type RuleMatch = RuleMatchBase<RuleMatchKind.none> | RuleMatchBase<RuleMatchKind.missingSource> | RuleMatchBaseSource<RuleMatchKind.noMatch> | RuleMatchBaseInvalidSource<RuleMatchKind.invalidSource> | RuleMatchBaseInvalidExternal<RuleMatchKind.invalidExternal> | RuleMatchBaseInvalidPattern<RuleMatchKind.invalidInternal> | RuleMatchBaseExternal<RuleMatchKind.external> | RuleMatchBasePattern<RuleMatchKind.internal>;
128
+ /**
129
+ * Check if a rule match is invalid.
130
+ *
131
+ * @since 0.11.0
132
+ */
133
+ export declare function isRuleMatchInvalid(match: RuleMatch): match is RuleMatchBaseInvalidSource<RuleMatchKind.invalidSource> | RuleMatchBaseInvalidExternal<RuleMatchKind.invalidExternal> | RuleMatchBaseInvalidPattern<RuleMatchKind.invalidInternal>;
92
134
  /**
93
135
  * @see {@link ruleTest}
94
136
  *
@@ -106,16 +148,22 @@ export interface RuleTestOptions extends PatternFinderOptions {
106
148
  */
107
149
  entry: string;
108
150
  /**
109
- * Result of the `dirname(entry)` call.
151
+ * Pre-lowercased entry path.
110
152
  *
111
- * @since 0.10.1
153
+ * @since 0.11.0
112
154
  */
113
- parentPath: string;
155
+ lowerEntry?: string;
114
156
  }
157
+ /**
158
+ * Synchronous version of {@link ruleTest}.
159
+ *
160
+ * @since 0.11.0
161
+ */
162
+ export declare function ruleTestSync(options: RuleTestOptions): RuleMatch;
115
163
  /**
116
164
  * Checks whether a given entry should be ignored based on internal and external patterns.
117
165
  * Populates unknown sources using {@link resolveSources}.
118
166
  *
119
167
  * @since 0.6.0
120
168
  */
121
- export declare function ruleTest(options: RuleTestOptions): Promise<RuleMatch>;
169
+ export declare function ruleTest(options: RuleTestOptions, cb: (err: Error | null, match: RuleMatch) => void): void;
@@ -1,67 +1,119 @@
1
1
  import { patternCacheTest } from "./patternList.js";
2
- function cacheTest(rs, path) {
3
- for (const r of rs) {
2
+ /**
3
+ * The kind of a pattern match.
4
+ *
5
+ * @since 0.11.0
6
+ */
7
+ export var RuleMatchKind;
8
+ (function (RuleMatchKind) {
9
+ RuleMatchKind[RuleMatchKind["none"] = 0] = "none";
10
+ RuleMatchKind[RuleMatchKind["missingSource"] = 1] = "missingSource";
11
+ RuleMatchKind[RuleMatchKind["noMatch"] = 2] = "noMatch";
12
+ RuleMatchKind[RuleMatchKind["invalidSource"] = 3] = "invalidSource";
13
+ RuleMatchKind[RuleMatchKind["invalidExternal"] = 4] = "invalidExternal";
14
+ RuleMatchKind[RuleMatchKind["invalidInternal"] = 5] = "invalidInternal";
15
+ RuleMatchKind[RuleMatchKind["external"] = 6] = "external";
16
+ RuleMatchKind[RuleMatchKind["internal"] = 7] = "internal";
17
+ })(RuleMatchKind || (RuleMatchKind = {}));
18
+ /**
19
+ * Check if a rule match is invalid.
20
+ *
21
+ * @since 0.11.0
22
+ */
23
+ export function isRuleMatchInvalid(match) {
24
+ const k = match.kind;
25
+ return (k === RuleMatchKind.invalidSource ||
26
+ k === RuleMatchKind.invalidExternal ||
27
+ k === RuleMatchKind.invalidInternal);
28
+ }
29
+ function cacheTest(rs, path, matchCtx) {
30
+ const len = rs.length;
31
+ for (let i = 0; i < len; i++) {
32
+ const r = rs[i];
4
33
  try {
5
- if (patternCacheTest(r, path)) {
6
- return [r.pattern, undefined];
34
+ if (patternCacheTest(r, path, matchCtx)) {
35
+ return r;
7
36
  }
8
37
  }
9
38
  catch (err) {
10
- return [r.pattern, err];
39
+ return err;
11
40
  }
12
41
  }
13
- return ["", undefined];
42
+ return null;
14
43
  }
15
- function testInternal(options, path) {
16
- for (const si of options.target.internalRules) {
17
- const compiled = si.compiled;
18
- if (compiled === null)
44
+ /**
45
+ * Synchronous version of {@link ruleTest}.
46
+ *
47
+ * @since 0.11.0
48
+ */
49
+ export function ruleTestSync(options) {
50
+ const src = options.resource;
51
+ if (src === undefined) {
52
+ throw new Error("view-ignored has crashed: no source cached.");
53
+ }
54
+ if (src === null) {
55
+ return { ignored: false, kind: RuleMatchKind.missingSource };
56
+ }
57
+ if ("error" in src) {
58
+ return { ...src, ignored: true, kind: RuleMatchKind.invalidSource };
59
+ }
60
+ const entry = options.entry;
61
+ const matchCtx = { lower: options.lowerEntry };
62
+ const internalRules = options.target.internalRules;
63
+ for (let i = 0, len = internalRules.length; i < len; i++) {
64
+ const rule = internalRules[i];
65
+ const res = cacheTest(rule.compiled, entry, matchCtx);
66
+ if (res === null)
19
67
  continue;
20
- let [patternMatch, error] = cacheTest(compiled, path);
21
- if (error)
68
+ if (res instanceof Error) {
22
69
  return {
23
- kind: "invalid-internal-pattern",
24
- pattern: patternMatch,
25
- error,
70
+ error: res,
26
71
  ignored: false,
72
+ kind: RuleMatchKind.invalidInternal,
73
+ pattern: "",
27
74
  };
28
- if (patternMatch)
29
- return {
30
- kind: "internal",
31
- pattern: patternMatch,
32
- ignored: si.excludes,
33
- };
75
+ }
76
+ return {
77
+ ignored: rule.excludes,
78
+ kind: RuleMatchKind.internal,
79
+ pattern: res.pattern,
80
+ };
34
81
  }
35
- return null;
36
- }
37
- function testExternal(options, path, source) {
38
- for (const si of source.pattern) {
39
- const compiled = si.compiled;
40
- if (compiled === null) {
82
+ const rules = src.rules;
83
+ const elen = rules.length;
84
+ if (elen === 0) {
85
+ return (src._noMatchCache ??
86
+ (src._noMatchCache = {
87
+ ignored: src.inverted,
88
+ kind: RuleMatchKind.noMatch,
89
+ source: src,
90
+ }));
91
+ }
92
+ for (let i = 0; i < elen; i++) {
93
+ const rule = rules[i];
94
+ const res = cacheTest(rule.compiled, entry, matchCtx);
95
+ if (res === null)
41
96
  continue;
42
- }
43
- let [patternMatch, err] = cacheTest(compiled, path);
44
- if (err) {
45
- source.error = err;
46
- options.ctx?.failed.push(source);
97
+ if (res instanceof Error) {
47
98
  return {
48
- kind: "invalid-pattern",
99
+ error: res,
49
100
  ignored: false,
50
- source,
101
+ kind: RuleMatchKind.invalidExternal,
102
+ pattern: "",
103
+ source: src,
51
104
  };
52
105
  }
53
- if (patternMatch)
54
- return {
55
- kind: "external",
56
- pattern: patternMatch,
57
- ignored: si.excludes,
58
- source,
59
- };
106
+ return {
107
+ ignored: rule.excludes,
108
+ kind: RuleMatchKind.external,
109
+ pattern: res.pattern,
110
+ source: src,
111
+ };
60
112
  }
61
113
  return {
62
- kind: "no-match",
63
- ignored: source.inverted,
64
- source,
114
+ ignored: src.inverted,
115
+ kind: RuleMatchKind.noMatch,
116
+ source: src,
65
117
  };
66
118
  }
67
119
  /**
@@ -70,26 +122,11 @@ function testExternal(options, path, source) {
70
122
  *
71
123
  * @since 0.6.0
72
124
  */
73
- export async function ruleTest(options) {
74
- const parent = options.parentPath;
75
- let source = options.ctx?.external.get(parent);
76
- // if (source === undefined) {
77
- // await resolveSources({ ...options, dir: parent })
78
- // source = options.ctx.external.get(parent)
79
- // }
80
- if (source === undefined) {
81
- throw new Error("view-ignored has crashed: no source cached.");
82
- }
83
- if (source === "none") {
84
- return { kind: "missing-source", ignored: false };
85
- }
86
- if (typeof source === "object" && source.error) {
87
- return { kind: "broken-source", ignored: true, source };
125
+ export function ruleTest(options, cb) {
126
+ try {
127
+ cb(null, ruleTestSync(options));
88
128
  }
89
- let internalMatch = testInternal(options, options.entry);
90
- if (internalMatch !== null) {
91
- return internalMatch;
129
+ catch (err) {
130
+ cb(err, null);
92
131
  }
93
- const externalMatch = testExternal(options, options.entry, source);
94
- return externalMatch;
95
132
  }
@@ -1,25 +1,25 @@
1
- import type { Rule } from "./rule.js";
1
+ import type { Rule, RuleMatch } from "./rule.js";
2
2
  /**
3
3
  * Represents a source of external patterns.
4
4
  *
5
5
  * @since 0.6.0
6
6
  */
7
7
  export type Source = {
8
+ /**
9
+ * @internal
10
+ *
11
+ * @since 0.11.0
12
+ */
13
+ _noMatchCache?: RuleMatch;
8
14
  /**
9
15
  * Patterns defined within the source file.
10
16
  * Those patterns are for ignoring files.
11
17
  *
12
18
  * @see {@link ruleTest}
13
19
  *
14
- * @since 0.6.0
20
+ * @since 0.11.0
15
21
  */
16
- pattern: Rule[];
17
- /**
18
- * Name of the source file.
19
- *
20
- * @since 0.6.0
21
- */
22
- name: string;
22
+ rules: Rule[];
23
23
  /**
24
24
  * Relative path to the source file.
25
25
  *
@@ -36,14 +36,6 @@ export type Source = {
36
36
  * @since 0.6.0
37
37
  */
38
38
  inverted: boolean;
39
- /**
40
- * Error encountered during extraction, if any.
41
- *
42
- * @see {@link ExtractorFn}
43
- *
44
- * @since 0.6.0
45
- */
46
- error?: Error;
47
39
  };
48
40
  /**
49
41
  * Adds a negatable pattern to the source's rules.