@markuplint/file-resolver 4.0.0-alpha.3 → 4.0.0-dev.28
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/lib/auto-load-rules.js +6 -6
- package/lib/config-provider.d.ts +4 -1
- package/lib/config-provider.js +93 -80
- package/lib/cosmiconfig.d.ts +9 -3
- package/lib/cosmiconfig.js +15 -12
- package/lib/ml-file/ml-file.js +16 -6
- package/lib/resolve-parser.js +1 -0
- package/lib/resolve-plugins.js +3 -3
- package/lib/resolve-rules.js +10 -10
- package/lib/resolve-specs.js +3 -1
- package/lib/utils.js +1 -1
- package/package.json +12 -13
package/lib/auto-load-rules.js
CHANGED
|
@@ -20,8 +20,8 @@ export async function autoLoadRules(ruleset) {
|
|
|
20
20
|
seed = null;
|
|
21
21
|
}
|
|
22
22
|
}
|
|
23
|
-
catch (
|
|
24
|
-
errors.push(
|
|
23
|
+
catch (error) {
|
|
24
|
+
errors.push(error);
|
|
25
25
|
}
|
|
26
26
|
if (seed) {
|
|
27
27
|
const rule = new MLRule({
|
|
@@ -39,8 +39,8 @@ export async function autoLoadRules(ruleset) {
|
|
|
39
39
|
seed = null;
|
|
40
40
|
}
|
|
41
41
|
}
|
|
42
|
-
catch (
|
|
43
|
-
errors.push(
|
|
42
|
+
catch (error) {
|
|
43
|
+
errors.push(error);
|
|
44
44
|
}
|
|
45
45
|
if (seed) {
|
|
46
46
|
const rule = new MLRule({
|
|
@@ -54,8 +54,8 @@ export async function autoLoadRules(ruleset) {
|
|
|
54
54
|
}
|
|
55
55
|
return {
|
|
56
56
|
// Clone
|
|
57
|
-
rules: rules
|
|
57
|
+
rules: [...rules],
|
|
58
58
|
// Clone
|
|
59
|
-
errors: errors
|
|
59
|
+
errors: [...errors],
|
|
60
60
|
};
|
|
61
61
|
}
|
package/lib/config-provider.d.ts
CHANGED
|
@@ -4,12 +4,15 @@ import type { Config } from '@markuplint/ml-config';
|
|
|
4
4
|
import type { Nullable } from '@markuplint/shared';
|
|
5
5
|
export declare class ConfigProvider {
|
|
6
6
|
#private;
|
|
7
|
+
recursiveLoad(key: string, cache: boolean, referrer: string, depth?: number): Promise<{
|
|
8
|
+
stack: Set<string>;
|
|
9
|
+
errs: Error[];
|
|
10
|
+
}>;
|
|
7
11
|
resolve(targetFile: Readonly<MLFile>, names: readonly Nullable<string>[], cache?: boolean): Promise<ConfigSet>;
|
|
8
12
|
search(targetFile: Readonly<MLFile>): Promise<string | null>;
|
|
9
13
|
set(config: Config, key?: string): string;
|
|
10
14
|
private _load;
|
|
11
15
|
private _mergeConfigs;
|
|
12
16
|
private _pathResolve;
|
|
13
|
-
private _recursiveLoad;
|
|
14
17
|
private _validateConfig;
|
|
15
18
|
}
|
package/lib/config-provider.js
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
2
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
3
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
4
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
5
|
+
};
|
|
1
6
|
var _ConfigProvider_cache, _ConfigProvider_held, _ConfigProvider_recursiveLoadKeyAndDepth, _ConfigProvider_store;
|
|
2
|
-
import { __classPrivateFieldGet } from "tslib";
|
|
3
7
|
import { createRequire } from 'node:module';
|
|
4
8
|
import path from 'node:path';
|
|
5
9
|
import { mergeConfig } from '@markuplint/ml-config';
|
|
@@ -7,7 +11,7 @@ import { getPreset } from '@markuplint/ml-core';
|
|
|
7
11
|
import { ConfigParserError } from '@markuplint/parser-utils';
|
|
8
12
|
import { InvalidSelectorError, createSelector } from '@markuplint/selector';
|
|
9
13
|
import { nonNullableFilter, toNoEmptyStringArrayFromStringOrArray } from '@markuplint/shared';
|
|
10
|
-
import { load as loadConfig, search } from './cosmiconfig.js';
|
|
14
|
+
import { ConfigLoadError, load as loadConfig, search } from './cosmiconfig.js';
|
|
11
15
|
import { log } from './debug.js';
|
|
12
16
|
import { cacheClear, resolvePlugins } from './resolve-plugins.js';
|
|
13
17
|
import { fileExists, uuid } from './utils.js';
|
|
@@ -21,6 +25,44 @@ export class ConfigProvider {
|
|
|
21
25
|
_ConfigProvider_recursiveLoadKeyAndDepth.set(this, new Map());
|
|
22
26
|
_ConfigProvider_store.set(this, new Map());
|
|
23
27
|
}
|
|
28
|
+
async recursiveLoad(key, cache, referrer, depth = 1) {
|
|
29
|
+
const stack = new Set();
|
|
30
|
+
const errs = [];
|
|
31
|
+
const ancestorDepth = __classPrivateFieldGet(this, _ConfigProvider_recursiveLoadKeyAndDepth, "f").get(key);
|
|
32
|
+
if (ancestorDepth != null && ancestorDepth < depth) {
|
|
33
|
+
return {
|
|
34
|
+
stack,
|
|
35
|
+
errs: [new CircularReferenceError(`Circular reference detected: ${key}`)],
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
__classPrivateFieldGet(this, _ConfigProvider_recursiveLoadKeyAndDepth, "f").set(key, depth);
|
|
39
|
+
let config = __classPrivateFieldGet(this, _ConfigProvider_store, "f").get(key);
|
|
40
|
+
if (!config) {
|
|
41
|
+
config = await this._load(key, cache, referrer);
|
|
42
|
+
}
|
|
43
|
+
if (!config) {
|
|
44
|
+
return { stack, errs: [] };
|
|
45
|
+
}
|
|
46
|
+
if (config instanceof ConfigLoadError) {
|
|
47
|
+
stack.add(config.filePath);
|
|
48
|
+
return {
|
|
49
|
+
stack,
|
|
50
|
+
errs: [config],
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
const depKeys = config.extends === null ? null : toNoEmptyStringArrayFromStringOrArray(config.extends);
|
|
54
|
+
if (depKeys) {
|
|
55
|
+
for (const depKey of depKeys) {
|
|
56
|
+
const keys = await this.recursiveLoad(depKey, cache, key, depth + 1);
|
|
57
|
+
for (const key of keys.stack) {
|
|
58
|
+
stack.add(key);
|
|
59
|
+
}
|
|
60
|
+
errs.push(...keys.errs);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
stack.add(key);
|
|
64
|
+
return { stack, errs };
|
|
65
|
+
}
|
|
24
66
|
async resolve(targetFile, names, cache = true) {
|
|
25
67
|
if (!cache) {
|
|
26
68
|
__classPrivateFieldGet(this, _ConfigProvider_store, "f").clear();
|
|
@@ -33,8 +75,8 @@ export class ConfigProvider {
|
|
|
33
75
|
if (currentConfig) {
|
|
34
76
|
return currentConfig;
|
|
35
77
|
}
|
|
36
|
-
let configSet = await this._mergeConfigs(keys, cache);
|
|
37
|
-
const filePath =
|
|
78
|
+
let configSet = await this._mergeConfigs(keys, cache, targetFile.path);
|
|
79
|
+
const filePath = [...configSet.files].reverse()[0];
|
|
38
80
|
if (!filePath) {
|
|
39
81
|
throw new ConfigParserError('Config file not found', {
|
|
40
82
|
filePath: targetFile.path,
|
|
@@ -44,7 +86,7 @@ export class ConfigProvider {
|
|
|
44
86
|
configSet.errs.push(...errors);
|
|
45
87
|
const plugins = await resolvePlugins(configSet.config.plugins);
|
|
46
88
|
if (__classPrivateFieldGet(this, _ConfigProvider_held, "f").size > 0) {
|
|
47
|
-
const extendHelds =
|
|
89
|
+
const extendHelds = [...__classPrivateFieldGet(this, _ConfigProvider_held, "f").values()];
|
|
48
90
|
for (const held of extendHelds) {
|
|
49
91
|
const [, prefix, namespace, name] = held.match(/^([a-z]+:)([^/]+)(?:\/(.+))?$/) ?? [];
|
|
50
92
|
switch (prefix) {
|
|
@@ -58,7 +100,7 @@ export class ConfigProvider {
|
|
|
58
100
|
}
|
|
59
101
|
}
|
|
60
102
|
}
|
|
61
|
-
configSet = await this._mergeConfigs([...keys, ...extendHelds], cache);
|
|
103
|
+
configSet = await this._mergeConfigs([...keys, ...extendHelds], cache, targetFile.path);
|
|
62
104
|
__classPrivateFieldGet(this, _ConfigProvider_held, "f").clear();
|
|
63
105
|
}
|
|
64
106
|
// Resolves `overrides`
|
|
@@ -104,7 +146,7 @@ export class ConfigProvider {
|
|
|
104
146
|
__classPrivateFieldGet(this, _ConfigProvider_store, "f").set(key, config);
|
|
105
147
|
return key;
|
|
106
148
|
}
|
|
107
|
-
async _load(filePath, cache) {
|
|
149
|
+
async _load(filePath, cache, referrer) {
|
|
108
150
|
const entity = __classPrivateFieldGet(this, _ConfigProvider_store, "f").get(filePath);
|
|
109
151
|
if (entity) {
|
|
110
152
|
return entity;
|
|
@@ -118,37 +160,37 @@ export class ConfigProvider {
|
|
|
118
160
|
}
|
|
119
161
|
if (isPlugin(filePath)) {
|
|
120
162
|
__classPrivateFieldGet(this, _ConfigProvider_held, "f").add(filePath);
|
|
121
|
-
return
|
|
163
|
+
return;
|
|
122
164
|
}
|
|
123
165
|
if (!(await moduleExists(filePath)) && !path.isAbsolute(filePath)) {
|
|
124
166
|
throw new TypeError(`${filePath} is not an absolute path`);
|
|
125
167
|
}
|
|
126
|
-
const config = await load(filePath, cache);
|
|
127
|
-
if (
|
|
128
|
-
return
|
|
168
|
+
const config = await load(filePath, cache, referrer);
|
|
169
|
+
if (config instanceof ConfigLoadError) {
|
|
170
|
+
return config;
|
|
129
171
|
}
|
|
130
172
|
const pathResolvedConfig = await this._pathResolve(config, filePath);
|
|
131
173
|
__classPrivateFieldGet(this, _ConfigProvider_store, "f").set(filePath, pathResolvedConfig);
|
|
132
174
|
return pathResolvedConfig;
|
|
133
175
|
}
|
|
134
|
-
async _mergeConfigs(keys, cache) {
|
|
176
|
+
async _mergeConfigs(keys, cache, referrer) {
|
|
135
177
|
const resolvedKeys = new Set();
|
|
136
178
|
const errs = [];
|
|
137
179
|
for (const key of keys) {
|
|
138
180
|
__classPrivateFieldGet(this, _ConfigProvider_recursiveLoadKeyAndDepth, "f").clear();
|
|
139
|
-
const keySet = await this.
|
|
181
|
+
const keySet = await this.recursiveLoad(key, cache, referrer);
|
|
140
182
|
for (const k of keySet.stack) {
|
|
141
183
|
resolvedKeys.add(k);
|
|
142
184
|
}
|
|
143
|
-
|
|
144
|
-
errs.push(...keySet.errs);
|
|
145
|
-
}
|
|
185
|
+
errs.push(...keySet.errs);
|
|
146
186
|
}
|
|
147
|
-
const configs =
|
|
148
|
-
.map(name => __classPrivateFieldGet(this, _ConfigProvider_store, "f").get(name))
|
|
149
|
-
.filter(nonNullableFilter);
|
|
187
|
+
const configs = [...resolvedKeys].map(name => __classPrivateFieldGet(this, _ConfigProvider_store, "f").get(name)).filter(nonNullableFilter);
|
|
150
188
|
let resultConfig = {};
|
|
151
189
|
for (const config of configs) {
|
|
190
|
+
if (config instanceof ConfigLoadError) {
|
|
191
|
+
errs.push(config);
|
|
192
|
+
continue;
|
|
193
|
+
}
|
|
152
194
|
resultConfig = mergeConfig(resultConfig, config);
|
|
153
195
|
}
|
|
154
196
|
return {
|
|
@@ -169,69 +211,42 @@ export class ConfigProvider {
|
|
|
169
211
|
overrides: await pathResolve(dir, config.overrides, undefined, true),
|
|
170
212
|
};
|
|
171
213
|
}
|
|
172
|
-
async _recursiveLoad(key, cache, depth = 1) {
|
|
173
|
-
const stack = new Set();
|
|
174
|
-
const errs = [];
|
|
175
|
-
const ancestorDepth = __classPrivateFieldGet(this, _ConfigProvider_recursiveLoadKeyAndDepth, "f").get(key);
|
|
176
|
-
if (ancestorDepth != null && ancestorDepth < depth) {
|
|
177
|
-
return {
|
|
178
|
-
stack,
|
|
179
|
-
errs: [new CircularReferenceError(`Circular reference detected: ${key}`)],
|
|
180
|
-
};
|
|
181
|
-
}
|
|
182
|
-
__classPrivateFieldGet(this, _ConfigProvider_recursiveLoadKeyAndDepth, "f").set(key, depth);
|
|
183
|
-
let config = __classPrivateFieldGet(this, _ConfigProvider_store, "f").get(key) ?? null;
|
|
184
|
-
if (!config) {
|
|
185
|
-
config = await this._load(key, cache);
|
|
186
|
-
}
|
|
187
|
-
if (!config) {
|
|
188
|
-
return { stack, errs: null };
|
|
189
|
-
}
|
|
190
|
-
const depKeys = config.extends !== null ? toNoEmptyStringArrayFromStringOrArray(config.extends) : null;
|
|
191
|
-
if (depKeys) {
|
|
192
|
-
for (const depKey of depKeys) {
|
|
193
|
-
const keys = await this._recursiveLoad(depKey, cache, depth + 1);
|
|
194
|
-
for (const key of keys.stack) {
|
|
195
|
-
stack.add(key);
|
|
196
|
-
}
|
|
197
|
-
if (keys.errs) {
|
|
198
|
-
errs.push(...keys.errs);
|
|
199
|
-
}
|
|
200
|
-
}
|
|
201
|
-
}
|
|
202
|
-
stack.add(key);
|
|
203
|
-
return { stack, errs };
|
|
204
|
-
}
|
|
205
214
|
_validateConfig(config, filePath) {
|
|
206
215
|
const errors = [];
|
|
207
|
-
config.nodeRules
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
216
|
+
if (config.nodeRules)
|
|
217
|
+
for (const rule of config.nodeRules) {
|
|
218
|
+
if (rule.selector) {
|
|
219
|
+
try {
|
|
220
|
+
createSelector(rule.selector);
|
|
221
|
+
}
|
|
222
|
+
catch (error) {
|
|
223
|
+
if (error instanceof InvalidSelectorError) {
|
|
224
|
+
errors.push(new ConfigParserError(error.message, {
|
|
225
|
+
filePath,
|
|
226
|
+
raw: rule.selector,
|
|
227
|
+
}));
|
|
228
|
+
}
|
|
218
229
|
}
|
|
219
230
|
}
|
|
220
231
|
}
|
|
221
|
-
});
|
|
222
232
|
return errors;
|
|
223
233
|
}
|
|
224
234
|
}
|
|
225
235
|
_ConfigProvider_cache = new WeakMap(), _ConfigProvider_held = new WeakMap(), _ConfigProvider_recursiveLoadKeyAndDepth = new WeakMap(), _ConfigProvider_store = new WeakMap();
|
|
226
|
-
async function load(filePath, cache) {
|
|
236
|
+
async function load(filePath, cache, referrer) {
|
|
227
237
|
if (!fileExists(filePath) && (await moduleExists(filePath))) {
|
|
228
238
|
const mod = await import(filePath);
|
|
229
|
-
const config = mod?.default ??
|
|
239
|
+
const config = mod?.default ?? new ConfigLoadError('Module is not found', filePath, referrer);
|
|
230
240
|
return config;
|
|
231
241
|
}
|
|
232
|
-
const res = await loadConfig(filePath, !cache)
|
|
233
|
-
|
|
234
|
-
|
|
242
|
+
const res = await loadConfig(filePath, !cache, referrer).catch((error) => {
|
|
243
|
+
if (error instanceof ConfigLoadError) {
|
|
244
|
+
return error;
|
|
245
|
+
}
|
|
246
|
+
throw error;
|
|
247
|
+
});
|
|
248
|
+
if (res instanceof ConfigLoadError) {
|
|
249
|
+
return res;
|
|
235
250
|
}
|
|
236
251
|
return res.config;
|
|
237
252
|
}
|
|
@@ -286,33 +301,31 @@ async function moduleExists(name) {
|
|
|
286
301
|
try {
|
|
287
302
|
await import(name);
|
|
288
303
|
}
|
|
289
|
-
catch (
|
|
290
|
-
if (
|
|
291
|
-
|
|
292
|
-
return true;
|
|
293
|
-
}
|
|
304
|
+
catch (error) {
|
|
305
|
+
if (error instanceof Error && /^parse failure/i.test(error.message)) {
|
|
306
|
+
return true;
|
|
294
307
|
}
|
|
295
308
|
try {
|
|
296
309
|
require.resolve(name);
|
|
297
310
|
}
|
|
298
|
-
catch (
|
|
311
|
+
catch (error) {
|
|
299
312
|
if (
|
|
300
313
|
// @ts-ignore
|
|
301
|
-
'code' in
|
|
314
|
+
'code' in error &&
|
|
302
315
|
// @ts-ignore
|
|
303
|
-
|
|
316
|
+
error.code === 'ERR_PACKAGE_PATH_NOT_EXPORTED') {
|
|
304
317
|
// Even if there are issues with the fields,
|
|
305
318
|
// assume that the module exists and return true.
|
|
306
319
|
return true;
|
|
307
320
|
}
|
|
308
321
|
if (
|
|
309
322
|
// @ts-ignore
|
|
310
|
-
'code' in
|
|
323
|
+
'code' in error &&
|
|
311
324
|
// @ts-ignore
|
|
312
|
-
|
|
325
|
+
error.code === 'MODULE_NOT_FOUND') {
|
|
313
326
|
return false;
|
|
314
327
|
}
|
|
315
|
-
throw
|
|
328
|
+
throw error;
|
|
316
329
|
}
|
|
317
330
|
}
|
|
318
331
|
return true;
|
package/lib/cosmiconfig.d.ts
CHANGED
|
@@ -1,11 +1,17 @@
|
|
|
1
1
|
import type { LoaderSync } from 'cosmiconfig';
|
|
2
2
|
type CosmiConfig = ReturnType<LoaderSync>;
|
|
3
|
-
export declare function search<T = CosmiConfig>(
|
|
3
|
+
export declare function search<T = CosmiConfig>(filePath: string, cacheClear: boolean): Promise<{
|
|
4
4
|
filePath: string;
|
|
5
5
|
config: T;
|
|
6
6
|
} | null>;
|
|
7
|
-
export declare function load<T = CosmiConfig>(filePath: string, cacheClear: boolean): Promise<{
|
|
7
|
+
export declare function load<T = CosmiConfig>(filePath: string, cacheClear: boolean, referrer: string): Promise<ConfigLoadError | {
|
|
8
8
|
filePath: string;
|
|
9
9
|
config: T;
|
|
10
|
-
}
|
|
10
|
+
}>;
|
|
11
|
+
export declare class ConfigLoadError extends Error {
|
|
12
|
+
filePath: string;
|
|
13
|
+
name: string;
|
|
14
|
+
referrer: string;
|
|
15
|
+
constructor(message: string, filePath: string, referrer: string);
|
|
16
|
+
}
|
|
11
17
|
export {};
|
package/lib/cosmiconfig.js
CHANGED
|
@@ -19,13 +19,13 @@ const explorer = cosmiconfig('markuplint', {
|
|
|
19
19
|
}),
|
|
20
20
|
},
|
|
21
21
|
});
|
|
22
|
-
export async function search(
|
|
22
|
+
export async function search(filePath, cacheClear) {
|
|
23
23
|
if (cacheClear) {
|
|
24
24
|
explorer.clearCaches();
|
|
25
25
|
}
|
|
26
|
-
dir = path.dirname(
|
|
26
|
+
const dir = path.dirname(filePath);
|
|
27
27
|
searchLog('Search dir: %s', dir);
|
|
28
|
-
const result = await explorer.search(dir).catch(cacheConfigError(dir));
|
|
28
|
+
const result = await explorer.search(dir).catch(cacheConfigError(dir, filePath));
|
|
29
29
|
searchLog('Search result: %O', result);
|
|
30
30
|
if (!result || result.isEmpty) {
|
|
31
31
|
return null;
|
|
@@ -35,36 +35,39 @@ export async function search(dir, cacheClear) {
|
|
|
35
35
|
config: result.config,
|
|
36
36
|
};
|
|
37
37
|
}
|
|
38
|
-
export async function load(filePath, cacheClear) {
|
|
38
|
+
export async function load(filePath, cacheClear, referrer) {
|
|
39
39
|
if (cacheClear) {
|
|
40
40
|
explorer.clearCaches();
|
|
41
41
|
}
|
|
42
|
-
const result = await explorer.load(filePath).catch(cacheConfigError(filePath));
|
|
42
|
+
const result = await explorer.load(filePath).catch(cacheConfigError(filePath, referrer));
|
|
43
43
|
if (!result || result.isEmpty) {
|
|
44
|
-
return
|
|
44
|
+
return new ConfigLoadError('Config file is empty', filePath, referrer);
|
|
45
45
|
}
|
|
46
46
|
return {
|
|
47
47
|
filePath: result.filepath,
|
|
48
48
|
config: result.config,
|
|
49
49
|
};
|
|
50
50
|
}
|
|
51
|
-
class ConfigLoadError extends Error {
|
|
52
|
-
constructor(message, filePath) {
|
|
53
|
-
super(message + ` in ${
|
|
51
|
+
export class ConfigLoadError extends Error {
|
|
52
|
+
constructor(message, filePath, referrer) {
|
|
53
|
+
super(message + ` in ${referrer}`);
|
|
54
54
|
this.name = 'ConfigLoadError';
|
|
55
|
+
this.filePath = filePath;
|
|
56
|
+
this.referrer = referrer;
|
|
55
57
|
}
|
|
56
58
|
}
|
|
57
|
-
function cacheConfigError(fileOrDirPath) {
|
|
59
|
+
function cacheConfigError(fileOrDirPath, referrer) {
|
|
58
60
|
return (reason) => {
|
|
59
61
|
if (reason instanceof Error) {
|
|
60
62
|
switch (reason.name) {
|
|
61
|
-
case 'YAMLException':
|
|
63
|
+
case 'YAMLException': {
|
|
62
64
|
throw new ConfigParserError(reason.message, {
|
|
63
65
|
// @ts-ignore
|
|
64
66
|
filePath: reason.filepath ?? fileOrDirPath,
|
|
65
67
|
});
|
|
68
|
+
}
|
|
66
69
|
}
|
|
67
|
-
throw new ConfigLoadError(reason.message, fileOrDirPath);
|
|
70
|
+
throw new ConfigLoadError(reason.message, fileOrDirPath, referrer);
|
|
68
71
|
}
|
|
69
72
|
throw reason;
|
|
70
73
|
};
|
package/lib/ml-file/ml-file.js
CHANGED
|
@@ -1,5 +1,15 @@
|
|
|
1
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
2
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
3
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
4
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
5
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
6
|
+
};
|
|
7
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
8
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
9
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
10
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
11
|
+
};
|
|
1
12
|
var _MLFile_basename, _MLFile_code, _MLFile_dirname, _MLFile_stat, _MLFile_type;
|
|
2
|
-
import { __classPrivateFieldGet, __classPrivateFieldSet } from "tslib";
|
|
3
13
|
import { promises as fs } from 'node:fs';
|
|
4
14
|
import path from 'node:path';
|
|
5
15
|
import ignore from 'ignore';
|
|
@@ -96,7 +106,7 @@ export class MLFile {
|
|
|
96
106
|
__classPrivateFieldSet(this, _MLFile_code, code, "f");
|
|
97
107
|
}
|
|
98
108
|
async _fetch() {
|
|
99
|
-
const code = await fs.readFile(this.path, { encoding: '
|
|
109
|
+
const code = await fs.readFile(this.path, { encoding: 'utf8' });
|
|
100
110
|
__classPrivateFieldSet(this, _MLFile_code, code, "f");
|
|
101
111
|
return code;
|
|
102
112
|
}
|
|
@@ -113,15 +123,15 @@ async function stat(filePath) {
|
|
|
113
123
|
try {
|
|
114
124
|
return await fs.stat(filePath);
|
|
115
125
|
}
|
|
116
|
-
catch (
|
|
126
|
+
catch (error) {
|
|
117
127
|
if (
|
|
118
128
|
// @ts-ignore
|
|
119
|
-
'code' in
|
|
129
|
+
'code' in error &&
|
|
120
130
|
// @ts-ignore
|
|
121
|
-
|
|
131
|
+
error.code === 'ENOENT') {
|
|
122
132
|
return null;
|
|
123
133
|
}
|
|
124
|
-
throw
|
|
134
|
+
throw error;
|
|
125
135
|
}
|
|
126
136
|
}
|
|
127
137
|
function pathNormalize(filePath, relative = false) {
|
package/lib/resolve-parser.js
CHANGED
|
@@ -10,6 +10,7 @@ export async function resolveParser(file, parserConfig, parserOptions) {
|
|
|
10
10
|
let parserModName = '@markuplint/html-parser';
|
|
11
11
|
let matched = false;
|
|
12
12
|
for (const pattern of Object.keys(parserConfig)) {
|
|
13
|
+
// eslint-disable-next-line unicorn/prefer-regexp-test
|
|
13
14
|
if (path.basename(file.path).match(toRegexp(pattern))) {
|
|
14
15
|
const modName = parserConfig[pattern];
|
|
15
16
|
if (!modName) {
|
package/lib/resolve-plugins.js
CHANGED
|
@@ -5,7 +5,7 @@ export async function resolvePlugins(pluginPaths) {
|
|
|
5
5
|
}
|
|
6
6
|
const plugins = await Promise.all(pluginPaths.map(p => importPlugin(p)));
|
|
7
7
|
// Clone
|
|
8
|
-
return plugins
|
|
8
|
+
return [...plugins];
|
|
9
9
|
}
|
|
10
10
|
export function cacheClear() {
|
|
11
11
|
cache.clear();
|
|
@@ -32,7 +32,7 @@ async function importPlugin(pluginPath) {
|
|
|
32
32
|
name = config.name
|
|
33
33
|
.toLowerCase()
|
|
34
34
|
.replace(/^(?:markuplint-rule-|@markuplint\/rule-)/i, '')
|
|
35
|
-
.
|
|
35
|
+
.replaceAll(/\s+|\/|\\|\./g, '-');
|
|
36
36
|
// eslint-disable-next-line no-console
|
|
37
37
|
console.info(`The plugin name became "${name}"`);
|
|
38
38
|
}
|
|
@@ -48,7 +48,7 @@ function getPluginConfig(pluginPath) {
|
|
|
48
48
|
return pluginPath;
|
|
49
49
|
}
|
|
50
50
|
async function failSafeImport(name) {
|
|
51
|
-
const res = await import(name).catch(
|
|
51
|
+
const res = await import(name).catch(error => error);
|
|
52
52
|
if ('code' in res && res === 'MODULE_NOT_FOUND') {
|
|
53
53
|
return null;
|
|
54
54
|
}
|
package/lib/resolve-rules.js
CHANGED
|
@@ -7,30 +7,30 @@ export async function resolveRules(plugins, ruleset, importPreset,
|
|
|
7
7
|
*/
|
|
8
8
|
autoLoad) {
|
|
9
9
|
const rules = importPreset ? await importPresetRules() : [];
|
|
10
|
-
|
|
10
|
+
for (const plugin of plugins) {
|
|
11
11
|
if (!plugin.rules) {
|
|
12
|
-
|
|
12
|
+
continue;
|
|
13
13
|
}
|
|
14
|
-
Object.entries(plugin.rules)
|
|
14
|
+
for (const [name, seed] of Object.entries(plugin.rules)) {
|
|
15
15
|
const rule = new MLRule({
|
|
16
16
|
name: `${plugin.name}/${name}`,
|
|
17
17
|
...seed,
|
|
18
18
|
});
|
|
19
19
|
rules.push(rule);
|
|
20
|
-
}
|
|
21
|
-
}
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
22
|
if (autoLoad) {
|
|
23
23
|
const { rules: additionalRules } = await autoLoadRules(ruleset);
|
|
24
|
-
|
|
24
|
+
for (const rule of additionalRules) {
|
|
25
25
|
rules.push(rule);
|
|
26
|
-
}
|
|
26
|
+
}
|
|
27
27
|
}
|
|
28
28
|
// Clone
|
|
29
|
-
return rules
|
|
29
|
+
return [...rules];
|
|
30
30
|
}
|
|
31
31
|
async function importPresetRules() {
|
|
32
32
|
if (cachedPresetRules) {
|
|
33
|
-
return cachedPresetRules
|
|
33
|
+
return [...cachedPresetRules];
|
|
34
34
|
}
|
|
35
35
|
const modName = '@markuplint/rules';
|
|
36
36
|
const mod = await import(modName);
|
|
@@ -44,5 +44,5 @@ async function importPresetRules() {
|
|
|
44
44
|
});
|
|
45
45
|
cachedPresetRules = ruleList;
|
|
46
46
|
// Clone
|
|
47
|
-
return ruleList
|
|
47
|
+
return [...ruleList];
|
|
48
48
|
}
|
package/lib/resolve-specs.js
CHANGED
|
@@ -41,6 +41,7 @@ export async function resolveSpecs(filePath, specConfig) {
|
|
|
41
41
|
}
|
|
42
42
|
else {
|
|
43
43
|
for (const pattern of Object.keys(specConfig)) {
|
|
44
|
+
// eslint-disable-next-line unicorn/prefer-regexp-test
|
|
44
45
|
if (path.basename(filePath).match(toRegexp(pattern))) {
|
|
45
46
|
const specModName = specConfig[pattern];
|
|
46
47
|
if (!specModName) {
|
|
@@ -65,7 +66,8 @@ async function importSpecs(specModName) {
|
|
|
65
66
|
return spec;
|
|
66
67
|
}
|
|
67
68
|
}
|
|
68
|
-
const
|
|
69
|
+
const mod = await import(specModName);
|
|
70
|
+
const spec = mod.default;
|
|
69
71
|
// @ts-ignore
|
|
70
72
|
caches.set(specModName, spec);
|
|
71
73
|
return spec;
|
package/lib/utils.js
CHANGED
|
@@ -9,7 +9,7 @@ export function fileExists(filePath) {
|
|
|
9
9
|
return fs.existsSync(filePath);
|
|
10
10
|
}
|
|
11
11
|
export function toRegexp(pattern) {
|
|
12
|
-
const matched = pattern.match(/^\/(.+)\/([
|
|
12
|
+
const matched = pattern.match(/^\/(.+)\/([gi]*)$/i);
|
|
13
13
|
if (matched && matched[1]) {
|
|
14
14
|
return new RegExp(matched[1], matched[2]);
|
|
15
15
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@markuplint/file-resolver",
|
|
3
|
-
"version": "4.0.0-
|
|
3
|
+
"version": "4.0.0-dev.28+0131de5e",
|
|
4
4
|
"description": "The file resolver of markuplint",
|
|
5
5
|
"repository": "git@github.com:markuplint/markuplint.git",
|
|
6
6
|
"author": "Yusuke Hirao <yusukehirao@me.com>",
|
|
@@ -24,24 +24,23 @@
|
|
|
24
24
|
"clean": "tsc --build --clean"
|
|
25
25
|
},
|
|
26
26
|
"devDependencies": {
|
|
27
|
-
"@types/node": "20.8.
|
|
27
|
+
"@types/node": "20.8.7"
|
|
28
28
|
},
|
|
29
29
|
"dependencies": {
|
|
30
|
-
"@markuplint/html-parser": "4.0.0-
|
|
31
|
-
"@markuplint/ml-ast": "4.0.0-
|
|
32
|
-
"@markuplint/ml-config": "4.0.0-
|
|
33
|
-
"@markuplint/ml-core": "4.0.0-
|
|
34
|
-
"@markuplint/ml-spec": "4.0.0-
|
|
35
|
-
"@markuplint/parser-utils": "4.0.0-
|
|
36
|
-
"@markuplint/selector": "4.0.0-
|
|
37
|
-
"@markuplint/shared": "4.0.0-
|
|
30
|
+
"@markuplint/html-parser": "4.0.0-dev.28+0131de5e",
|
|
31
|
+
"@markuplint/ml-ast": "4.0.0-dev.28+0131de5e",
|
|
32
|
+
"@markuplint/ml-config": "4.0.0-dev.28+0131de5e",
|
|
33
|
+
"@markuplint/ml-core": "4.0.0-dev.28+0131de5e",
|
|
34
|
+
"@markuplint/ml-spec": "4.0.0-dev.28+0131de5e",
|
|
35
|
+
"@markuplint/parser-utils": "4.0.0-dev.28+0131de5e",
|
|
36
|
+
"@markuplint/selector": "4.0.0-dev.28+0131de5e",
|
|
37
|
+
"@markuplint/shared": "4.0.0-dev.28+0131de5e",
|
|
38
38
|
"cosmiconfig": "^8.3.6",
|
|
39
39
|
"debug": "^4.3.4",
|
|
40
40
|
"glob": "^10.3.6",
|
|
41
41
|
"ignore": "^5.2.4",
|
|
42
42
|
"jsonc": "^2.0.0",
|
|
43
|
-
"minimatch": "^9.0.3"
|
|
44
|
-
"tslib": "^2.6.2"
|
|
43
|
+
"minimatch": "^9.0.3"
|
|
45
44
|
},
|
|
46
|
-
"gitHead": "
|
|
45
|
+
"gitHead": "0131de5ea9dd6d3fd5472d7b414b66644c758881"
|
|
47
46
|
}
|