@markuplint/file-resolver 3.15.0 → 4.0.0-alpha.10

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 (52) hide show
  1. package/LICENSE +1 -1
  2. package/lib/auto-load-rules.d.ts +2 -2
  3. package/lib/auto-load-rules.js +12 -39
  4. package/lib/config-load-error.d.ts +6 -0
  5. package/lib/config-load-error.js +8 -0
  6. package/lib/config-provider.d.ts +14 -11
  7. package/lib/config-provider.js +146 -198
  8. package/lib/cosmiconfig.d.ts +8 -13
  9. package/lib/cosmiconfig.js +25 -33
  10. package/lib/debug.d.ts +3 -0
  11. package/lib/debug.js +2 -0
  12. package/lib/force-import-json-in-module.d.ts +1 -0
  13. package/lib/force-import-json-in-module.js +31 -0
  14. package/lib/general-import.d.ts +1 -0
  15. package/lib/general-import.js +40 -0
  16. package/lib/get-preset.d.ts +2 -0
  17. package/lib/get-preset.js +13 -0
  18. package/lib/index.d.ts +7 -7
  19. package/lib/index.js +6 -9
  20. package/lib/is-plugin-module-name.d.ts +1 -0
  21. package/lib/is-plugin-module-name.js +3 -0
  22. package/lib/is-preset-module-name.d.ts +1 -0
  23. package/lib/is-preset-module-name.js +3 -0
  24. package/lib/ml-file/get-anonymous-file.d.ts +1 -1
  25. package/lib/ml-file/get-anonymous-file.js +3 -7
  26. package/lib/ml-file/get-file.d.ts +1 -1
  27. package/lib/ml-file/get-file.js +3 -7
  28. package/lib/ml-file/get-files.d.ts +2 -2
  29. package/lib/ml-file/get-files.js +7 -9
  30. package/lib/ml-file/index.d.ts +4 -4
  31. package/lib/ml-file/index.js +3 -6
  32. package/lib/ml-file/ml-file.d.ts +22 -22
  33. package/lib/ml-file/ml-file.js +54 -48
  34. package/lib/module-exists.d.ts +1 -0
  35. package/lib/module-exists.js +49 -0
  36. package/lib/path-to-abs-or-name.d.ts +1 -0
  37. package/lib/path-to-abs-or-name.js +38 -0
  38. package/lib/resolve-files.d.ts +3 -3
  39. package/lib/resolve-files.js +4 -8
  40. package/lib/resolve-name-or-abs-path.d.ts +1 -0
  41. package/lib/resolve-name-or-abs-path.js +14 -0
  42. package/lib/resolve-parser.d.ts +7 -11
  43. package/lib/resolve-parser.js +16 -12
  44. package/lib/resolve-plugins.js +24 -56
  45. package/lib/resolve-rules.d.ts +5 -9
  46. package/lib/resolve-rules.js +18 -44
  47. package/lib/resolve-specs.d.ts +2 -5
  48. package/lib/resolve-specs.js +10 -11
  49. package/lib/types.d.ts +18 -20
  50. package/lib/types.js +1 -2
  51. package/lib/utils.js +6 -13
  52. package/package.json +22 -18
package/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2017-2019 Yusuke Hirao
3
+ Copyright (c) 2017-2024 Yusuke Hirao
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
@@ -3,6 +3,6 @@ import type { Ruleset, AnyMLRule } from '@markuplint/ml-core';
3
3
  * @deprecated
4
4
  */
5
5
  export declare function autoLoadRules(ruleset: Ruleset): Promise<{
6
- rules: AnyMLRule[];
7
- errors: unknown[];
6
+ rules: AnyMLRule[];
7
+ errors: unknown[];
8
8
  }>;
@@ -1,35 +1,9 @@
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.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,17 +14,17 @@ async function autoLoadRules(ruleset) {
40
14
  }
41
15
  let seed = null;
42
16
  try {
43
- const _module = await Promise.resolve(`${`@markuplint/rule-${ruleName}`}`).then(s => __importStar(require(s)));
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;
47
21
  }
48
22
  }
