@markuplint/file-resolver 3.13.0 → 4.0.0-alpha.2
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 -33
- package/lib/config-provider.d.ts +2 -2
- package/lib/config-provider.js +102 -91
- package/lib/cosmiconfig.js +12 -21
- package/lib/index.d.ts +7 -7
- package/lib/index.js +6 -9
- package/lib/ml-file/get-anonymous-file.d.ts +1 -1
- package/lib/ml-file/get-anonymous-file.js +3 -7
- package/lib/ml-file/get-file.d.ts +1 -1
- package/lib/ml-file/get-file.js +3 -7
- package/lib/ml-file/get-files.d.ts +1 -1
- package/lib/ml-file/get-files.js +5 -9
- package/lib/ml-file/index.d.ts +4 -4
- package/lib/ml-file/index.js +3 -6
- package/lib/ml-file/ml-file.d.ts +2 -2
- package/lib/ml-file/ml-file.js +40 -44
- package/lib/resolve-files.d.ts +2 -2
- package/lib/resolve-files.js +4 -8
- package/lib/resolve-parser.d.ts +1 -1
- package/lib/resolve-parser.js +6 -11
- package/lib/resolve-plugins.js +3 -31
- package/lib/resolve-rules.js +8 -34
- package/lib/resolve-specs.js +5 -10
- package/lib/types.js +1 -2
- package/lib/utils.js +5 -12
- package/package.json +20 -16
package/lib/auto-load-rules.js
CHANGED
|
@@ -1,35 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
-
if (mod && mod.__esModule) return mod;
|
|
20
|
-
var result = {};
|
|
21
|
-
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
-
__setModuleDefault(result, mod);
|
|
23
|
-
return result;
|
|
24
|
-
};
|
|
25
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
-
exports.autoLoadRules = void 0;
|
|
27
|
-
const ml_core_1 = require("@markuplint/ml-core");
|
|
1
|
+
import { MLRule } from '@markuplint/ml-core';
|
|
28
2
|
const cache = new Map();
|
|
29
3
|
/**
|
|
30
4
|
* @deprecated
|
|
31
5
|
*/
|
|
32
|
-
async function autoLoadRules(ruleset) {
|
|
6
|
+
export async function autoLoadRules(ruleset) {
|
|
33
7
|
const rules = [];
|
|
34
8
|
const errors = [];
|
|
35
9
|
for (const ruleName of Object.keys(ruleset.rules)) {
|
|
@@ -40,7 +14,7 @@ async function autoLoadRules(ruleset) {
|
|
|
40
14
|
}
|
|
41
15
|
let seed = null;
|
|
42
16
|
try {
|
|
43
|
-
const _module = await
|
|
17
|
+
const _module = await import(`@markuplint/rule-${ruleName}`);
|
|
44
18
|
seed = _module.default;
|
|
45
19
|
if (!(seed && 'defaultValue' in seed && 'defaultOptions' in seed && 'verify' in seed)) {
|
|
46
20
|
seed = null;
|
|
@@ -50,7 +24,7 @@ async function autoLoadRules(ruleset) {
|
|
|
50
24
|
errors.push(e);
|
|
51
25
|
}
|
|
52
26
|
if (seed) {
|
|
53
|
-
const rule = new
|
|
27
|
+
const rule = new MLRule({
|
|
54
28
|
name: ruleName,
|
|
55
29
|
...seed,
|
|
56
30
|
});
|
|
@@ -59,7 +33,7 @@ async function autoLoadRules(ruleset) {
|
|
|
59
33
|
continue;
|
|
60
34
|
}
|
|
61
35
|
try {
|
|
62
|
-
const _module = await
|
|
36
|
+
const _module = await import(`markuplint-rule-${ruleName}`);
|
|
63
37
|
seed = _module.default;
|
|
64
38
|
if (!(seed && 'defaultValue' in seed && 'defaultOptions' in seed && 'verify' in seed)) {
|
|
65
39
|
seed = null;
|
|
@@ -69,7 +43,7 @@ async function autoLoadRules(ruleset) {
|
|
|
69
43
|
errors.push(e);
|
|
70
44
|
}
|
|
71
45
|
if (seed) {
|
|
72
|
-
const rule = new
|
|
46
|
+
const rule = new MLRule({
|
|
73
47
|
name: ruleName,
|
|
74
48
|
...seed,
|
|
75
49
|
});
|
|
@@ -85,4 +59,3 @@ async function autoLoadRules(ruleset) {
|
|
|
85
59
|
errors: errors.slice(),
|
|
86
60
|
};
|
|
87
61
|
}
|
|
88
|
-
exports.autoLoadRules = autoLoadRules;
|
package/lib/config-provider.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type { MLFile } from './ml-file';
|
|
2
|
-
import type { ConfigSet } from './types';
|
|
1
|
+
import type { MLFile } from './ml-file/index.js';
|
|
2
|
+
import type { ConfigSet } from './types.js';
|
|
3
3
|
import type { Config } from '@markuplint/ml-config';
|
|
4
4
|
import type { Nullable } from '@markuplint/shared';
|
|
5
5
|
export declare class ConfigProvider {
|
package/lib/config-provider.js
CHANGED
|
@@ -1,19 +1,18 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
var _ConfigProvider_cache, _ConfigProvider_held, _ConfigProvider_recursiveLoadKeyAndDepth, _ConfigProvider_store;
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
const
|
|
2
|
+
import { __classPrivateFieldGet } from "tslib";
|
|
3
|
+
import { createRequire } from 'node:module';
|
|
4
|
+
import path from 'node:path';
|
|
5
|
+
import { mergeConfig } from '@markuplint/ml-config';
|
|
6
|
+
import { getPreset } from '@markuplint/ml-core';
|
|
7
|
+
import { ConfigParserError } from '@markuplint/parser-utils';
|
|
8
|
+
import { InvalidSelectorError, createSelector } from '@markuplint/selector';
|
|
9
|
+
import { nonNullableFilter, toNoEmptyStringArrayFromStringOrArray } from '@markuplint/shared';
|
|
10
|
+
import { load as loadConfig, search } from './cosmiconfig.js';
|
|
11
|
+
import { cacheClear, resolvePlugins } from './resolve-plugins.js';
|
|
12
|
+
import { fileExists, uuid } from './utils.js';
|
|
13
|
+
const require = createRequire(import.meta.url);
|
|
15
14
|
const KEY_SEPARATOR = '__ML_CONFIG_MERGE__';
|
|
16
|
-
class ConfigProvider {
|
|
15
|
+
export class ConfigProvider {
|
|
17
16
|
constructor() {
|
|
18
17
|
_ConfigProvider_cache.set(this, new Map());
|
|
19
18
|
_ConfigProvider_held.set(this, new Set());
|
|
@@ -21,36 +20,35 @@ class ConfigProvider {
|
|
|
21
20
|
_ConfigProvider_store.set(this, new Map());
|
|
22
21
|
}
|
|
23
22
|
async resolve(targetFile, names, cache = true) {
|
|
24
|
-
var _a, _b;
|
|
25
23
|
if (!cache) {
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
24
|
+
__classPrivateFieldGet(this, _ConfigProvider_store, "f").clear();
|
|
25
|
+
__classPrivateFieldGet(this, _ConfigProvider_cache, "f").clear();
|
|
26
|
+
cacheClear();
|
|
29
27
|
}
|
|
30
|
-
const keys = names.filter(
|
|
28
|
+
const keys = names.filter(nonNullableFilter);
|
|
31
29
|
const key = keys.join(KEY_SEPARATOR);
|
|
32
|
-
const currentConfig =
|
|
30
|
+
const currentConfig = __classPrivateFieldGet(this, _ConfigProvider_cache, "f").get(key);
|
|
33
31
|
if (currentConfig) {
|
|
34
32
|
return currentConfig;
|
|
35
33
|
}
|
|
36
34
|
let configSet = await this._mergeConfigs(keys, cache);
|
|
37
35
|
const filePath = Array.from(configSet.files).reverse()[0];
|
|
38
36
|
if (!filePath) {
|
|
39
|
-
throw new
|
|
37
|
+
throw new ConfigParserError('Config file not found', {
|
|
40
38
|
filePath: targetFile.path,
|
|
41
39
|
});
|
|
42
40
|
}
|
|
43
41
|
const errors = this._validateConfig(configSet.config, filePath);
|
|
44
42
|
configSet.errs.push(...errors);
|
|
45
|
-
const plugins = await
|
|
46
|
-
if (
|
|
47
|
-
const extendHelds = Array.from(
|
|
43
|
+
const plugins = await resolvePlugins(configSet.config.plugins);
|
|
44
|
+
if (__classPrivateFieldGet(this, _ConfigProvider_held, "f").size > 0) {
|
|
45
|
+
const extendHelds = Array.from(__classPrivateFieldGet(this, _ConfigProvider_held, "f").values());
|
|
48
46
|
for (const held of extendHelds) {
|
|
49
|
-
const [, prefix, namespace, name] =
|
|
47
|
+
const [, prefix, namespace, name] = held.match(/^([a-z]+:)([^/]+)(?:\/(.+))?$/) ?? [];
|
|
50
48
|
switch (prefix) {
|
|
51
49
|
case 'plugin:': {
|
|
52
50
|
const plugin = plugins.find(plugin => plugin.name === namespace);
|
|
53
|
-
const config =
|
|
51
|
+
const config = plugin?.configs?.[name ?? ''];
|
|
54
52
|
if (config) {
|
|
55
53
|
this.set(config, held);
|
|
56
54
|
}
|
|
@@ -59,7 +57,7 @@ class ConfigProvider {
|
|
|
59
57
|
}
|
|
60
58
|
}
|
|
61
59
|
configSet = await this._mergeConfigs([...keys, ...extendHelds], cache);
|
|
62
|
-
|
|
60
|
+
__classPrivateFieldGet(this, _ConfigProvider_held, "f").clear();
|
|
63
61
|
}
|
|
64
62
|
// Resolves `overrides`
|
|
65
63
|
if (configSet.config.overrides) {
|
|
@@ -78,60 +76,59 @@ class ConfigProvider {
|
|
|
78
76
|
...configSet,
|
|
79
77
|
plugins,
|
|
80
78
|
};
|
|
81
|
-
|
|
79
|
+
__classPrivateFieldGet(this, _ConfigProvider_cache, "f").set(key, result);
|
|
82
80
|
return result;
|
|
83
81
|
}
|
|
84
82
|
async search(targetFile) {
|
|
85
83
|
if (!(await targetFile.dirExists())) {
|
|
86
84
|
return null;
|
|
87
85
|
}
|
|
88
|
-
const res = await
|
|
86
|
+
const res = await search(targetFile.path, false);
|
|
89
87
|
if (!res) {
|
|
90
88
|
return null;
|
|
91
89
|
}
|
|
92
90
|
const { filePath, config } = res;
|
|
93
|
-
const pathResolvedConfig = this._pathResolve(config, filePath);
|
|
94
|
-
|
|
91
|
+
const pathResolvedConfig = await this._pathResolve(config, filePath);
|
|
92
|
+
__classPrivateFieldGet(this, _ConfigProvider_store, "f").set(filePath, pathResolvedConfig);
|
|
95
93
|
return filePath;
|
|
96
94
|
}
|
|
97
95
|
set(config, key) {
|
|
98
|
-
key = key
|
|
99
|
-
|
|
96
|
+
key = key ?? uuid();
|
|
97
|
+
__classPrivateFieldGet(this, _ConfigProvider_store, "f").set(key, config);
|
|
100
98
|
return key;
|
|
101
99
|
}
|
|
102
100
|
async _load(filePath, cache) {
|
|
103
|
-
|
|
104
|
-
const entity = tslib_1.__classPrivateFieldGet(this, _ConfigProvider_store, "f").get(filePath);
|
|
101
|
+
const entity = __classPrivateFieldGet(this, _ConfigProvider_store, "f").get(filePath);
|
|
105
102
|
if (entity) {
|
|
106
103
|
return entity;
|
|
107
104
|
}
|
|
108
105
|
if (isPreset(filePath)) {
|
|
109
|
-
const [, name] =
|
|
110
|
-
const config = await
|
|
111
|
-
const pathResolvedConfig = this._pathResolve(config, filePath);
|
|
112
|
-
|
|
106
|
+
const [, name] = filePath.match(/^markuplint:(.+)$/i) ?? [];
|
|
107
|
+
const config = await getPreset(name ?? filePath);
|
|
108
|
+
const pathResolvedConfig = await this._pathResolve(config, filePath);
|
|
109
|
+
__classPrivateFieldGet(this, _ConfigProvider_store, "f").set(filePath, pathResolvedConfig);
|
|
113
110
|
return pathResolvedConfig;
|
|
114
111
|
}
|
|
115
112
|
if (isPlugin(filePath)) {
|
|
116
|
-
|
|
113
|
+
__classPrivateFieldGet(this, _ConfigProvider_held, "f").add(filePath);
|
|
117
114
|
return null;
|
|
118
115
|
}
|
|
119
|
-
if (!moduleExists(filePath) && !
|
|
116
|
+
if (!(await moduleExists(filePath)) && !path.isAbsolute(filePath)) {
|
|
120
117
|
throw new TypeError(`${filePath} is not an absolute path`);
|
|
121
118
|
}
|
|
122
119
|
const config = await load(filePath, cache);
|
|
123
120
|
if (!config) {
|
|
124
121
|
return null;
|
|
125
122
|
}
|
|
126
|
-
const pathResolvedConfig = this._pathResolve(config, filePath);
|
|
127
|
-
|
|
123
|
+
const pathResolvedConfig = await this._pathResolve(config, filePath);
|
|
124
|
+
__classPrivateFieldGet(this, _ConfigProvider_store, "f").set(filePath, pathResolvedConfig);
|
|
128
125
|
return pathResolvedConfig;
|
|
129
126
|
}
|
|
130
127
|
async _mergeConfigs(keys, cache) {
|
|
131
128
|
const resolvedKeys = new Set();
|
|
132
129
|
const errs = [];
|
|
133
130
|
for (const key of keys) {
|
|
134
|
-
|
|
131
|
+
__classPrivateFieldGet(this, _ConfigProvider_recursiveLoadKeyAndDepth, "f").clear();
|
|
135
132
|
const keySet = await this._recursiveLoad(key, cache);
|
|
136
133
|
for (const k of keySet.stack) {
|
|
137
134
|
resolvedKeys.add(k);
|
|
@@ -141,11 +138,11 @@ class ConfigProvider {
|
|
|
141
138
|
}
|
|
142
139
|
}
|
|
143
140
|
const configs = Array.from(resolvedKeys)
|
|
144
|
-
.map(name =>
|
|
145
|
-
.filter(
|
|
141
|
+
.map(name => __classPrivateFieldGet(this, _ConfigProvider_store, "f").get(name))
|
|
142
|
+
.filter(nonNullableFilter);
|
|
146
143
|
let resultConfig = {};
|
|
147
144
|
for (const config of configs) {
|
|
148
|
-
resultConfig =
|
|
145
|
+
resultConfig = mergeConfig(resultConfig, config);
|
|
149
146
|
}
|
|
150
147
|
return {
|
|
151
148
|
config: resultConfig,
|
|
@@ -153,38 +150,37 @@ class ConfigProvider {
|
|
|
153
150
|
errs,
|
|
154
151
|
};
|
|
155
152
|
}
|
|
156
|
-
_pathResolve(config, filePath) {
|
|
157
|
-
const dir =
|
|
153
|
+
async _pathResolve(config, filePath) {
|
|
154
|
+
const dir = path.dirname(filePath);
|
|
158
155
|
return {
|
|
159
156
|
...config,
|
|
160
|
-
extends: pathResolve(dir, config.extends),
|
|
161
|
-
plugins: pathResolve(dir, config.plugins, ['name']),
|
|
162
|
-
parser: pathResolve(dir, config.parser),
|
|
163
|
-
specs: pathResolve(dir, config.specs),
|
|
164
|
-
excludeFiles: pathResolve(dir, config.excludeFiles),
|
|
165
|
-
overrides: pathResolve(dir, config.overrides, undefined, true),
|
|
157
|
+
extends: await pathResolve(dir, config.extends),
|
|
158
|
+
plugins: await pathResolve(dir, config.plugins, ['name']),
|
|
159
|
+
parser: await pathResolve(dir, config.parser),
|
|
160
|
+
specs: await pathResolve(dir, config.specs),
|
|
161
|
+
excludeFiles: await pathResolve(dir, config.excludeFiles),
|
|
162
|
+
overrides: await pathResolve(dir, config.overrides, undefined, true),
|
|
166
163
|
};
|
|
167
164
|
}
|
|
168
165
|
async _recursiveLoad(key, cache, depth = 1) {
|
|
169
|
-
var _a;
|
|
170
166
|
const stack = new Set();
|
|
171
167
|
const errs = [];
|
|
172
|
-
const ancestorDepth =
|
|
168
|
+
const ancestorDepth = __classPrivateFieldGet(this, _ConfigProvider_recursiveLoadKeyAndDepth, "f").get(key);
|
|
173
169
|
if (ancestorDepth != null && ancestorDepth < depth) {
|
|
174
170
|
return {
|
|
175
171
|
stack,
|
|
176
172
|
errs: [new CircularReferenceError(`Circular reference detected: ${key}`)],
|
|
177
173
|
};
|
|
178
174
|
}
|
|
179
|
-
|
|
180
|
-
let config =
|
|
175
|
+
__classPrivateFieldGet(this, _ConfigProvider_recursiveLoadKeyAndDepth, "f").set(key, depth);
|
|
176
|
+
let config = __classPrivateFieldGet(this, _ConfigProvider_store, "f").get(key) ?? null;
|
|
181
177
|
if (!config) {
|
|
182
178
|
config = await this._load(key, cache);
|
|
183
179
|
}
|
|
184
180
|
if (!config) {
|
|
185
181
|
return { stack, errs: null };
|
|
186
182
|
}
|
|
187
|
-
const depKeys = config.extends !== null ?
|
|
183
|
+
const depKeys = config.extends !== null ? toNoEmptyStringArrayFromStringOrArray(config.extends) : null;
|
|
188
184
|
if (depKeys) {
|
|
189
185
|
for (const depKey of depKeys) {
|
|
190
186
|
const keys = await this._recursiveLoad(depKey, cache, depth + 1);
|
|
@@ -200,16 +196,15 @@ class ConfigProvider {
|
|
|
200
196
|
return { stack, errs };
|
|
201
197
|
}
|
|
202
198
|
_validateConfig(config, filePath) {
|
|
203
|
-
var _a;
|
|
204
199
|
const errors = [];
|
|
205
|
-
|
|
200
|
+
config.nodeRules?.forEach(rule => {
|
|
206
201
|
if (rule.selector) {
|
|
207
202
|
try {
|
|
208
|
-
|
|
203
|
+
createSelector(rule.selector);
|
|
209
204
|
}
|
|
210
205
|
catch (error) {
|
|
211
|
-
if (error instanceof
|
|
212
|
-
errors.push(new
|
|
206
|
+
if (error instanceof InvalidSelectorError) {
|
|
207
|
+
errors.push(new ConfigParserError(error.message, {
|
|
213
208
|
filePath,
|
|
214
209
|
raw: rule.selector,
|
|
215
210
|
}));
|
|
@@ -220,22 +215,20 @@ class ConfigProvider {
|
|
|
220
215
|
return errors;
|
|
221
216
|
}
|
|
222
217
|
}
|
|
223
|
-
exports.ConfigProvider = ConfigProvider;
|
|
224
218
|
_ConfigProvider_cache = new WeakMap(), _ConfigProvider_held = new WeakMap(), _ConfigProvider_recursiveLoadKeyAndDepth = new WeakMap(), _ConfigProvider_store = new WeakMap();
|
|
225
219
|
async function load(filePath, cache) {
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
const
|
|
229
|
-
const config = (_a = mod === null || mod === void 0 ? void 0 : mod.default) !== null && _a !== void 0 ? _a : null;
|
|
220
|
+
if (!fileExists(filePath) && (await moduleExists(filePath))) {
|
|
221
|
+
const mod = await import(filePath);
|
|
222
|
+
const config = mod?.default ?? null;
|
|
230
223
|
return config;
|
|
231
224
|
}
|
|
232
|
-
const res = await (
|
|
225
|
+
const res = await loadConfig(filePath, !cache);
|
|
233
226
|
if (!res) {
|
|
234
227
|
return null;
|
|
235
228
|
}
|
|
236
229
|
return res.config;
|
|
237
230
|
}
|
|
238
|
-
function pathResolve(dir, filePath, resolveProps, resolveKey = false) {
|
|
231
|
+
async function pathResolve(dir, filePath, resolveProps, resolveKey = false) {
|
|
239
232
|
if (filePath == null) {
|
|
240
233
|
// @ts-ignore
|
|
241
234
|
return undefined;
|
|
@@ -246,20 +239,20 @@ function pathResolve(dir, filePath, resolveProps, resolveKey = false) {
|
|
|
246
239
|
}
|
|
247
240
|
if (Array.isArray(filePath)) {
|
|
248
241
|
// @ts-ignore
|
|
249
|
-
return filePath.map(fp => pathResolve(dir, fp, resolveProps));
|
|
242
|
+
return Promise.all(filePath.map(fp => pathResolve(dir, fp, resolveProps)));
|
|
250
243
|
}
|
|
251
244
|
const res = {};
|
|
252
245
|
for (const [key, fp] of Object.entries(filePath)) {
|
|
253
246
|
let _key = key;
|
|
254
247
|
if (resolveKey) {
|
|
255
|
-
_key = resolve(dir, key);
|
|
248
|
+
_key = await resolve(dir, key);
|
|
256
249
|
}
|
|
257
250
|
if (typeof fp === 'string') {
|
|
258
251
|
if (!resolveProps) {
|
|
259
|
-
res[_key] = resolve(dir, fp);
|
|
252
|
+
res[_key] = await resolve(dir, fp);
|
|
260
253
|
}
|
|
261
254
|
else if (resolveProps.includes(key)) {
|
|
262
|
-
res[_key] = resolve(dir, fp);
|
|
255
|
+
res[_key] = await resolve(dir, fp);
|
|
263
256
|
}
|
|
264
257
|
else {
|
|
265
258
|
res[_key] = fp;
|
|
@@ -272,30 +265,48 @@ function pathResolve(dir, filePath, resolveProps, resolveKey = false) {
|
|
|
272
265
|
// @ts-ignore
|
|
273
266
|
return res;
|
|
274
267
|
}
|
|
275
|
-
function resolve(dir, pathOrModName) {
|
|
276
|
-
|
|
277
|
-
if (moduleExists(pathOrModName) || isPreset(pathOrModName) || isPlugin(pathOrModName)) {
|
|
268
|
+
async function resolve(dir, pathOrModName) {
|
|
269
|
+
if ((await moduleExists(pathOrModName)) || isPreset(pathOrModName) || isPlugin(pathOrModName)) {
|
|
278
270
|
return pathOrModName;
|
|
279
271
|
}
|
|
280
|
-
const bangAndPath =
|
|
281
|
-
const bang =
|
|
282
|
-
const pathname =
|
|
283
|
-
const absPath =
|
|
272
|
+
const bangAndPath = /^(!)(.*)/.exec(pathOrModName) ?? [];
|
|
273
|
+
const bang = bangAndPath[1] ?? '';
|
|
274
|
+
const pathname = bangAndPath[2] ?? pathOrModName;
|
|
275
|
+
const absPath = path.resolve(dir, pathname);
|
|
284
276
|
return bang + absPath;
|
|
285
277
|
}
|
|
286
|
-
function moduleExists(name) {
|
|
278
|
+
async function moduleExists(name) {
|
|
287
279
|
try {
|
|
288
|
-
|
|
280
|
+
await import(name);
|
|
289
281
|
}
|
|
290
282
|
catch (err) {
|
|
291
|
-
if (
|
|
292
|
-
|
|
293
|
-
|
|
283
|
+
if (err instanceof Error) {
|
|
284
|
+
if (/^Parse failure/i.test(err.message)) {
|
|
285
|
+
return true;
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
try {
|
|
289
|
+
require.resolve(name);
|
|
290
|
+
}
|
|
291
|
+
catch (err) {
|
|
292
|
+
if (
|
|
294
293
|
// @ts-ignore
|
|
295
|
-
|
|
296
|
-
|
|
294
|
+
'code' in err &&
|
|
295
|
+
// @ts-ignore
|
|
296
|
+
err.code === 'ERR_PACKAGE_PATH_NOT_EXPORTED') {
|
|
297
|
+
// Even if there are issues with the fields,
|
|
298
|
+
// assume that the module exists and return true.
|
|
299
|
+
return true;
|
|
300
|
+
}
|
|
301
|
+
if (
|
|
302
|
+
// @ts-ignore
|
|
303
|
+
'code' in err &&
|
|
304
|
+
// @ts-ignore
|
|
305
|
+
err.code === 'MODULE_NOT_FOUND') {
|
|
306
|
+
return false;
|
|
307
|
+
}
|
|
308
|
+
throw err;
|
|
297
309
|
}
|
|
298
|
-
throw err;
|
|
299
310
|
}
|
|
300
311
|
return true;
|
|
301
312
|
}
|
package/lib/cosmiconfig.js
CHANGED
|
@@ -1,33 +1,27 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
const
|
|
6
|
-
const parser_utils_1 = require("@markuplint/parser-utils");
|
|
7
|
-
const cosmiconfig_1 = require("cosmiconfig");
|
|
8
|
-
const cosmiconfig_typescript_loader_1 = require("cosmiconfig-typescript-loader");
|
|
9
|
-
const jsonc_1 = require("jsonc");
|
|
10
|
-
const explorer = (0, cosmiconfig_1.cosmiconfig)('markuplint', {
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
import { ConfigParserError } from '@markuplint/parser-utils';
|
|
3
|
+
import { cosmiconfig, defaultLoaders } from 'cosmiconfig';
|
|
4
|
+
import { jsonc } from 'jsonc';
|
|
5
|
+
const explorer = cosmiconfig('markuplint', {
|
|
11
6
|
loaders: {
|
|
12
|
-
'.ts': (0, cosmiconfig_typescript_loader_1.TypeScriptLoader)(),
|
|
13
7
|
noExt: ((path, content) => {
|
|
14
8
|
try {
|
|
15
|
-
return
|
|
9
|
+
return jsonc.parse(content);
|
|
16
10
|
}
|
|
17
11
|
catch (error) {
|
|
18
12
|
if (error instanceof Error && error.name === 'JSONError') {
|
|
19
|
-
return
|
|
13
|
+
return defaultLoaders['noExt'](path, content);
|
|
20
14
|
}
|
|
21
15
|
throw error;
|
|
22
16
|
}
|
|
23
17
|
}),
|
|
24
18
|
},
|
|
25
19
|
});
|
|
26
|
-
async function search(dir, cacheClear) {
|
|
20
|
+
export async function search(dir, cacheClear) {
|
|
27
21
|
if (cacheClear) {
|
|
28
22
|
explorer.clearCaches();
|
|
29
23
|
}
|
|
30
|
-
dir =
|
|
24
|
+
dir = path.dirname(dir);
|
|
31
25
|
const result = await explorer.search(dir).catch(cacheConfigError(dir));
|
|
32
26
|
if (!result || result.isEmpty) {
|
|
33
27
|
return null;
|
|
@@ -37,8 +31,7 @@ async function search(dir, cacheClear) {
|
|
|
37
31
|
config: result.config,
|
|
38
32
|
};
|
|
39
33
|
}
|
|
40
|
-
|
|
41
|
-
async function load(filePath, cacheClear) {
|
|
34
|
+
export async function load(filePath, cacheClear) {
|
|
42
35
|
if (cacheClear) {
|
|
43
36
|
explorer.clearCaches();
|
|
44
37
|
}
|
|
@@ -51,7 +44,6 @@ async function load(filePath, cacheClear) {
|
|
|
51
44
|
config: result.config,
|
|
52
45
|
};
|
|
53
46
|
}
|
|
54
|
-
exports.load = load;
|
|
55
47
|
class ConfigLoadError extends Error {
|
|
56
48
|
constructor(message, filePath) {
|
|
57
49
|
super(message + ` in ${filePath}`);
|
|
@@ -60,13 +52,12 @@ class ConfigLoadError extends Error {
|
|
|
60
52
|
}
|
|
61
53
|
function cacheConfigError(fileOrDirPath) {
|
|
62
54
|
return (reason) => {
|
|
63
|
-
var _a;
|
|
64
55
|
if (reason instanceof Error) {
|
|
65
56
|
switch (reason.name) {
|
|
66
57
|
case 'YAMLException':
|
|
67
|
-
throw new
|
|
58
|
+
throw new ConfigParserError(reason.message, {
|
|
68
59
|
// @ts-ignore
|
|
69
|
-
filePath:
|
|
60
|
+
filePath: reason.filepath ?? fileOrDirPath,
|
|
70
61
|
});
|
|
71
62
|
}
|
|
72
63
|
throw new ConfigLoadError(reason.message, fileOrDirPath);
|
package/lib/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
export type { MLFile } from './ml-file';
|
|
2
|
-
export * from './config-provider';
|
|
3
|
-
export * from './resolve-files';
|
|
4
|
-
export * from './resolve-parser';
|
|
5
|
-
export * from './resolve-rules';
|
|
6
|
-
export * from './resolve-specs';
|
|
7
|
-
export * from './types';
|
|
1
|
+
export type { MLFile } from './ml-file/index.js';
|
|
2
|
+
export * from './config-provider.js';
|
|
3
|
+
export * from './resolve-files.js';
|
|
4
|
+
export * from './resolve-parser.js';
|
|
5
|
+
export * from './resolve-rules.js';
|
|
6
|
+
export * from './resolve-specs.js';
|
|
7
|
+
export * from './types.js';
|
package/lib/index.js
CHANGED
|
@@ -1,9 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
tslib_1.__exportStar(require("./resolve-rules"), exports);
|
|
8
|
-
tslib_1.__exportStar(require("./resolve-specs"), exports);
|
|
9
|
-
tslib_1.__exportStar(require("./types"), exports);
|
|
1
|
+
export * from './config-provider.js';
|
|
2
|
+
export * from './resolve-files.js';
|
|
3
|
+
export * from './resolve-parser.js';
|
|
4
|
+
export * from './resolve-rules.js';
|
|
5
|
+
export * from './resolve-specs.js';
|
|
6
|
+
export * from './types.js';
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { MLFile } from './ml-file';
|
|
1
|
+
import { MLFile } from './ml-file.js';
|
|
2
2
|
export declare function getAnonymousFile(context: string, workspace?: string, name?: string): MLFile;
|
|
@@ -1,13 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const ml_file_1 = require("./ml-file");
|
|
5
|
-
function getAnonymousFile(context, workspace, name) {
|
|
6
|
-
const file = new ml_file_1.MLFile({
|
|
1
|
+
import { MLFile } from './ml-file.js';
|
|
2
|
+
export function getAnonymousFile(context, workspace, name) {
|
|
3
|
+
const file = new MLFile({
|
|
7
4
|
sourceCode: context,
|
|
8
5
|
workspace,
|
|
9
6
|
name,
|
|
10
7
|
});
|
|
11
8
|
return file;
|
|
12
9
|
}
|
|
13
|
-
exports.getAnonymousFile = getAnonymousFile;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { MLFile } from './ml-file';
|
|
1
|
+
import { MLFile } from './ml-file.js';
|
|
2
2
|
export declare function getFile(filePath: string): MLFile;
|
package/lib/ml-file/get-file.js
CHANGED
|
@@ -1,9 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const ml_file_1 = require("./ml-file");
|
|
5
|
-
function getFile(filePath) {
|
|
6
|
-
const file = new ml_file_1.MLFile(filePath);
|
|
1
|
+
import { MLFile } from './ml-file.js';
|
|
2
|
+
export function getFile(filePath) {
|
|
3
|
+
const file = new MLFile(filePath);
|
|
7
4
|
return file;
|
|
8
5
|
}
|
|
9
|
-
exports.getFile = getFile;
|
package/lib/ml-file/get-files.js
CHANGED
|
@@ -1,8 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
exports.getFiles = void 0;
|
|
4
|
-
const glob_1 = require("glob");
|
|
5
|
-
const get_file_1 = require("./get-file");
|
|
1
|
+
import { glob } from 'glob';
|
|
2
|
+
import { getFile } from './get-file.js';
|
|
6
3
|
/**
|
|
7
4
|
* Get files
|
|
8
5
|
*
|
|
@@ -10,8 +7,7 @@ const get_file_1 = require("./get-file");
|
|
|
10
7
|
*
|
|
11
8
|
* @param filePathOrGlob
|
|
12
9
|
*/
|
|
13
|
-
async function getFiles(filePathOrGlob) {
|
|
14
|
-
const fileList = await
|
|
15
|
-
return fileList.map(fileName =>
|
|
10
|
+
export async function getFiles(filePathOrGlob) {
|
|
11
|
+
const fileList = await glob(filePathOrGlob, {}).catch(() => []);
|
|
12
|
+
return fileList.map(fileName => getFile(fileName));
|
|
16
13
|
}
|
|
17
|
-
exports.getFiles = getFiles;
|
package/lib/ml-file/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export type { MLFile } from './ml-file';
|
|
2
|
-
export * from './get-anonymous-file';
|
|
3
|
-
export * from './get-file';
|
|
4
|
-
export * from './get-files';
|
|
1
|
+
export type { MLFile } from './ml-file.js';
|
|
2
|
+
export * from './get-anonymous-file.js';
|
|
3
|
+
export * from './get-file.js';
|
|
4
|
+
export * from './get-files.js';
|
package/lib/ml-file/index.js
CHANGED
|
@@ -1,6 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
tslib_1.__exportStar(require("./get-anonymous-file"), exports);
|
|
5
|
-
tslib_1.__exportStar(require("./get-file"), exports);
|
|
6
|
-
tslib_1.__exportStar(require("./get-files"), exports);
|
|
1
|
+
export * from './get-anonymous-file.js';
|
|
2
|
+
export * from './get-file.js';
|
|
3
|
+
export * from './get-files.js';
|
package/lib/ml-file/ml-file.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { Target } from '../types';
|
|
1
|
+
import type { Target } from '../types.js';
|
|
2
2
|
export declare class MLFile {
|
|
3
3
|
#private;
|
|
4
4
|
constructor(target: Target);
|
|
@@ -14,7 +14,7 @@ export declare class MLFile {
|
|
|
14
14
|
get path(): string;
|
|
15
15
|
dirExists(): Promise<boolean>;
|
|
16
16
|
getCode(): Promise<string>;
|
|
17
|
-
ignored(globPath: string | readonly string[]):
|
|
17
|
+
ignored(globPath: string | readonly string[]): any;
|
|
18
18
|
isExist(): Promise<boolean>;
|
|
19
19
|
isFile(): Promise<boolean>;
|
|
20
20
|
matches(globPath: string): boolean;
|
package/lib/ml-file/ml-file.js
CHANGED
|
@@ -1,15 +1,11 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
var _MLFile_basename, _MLFile_code, _MLFile_dirname, _MLFile_stat, _MLFile_type;
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
const minimatch_1 = require("minimatch");
|
|
10
|
-
class MLFile {
|
|
2
|
+
import { __classPrivateFieldGet, __classPrivateFieldSet } from "tslib";
|
|
3
|
+
import { promises as fs } from 'node:fs';
|
|
4
|
+
import path from 'node:path';
|
|
5
|
+
import ignore from 'ignore';
|
|
6
|
+
import { minimatch } from 'minimatch';
|
|
7
|
+
export class MLFile {
|
|
11
8
|
constructor(target) {
|
|
12
|
-
var _a, _b;
|
|
13
9
|
_MLFile_basename.set(this, void 0);
|
|
14
10
|
_MLFile_code.set(this, void 0);
|
|
15
11
|
_MLFile_dirname.set(this, void 0);
|
|
@@ -21,25 +17,25 @@ class MLFile {
|
|
|
21
17
|
_MLFile_stat.set(this, undefined);
|
|
22
18
|
_MLFile_type.set(this, void 0);
|
|
23
19
|
if (typeof target === 'string') {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
20
|
+
__classPrivateFieldSet(this, _MLFile_basename, path.basename(target), "f");
|
|
21
|
+
__classPrivateFieldSet(this, _MLFile_dirname, path.dirname(target), "f");
|
|
22
|
+
__classPrivateFieldSet(this, _MLFile_code, null, "f");
|
|
23
|
+
__classPrivateFieldSet(this, _MLFile_type, 'file-base', "f");
|
|
28
24
|
return;
|
|
29
25
|
}
|
|
30
|
-
if (!target.workspace && target.name &&
|
|
31
|
-
|
|
32
|
-
|
|
26
|
+
if (!target.workspace && target.name && path.isAbsolute(target.name)) {
|
|
27
|
+
__classPrivateFieldSet(this, _MLFile_basename, path.basename(target.name), "f");
|
|
28
|
+
__classPrivateFieldSet(this, _MLFile_dirname, path.dirname(target.name), "f");
|
|
33
29
|
}
|
|
34
30
|
else {
|
|
35
|
-
|
|
36
|
-
|
|
31
|
+
__classPrivateFieldSet(this, _MLFile_basename, target.name ?? '<AnonymousFile>', "f");
|
|
32
|
+
__classPrivateFieldSet(this, _MLFile_dirname, target.workspace ?? process.cwd(), "f");
|
|
37
33
|
}
|
|
38
|
-
|
|
39
|
-
|
|
34
|
+
__classPrivateFieldSet(this, _MLFile_code, target.sourceCode, "f");
|
|
35
|
+
__classPrivateFieldSet(this, _MLFile_type, 'code-base', "f");
|
|
40
36
|
}
|
|
41
37
|
get dirname() {
|
|
42
|
-
return
|
|
38
|
+
return __classPrivateFieldGet(this, _MLFile_dirname, "f");
|
|
43
39
|
}
|
|
44
40
|
/**
|
|
45
41
|
* Normalized `MLFile.dirname`
|
|
@@ -54,16 +50,16 @@ class MLFile {
|
|
|
54
50
|
return pathNormalize(this.path);
|
|
55
51
|
}
|
|
56
52
|
get path() {
|
|
57
|
-
return
|
|
53
|
+
return path.resolve(__classPrivateFieldGet(this, _MLFile_dirname, "f"), __classPrivateFieldGet(this, _MLFile_basename, "f"));
|
|
58
54
|
}
|
|
59
55
|
async dirExists() {
|
|
60
|
-
return !!(await stat(
|
|
56
|
+
return !!(await stat(__classPrivateFieldGet(this, _MLFile_dirname, "f")));
|
|
61
57
|
}
|
|
62
58
|
async getCode() {
|
|
63
|
-
if (
|
|
64
|
-
return
|
|
59
|
+
if (__classPrivateFieldGet(this, _MLFile_code, "f") != null) {
|
|
60
|
+
return __classPrivateFieldGet(this, _MLFile_code, "f");
|
|
65
61
|
}
|
|
66
|
-
if (
|
|
62
|
+
if (__classPrivateFieldGet(this, _MLFile_type, "f") === 'file-base' && (await this.isExist())) {
|
|
67
63
|
return await this._fetch();
|
|
68
64
|
}
|
|
69
65
|
return '';
|
|
@@ -71,51 +67,51 @@ class MLFile {
|
|
|
71
67
|
ignored(globPath) {
|
|
72
68
|
globPath = typeof globPath === 'string' ? [globPath] : globPath;
|
|
73
69
|
const normalizedPaths = globPath.map(p => pathNormalize(p, true));
|
|
74
|
-
|
|
70
|
+
// @ts-ignore
|
|
71
|
+
const ig = ignore().add(normalizedPaths);
|
|
75
72
|
const ignored = ig.ignores(pathNormalize(this.nPath, true));
|
|
76
73
|
return ignored;
|
|
77
74
|
}
|
|
78
75
|
async isExist() {
|
|
79
|
-
if (
|
|
76
|
+
if (__classPrivateFieldGet(this, _MLFile_type, "f") === 'code-base') {
|
|
80
77
|
return true;
|
|
81
78
|
}
|
|
82
79
|
const stat = await this._stat();
|
|
83
80
|
return !!stat;
|
|
84
81
|
}
|
|
85
82
|
async isFile() {
|
|
86
|
-
if (
|
|
83
|
+
if (__classPrivateFieldGet(this, _MLFile_type, "f") === 'code-base') {
|
|
87
84
|
return true;
|
|
88
85
|
}
|
|
89
86
|
const stat = await this._stat();
|
|
90
87
|
return !!stat && stat.isFile();
|
|
91
88
|
}
|
|
92
89
|
matches(globPath) {
|
|
93
|
-
return
|
|
90
|
+
return minimatch(this.nPath, pathNormalize(globPath));
|
|
94
91
|
}
|
|
95
92
|
setCode(code) {
|
|
96
|
-
if (
|
|
93
|
+
if (__classPrivateFieldGet(this, _MLFile_type, "f") === 'file-base') {
|
|
97
94
|
throw new Error(`This file object is readonly (File-base: ${this.path})`);
|
|
98
95
|
}
|
|
99
|
-
|
|
96
|
+
__classPrivateFieldSet(this, _MLFile_code, code, "f");
|
|
100
97
|
}
|
|
101
98
|
async _fetch() {
|
|
102
|
-
const code = await
|
|
103
|
-
|
|
99
|
+
const code = await fs.readFile(this.path, { encoding: 'utf-8' });
|
|
100
|
+
__classPrivateFieldSet(this, _MLFile_code, code, "f");
|
|
104
101
|
return code;
|
|
105
102
|
}
|
|
106
103
|
async _stat() {
|
|
107
|
-
if (
|
|
108
|
-
return
|
|
104
|
+
if (__classPrivateFieldGet(this, _MLFile_stat, "f")) {
|
|
105
|
+
return __classPrivateFieldGet(this, _MLFile_stat, "f");
|
|
109
106
|
}
|
|
110
|
-
|
|
111
|
-
return
|
|
107
|
+
__classPrivateFieldSet(this, _MLFile_stat, await stat(this.path), "f");
|
|
108
|
+
return __classPrivateFieldGet(this, _MLFile_stat, "f");
|
|
112
109
|
}
|
|
113
110
|
}
|
|
114
|
-
exports.MLFile = MLFile;
|
|
115
111
|
_MLFile_basename = new WeakMap(), _MLFile_code = new WeakMap(), _MLFile_dirname = new WeakMap(), _MLFile_stat = new WeakMap(), _MLFile_type = new WeakMap();
|
|
116
112
|
async function stat(filePath) {
|
|
117
113
|
try {
|
|
118
|
-
return await
|
|
114
|
+
return await fs.stat(filePath);
|
|
119
115
|
}
|
|
120
116
|
catch (err) {
|
|
121
117
|
if (
|
|
@@ -134,14 +130,14 @@ function pathNormalize(filePath, relative = false) {
|
|
|
134
130
|
filePath = filePath.slice(1);
|
|
135
131
|
}
|
|
136
132
|
// Remove the local disk scheme of Windows OS
|
|
137
|
-
if (
|
|
133
|
+
if (path.isAbsolute(filePath)) {
|
|
138
134
|
filePath = filePath.replace(/^[a-z]+:/i, '');
|
|
139
135
|
if (relative) {
|
|
140
|
-
filePath =
|
|
136
|
+
filePath = path.relative(path.sep, filePath);
|
|
141
137
|
}
|
|
142
138
|
}
|
|
143
139
|
// Replace the separator of Windows OS
|
|
144
|
-
filePath = filePath.split(
|
|
140
|
+
filePath = filePath.split(path.sep).join('/');
|
|
145
141
|
if (hasBang) {
|
|
146
142
|
filePath = `!${filePath}`;
|
|
147
143
|
}
|
package/lib/resolve-files.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import type { MLFile } from './ml-file';
|
|
2
|
-
import type { Target } from './types';
|
|
1
|
+
import type { MLFile } from './ml-file/index.js';
|
|
2
|
+
import type { Target } from './types.js';
|
|
3
3
|
export declare function resolveFiles(targetList: readonly Readonly<Target>[]): Promise<MLFile[]>;
|
package/lib/resolve-files.js
CHANGED
|
@@ -1,17 +1,13 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
exports.resolveFiles = void 0;
|
|
4
|
-
const ml_file_1 = require("./ml-file");
|
|
5
|
-
async function resolveFiles(targetList) {
|
|
1
|
+
import { getAnonymousFile, getFiles } from './ml-file/index.js';
|
|
2
|
+
export async function resolveFiles(targetList) {
|
|
6
3
|
const res = [];
|
|
7
4
|
for (const target of targetList) {
|
|
8
5
|
if (typeof target === 'string') {
|
|
9
|
-
const file = await
|
|
6
|
+
const file = await getFiles(target);
|
|
10
7
|
res.push(...file);
|
|
11
8
|
continue;
|
|
12
9
|
}
|
|
13
|
-
res.push(
|
|
10
|
+
res.push(getAnonymousFile(target.sourceCode, target.workspace, target.name));
|
|
14
11
|
}
|
|
15
12
|
return res;
|
|
16
13
|
}
|
|
17
|
-
exports.resolveFiles = resolveFiles;
|
package/lib/resolve-parser.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { MLFile } from './ml-file';
|
|
1
|
+
import type { MLFile } from './ml-file/index.js';
|
|
2
2
|
import type { MLMarkupLanguageParser, ParserOptions } from '@markuplint/ml-ast';
|
|
3
3
|
import type { ParserConfig } from '@markuplint/ml-config';
|
|
4
4
|
export declare function resolveParser(file: Readonly<MLFile>, parserConfig?: ParserConfig, parserOptions?: ParserOptions): Promise<{
|
package/lib/resolve-parser.js
CHANGED
|
@@ -1,20 +1,16 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
exports.resolveParser = void 0;
|
|
4
|
-
const tslib_1 = require("tslib");
|
|
5
|
-
const path_1 = tslib_1.__importDefault(require("path"));
|
|
6
|
-
const utils_1 = require("./utils");
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
import { toRegexp } from './utils.js';
|
|
7
3
|
const parsers = new Map();
|
|
8
|
-
async function resolveParser(file, parserConfig, parserOptions) {
|
|
4
|
+
export async function resolveParser(file, parserConfig, parserOptions) {
|
|
9
5
|
parserConfig = {
|
|
10
6
|
...parserConfig,
|
|
11
7
|
'/\\.html?$/i': '@markuplint/html-parser',
|
|
12
8
|
};
|
|
13
|
-
parserOptions = parserOptions
|
|
9
|
+
parserOptions = parserOptions ?? {};
|
|
14
10
|
let parserModName = '@markuplint/html-parser';
|
|
15
11
|
let matched = false;
|
|
16
12
|
for (const pattern of Object.keys(parserConfig)) {
|
|
17
|
-
if (
|
|
13
|
+
if (path.basename(file.path).match(toRegexp(pattern))) {
|
|
18
14
|
const modName = parserConfig[pattern];
|
|
19
15
|
if (!modName) {
|
|
20
16
|
continue;
|
|
@@ -32,12 +28,11 @@ async function resolveParser(file, parserConfig, parserOptions) {
|
|
|
32
28
|
matched,
|
|
33
29
|
};
|
|
34
30
|
}
|
|
35
|
-
exports.resolveParser = resolveParser;
|
|
36
31
|
async function importParser(parserModName) {
|
|
37
32
|
const entity = parsers.get(parserModName);
|
|
38
33
|
if (entity) {
|
|
39
34
|
return entity;
|
|
40
35
|
}
|
|
41
|
-
const parser = await
|
|
36
|
+
const parser = await import(parserModName);
|
|
42
37
|
return parser;
|
|
43
38
|
}
|
package/lib/resolve-plugins.js
CHANGED
|
@@ -1,31 +1,5 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
-
if (mod && mod.__esModule) return mod;
|
|
20
|
-
var result = {};
|
|
21
|
-
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
-
__setModuleDefault(result, mod);
|
|
23
|
-
return result;
|
|
24
|
-
};
|
|
25
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
-
exports.cacheClear = exports.resolvePlugins = void 0;
|
|
27
1
|
const cache = new Map();
|
|
28
|
-
async function resolvePlugins(pluginPaths) {
|
|
2
|
+
export async function resolvePlugins(pluginPaths) {
|
|
29
3
|
if (!pluginPaths) {
|
|
30
4
|
return [];
|
|
31
5
|
}
|
|
@@ -33,11 +7,9 @@ async function resolvePlugins(pluginPaths) {
|
|
|
33
7
|
// Clone
|
|
34
8
|
return plugins.slice();
|
|
35
9
|
}
|
|
36
|
-
|
|
37
|
-
function cacheClear() {
|
|
10
|
+
export function cacheClear() {
|
|
38
11
|
cache.clear();
|
|
39
12
|
}
|
|
40
|
-
exports.cacheClear = cacheClear;
|
|
41
13
|
async function importPlugin(pluginPath) {
|
|
42
14
|
const config = getPluginConfig(pluginPath);
|
|
43
15
|
const cached = cache.get(config.name);
|
|
@@ -76,7 +48,7 @@ function getPluginConfig(pluginPath) {
|
|
|
76
48
|
return pluginPath;
|
|
77
49
|
}
|
|
78
50
|
async function failSafeImport(name) {
|
|
79
|
-
const res = await
|
|
51
|
+
const res = await import(name).catch(e => e);
|
|
80
52
|
if ('code' in res && res === 'MODULE_NOT_FOUND') {
|
|
81
53
|
return null;
|
|
82
54
|
}
|
package/lib/resolve-rules.js
CHANGED
|
@@ -1,33 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
-
if (mod && mod.__esModule) return mod;
|
|
20
|
-
var result = {};
|
|
21
|
-
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
-
__setModuleDefault(result, mod);
|
|
23
|
-
return result;
|
|
24
|
-
};
|
|
25
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
-
exports.resolveRules = void 0;
|
|
27
|
-
const ml_core_1 = require("@markuplint/ml-core");
|
|
28
|
-
const auto_load_rules_1 = require("./auto-load-rules");
|
|
1
|
+
import { MLRule } from '@markuplint/ml-core';
|
|
2
|
+
import { autoLoadRules } from './auto-load-rules.js';
|
|
29
3
|
let cachedPresetRules = null;
|
|
30
|
-
async function resolveRules(plugins, ruleset, importPreset,
|
|
4
|
+
export async function resolveRules(plugins, ruleset, importPreset,
|
|
31
5
|
/**
|
|
32
6
|
* @deprecated
|
|
33
7
|
*/
|
|
@@ -38,7 +12,7 @@ autoLoad) {
|
|
|
38
12
|
return;
|
|
39
13
|
}
|
|
40
14
|
Object.entries(plugin.rules).forEach(([name, seed]) => {
|
|
41
|
-
const rule = new
|
|
15
|
+
const rule = new MLRule({
|
|
42
16
|
name: `${plugin.name}/${name}`,
|
|
43
17
|
...seed,
|
|
44
18
|
});
|
|
@@ -46,7 +20,7 @@ autoLoad) {
|
|
|
46
20
|
});
|
|
47
21
|
});
|
|
48
22
|
if (autoLoad) {
|
|
49
|
-
const { rules: additionalRules } = await
|
|
23
|
+
const { rules: additionalRules } = await autoLoadRules(ruleset);
|
|
50
24
|
additionalRules.forEach(rule => {
|
|
51
25
|
rules.push(rule);
|
|
52
26
|
});
|
|
@@ -54,15 +28,15 @@ autoLoad) {
|
|
|
54
28
|
// Clone
|
|
55
29
|
return rules.slice();
|
|
56
30
|
}
|
|
57
|
-
exports.resolveRules = resolveRules;
|
|
58
31
|
async function importPresetRules() {
|
|
59
32
|
if (cachedPresetRules) {
|
|
60
33
|
return cachedPresetRules.slice();
|
|
61
34
|
}
|
|
62
35
|
const modName = '@markuplint/rules';
|
|
63
|
-
const
|
|
36
|
+
const mod = await import(modName);
|
|
37
|
+
const presetRules = mod.default;
|
|
64
38
|
const ruleList = Object.entries(presetRules).map(([name, seed]) => {
|
|
65
|
-
const rule = new
|
|
39
|
+
const rule = new MLRule({
|
|
66
40
|
name,
|
|
67
41
|
...seed,
|
|
68
42
|
});
|
package/lib/resolve-specs.js
CHANGED
|
@@ -1,9 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
exports.resolveSpecs = void 0;
|
|
4
|
-
const tslib_1 = require("tslib");
|
|
5
|
-
const path_1 = tslib_1.__importDefault(require("path"));
|
|
6
|
-
const utils_1 = require("./utils");
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
import { toRegexp } from './utils.js';
|
|
7
3
|
const caches = new Map();
|
|
8
4
|
/**
|
|
9
5
|
* Loading and importing form specs.
|
|
@@ -35,7 +31,7 @@ const caches = new Map();
|
|
|
35
31
|
* @param specConfig The `spec` property part of the config
|
|
36
32
|
* @returns
|
|
37
33
|
*/
|
|
38
|
-
async function resolveSpecs(filePath, specConfig) {
|
|
34
|
+
export async function resolveSpecs(filePath, specConfig) {
|
|
39
35
|
const htmlSpec = await importSpecs('@markuplint/html-spec');
|
|
40
36
|
const extendedSpecs = [];
|
|
41
37
|
if (specConfig) {
|
|
@@ -45,7 +41,7 @@ async function resolveSpecs(filePath, specConfig) {
|
|
|
45
41
|
}
|
|
46
42
|
else {
|
|
47
43
|
for (const pattern of Object.keys(specConfig)) {
|
|
48
|
-
if (
|
|
44
|
+
if (path.basename(filePath).match(toRegexp(pattern))) {
|
|
49
45
|
const specModName = specConfig[pattern];
|
|
50
46
|
if (!specModName) {
|
|
51
47
|
continue;
|
|
@@ -61,7 +57,6 @@ async function resolveSpecs(filePath, specConfig) {
|
|
|
61
57
|
schemas,
|
|
62
58
|
};
|
|
63
59
|
}
|
|
64
|
-
exports.resolveSpecs = resolveSpecs;
|
|
65
60
|
async function importSpecs(specModName) {
|
|
66
61
|
{
|
|
67
62
|
// @ts-ignore
|
|
@@ -70,7 +65,7 @@ async function importSpecs(specModName) {
|
|
|
70
65
|
return spec;
|
|
71
66
|
}
|
|
72
67
|
}
|
|
73
|
-
const spec = (await
|
|
68
|
+
const spec = (await import(specModName)).default;
|
|
74
69
|
// @ts-ignore
|
|
75
70
|
caches.set(specModName, spec);
|
|
76
71
|
return spec;
|
package/lib/types.js
CHANGED
|
@@ -1,2 +1 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
1
|
+
export {};
|
package/lib/utils.js
CHANGED
|
@@ -1,24 +1,17 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.toRegexp = exports.fileExists = exports.uuid = void 0;
|
|
4
|
-
const tslib_1 = require("tslib");
|
|
5
|
-
const fs_1 = tslib_1.__importDefault(require("fs"));
|
|
1
|
+
import fs from 'node:fs';
|
|
6
2
|
let uuidNum = 0;
|
|
7
|
-
function uuid() {
|
|
3
|
+
export function uuid() {
|
|
8
4
|
const out = `${uuidNum}`;
|
|
9
5
|
uuidNum++;
|
|
10
6
|
return out;
|
|
11
7
|
}
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
return fs_1.default.existsSync(filePath);
|
|
8
|
+
export function fileExists(filePath) {
|
|
9
|
+
return fs.existsSync(filePath);
|
|
15
10
|
}
|
|
16
|
-
|
|
17
|
-
function toRegexp(pattern) {
|
|
11
|
+
export function toRegexp(pattern) {
|
|
18
12
|
const matched = pattern.match(/^\/(.+)\/([ig]*)$/i);
|
|
19
13
|
if (matched && matched[1]) {
|
|
20
14
|
return new RegExp(matched[1], matched[2]);
|
|
21
15
|
}
|
|
22
16
|
return pattern;
|
|
23
17
|
}
|
|
24
|
-
exports.toRegexp = toRegexp;
|
package/package.json
CHANGED
|
@@ -1,13 +1,18 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@markuplint/file-resolver",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "4.0.0-alpha.2",
|
|
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>",
|
|
7
7
|
"license": "MIT",
|
|
8
8
|
"private": false,
|
|
9
|
-
"
|
|
10
|
-
"
|
|
9
|
+
"type": "module",
|
|
10
|
+
"exports": {
|
|
11
|
+
".": {
|
|
12
|
+
"import": "./lib/index.js"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"types": "./lib/index.d.ts",
|
|
11
16
|
"publishConfig": {
|
|
12
17
|
"access": "public"
|
|
13
18
|
},
|
|
@@ -19,24 +24,23 @@
|
|
|
19
24
|
"clean": "tsc --build --clean"
|
|
20
25
|
},
|
|
21
26
|
"devDependencies": {
|
|
22
|
-
"@types/node": "20.
|
|
27
|
+
"@types/node": "20.8.3"
|
|
23
28
|
},
|
|
24
29
|
"dependencies": {
|
|
25
|
-
"@markuplint/html-parser": "
|
|
26
|
-
"@markuplint/ml-ast": "
|
|
27
|
-
"@markuplint/ml-config": "
|
|
28
|
-
"@markuplint/ml-core": "
|
|
29
|
-
"@markuplint/ml-spec": "
|
|
30
|
-
"@markuplint/parser-utils": "
|
|
31
|
-
"@markuplint/selector": "
|
|
32
|
-
"@markuplint/shared": "
|
|
33
|
-
"cosmiconfig": "^8.3.
|
|
34
|
-
"
|
|
35
|
-
"glob": "^10.3.4",
|
|
30
|
+
"@markuplint/html-parser": "4.0.0-alpha.2",
|
|
31
|
+
"@markuplint/ml-ast": "4.0.0-alpha.2",
|
|
32
|
+
"@markuplint/ml-config": "4.0.0-alpha.2",
|
|
33
|
+
"@markuplint/ml-core": "4.0.0-alpha.2",
|
|
34
|
+
"@markuplint/ml-spec": "4.0.0-alpha.2",
|
|
35
|
+
"@markuplint/parser-utils": "4.0.0-alpha.2",
|
|
36
|
+
"@markuplint/selector": "4.0.0-alpha.2",
|
|
37
|
+
"@markuplint/shared": "4.0.0-alpha.2",
|
|
38
|
+
"cosmiconfig": "^8.3.6",
|
|
39
|
+
"glob": "^10.3.6",
|
|
36
40
|
"ignore": "^5.2.4",
|
|
37
41
|
"jsonc": "^2.0.0",
|
|
38
42
|
"minimatch": "^9.0.3",
|
|
39
43
|
"tslib": "^2.6.2"
|
|
40
44
|
},
|
|
41
|
-
"gitHead": "
|
|
45
|
+
"gitHead": "51ad52c760435641f9bff8685707d8178b0787eb"
|
|
42
46
|
}
|