49
- catch (e) {
50
- errors.push(e);
23
+ catch (error) {
24
+ errors.push(error);
51
25
  }
52
26
  if (seed) {
53
- const rule = new ml_core_1.MLRule({
27
+ const rule = new MLRule({
54
28
  name: ruleName,
55
29
  ...seed,
56
30
  });
@@ -59,17 +33,17 @@ async function autoLoadRules(ruleset) {
59
33
  continue;
60
34
  }
61
35
  try {
62
- const _module = await Promise.resolve(`${`markuplint-rule-${ruleName}`}`).then(s => __importStar(require(s)));
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;
66
40
  }
67
41
  }
68
- catch (e) {
69
- errors.push(e);
42
+ catch (error) {
43
+ errors.push(error);
70
44
  }
71
45
  if (seed) {
72
- const rule = new ml_core_1.MLRule({
46
+ const rule = new MLRule({
73
47
  name: ruleName,
74
48
  ...seed,
75
49
  });
@@ -80,9 +54,8 @@ async function autoLoadRules(ruleset) {
80
54
  }
81
55
  return {
82
56
  // Clone
83
- rules: rules.slice(),
57
+ rules: [...rules],
84
58
  // Clone
85
- errors: errors.slice(),
59
+ errors: [...errors],
86
60
  };
87
61
  }
88
- exports.autoLoadRules = autoLoadRules;
@@ -0,0 +1,6 @@
1
+ export declare class ConfigLoadError extends Error {
2
+ filePath: string;
3
+ name: string;
4
+ referrer: string;
5
+ constructor(message: string, filePath: string, referrer: string);
6
+ }
@@ -0,0 +1,8 @@
1
+ export class ConfigLoadError extends Error {
2
+ constructor(message, filePath, referrer) {
3
+ super(message + ` in ${referrer}`);
4
+ this.name = 'ConfigLoadError';
5
+ this.filePath = filePath;
6
+ this.referrer = referrer;
7
+ }
8
+ }
@@ -1,15 +1,18 @@
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 {
6
- #private;
7
- resolve(targetFile: Readonly<MLFile>, names: readonly Nullable<string>[], cache?: boolean): Promise<ConfigSet>;
8
- search(targetFile: Readonly<MLFile>): Promise<string | null>;
9
- set(config: Config, key?: string): string;
10
- private _load;
11
- private _mergeConfigs;
12
- private _pathResolve;
13
- private _recursiveLoad;
14
- private _validateConfig;
6
+ #private;
7
+ recursiveLoad(key: string, cache: boolean, referrer: string, depth?: number): Promise<{
8
+ stack: Set<string>;
9
+ errs: Error[];
10
+ }>;
11
+ resolve(targetFile: Readonly<MLFile>, names: readonly Nullable<string>[], cache?: boolean): Promise<ConfigSet>;
12
+ search(targetFile: Readonly<MLFile>): Promise<string | null>;
13
+ set(config: Config, key?: string): string;
14
+ private _load;
15
+ private _mergeConfigs;
16
+ private _pathResolve;
17
+ private _validateConfig;
15
18
  }
@@ -1,56 +1,102 @@
1
- "use strict";
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
+ };
2
6
  var _ConfigProvider_cache, _ConfigProvider_held, _ConfigProvider_recursiveLoadKeyAndDepth, _ConfigProvider_store;
3
- Object.defineProperty(exports, "__esModule", { value: true });
4
- exports.ConfigProvider = void 0;
5
- const tslib_1 = require("tslib");
6
- const path_1 = tslib_1.__importDefault(require("path"));
7
- const ml_config_1 = require("@markuplint/ml-config");
8
- const ml_core_1 = require("@markuplint/ml-core");
9
- const parser_utils_1 = require("@markuplint/parser-utils");
10
- const selector_1 = require("@markuplint/selector");
11
- const shared_1 = require("@markuplint/shared");
12
- const cosmiconfig_1 = require("./cosmiconfig");
13
- const resolve_plugins_1 = require("./resolve-plugins");
14
- const utils_1 = require("./utils");
7
+ import path from 'node:path';
8
+ import { mergeConfig } from '@markuplint/ml-config';
9
+ import { ConfigParserError } from '@markuplint/parser-utils';
10
+ import { InvalidSelectorError, createSelector } from '@markuplint/selector';
11
+ import { nonNullableFilter, toNoEmptyStringArrayFromStringOrArray } from '@markuplint/shared';
12
+ import { ConfigLoadError } from './config-load-error.js';
13
+ import { load as loadConfig, search } from './cosmiconfig.js';
14
+ import { log } from './debug.js';
15
+ import { generalImport } from './general-import.js';
16
+ import { getPreset } from './get-preset.js';
17
+ import { isPluginModuleName } from './is-plugin-module-name.js';
18
+ import { isPresetModuleName } from './is-preset-module-name.js';
19
+ import { moduleExists } from './module-exists.js';
20
+ import { relPathToNameOrAbsPath } from './path-to-abs-or-name.js';
21
+ import { cacheClear, resolvePlugins } from './resolve-plugins.js';
22
+ import { fileExists, uuid } from './utils.js';
23
+ const cpLog = log.extend('config-provider');
15
24
  const KEY_SEPARATOR = '__ML_CONFIG_MERGE__';
16
- class ConfigProvider {
25
+ export class ConfigProvider {
17
26
  constructor() {
18
27
  _ConfigProvider_cache.set(this, new Map());
19
28
  _ConfigProvider_held.set(this, new Set());
20
29
  _ConfigProvider_recursiveLoadKeyAndDepth.set(this, new Map());
21
30
  _ConfigProvider_store.set(this, new Map());
22
31
  }
32
+ async recursiveLoad(key, cache, referrer, depth = 1) {
33
+ const stack = new Set();
34
+ const errs = [];
35
+ const ancestorDepth = __classPrivateFieldGet(this, _ConfigProvider_recursiveLoadKeyAndDepth, "f").get(key);
36
+ if (ancestorDepth != null && ancestorDepth < depth) {
37
+ return {
38
+ stack,
39
+ errs: [new CircularReferenceError(`Circular reference detected: ${key}`)],
40
+ };
41
+ }
42
+ __classPrivateFieldGet(this, _ConfigProvider_recursiveLoadKeyAndDepth, "f").set(key, depth);
43
+ let config = __classPrivateFieldGet(this, _ConfigProvider_store, "f").get(key);
44
+ if (!config) {
45
+ config = await this._load(key, cache, referrer);
46
+ }
47
+ if (!config) {
48
+ return { stack, errs: [] };
49
+ }
50
+ if (config instanceof ConfigLoadError) {
51
+ stack.add(config.filePath);
52
+ return {
53
+ stack,
54
+ errs: [config],
55
+ };
56
+ }
57
+ const depKeys = config.extends === null ? null : toNoEmptyStringArrayFromStringOrArray(config.extends);
58
+ if (depKeys) {
59
+ for (const depKey of depKeys) {
60
+ const keys = await this.recursiveLoad(depKey, cache, key, depth + 1);
61
+ for (const key of keys.stack) {
62
+ stack.add(key);
63
+ }
64
+ errs.push(...keys.errs);
65
+ }
66
+ }
67
+ stack.add(key);
68
+ return { stack, errs };
69
+ }
23
70
  async resolve(targetFile, names, cache = true) {
24
- var _a, _b;
25
71
  if (!cache) {
26
- tslib_1.__classPrivateFieldGet(this, _ConfigProvider_store, "f").clear();
27
- tslib_1.__classPrivateFieldGet(this, _ConfigProvider_cache, "f").clear();
28
- (0, resolve_plugins_1.cacheClear)();
72
+ __classPrivateFieldGet(this, _ConfigProvider_store, "f").clear();
73
+ __classPrivateFieldGet(this, _ConfigProvider_cache, "f").clear();
74
+ cacheClear();
29
75
  }
30
- const keys = names.filter(shared_1.nonNullableFilter);
76
+ const keys = names.filter(nonNullableFilter);
31
77
  const key = keys.join(KEY_SEPARATOR);
32
- const currentConfig = tslib_1.__classPrivateFieldGet(this, _ConfigProvider_cache, "f").get(key);
78
+ const currentConfig = __classPrivateFieldGet(this, _ConfigProvider_cache, "f").get(key);
33
79
  if (currentConfig) {
34
80
  return currentConfig;
35
81
  }
36
- let configSet = await this._mergeConfigs(keys, cache);
37
- const filePath = Array.from(configSet.files).reverse()[0];
82
+ let configSet = await this._mergeConfigs(keys, cache, targetFile.path);
83
+ const filePath = [...configSet.files].reverse()[0];
38
84
  if (!filePath) {
39
- throw new parser_utils_1.ConfigParserError('Config file not found', {
85
+ throw new ConfigParserError('Config file not found', {
40
86
  filePath: targetFile.path,
41
87
  });
42
88
  }
43
89
  const errors = this._validateConfig(configSet.config, filePath);
44
90
  configSet.errs.push(...errors);
45
- const plugins = await (0, resolve_plugins_1.resolvePlugins)(configSet.config.plugins);
46
- if (tslib_1.__classPrivateFieldGet(this, _ConfigProvider_held, "f").size > 0) {
47
- const extendHelds = Array.from(tslib_1.__classPrivateFieldGet(this, _ConfigProvider_held, "f").values());
91
+ const plugins = await resolvePlugins(configSet.config.plugins);
92
+ if (__classPrivateFieldGet(this, _ConfigProvider_held, "f").size > 0) {
93
+ const extendHelds = [...__classPrivateFieldGet(this, _ConfigProvider_held, "f").values()];
48
94
  for (const held of extendHelds) {
49
- const [, prefix, namespace, name] = (_a = held.match(/^([a-z]+:)([^/]+)(?:\/(.+))?$/)) !== null && _a !== void 0 ? _a : [];
95
+ const [, prefix, namespace, name] = held.match(/^([a-z]+:)([^/]+)(?:\/(.+))?$/) ?? [];
50
96
  switch (prefix) {
51
97
  case 'plugin:': {
52
98
  const plugin = plugins.find(plugin => plugin.name === namespace);
53
- const config = (_b = plugin === null || plugin === void 0 ? void 0 : plugin.configs) === null || _b === void 0 ? void 0 : _b[name !== null && name !== void 0 ? name : ''];
99
+ const config = plugin?.configs?.[name ?? ''];
54
100
  if (config) {
55
101
  this.set(config, held);
56
102
  }
@@ -58,8 +104,8 @@ class ConfigProvider {
58
104
  }
59
105
  }
60
106
  }
61
- configSet = await this._mergeConfigs([...keys, ...extendHelds], cache);
62
- tslib_1.__classPrivateFieldGet(this, _ConfigProvider_held, "f").clear();
107
+ configSet = await this._mergeConfigs([...keys, ...extendHelds], cache, targetFile.path);
108
+ __classPrivateFieldGet(this, _ConfigProvider_held, "f").clear();
63
109
  }
64
110
  // Resolves `overrides`
65
111
  if (configSet.config.overrides) {
@@ -78,74 +124,78 @@ class ConfigProvider {
78
124
  ...configSet,
79
125
  plugins,
80
126
  };
81
- tslib_1.__classPrivateFieldGet(this, _ConfigProvider_cache, "f").set(key, result);
127
+ __classPrivateFieldGet(this, _ConfigProvider_cache, "f").set(key, result);
82
128
  return result;
83
129
  }
84
130
  async search(targetFile) {
85
- if (!(await targetFile.dirExists())) {
131
+ const isExists = await targetFile.dirExists();
132
+ cpLog('search: %s', targetFile.path);
133
+ cpLog('isExists: %s', isExists);
134
+ if (!isExists) {
86
135
  return null;
87
136
  }
88
- const res = await (0, cosmiconfig_1.search)(targetFile.path, false);
137
+ const res = await search(targetFile.path, false);
138
+ cpLog('searched config: %O', res);
89
139
  if (!res) {
90
140
  return null;
91
141
  }
92
142
  const { filePath, config } = res;
93
- const pathResolvedConfig = this._pathResolve(config, filePath);
94
- tslib_1.__classPrivateFieldGet(this, _ConfigProvider_store, "f").set(filePath, pathResolvedConfig);
143
+ const pathResolvedConfig = await this._pathResolve(config, filePath);
144
+ __classPrivateFieldGet(this, _ConfigProvider_store, "f").set(filePath, pathResolvedConfig);
145
+ cpLog('Store key: %s', filePath);
95
146
  return filePath;
96
147
  }
97
148
  set(config, key) {
98
- key = key !== null && key !== void 0 ? key : (0, utils_1.uuid)();
99
- tslib_1.__classPrivateFieldGet(this, _ConfigProvider_store, "f").set(key, config);
149
+ key = key ?? uuid();
150
+ __classPrivateFieldGet(this, _ConfigProvider_store, "f").set(key, config);
100
151
  return key;
101
152
  }
102
- async _load(filePath, cache) {
103
- var _a;
104
- const entity = tslib_1.__classPrivateFieldGet(this, _ConfigProvider_store, "f").get(filePath);
153
+ async _load(filePath, cache, referrer) {
154
+ const entity = __classPrivateFieldGet(this, _ConfigProvider_store, "f").get(filePath);
105
155
  if (entity) {
106
156
  return entity;
107
157
  }
108
- if (isPreset(filePath)) {
109
- const [, name] = (_a = filePath.match(/^markuplint:(.+)$/i)) !== null && _a !== void 0 ? _a : [];
110
- const config = await (0, ml_core_1.getPreset)(name !== null && name !== void 0 ? name : filePath);
111
- const pathResolvedConfig = this._pathResolve(config, filePath);
112
- tslib_1.__classPrivateFieldGet(this, _ConfigProvider_store, "f").set(filePath, pathResolvedConfig);
158
+ if (isPresetModuleName(filePath)) {
159
+ const [, name] = filePath.match(/^markuplint:(.+)$/i) ?? [];
160
+ const config = await getPreset(name ?? filePath);
161
+ const pathResolvedConfig = await this._pathResolve(config, filePath);
162
+ __classPrivateFieldGet(this, _ConfigProvider_store, "f").set(filePath, pathResolvedConfig);
113
163
  return pathResolvedConfig;
114
164
  }
115
- if (isPlugin(filePath)) {
116
- tslib_1.__classPrivateFieldGet(this, _ConfigProvider_held, "f").add(filePath);
117
- return null;
165
+ if (isPluginModuleName(filePath)) {
166
+ __classPrivateFieldGet(this, _ConfigProvider_held, "f").add(filePath);
167
+ return;
118
168
  }
119
- if (!moduleExists(filePath) && !path_1.default.isAbsolute(filePath)) {
169
+ if (!(await moduleExists(filePath)) && !path.isAbsolute(filePath)) {
120
170
  throw new TypeError(`${filePath} is not an absolute path`);
121
171
  }
122
- const config = await load(filePath, cache);
123
- if (!config) {
124
- return null;
172
+ const config = await load(filePath, cache, referrer);
173
+ if (config instanceof ConfigLoadError) {
174
+ return config;
125
175
  }
126
- const pathResolvedConfig = this._pathResolve(config, filePath);
127
- tslib_1.__classPrivateFieldGet(this, _ConfigProvider_store, "f").set(filePath, pathResolvedConfig);
176
+ const pathResolvedConfig = await this._pathResolve(config, filePath);
177
+ __classPrivateFieldGet(this, _ConfigProvider_store, "f").set(filePath, pathResolvedConfig);
128
178
  return pathResolvedConfig;
129
179
  }
130
- async _mergeConfigs(keys, cache) {
180
+ async _mergeConfigs(keys, cache, referrer) {
131
181
  const resolvedKeys = new Set();
132
182
  const errs = [];
133
183
  for (const key of keys) {
134
- tslib_1.__classPrivateFieldGet(this, _ConfigProvider_recursiveLoadKeyAndDepth, "f").clear();
135
- const keySet = await this._recursiveLoad(key, cache);
184
+ __classPrivateFieldGet(this, _ConfigProvider_recursiveLoadKeyAndDepth, "f").clear();
185
+ const keySet = await this.recursiveLoad(key, cache, referrer);
136
186
  for (const k of keySet.stack) {
137
187
  resolvedKeys.add(k);
138
188
  }
139
- if (keySet.errs) {
140
- errs.push(...keySet.errs);
141
- }
189
+ errs.push(...keySet.errs);
142
190
  }
143
- const configs = Array.from(resolvedKeys)
144
- .map(name => tslib_1.__classPrivateFieldGet(this, _ConfigProvider_store, "f").get(name))
145
- .filter(shared_1.nonNullableFilter);
191
+ const configs = [...resolvedKeys].map(name => __classPrivateFieldGet(this, _ConfigProvider_store, "f").get(name)).filter(nonNullableFilter);
146
192
  let resultConfig = {};
147
193
  for (const config of configs) {
148
- resultConfig = (0, ml_config_1.mergeConfig)(resultConfig, config);
194
+ if (config instanceof ConfigLoadError) {
195
+ errs.push(config);
196
+ continue;
197
+ }
198
+ resultConfig = mergeConfig(resultConfig, config);
149
199
  }
150
200
  return {
151
201
  config: resultConfig,
@@ -153,157 +203,55 @@ class ConfigProvider {
153
203
  errs,
154
204
  };
155
205
  }
156
- _pathResolve(config, filePath) {
157
- const dir = path_1.default.dirname(filePath);
206
+ async _pathResolve(config, filePath) {
207
+ const dir = path.dirname(filePath);
158
208
  return {
159
209
  ...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),
210
+ extends: await relPathToNameOrAbsPath(dir, config.extends),
211
+ plugins: await relPathToNameOrAbsPath(dir, config.plugins, ['name']),
212
+ parser: await relPathToNameOrAbsPath(dir, config.parser),
213
+ specs: await relPathToNameOrAbsPath(dir, config.specs),
214
+ excludeFiles: await relPathToNameOrAbsPath(dir, config.excludeFiles),
215
+ overrides: await relPathToNameOrAbsPath(dir, config.overrides, undefined, true),
166
216
  };
167
217
  }
168
- async _recursiveLoad(key, cache, depth = 1) {
169
- var _a;
170
- const stack = new Set();
171
- const errs = [];
172
- const ancestorDepth = tslib_1.__classPrivateFieldGet(this, _ConfigProvider_recursiveLoadKeyAndDepth, "f").get(key);
173
- if (ancestorDepth != null && ancestorDepth < depth) {
174
- return {
175
- stack,
176
- errs: [new CircularReferenceError(`Circular reference detected: ${key}`)],
177
- };
178
- }
179
- tslib_1.__classPrivateFieldGet(this, _ConfigProvider_recursiveLoadKeyAndDepth, "f").set(key, depth);
180
- let config = (_a = tslib_1.__classPrivateFieldGet(this, _ConfigProvider_store, "f").get(key)) !== null && _a !== void 0 ? _a : null;
181
- if (!config) {
182
- config = await this._load(key, cache);
183
- }
184
- if (!config) {
185
- return { stack, errs: null };
186
- }
187
- const depKeys = config.extends !== null ? (0, shared_1.toNoEmptyStringArrayFromStringOrArray)(config.extends) : null;
188
- if (depKeys) {
189
- for (const depKey of depKeys) {
190
- const keys = await this._recursiveLoad(depKey, cache, depth + 1);
191
- for (const key of keys.stack) {
192
- stack.add(key);
193
- }
194
- if (keys.errs) {
195
- errs.push(...keys.errs);
196
- }
197
- }
198
- }
199
- stack.add(key);
200
- return { stack, errs };
201
- }
202
218
  _validateConfig(config, filePath) {
203
- var _a;
204
219
  const errors = [];
205
- (_a = config.nodeRules) === null || _a === void 0 ? void 0 : _a.forEach(rule => {
206
- if (rule.selector) {
207
- try {
208
- (0, selector_1.createSelector)(rule.selector);
209
- }
210
- catch (error) {
211
- if (error instanceof selector_1.InvalidSelectorError) {
212
- errors.push(new parser_utils_1.ConfigParserError(error.message, {
213
- filePath,
214
- raw: rule.selector,
215
- }));
220
+ if (config.nodeRules)
221
+ for (const rule of config.nodeRules) {
222
+ if (rule.selector) {
223
+ try {
224
+ createSelector(rule.selector);
225
+ }
226
+ catch (error) {
227
+ if (error instanceof InvalidSelectorError) {
228
+ errors.push(new ConfigParserError(error.message, {
229
+ filePath,
230
+ raw: rule.selector,
231
+ }));
232
+ }
216
233
  }
217
234
  }
218
235
  }
219
- });
220
236
  return errors;
221
237
  }
222
238
  }
223
- exports.ConfigProvider = ConfigProvider;
224
239
  _ConfigProvider_cache = new WeakMap(), _ConfigProvider_held = new WeakMap(), _ConfigProvider_recursiveLoadKeyAndDepth = new WeakMap(), _ConfigProvider_store = new WeakMap();
225
- async function load(filePath, cache) {
226
- var _a;
227
- if (!(0, utils_1.fileExists)(filePath) && moduleExists(filePath)) {
228
- const mod = await Promise.resolve(`${filePath}`).then(s => tslib_1.__importStar(require(s)));
229
- const config = (_a = mod === null || mod === void 0 ? void 0 : mod.default) !== null && _a !== void 0 ? _a : null;
240
+ async function load(filePath, cache, referrer) {
241
+ if (!fileExists(filePath) && (await moduleExists(filePath))) {
242
+ const config = (await generalImport(filePath)) ?? new ConfigLoadError('Module is not found', filePath, referrer);
230
243
  return config;
231
244
  }
232
- const res = await (0, cosmiconfig_1.load)(filePath, !cache);
233
- if (!res) {
234
- return null;
235
- }
236
- return res.config;
237
- }
238
- function pathResolve(dir, filePath, resolveProps, resolveKey = false) {
239
- if (filePath == null) {
240
- // @ts-ignore
241
- return undefined;
242
- }
243
- if (typeof filePath === 'string') {
244
- // @ts-ignore
245
- return resolve(dir, filePath);
246
- }
247
- if (Array.isArray(filePath)) {
248
- // @ts-ignore
249
- return filePath.map(fp => pathResolve(dir, fp, resolveProps));
250
- }
251
- const res = {};
252
- for (const [key, fp] of Object.entries(filePath)) {
253
- let _key = key;
254
- if (resolveKey) {
255
- _key = resolve(dir, key);
256
- }
257
- if (typeof fp === 'string') {
258
- if (!resolveProps) {
259
- res[_key] = resolve(dir, fp);
260
- }
261
- else if (resolveProps.includes(key)) {
262
- res[_key] = resolve(dir, fp);
263
- }
264
- else {
265
- res[_key] = fp;
266
- }
267
- }
268
- else {
269
- res[_key] = fp;
245
+ const res = await loadConfig(filePath, !cache, referrer).catch((error) => {
246
+ if (error instanceof ConfigLoadError) {
247
+ return error;
270
248
  }
249
+ throw error;
250
+ });
251
+ if (res instanceof ConfigLoadError) {
252
+ return res;
271
253
  }
272
- // @ts-ignore
273
- return res;
274
- }
275
- function resolve(dir, pathOrModName) {
276
- var _a, _b, _c;
277
- if (moduleExists(pathOrModName) || isPreset(pathOrModName) || isPlugin(pathOrModName)) {
278
- return pathOrModName;
279
- }
280
- const bangAndPath = (_a = /^(!)(.*)/.exec(pathOrModName)) !== null && _a !== void 0 ? _a : [];
281
- const bang = (_b = bangAndPath[1]) !== null && _b !== void 0 ? _b : '';
282
- const pathname = (_c = bangAndPath[2]) !== null && _c !== void 0 ? _c : pathOrModName;
283
- const absPath = path_1.default.resolve(dir, pathname);
284
- return bang + absPath;
285
- }
286
- function moduleExists(name) {
287
- try {
288
- require.resolve(name);
289
- }
290
- catch (err) {
291
- if (
292
- // @ts-ignore
293
- 'code' in err &&
294
- // @ts-ignore
295
- err.code === 'MODULE_NOT_FOUND') {
296
- return false;
297
- }
298
- throw err;
299
- }
300
- return true;
301
- }
302
- function isPreset(name) {
303
- return /^markuplint:/i.test(name);
304
- }
305
- function isPlugin(name) {
306
- return /^plugin:/i.test(name);
254
+ return res.config;
307
255
  }
308
256
  class CircularReferenceError extends ReferenceError {
309
257
  constructor() {
@@ -1,17 +1,12 @@
1
1
  import type { LoaderSync } from 'cosmiconfig';
2
+ import { ConfigLoadError } from './config-load-error.js';
2
3
  type CosmiConfig = ReturnType<LoaderSync>;
3
- export declare function search<T = CosmiConfig>(
4
- dir: string,
5
- cacheClear: boolean,
6
- ): Promise<{
7
- filePath: string;
8
- config: T;
9
- } | null>;
10
- export declare function load<T = CosmiConfig>(
11
- filePath: string,
12
- cacheClear: boolean,
13
- ): Promise<{
14
- filePath: string;
15
- config: T;
4
+ export declare function search<T = CosmiConfig>(filePath: string, cacheClear: boolean): Promise<{
5
+ filePath: string;
6
+ config: T;
16
7
  } | null>;
8
+ export declare function load<T = CosmiConfig>(filePath: string, cacheClear: boolean, referrer: string): Promise<ConfigLoadError | {
9
+ filePath: string;
10
+ config: T;
11
+ }>;
17
12
  export {};