ember-repl 6.0.0 → 7.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.md +1 -1
- package/README.md +2 -404
- package/config/.try.mjs +87 -0
- package/config/addon-main.cjs +7 -0
- package/config/attw.json +7 -0
- package/config/babel.publish.config.cjs +29 -0
- package/config/ember-cli-update.json +21 -0
- package/config/rollup.config.mjs +44 -0
- package/config/testem.cjs +35 -0
- package/config/tsconfig.publish.json +18 -0
- package/config/vite.config.mjs +36 -0
- package/declarations/compile/Compiled.d.ts +7 -0
- package/declarations/compile/Compiled.d.ts.map +1 -0
- package/declarations/compile/compile.d.ts +17 -0
- package/declarations/compile/compile.d.ts.map +1 -0
- package/declarations/compile/state.d.ts +43 -0
- package/declarations/compile/state.d.ts.map +1 -0
- package/declarations/compile/types.d.ts +5 -12
- package/declarations/compile/types.d.ts.map +1 -1
- package/declarations/index.d.ts +6 -2
- package/declarations/index.d.ts.map +1 -1
- package/declarations/services/compiler.d.ts +94 -0
- package/declarations/services/compiler.d.ts.map +1 -0
- package/declarations/services/known-modules.d.ts +7 -0
- package/declarations/services/known-modules.d.ts.map +1 -0
- package/declarations/setup.d.ts +7 -0
- package/declarations/setup.d.ts.map +1 -0
- package/declarations/test-support.d.ts +20 -0
- package/declarations/test-support.d.ts.map +1 -0
- package/dist/_commonjsHelpers-BAGoDD49.js +37 -0
- package/dist/_commonjsHelpers-BAGoDD49.js.map +1 -0
- package/dist/babel-8wMrbxkT.js +110427 -0
- package/dist/babel-8wMrbxkT.js.map +1 -0
- package/dist/blank-line-Bzg2Qt4K.js +482 -0
- package/dist/blank-line-Bzg2Qt4K.js.map +1 -0
- package/dist/compile/Compiled.js +26 -0
- package/dist/compile/Compiled.js.map +1 -0
- package/dist/compile/compile.js +62 -0
- package/dist/compile/compile.js.map +1 -0
- package/dist/compile/state.js +75 -0
- package/dist/compile/state.js.map +1 -0
- package/dist/compile/utils.js +213 -2
- package/dist/compile/utils.js.map +1 -1
- package/dist/default-CoqAuVeH.js +4 -0
- package/dist/default-CoqAuVeH.js.map +1 -0
- package/dist/index-BTx1k6gT.js +323 -0
- package/dist/index-BTx1k6gT.js.map +1 -0
- package/dist/index-Bxzjtr16.js +87 -0
- package/dist/index-Bxzjtr16.js.map +1 -0
- package/dist/index-C371bO_b.js +1553 -0
- package/dist/index-C371bO_b.js.map +1 -0
- package/dist/index-C4AyeeIa.js +5721 -0
- package/dist/index-C4AyeeIa.js.map +1 -0
- package/dist/index-C8S2G0FH.js +1953 -0
- package/dist/index-C8S2G0FH.js.map +1 -0
- package/dist/index-CCcIVEUK.js +409 -0
- package/dist/index-CCcIVEUK.js.map +1 -0
- package/dist/index-CDSIcg03.js +9070 -0
- package/dist/index-CDSIcg03.js.map +1 -0
- package/dist/index-D8szzCn3.js +2 -0
- package/dist/index-D8szzCn3.js.map +1 -0
- package/dist/index-DBBNT106.js +2644 -0
- package/dist/index-DBBNT106.js.map +1 -0
- package/dist/index-DP_Su7Zc.js +362 -0
- package/dist/index-DP_Su7Zc.js.map +1 -0
- package/dist/index-DejgrVqh.js +11299 -0
- package/dist/index-DejgrVqh.js.map +1 -0
- package/dist/index-Dr5iYoKt.js +1551 -0
- package/dist/index-Dr5iYoKt.js.map +1 -0
- package/dist/index-DxolpiGq.js +3336 -0
- package/dist/index-DxolpiGq.js.map +1 -0
- package/dist/index-ZyJlPFQY.js +249 -0
- package/dist/index-ZyJlPFQY.js.map +1 -0
- package/dist/index-k6CfLgeq.js +26 -0
- package/dist/index-k6CfLgeq.js.map +1 -0
- package/dist/index.js +4 -1
- package/dist/index.js.map +1 -1
- package/dist/services/compiler.js +329 -0
- package/dist/services/compiler.js.map +1 -0
- package/dist/services/known-modules.js +123 -0
- package/dist/services/known-modules.js.map +1 -0
- package/dist/setup.js +15 -0
- package/dist/setup.js.map +1 -0
- package/dist/test-support.js +33 -0
- package/dist/test-support.js.map +1 -0
- package/package.json +117 -138
- package/src/compile/Compiled.ts +45 -0
- package/src/compile/compile.ts +89 -0
- package/src/compile/state.ts +88 -0
- package/src/compile/types.ts +14 -13
- package/src/index.ts +6 -2
- package/src/services/compiler.ts +401 -0
- package/src/services/known-modules.ts +130 -0
- package/src/setup.ts +26 -0
- package/src/test-support.ts +64 -0
- package/addon-main.cjs +0 -5
- package/declarations/__PRIVATE__.d.ts +0 -2
- package/declarations/__PRIVATE__.d.ts.map +0 -1
- package/declarations/compile/formats/gjs/babel.d.ts +0 -7
- package/declarations/compile/formats/gjs/babel.d.ts.map +0 -1
- package/declarations/compile/formats/gjs/eval.d.ts +0 -8
- package/declarations/compile/formats/gjs/eval.d.ts.map +0 -1
- package/declarations/compile/formats/gjs/index.d.ts +0 -24
- package/declarations/compile/formats/gjs/index.d.ts.map +0 -1
- package/declarations/compile/formats/gjs/known-modules.d.ts +0 -48
- package/declarations/compile/formats/gjs/known-modules.d.ts.map +0 -1
- package/declarations/compile/formats/hbs.d.ts +0 -17
- package/declarations/compile/formats/hbs.d.ts.map +0 -1
- package/declarations/compile/formats/markdown.d.ts +0 -22
- package/declarations/compile/formats/markdown.d.ts.map +0 -1
- package/declarations/compile/formats.d.ts +0 -17
- package/declarations/compile/formats.d.ts.map +0 -1
- package/declarations/compile/index.d.ts +0 -80
- package/declarations/compile/index.d.ts.map +0 -1
- package/declarations/test-support/index.d.ts +0 -2
- package/declarations/test-support/index.d.ts.map +0 -1
- package/dist/__PRIVATE__.js +0 -2
- package/dist/__PRIVATE__.js.map +0 -1
- package/dist/compile/formats/gjs/babel.js +0 -2
- package/dist/compile/formats/gjs/babel.js.map +0 -1
- package/dist/compile/formats/gjs/eval.js +0 -19
- package/dist/compile/formats/gjs/eval.js.map +0 -1
- package/dist/compile/formats/gjs/index.js +0 -122
- package/dist/compile/formats/gjs/index.js.map +0 -1
- package/dist/compile/formats/gjs/known-modules.js +0 -52
- package/dist/compile/formats/gjs/known-modules.js.map +0 -1
- package/dist/compile/formats/hbs.js +0 -93
- package/dist/compile/formats/hbs.js.map +0 -1
- package/dist/compile/formats/markdown.js +0 -266
- package/dist/compile/formats/markdown.js.map +0 -1
- package/dist/compile/formats.js +0 -173
- package/dist/compile/formats.js.map +0 -1
- package/dist/compile/index.js +0 -113
- package/dist/compile/index.js.map +0 -1
- package/dist/test-support/index.js +0 -8
- package/dist/test-support/index.js.map +0 -1
- package/src/__PRIVATE__.ts +0 -1
- package/src/compile/formats/gjs/babel.ts +0 -7
- package/src/compile/formats/gjs/eval.ts +0 -29
- package/src/compile/formats/gjs/index.ts +0 -153
- package/src/compile/formats/gjs/known-modules.ts +0 -49
- package/src/compile/formats/hbs.ts +0 -100
- package/src/compile/formats/markdown.ts +0 -345
- package/src/compile/formats.ts +0 -178
- package/src/compile/index.ts +0 -219
- package/src/test-support/index.ts +0 -5
|
@@ -0,0 +1,2644 @@
|
|
|
1
|
+
import { g as getDefaultExportFromCjs } from './_commonjsHelpers-BAGoDD49.js';
|
|
2
|
+
import { s as stringifyPosition } from './index-Bxzjtr16.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Throw a given error.
|
|
6
|
+
*
|
|
7
|
+
* @param {Error|null|undefined} [error]
|
|
8
|
+
* Maybe error.
|
|
9
|
+
* @returns {asserts error is null|undefined}
|
|
10
|
+
*/
|
|
11
|
+
function bail(error) {
|
|
12
|
+
if (error) {
|
|
13
|
+
throw error;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
var extend$1;
|
|
18
|
+
var hasRequiredExtend;
|
|
19
|
+
|
|
20
|
+
function requireExtend () {
|
|
21
|
+
if (hasRequiredExtend) return extend$1;
|
|
22
|
+
hasRequiredExtend = 1;
|
|
23
|
+
|
|
24
|
+
var hasOwn = Object.prototype.hasOwnProperty;
|
|
25
|
+
var toStr = Object.prototype.toString;
|
|
26
|
+
var defineProperty = Object.defineProperty;
|
|
27
|
+
var gOPD = Object.getOwnPropertyDescriptor;
|
|
28
|
+
var isArray = function isArray(arr) {
|
|
29
|
+
if (typeof Array.isArray === 'function') {
|
|
30
|
+
return Array.isArray(arr);
|
|
31
|
+
}
|
|
32
|
+
return toStr.call(arr) === '[object Array]';
|
|
33
|
+
};
|
|
34
|
+
var isPlainObject = function isPlainObject(obj) {
|
|
35
|
+
if (!obj || toStr.call(obj) !== '[object Object]') {
|
|
36
|
+
return false;
|
|
37
|
+
}
|
|
38
|
+
var hasOwnConstructor = hasOwn.call(obj, 'constructor');
|
|
39
|
+
var hasIsPrototypeOf = obj.constructor && obj.constructor.prototype && hasOwn.call(obj.constructor.prototype, 'isPrototypeOf');
|
|
40
|
+
// Not own constructor property must be Object
|
|
41
|
+
if (obj.constructor && !hasOwnConstructor && !hasIsPrototypeOf) {
|
|
42
|
+
return false;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// Own properties are enumerated firstly, so to speed up,
|
|
46
|
+
// if last one is own, then all properties are own.
|
|
47
|
+
var key;
|
|
48
|
+
for (key in obj) {/**/}
|
|
49
|
+
return typeof key === 'undefined' || hasOwn.call(obj, key);
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
// If name is '__proto__', and Object.defineProperty is available, define __proto__ as an own property on target
|
|
53
|
+
var setProperty = function setProperty(target, options) {
|
|
54
|
+
if (defineProperty && options.name === '__proto__') {
|
|
55
|
+
defineProperty(target, options.name, {
|
|
56
|
+
enumerable: true,
|
|
57
|
+
configurable: true,
|
|
58
|
+
value: options.newValue,
|
|
59
|
+
writable: true
|
|
60
|
+
});
|
|
61
|
+
} else {
|
|
62
|
+
target[options.name] = options.newValue;
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
// Return undefined instead of __proto__ if '__proto__' is not an own property
|
|
67
|
+
var getProperty = function getProperty(obj, name) {
|
|
68
|
+
if (name === '__proto__') {
|
|
69
|
+
if (!hasOwn.call(obj, name)) {
|
|
70
|
+
return void 0;
|
|
71
|
+
} else if (gOPD) {
|
|
72
|
+
// In early versions of node, obj['__proto__'] is buggy when obj has
|
|
73
|
+
// __proto__ as an own property. Object.getOwnPropertyDescriptor() works.
|
|
74
|
+
return gOPD(obj, name).value;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
return obj[name];
|
|
78
|
+
};
|
|
79
|
+
extend$1 = function extend() {
|
|
80
|
+
var options, name, src, copy, copyIsArray, clone;
|
|
81
|
+
var target = arguments[0];
|
|
82
|
+
var i = 1;
|
|
83
|
+
var length = arguments.length;
|
|
84
|
+
var deep = false;
|
|
85
|
+
|
|
86
|
+
// Handle a deep copy situation
|
|
87
|
+
if (typeof target === 'boolean') {
|
|
88
|
+
deep = target;
|
|
89
|
+
target = arguments[1] || {};
|
|
90
|
+
// skip the boolean and the target
|
|
91
|
+
i = 2;
|
|
92
|
+
}
|
|
93
|
+
if (target == null || typeof target !== 'object' && typeof target !== 'function') {
|
|
94
|
+
target = {};
|
|
95
|
+
}
|
|
96
|
+
for (; i < length; ++i) {
|
|
97
|
+
options = arguments[i];
|
|
98
|
+
// Only deal with non-null/undefined values
|
|
99
|
+
if (options != null) {
|
|
100
|
+
// Extend the base object
|
|
101
|
+
for (name in options) {
|
|
102
|
+
src = getProperty(target, name);
|
|
103
|
+
copy = getProperty(options, name);
|
|
104
|
+
|
|
105
|
+
// Prevent never-ending loop
|
|
106
|
+
if (target !== copy) {
|
|
107
|
+
// Recurse if we're merging plain objects or arrays
|
|
108
|
+
if (deep && copy && (isPlainObject(copy) || (copyIsArray = isArray(copy)))) {
|
|
109
|
+
if (copyIsArray) {
|
|
110
|
+
copyIsArray = false;
|
|
111
|
+
clone = src && isArray(src) ? src : [];
|
|
112
|
+
} else {
|
|
113
|
+
clone = src && isPlainObject(src) ? src : {};
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// Never move original objects, clone them
|
|
117
|
+
setProperty(target, {
|
|
118
|
+
name: name,
|
|
119
|
+
newValue: extend(deep, clone, copy)
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
// Don't bring in undefined values
|
|
123
|
+
} else if (typeof copy !== 'undefined') {
|
|
124
|
+
setProperty(target, {
|
|
125
|
+
name: name,
|
|
126
|
+
newValue: copy
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// Return the modified object
|
|
135
|
+
return target;
|
|
136
|
+
};
|
|
137
|
+
return extend$1;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
var extendExports = requireExtend();
|
|
141
|
+
var extend = /*@__PURE__*/getDefaultExportFromCjs(extendExports);
|
|
142
|
+
|
|
143
|
+
function isPlainObject(value) {
|
|
144
|
+
if (typeof value !== 'object' || value === null) {
|
|
145
|
+
return false;
|
|
146
|
+
}
|
|
147
|
+
const prototype = Object.getPrototypeOf(value);
|
|
148
|
+
return (prototype === null || prototype === Object.prototype || Object.getPrototypeOf(prototype) === null) && !(Symbol.toStringTag in value) && !(Symbol.iterator in value);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// To do: remove `void`s
|
|
152
|
+
// To do: remove `null` from output of our APIs, allow it as user APIs.
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* @typedef {(error?: Error | null | undefined, ...output: Array<any>) => void} Callback
|
|
156
|
+
* Callback.
|
|
157
|
+
*
|
|
158
|
+
* @typedef {(...input: Array<any>) => any} Middleware
|
|
159
|
+
* Ware.
|
|
160
|
+
*
|
|
161
|
+
* @typedef Pipeline
|
|
162
|
+
* Pipeline.
|
|
163
|
+
* @property {Run} run
|
|
164
|
+
* Run the pipeline.
|
|
165
|
+
* @property {Use} use
|
|
166
|
+
* Add middleware.
|
|
167
|
+
*
|
|
168
|
+
* @typedef {(...input: Array<any>) => void} Run
|
|
169
|
+
* Call all middleware.
|
|
170
|
+
*
|
|
171
|
+
* Calls `done` on completion with either an error or the output of the
|
|
172
|
+
* last middleware.
|
|
173
|
+
*
|
|
174
|
+
* > 👉 **Note**: as the length of input defines whether async functions get a
|
|
175
|
+
* > `next` function,
|
|
176
|
+
* > it’s recommended to keep `input` at one value normally.
|
|
177
|
+
|
|
178
|
+
*
|
|
179
|
+
* @typedef {(fn: Middleware) => Pipeline} Use
|
|
180
|
+
* Add middleware.
|
|
181
|
+
*/
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* Create new middleware.
|
|
185
|
+
*
|
|
186
|
+
* @returns {Pipeline}
|
|
187
|
+
* Pipeline.
|
|
188
|
+
*/
|
|
189
|
+
function trough() {
|
|
190
|
+
/** @type {Array<Middleware>} */
|
|
191
|
+
const fns = [];
|
|
192
|
+
/** @type {Pipeline} */
|
|
193
|
+
const pipeline = {
|
|
194
|
+
run,
|
|
195
|
+
use
|
|
196
|
+
};
|
|
197
|
+
return pipeline;
|
|
198
|
+
|
|
199
|
+
/** @type {Run} */
|
|
200
|
+
function run(...values) {
|
|
201
|
+
let middlewareIndex = -1;
|
|
202
|
+
/** @type {Callback} */
|
|
203
|
+
const callback = values.pop();
|
|
204
|
+
if (typeof callback !== 'function') {
|
|
205
|
+
throw new TypeError('Expected function as last argument, not ' + callback);
|
|
206
|
+
}
|
|
207
|
+
next(null, ...values);
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* Run the next `fn`, or we’re done.
|
|
211
|
+
*
|
|
212
|
+
* @param {Error | null | undefined} error
|
|
213
|
+
* @param {Array<any>} output
|
|
214
|
+
*/
|
|
215
|
+
function next(error, ...output) {
|
|
216
|
+
const fn = fns[++middlewareIndex];
|
|
217
|
+
let index = -1;
|
|
218
|
+
if (error) {
|
|
219
|
+
callback(error);
|
|
220
|
+
return;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
// Copy non-nullish input into values.
|
|
224
|
+
while (++index < values.length) {
|
|
225
|
+
if (output[index] === null || output[index] === undefined) {
|
|
226
|
+
output[index] = values[index];
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
// Save the newly created `output` for the next call.
|
|
231
|
+
values = output;
|
|
232
|
+
|
|
233
|
+
// Next or done.
|
|
234
|
+
if (fn) {
|
|
235
|
+
wrap(fn, next)(...output);
|
|
236
|
+
} else {
|
|
237
|
+
callback(null, ...output);
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/** @type {Use} */
|
|
243
|
+
function use(middelware) {
|
|
244
|
+
if (typeof middelware !== 'function') {
|
|
245
|
+
throw new TypeError('Expected `middelware` to be a function, not ' + middelware);
|
|
246
|
+
}
|
|
247
|
+
fns.push(middelware);
|
|
248
|
+
return pipeline;
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* Wrap `middleware` into a uniform interface.
|
|
254
|
+
*
|
|
255
|
+
* You can pass all input to the resulting function.
|
|
256
|
+
* `callback` is then called with the output of `middleware`.
|
|
257
|
+
*
|
|
258
|
+
* If `middleware` accepts more arguments than the later given in input,
|
|
259
|
+
* an extra `done` function is passed to it after that input,
|
|
260
|
+
* which must be called by `middleware`.
|
|
261
|
+
*
|
|
262
|
+
* The first value in `input` is the main input value.
|
|
263
|
+
* All other input values are the rest input values.
|
|
264
|
+
* The values given to `callback` are the input values,
|
|
265
|
+
* merged with every non-nullish output value.
|
|
266
|
+
*
|
|
267
|
+
* * if `middleware` throws an error,
|
|
268
|
+
* returns a promise that is rejected,
|
|
269
|
+
* or calls the given `done` function with an error,
|
|
270
|
+
* `callback` is called with that error
|
|
271
|
+
* * if `middleware` returns a value or returns a promise that is resolved,
|
|
272
|
+
* that value is the main output value
|
|
273
|
+
* * if `middleware` calls `done`,
|
|
274
|
+
* all non-nullish values except for the first one (the error) overwrite the
|
|
275
|
+
* output values
|
|
276
|
+
*
|
|
277
|
+
* @param {Middleware} middleware
|
|
278
|
+
* Function to wrap.
|
|
279
|
+
* @param {Callback} callback
|
|
280
|
+
* Callback called with the output of `middleware`.
|
|
281
|
+
* @returns {Run}
|
|
282
|
+
* Wrapped middleware.
|
|
283
|
+
*/
|
|
284
|
+
function wrap(middleware, callback) {
|
|
285
|
+
/** @type {boolean} */
|
|
286
|
+
let called;
|
|
287
|
+
return wrapped;
|
|
288
|
+
|
|
289
|
+
/**
|
|
290
|
+
* Call `middleware`.
|
|
291
|
+
* @this {any}
|
|
292
|
+
* @param {Array<any>} parameters
|
|
293
|
+
* @returns {void}
|
|
294
|
+
*/
|
|
295
|
+
function wrapped(...parameters) {
|
|
296
|
+
const fnExpectsCallback = middleware.length > parameters.length;
|
|
297
|
+
/** @type {any} */
|
|
298
|
+
let result;
|
|
299
|
+
if (fnExpectsCallback) {
|
|
300
|
+
parameters.push(done);
|
|
301
|
+
}
|
|
302
|
+
try {
|
|
303
|
+
result = middleware.apply(this, parameters);
|
|
304
|
+
} catch (error) {
|
|
305
|
+
const exception = /** @type {Error} */error;
|
|
306
|
+
|
|
307
|
+
// Well, this is quite the pickle.
|
|
308
|
+
// `middleware` received a callback and called it synchronously, but that
|
|
309
|
+
// threw an error.
|
|
310
|
+
// The only thing left to do is to throw the thing instead.
|
|
311
|
+
if (fnExpectsCallback && called) {
|
|
312
|
+
throw exception;
|
|
313
|
+
}
|
|
314
|
+
return done(exception);
|
|
315
|
+
}
|
|
316
|
+
if (!fnExpectsCallback) {
|
|
317
|
+
if (result && result.then && typeof result.then === 'function') {
|
|
318
|
+
result.then(then, done);
|
|
319
|
+
} else if (result instanceof Error) {
|
|
320
|
+
done(result);
|
|
321
|
+
} else {
|
|
322
|
+
then(result);
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
/**
|
|
328
|
+
* Call `callback`, only once.
|
|
329
|
+
*
|
|
330
|
+
* @type {Callback}
|
|
331
|
+
*/
|
|
332
|
+
function done(error, ...output) {
|
|
333
|
+
if (!called) {
|
|
334
|
+
called = true;
|
|
335
|
+
callback(error, ...output);
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
/**
|
|
340
|
+
* Call `done` with one value.
|
|
341
|
+
*
|
|
342
|
+
* @param {any} [value]
|
|
343
|
+
*/
|
|
344
|
+
function then(value) {
|
|
345
|
+
done(null, value);
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
/**
|
|
350
|
+
* @typedef {import('unist').Node} Node
|
|
351
|
+
* @typedef {import('unist').Point} Point
|
|
352
|
+
* @typedef {import('unist').Position} Position
|
|
353
|
+
*/
|
|
354
|
+
|
|
355
|
+
|
|
356
|
+
/**
|
|
357
|
+
* Message.
|
|
358
|
+
*/
|
|
359
|
+
class VFileMessage extends Error {
|
|
360
|
+
/**
|
|
361
|
+
* Create a message for `reason`.
|
|
362
|
+
*
|
|
363
|
+
* > 🪦 **Note**: also has obsolete signatures.
|
|
364
|
+
*
|
|
365
|
+
* @overload
|
|
366
|
+
* @param {string} reason
|
|
367
|
+
* @param {Options | null | undefined} [options]
|
|
368
|
+
* @returns
|
|
369
|
+
*
|
|
370
|
+
* @overload
|
|
371
|
+
* @param {string} reason
|
|
372
|
+
* @param {Node | NodeLike | null | undefined} parent
|
|
373
|
+
* @param {string | null | undefined} [origin]
|
|
374
|
+
* @returns
|
|
375
|
+
*
|
|
376
|
+
* @overload
|
|
377
|
+
* @param {string} reason
|
|
378
|
+
* @param {Point | Position | null | undefined} place
|
|
379
|
+
* @param {string | null | undefined} [origin]
|
|
380
|
+
* @returns
|
|
381
|
+
*
|
|
382
|
+
* @overload
|
|
383
|
+
* @param {string} reason
|
|
384
|
+
* @param {string | null | undefined} [origin]
|
|
385
|
+
* @returns
|
|
386
|
+
*
|
|
387
|
+
* @overload
|
|
388
|
+
* @param {Error | VFileMessage} cause
|
|
389
|
+
* @param {Node | NodeLike | null | undefined} parent
|
|
390
|
+
* @param {string | null | undefined} [origin]
|
|
391
|
+
* @returns
|
|
392
|
+
*
|
|
393
|
+
* @overload
|
|
394
|
+
* @param {Error | VFileMessage} cause
|
|
395
|
+
* @param {Point | Position | null | undefined} place
|
|
396
|
+
* @param {string | null | undefined} [origin]
|
|
397
|
+
* @returns
|
|
398
|
+
*
|
|
399
|
+
* @overload
|
|
400
|
+
* @param {Error | VFileMessage} cause
|
|
401
|
+
* @param {string | null | undefined} [origin]
|
|
402
|
+
* @returns
|
|
403
|
+
*
|
|
404
|
+
* @param {Error | VFileMessage | string} causeOrReason
|
|
405
|
+
* Reason for message, should use markdown.
|
|
406
|
+
* @param {Node | NodeLike | Options | Point | Position | string | null | undefined} [optionsOrParentOrPlace]
|
|
407
|
+
* Configuration (optional).
|
|
408
|
+
* @param {string | null | undefined} [origin]
|
|
409
|
+
* Place in code where the message originates (example:
|
|
410
|
+
* `'my-package:my-rule'` or `'my-rule'`).
|
|
411
|
+
* @returns
|
|
412
|
+
* Instance of `VFileMessage`.
|
|
413
|
+
*/
|
|
414
|
+
// eslint-disable-next-line complexity
|
|
415
|
+
constructor(causeOrReason, optionsOrParentOrPlace, origin) {
|
|
416
|
+
super();
|
|
417
|
+
if (typeof optionsOrParentOrPlace === 'string') {
|
|
418
|
+
origin = optionsOrParentOrPlace;
|
|
419
|
+
optionsOrParentOrPlace = undefined;
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
/** @type {string} */
|
|
423
|
+
let reason = '';
|
|
424
|
+
/** @type {Options} */
|
|
425
|
+
let options = {};
|
|
426
|
+
let legacyCause = false;
|
|
427
|
+
if (optionsOrParentOrPlace) {
|
|
428
|
+
// Point.
|
|
429
|
+
if ('line' in optionsOrParentOrPlace && 'column' in optionsOrParentOrPlace) {
|
|
430
|
+
options = {
|
|
431
|
+
place: optionsOrParentOrPlace
|
|
432
|
+
};
|
|
433
|
+
}
|
|
434
|
+
// Position.
|
|
435
|
+
else if ('start' in optionsOrParentOrPlace && 'end' in optionsOrParentOrPlace) {
|
|
436
|
+
options = {
|
|
437
|
+
place: optionsOrParentOrPlace
|
|
438
|
+
};
|
|
439
|
+
}
|
|
440
|
+
// Node.
|
|
441
|
+
else if ('type' in optionsOrParentOrPlace) {
|
|
442
|
+
options = {
|
|
443
|
+
ancestors: [optionsOrParentOrPlace],
|
|
444
|
+
place: optionsOrParentOrPlace.position
|
|
445
|
+
};
|
|
446
|
+
}
|
|
447
|
+
// Options.
|
|
448
|
+
else {
|
|
449
|
+
options = {
|
|
450
|
+
...optionsOrParentOrPlace
|
|
451
|
+
};
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
if (typeof causeOrReason === 'string') {
|
|
455
|
+
reason = causeOrReason;
|
|
456
|
+
}
|
|
457
|
+
// Error.
|
|
458
|
+
else if (!options.cause && causeOrReason) {
|
|
459
|
+
legacyCause = true;
|
|
460
|
+
reason = causeOrReason.message;
|
|
461
|
+
options.cause = causeOrReason;
|
|
462
|
+
}
|
|
463
|
+
if (!options.ruleId && !options.source && typeof origin === 'string') {
|
|
464
|
+
const index = origin.indexOf(':');
|
|
465
|
+
if (index === -1) {
|
|
466
|
+
options.ruleId = origin;
|
|
467
|
+
} else {
|
|
468
|
+
options.source = origin.slice(0, index);
|
|
469
|
+
options.ruleId = origin.slice(index + 1);
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
if (!options.place && options.ancestors && options.ancestors) {
|
|
473
|
+
const parent = options.ancestors[options.ancestors.length - 1];
|
|
474
|
+
if (parent) {
|
|
475
|
+
options.place = parent.position;
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
const start = options.place && 'start' in options.place ? options.place.start : options.place;
|
|
479
|
+
|
|
480
|
+
/* eslint-disable no-unused-expressions */
|
|
481
|
+
/**
|
|
482
|
+
* Stack of ancestor nodes surrounding the message.
|
|
483
|
+
*
|
|
484
|
+
* @type {Array<Node> | undefined}
|
|
485
|
+
*/
|
|
486
|
+
this.ancestors = options.ancestors || undefined;
|
|
487
|
+
|
|
488
|
+
/**
|
|
489
|
+
* Original error cause of the message.
|
|
490
|
+
*
|
|
491
|
+
* @type {Error | undefined}
|
|
492
|
+
*/
|
|
493
|
+
this.cause = options.cause || undefined;
|
|
494
|
+
|
|
495
|
+
/**
|
|
496
|
+
* Starting column of message.
|
|
497
|
+
*
|
|
498
|
+
* @type {number | undefined}
|
|
499
|
+
*/
|
|
500
|
+
this.column = start ? start.column : undefined;
|
|
501
|
+
|
|
502
|
+
/**
|
|
503
|
+
* State of problem.
|
|
504
|
+
*
|
|
505
|
+
* * `true` — error, file not usable
|
|
506
|
+
* * `false` — warning, change may be needed
|
|
507
|
+
* * `undefined` — change likely not needed
|
|
508
|
+
*
|
|
509
|
+
* @type {boolean | null | undefined}
|
|
510
|
+
*/
|
|
511
|
+
this.fatal = undefined;
|
|
512
|
+
|
|
513
|
+
/**
|
|
514
|
+
* Path of a file (used throughout the `VFile` ecosystem).
|
|
515
|
+
*
|
|
516
|
+
* @type {string | undefined}
|
|
517
|
+
*/
|
|
518
|
+
this.file;
|
|
519
|
+
|
|
520
|
+
// Field from `Error`.
|
|
521
|
+
/**
|
|
522
|
+
* Reason for message.
|
|
523
|
+
*
|
|
524
|
+
* @type {string}
|
|
525
|
+
*/
|
|
526
|
+
this.message = reason;
|
|
527
|
+
|
|
528
|
+
/**
|
|
529
|
+
* Starting line of error.
|
|
530
|
+
*
|
|
531
|
+
* @type {number | undefined}
|
|
532
|
+
*/
|
|
533
|
+
this.line = start ? start.line : undefined;
|
|
534
|
+
|
|
535
|
+
// Field from `Error`.
|
|
536
|
+
/**
|
|
537
|
+
* Serialized positional info of message.
|
|
538
|
+
*
|
|
539
|
+
* On normal errors, this would be something like `ParseError`, buit in
|
|
540
|
+
* `VFile` messages we use this space to show where an error happened.
|
|
541
|
+
*/
|
|
542
|
+
this.name = stringifyPosition(options.place) || '1:1';
|
|
543
|
+
|
|
544
|
+
/**
|
|
545
|
+
* Place of message.
|
|
546
|
+
*
|
|
547
|
+
* @type {Point | Position | undefined}
|
|
548
|
+
*/
|
|
549
|
+
this.place = options.place || undefined;
|
|
550
|
+
|
|
551
|
+
/**
|
|
552
|
+
* Reason for message, should use markdown.
|
|
553
|
+
*
|
|
554
|
+
* @type {string}
|
|
555
|
+
*/
|
|
556
|
+
this.reason = this.message;
|
|
557
|
+
|
|
558
|
+
/**
|
|
559
|
+
* Category of message (example: `'my-rule'`).
|
|
560
|
+
*
|
|
561
|
+
* @type {string | undefined}
|
|
562
|
+
*/
|
|
563
|
+
this.ruleId = options.ruleId || undefined;
|
|
564
|
+
|
|
565
|
+
/**
|
|
566
|
+
* Namespace of message (example: `'my-package'`).
|
|
567
|
+
*
|
|
568
|
+
* @type {string | undefined}
|
|
569
|
+
*/
|
|
570
|
+
this.source = options.source || undefined;
|
|
571
|
+
|
|
572
|
+
// Field from `Error`.
|
|
573
|
+
/**
|
|
574
|
+
* Stack of message.
|
|
575
|
+
*
|
|
576
|
+
* This is used by normal errors to show where something happened in
|
|
577
|
+
* programming code, irrelevant for `VFile` messages,
|
|
578
|
+
*
|
|
579
|
+
* @type {string}
|
|
580
|
+
*/
|
|
581
|
+
this.stack = legacyCause && options.cause && typeof options.cause.stack === 'string' ? options.cause.stack : '';
|
|
582
|
+
|
|
583
|
+
// The following fields are “well known”.
|
|
584
|
+
// Not standard.
|
|
585
|
+
// Feel free to add other non-standard fields to your messages.
|
|
586
|
+
|
|
587
|
+
/**
|
|
588
|
+
* Specify the source value that’s being reported, which is deemed
|
|
589
|
+
* incorrect.
|
|
590
|
+
*
|
|
591
|
+
* @type {string | undefined}
|
|
592
|
+
*/
|
|
593
|
+
this.actual;
|
|
594
|
+
|
|
595
|
+
/**
|
|
596
|
+
* Suggest acceptable values that can be used instead of `actual`.
|
|
597
|
+
*
|
|
598
|
+
* @type {Array<string> | undefined}
|
|
599
|
+
*/
|
|
600
|
+
this.expected;
|
|
601
|
+
|
|
602
|
+
/**
|
|
603
|
+
* Long form description of the message (you should use markdown).
|
|
604
|
+
*
|
|
605
|
+
* @type {string | undefined}
|
|
606
|
+
*/
|
|
607
|
+
this.note;
|
|
608
|
+
|
|
609
|
+
/**
|
|
610
|
+
* Link to docs for the message.
|
|
611
|
+
*
|
|
612
|
+
* > 👉 **Note**: this must be an absolute URL that can be passed as `x`
|
|
613
|
+
* > to `new URL(x)`.
|
|
614
|
+
*
|
|
615
|
+
* @type {string | undefined}
|
|
616
|
+
*/
|
|
617
|
+
this.url;
|
|
618
|
+
/* eslint-enable no-unused-expressions */
|
|
619
|
+
}
|
|
620
|
+
}
|
|
621
|
+
VFileMessage.prototype.file = '';
|
|
622
|
+
VFileMessage.prototype.name = '';
|
|
623
|
+
VFileMessage.prototype.reason = '';
|
|
624
|
+
VFileMessage.prototype.message = '';
|
|
625
|
+
VFileMessage.prototype.stack = '';
|
|
626
|
+
VFileMessage.prototype.column = undefined;
|
|
627
|
+
VFileMessage.prototype.line = undefined;
|
|
628
|
+
VFileMessage.prototype.ancestors = undefined;
|
|
629
|
+
VFileMessage.prototype.cause = undefined;
|
|
630
|
+
VFileMessage.prototype.fatal = undefined;
|
|
631
|
+
VFileMessage.prototype.place = undefined;
|
|
632
|
+
VFileMessage.prototype.ruleId = undefined;
|
|
633
|
+
VFileMessage.prototype.source = undefined;
|
|
634
|
+
|
|
635
|
+
// A derivative work based on:
|
|
636
|
+
// <https://github.com/browserify/path-browserify>.
|
|
637
|
+
// Which is licensed:
|
|
638
|
+
//
|
|
639
|
+
// MIT License
|
|
640
|
+
//
|
|
641
|
+
// Copyright (c) 2013 James Halliday
|
|
642
|
+
//
|
|
643
|
+
// Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
644
|
+
// this software and associated documentation files (the "Software"), to deal in
|
|
645
|
+
// the Software without restriction, including without limitation the rights to
|
|
646
|
+
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
647
|
+
// the Software, and to permit persons to whom the Software is furnished to do so,
|
|
648
|
+
// subject to the following conditions:
|
|
649
|
+
//
|
|
650
|
+
// The above copyright notice and this permission notice shall be included in all
|
|
651
|
+
// copies or substantial portions of the Software.
|
|
652
|
+
//
|
|
653
|
+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
654
|
+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
|
655
|
+
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
656
|
+
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
|
657
|
+
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
658
|
+
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
659
|
+
// A derivative work based on:
|
|
660
|
+
//
|
|
661
|
+
// Parts of that are extracted from Node’s internal `path` module:
|
|
662
|
+
// <https://github.com/nodejs/node/blob/master/lib/path.js>.
|
|
663
|
+
// Which is licensed:
|
|
664
|
+
//
|
|
665
|
+
// Copyright Joyent, Inc. and other Node contributors.
|
|
666
|
+
//
|
|
667
|
+
// Permission is hereby granted, free of charge, to any person obtaining a
|
|
668
|
+
// copy of this software and associated documentation files (the
|
|
669
|
+
// "Software"), to deal in the Software without restriction, including
|
|
670
|
+
// without limitation the rights to use, copy, modify, merge, publish,
|
|
671
|
+
// distribute, sublicense, and/or sell copies of the Software, and to permit
|
|
672
|
+
// persons to whom the Software is furnished to do so, subject to the
|
|
673
|
+
// following conditions:
|
|
674
|
+
//
|
|
675
|
+
// The above copyright notice and this permission notice shall be included
|
|
676
|
+
// in all copies or substantial portions of the Software.
|
|
677
|
+
//
|
|
678
|
+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
679
|
+
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
680
|
+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
|
681
|
+
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
|
682
|
+
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|
683
|
+
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
|
684
|
+
// USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
685
|
+
|
|
686
|
+
const minpath = {
|
|
687
|
+
basename,
|
|
688
|
+
dirname,
|
|
689
|
+
extname,
|
|
690
|
+
join,
|
|
691
|
+
sep: '/'
|
|
692
|
+
};
|
|
693
|
+
|
|
694
|
+
/* eslint-disable max-depth, complexity */
|
|
695
|
+
|
|
696
|
+
/**
|
|
697
|
+
* Get the basename from a path.
|
|
698
|
+
*
|
|
699
|
+
* @param {string} path
|
|
700
|
+
* File path.
|
|
701
|
+
* @param {string | null | undefined} [extname]
|
|
702
|
+
* Extension to strip.
|
|
703
|
+
* @returns {string}
|
|
704
|
+
* Stem or basename.
|
|
705
|
+
*/
|
|
706
|
+
function basename(path, extname) {
|
|
707
|
+
if (extname !== undefined && typeof extname !== 'string') {
|
|
708
|
+
throw new TypeError('"ext" argument must be a string');
|
|
709
|
+
}
|
|
710
|
+
assertPath$1(path);
|
|
711
|
+
let start = 0;
|
|
712
|
+
let end = -1;
|
|
713
|
+
let index = path.length;
|
|
714
|
+
/** @type {boolean | undefined} */
|
|
715
|
+
let seenNonSlash;
|
|
716
|
+
if (extname === undefined || extname.length === 0 || extname.length > path.length) {
|
|
717
|
+
while (index--) {
|
|
718
|
+
if (path.codePointAt(index) === 47 /* `/` */) {
|
|
719
|
+
// If we reached a path separator that was not part of a set of path
|
|
720
|
+
// separators at the end of the string, stop now.
|
|
721
|
+
if (seenNonSlash) {
|
|
722
|
+
start = index + 1;
|
|
723
|
+
break;
|
|
724
|
+
}
|
|
725
|
+
} else if (end < 0) {
|
|
726
|
+
// We saw the first non-path separator, mark this as the end of our
|
|
727
|
+
// path component.
|
|
728
|
+
seenNonSlash = true;
|
|
729
|
+
end = index + 1;
|
|
730
|
+
}
|
|
731
|
+
}
|
|
732
|
+
return end < 0 ? '' : path.slice(start, end);
|
|
733
|
+
}
|
|
734
|
+
if (extname === path) {
|
|
735
|
+
return '';
|
|
736
|
+
}
|
|
737
|
+
let firstNonSlashEnd = -1;
|
|
738
|
+
let extnameIndex = extname.length - 1;
|
|
739
|
+
while (index--) {
|
|
740
|
+
if (path.codePointAt(index) === 47 /* `/` */) {
|
|
741
|
+
// If we reached a path separator that was not part of a set of path
|
|
742
|
+
// separators at the end of the string, stop now.
|
|
743
|
+
if (seenNonSlash) {
|
|
744
|
+
start = index + 1;
|
|
745
|
+
break;
|
|
746
|
+
}
|
|
747
|
+
} else {
|
|
748
|
+
if (firstNonSlashEnd < 0) {
|
|
749
|
+
// We saw the first non-path separator, remember this index in case
|
|
750
|
+
// we need it if the extension ends up not matching.
|
|
751
|
+
seenNonSlash = true;
|
|
752
|
+
firstNonSlashEnd = index + 1;
|
|
753
|
+
}
|
|
754
|
+
if (extnameIndex > -1) {
|
|
755
|
+
// Try to match the explicit extension.
|
|
756
|
+
if (path.codePointAt(index) === extname.codePointAt(extnameIndex--)) {
|
|
757
|
+
if (extnameIndex < 0) {
|
|
758
|
+
// We matched the extension, so mark this as the end of our path
|
|
759
|
+
// component
|
|
760
|
+
end = index;
|
|
761
|
+
}
|
|
762
|
+
} else {
|
|
763
|
+
// Extension does not match, so our result is the entire path
|
|
764
|
+
// component
|
|
765
|
+
extnameIndex = -1;
|
|
766
|
+
end = firstNonSlashEnd;
|
|
767
|
+
}
|
|
768
|
+
}
|
|
769
|
+
}
|
|
770
|
+
}
|
|
771
|
+
if (start === end) {
|
|
772
|
+
end = firstNonSlashEnd;
|
|
773
|
+
} else if (end < 0) {
|
|
774
|
+
end = path.length;
|
|
775
|
+
}
|
|
776
|
+
return path.slice(start, end);
|
|
777
|
+
}
|
|
778
|
+
|
|
779
|
+
/**
|
|
780
|
+
* Get the dirname from a path.
|
|
781
|
+
*
|
|
782
|
+
* @param {string} path
|
|
783
|
+
* File path.
|
|
784
|
+
* @returns {string}
|
|
785
|
+
* File path.
|
|
786
|
+
*/
|
|
787
|
+
function dirname(path) {
|
|
788
|
+
assertPath$1(path);
|
|
789
|
+
if (path.length === 0) {
|
|
790
|
+
return '.';
|
|
791
|
+
}
|
|
792
|
+
let end = -1;
|
|
793
|
+
let index = path.length;
|
|
794
|
+
/** @type {boolean | undefined} */
|
|
795
|
+
let unmatchedSlash;
|
|
796
|
+
|
|
797
|
+
// Prefix `--` is important to not run on `0`.
|
|
798
|
+
while (--index) {
|
|
799
|
+
if (path.codePointAt(index) === 47 /* `/` */) {
|
|
800
|
+
if (unmatchedSlash) {
|
|
801
|
+
end = index;
|
|
802
|
+
break;
|
|
803
|
+
}
|
|
804
|
+
} else if (!unmatchedSlash) {
|
|
805
|
+
// We saw the first non-path separator
|
|
806
|
+
unmatchedSlash = true;
|
|
807
|
+
}
|
|
808
|
+
}
|
|
809
|
+
return end < 0 ? path.codePointAt(0) === 47 /* `/` */ ? '/' : '.' : end === 1 && path.codePointAt(0) === 47 /* `/` */ ? '//' : path.slice(0, end);
|
|
810
|
+
}
|
|
811
|
+
|
|
812
|
+
/**
|
|
813
|
+
* Get an extname from a path.
|
|
814
|
+
*
|
|
815
|
+
* @param {string} path
|
|
816
|
+
* File path.
|
|
817
|
+
* @returns {string}
|
|
818
|
+
* Extname.
|
|
819
|
+
*/
|
|
820
|
+
function extname(path) {
|
|
821
|
+
assertPath$1(path);
|
|
822
|
+
let index = path.length;
|
|
823
|
+
let end = -1;
|
|
824
|
+
let startPart = 0;
|
|
825
|
+
let startDot = -1;
|
|
826
|
+
// Track the state of characters (if any) we see before our first dot and
|
|
827
|
+
// after any path separator we find.
|
|
828
|
+
let preDotState = 0;
|
|
829
|
+
/** @type {boolean | undefined} */
|
|
830
|
+
let unmatchedSlash;
|
|
831
|
+
while (index--) {
|
|
832
|
+
const code = path.codePointAt(index);
|
|
833
|
+
if (code === 47 /* `/` */) {
|
|
834
|
+
// If we reached a path separator that was not part of a set of path
|
|
835
|
+
// separators at the end of the string, stop now.
|
|
836
|
+
if (unmatchedSlash) {
|
|
837
|
+
startPart = index + 1;
|
|
838
|
+
break;
|
|
839
|
+
}
|
|
840
|
+
continue;
|
|
841
|
+
}
|
|
842
|
+
if (end < 0) {
|
|
843
|
+
// We saw the first non-path separator, mark this as the end of our
|
|
844
|
+
// extension.
|
|
845
|
+
unmatchedSlash = true;
|
|
846
|
+
end = index + 1;
|
|
847
|
+
}
|
|
848
|
+
if (code === 46 /* `.` */) {
|
|
849
|
+
// If this is our first dot, mark it as the start of our extension.
|
|
850
|
+
if (startDot < 0) {
|
|
851
|
+
startDot = index;
|
|
852
|
+
} else if (preDotState !== 1) {
|
|
853
|
+
preDotState = 1;
|
|
854
|
+
}
|
|
855
|
+
} else if (startDot > -1) {
|
|
856
|
+
// We saw a non-dot and non-path separator before our dot, so we should
|
|
857
|
+
// have a good chance at having a non-empty extension.
|
|
858
|
+
preDotState = -1;
|
|
859
|
+
}
|
|
860
|
+
}
|
|
861
|
+
if (startDot < 0 || end < 0 ||
|
|
862
|
+
// We saw a non-dot character immediately before the dot.
|
|
863
|
+
preDotState === 0 ||
|
|
864
|
+
// The (right-most) trimmed path component is exactly `..`.
|
|
865
|
+
preDotState === 1 && startDot === end - 1 && startDot === startPart + 1) {
|
|
866
|
+
return '';
|
|
867
|
+
}
|
|
868
|
+
return path.slice(startDot, end);
|
|
869
|
+
}
|
|
870
|
+
|
|
871
|
+
/**
|
|
872
|
+
* Join segments from a path.
|
|
873
|
+
*
|
|
874
|
+
* @param {Array<string>} segments
|
|
875
|
+
* Path segments.
|
|
876
|
+
* @returns {string}
|
|
877
|
+
* File path.
|
|
878
|
+
*/
|
|
879
|
+
function join(...segments) {
|
|
880
|
+
let index = -1;
|
|
881
|
+
/** @type {string | undefined} */
|
|
882
|
+
let joined;
|
|
883
|
+
while (++index < segments.length) {
|
|
884
|
+
assertPath$1(segments[index]);
|
|
885
|
+
if (segments[index]) {
|
|
886
|
+
joined = joined === undefined ? segments[index] : joined + '/' + segments[index];
|
|
887
|
+
}
|
|
888
|
+
}
|
|
889
|
+
return joined === undefined ? '.' : normalize(joined);
|
|
890
|
+
}
|
|
891
|
+
|
|
892
|
+
/**
|
|
893
|
+
* Normalize a basic file path.
|
|
894
|
+
*
|
|
895
|
+
* @param {string} path
|
|
896
|
+
* File path.
|
|
897
|
+
* @returns {string}
|
|
898
|
+
* File path.
|
|
899
|
+
*/
|
|
900
|
+
// Note: `normalize` is not exposed as `path.normalize`, so some code is
|
|
901
|
+
// manually removed from it.
|
|
902
|
+
function normalize(path) {
|
|
903
|
+
assertPath$1(path);
|
|
904
|
+
const absolute = path.codePointAt(0) === 47; /* `/` */
|
|
905
|
+
|
|
906
|
+
// Normalize the path according to POSIX rules.
|
|
907
|
+
let value = normalizeString(path, !absolute);
|
|
908
|
+
if (value.length === 0 && !absolute) {
|
|
909
|
+
value = '.';
|
|
910
|
+
}
|
|
911
|
+
if (value.length > 0 && path.codePointAt(path.length - 1) === 47 /* / */) {
|
|
912
|
+
value += '/';
|
|
913
|
+
}
|
|
914
|
+
return absolute ? '/' + value : value;
|
|
915
|
+
}
|
|
916
|
+
|
|
917
|
+
/**
|
|
918
|
+
* Resolve `.` and `..` elements in a path with directory names.
|
|
919
|
+
*
|
|
920
|
+
* @param {string} path
|
|
921
|
+
* File path.
|
|
922
|
+
* @param {boolean} allowAboveRoot
|
|
923
|
+
* Whether `..` can move above root.
|
|
924
|
+
* @returns {string}
|
|
925
|
+
* File path.
|
|
926
|
+
*/
|
|
927
|
+
function normalizeString(path, allowAboveRoot) {
|
|
928
|
+
let result = '';
|
|
929
|
+
let lastSegmentLength = 0;
|
|
930
|
+
let lastSlash = -1;
|
|
931
|
+
let dots = 0;
|
|
932
|
+
let index = -1;
|
|
933
|
+
/** @type {number | undefined} */
|
|
934
|
+
let code;
|
|
935
|
+
/** @type {number} */
|
|
936
|
+
let lastSlashIndex;
|
|
937
|
+
while (++index <= path.length) {
|
|
938
|
+
if (index < path.length) {
|
|
939
|
+
code = path.codePointAt(index);
|
|
940
|
+
} else if (code === 47 /* `/` */) {
|
|
941
|
+
break;
|
|
942
|
+
} else {
|
|
943
|
+
code = 47; /* `/` */
|
|
944
|
+
}
|
|
945
|
+
if (code === 47 /* `/` */) {
|
|
946
|
+
if (lastSlash === index - 1 || dots === 1) ; else if (lastSlash !== index - 1 && dots === 2) {
|
|
947
|
+
if (result.length < 2 || lastSegmentLength !== 2 || result.codePointAt(result.length - 1) !== 46 /* `.` */ || result.codePointAt(result.length - 2) !== 46 /* `.` */) {
|
|
948
|
+
if (result.length > 2) {
|
|
949
|
+
lastSlashIndex = result.lastIndexOf('/');
|
|
950
|
+
if (lastSlashIndex !== result.length - 1) {
|
|
951
|
+
if (lastSlashIndex < 0) {
|
|
952
|
+
result = '';
|
|
953
|
+
lastSegmentLength = 0;
|
|
954
|
+
} else {
|
|
955
|
+
result = result.slice(0, lastSlashIndex);
|
|
956
|
+
lastSegmentLength = result.length - 1 - result.lastIndexOf('/');
|
|
957
|
+
}
|
|
958
|
+
lastSlash = index;
|
|
959
|
+
dots = 0;
|
|
960
|
+
continue;
|
|
961
|
+
}
|
|
962
|
+
} else if (result.length > 0) {
|
|
963
|
+
result = '';
|
|
964
|
+
lastSegmentLength = 0;
|
|
965
|
+
lastSlash = index;
|
|
966
|
+
dots = 0;
|
|
967
|
+
continue;
|
|
968
|
+
}
|
|
969
|
+
}
|
|
970
|
+
if (allowAboveRoot) {
|
|
971
|
+
result = result.length > 0 ? result + '/..' : '..';
|
|
972
|
+
lastSegmentLength = 2;
|
|
973
|
+
}
|
|
974
|
+
} else {
|
|
975
|
+
if (result.length > 0) {
|
|
976
|
+
result += '/' + path.slice(lastSlash + 1, index);
|
|
977
|
+
} else {
|
|
978
|
+
result = path.slice(lastSlash + 1, index);
|
|
979
|
+
}
|
|
980
|
+
lastSegmentLength = index - lastSlash - 1;
|
|
981
|
+
}
|
|
982
|
+
lastSlash = index;
|
|
983
|
+
dots = 0;
|
|
984
|
+
} else if (code === 46 /* `.` */ && dots > -1) {
|
|
985
|
+
dots++;
|
|
986
|
+
} else {
|
|
987
|
+
dots = -1;
|
|
988
|
+
}
|
|
989
|
+
}
|
|
990
|
+
return result;
|
|
991
|
+
}
|
|
992
|
+
|
|
993
|
+
/**
|
|
994
|
+
* Make sure `path` is a string.
|
|
995
|
+
*
|
|
996
|
+
* @param {string} path
|
|
997
|
+
* File path.
|
|
998
|
+
* @returns {asserts path is string}
|
|
999
|
+
* Nothing.
|
|
1000
|
+
*/
|
|
1001
|
+
function assertPath$1(path) {
|
|
1002
|
+
if (typeof path !== 'string') {
|
|
1003
|
+
throw new TypeError('Path must be a string. Received ' + JSON.stringify(path));
|
|
1004
|
+
}
|
|
1005
|
+
}
|
|
1006
|
+
|
|
1007
|
+
/* eslint-enable max-depth, complexity */
|
|
1008
|
+
|
|
1009
|
+
// Somewhat based on:
|
|
1010
|
+
// <https://github.com/defunctzombie/node-process/blob/master/browser.js>.
|
|
1011
|
+
// But I don’t think one tiny line of code can be copyrighted. 😅
|
|
1012
|
+
const minproc = {
|
|
1013
|
+
cwd
|
|
1014
|
+
};
|
|
1015
|
+
function cwd() {
|
|
1016
|
+
return '/';
|
|
1017
|
+
}
|
|
1018
|
+
|
|
1019
|
+
/**
|
|
1020
|
+
* Checks if a value has the shape of a WHATWG URL object.
|
|
1021
|
+
*
|
|
1022
|
+
* Using a symbol or instanceof would not be able to recognize URL objects
|
|
1023
|
+
* coming from other implementations (e.g. in Electron), so instead we are
|
|
1024
|
+
* checking some well known properties for a lack of a better test.
|
|
1025
|
+
*
|
|
1026
|
+
* We use `href` and `protocol` as they are the only properties that are
|
|
1027
|
+
* easy to retrieve and calculate due to the lazy nature of the getters.
|
|
1028
|
+
*
|
|
1029
|
+
* We check for auth attribute to distinguish legacy url instance with
|
|
1030
|
+
* WHATWG URL instance.
|
|
1031
|
+
*
|
|
1032
|
+
* @param {unknown} fileUrlOrPath
|
|
1033
|
+
* File path or URL.
|
|
1034
|
+
* @returns {fileUrlOrPath is URL}
|
|
1035
|
+
* Whether it’s a URL.
|
|
1036
|
+
*/
|
|
1037
|
+
// From: <https://github.com/nodejs/node/blob/6a3403c/lib/internal/url.js#L720>
|
|
1038
|
+
function isUrl(fileUrlOrPath) {
|
|
1039
|
+
return Boolean(fileUrlOrPath !== null && typeof fileUrlOrPath === 'object' && 'href' in fileUrlOrPath && fileUrlOrPath.href && 'protocol' in fileUrlOrPath && fileUrlOrPath.protocol &&
|
|
1040
|
+
// @ts-expect-error: indexing is fine.
|
|
1041
|
+
fileUrlOrPath.auth === undefined);
|
|
1042
|
+
}
|
|
1043
|
+
|
|
1044
|
+
// See: <https://github.com/nodejs/node/blob/6a3403c/lib/internal/url.js>
|
|
1045
|
+
|
|
1046
|
+
/**
|
|
1047
|
+
* @param {URL | string} path
|
|
1048
|
+
* File URL.
|
|
1049
|
+
* @returns {string}
|
|
1050
|
+
* File URL.
|
|
1051
|
+
*/
|
|
1052
|
+
function urlToPath(path) {
|
|
1053
|
+
if (typeof path === 'string') {
|
|
1054
|
+
path = new URL(path);
|
|
1055
|
+
} else if (!isUrl(path)) {
|
|
1056
|
+
/** @type {NodeJS.ErrnoException} */
|
|
1057
|
+
const error = new TypeError('The "path" argument must be of type string or an instance of URL. Received `' + path + '`');
|
|
1058
|
+
error.code = 'ERR_INVALID_ARG_TYPE';
|
|
1059
|
+
throw error;
|
|
1060
|
+
}
|
|
1061
|
+
if (path.protocol !== 'file:') {
|
|
1062
|
+
/** @type {NodeJS.ErrnoException} */
|
|
1063
|
+
const error = new TypeError('The URL must be of scheme file');
|
|
1064
|
+
error.code = 'ERR_INVALID_URL_SCHEME';
|
|
1065
|
+
throw error;
|
|
1066
|
+
}
|
|
1067
|
+
return getPathFromURLPosix(path);
|
|
1068
|
+
}
|
|
1069
|
+
|
|
1070
|
+
/**
|
|
1071
|
+
* Get a path from a POSIX URL.
|
|
1072
|
+
*
|
|
1073
|
+
* @param {URL} url
|
|
1074
|
+
* URL.
|
|
1075
|
+
* @returns {string}
|
|
1076
|
+
* File path.
|
|
1077
|
+
*/
|
|
1078
|
+
function getPathFromURLPosix(url) {
|
|
1079
|
+
if (url.hostname !== '') {
|
|
1080
|
+
/** @type {NodeJS.ErrnoException} */
|
|
1081
|
+
const error = new TypeError('File URL host must be "localhost" or empty on darwin');
|
|
1082
|
+
error.code = 'ERR_INVALID_FILE_URL_HOST';
|
|
1083
|
+
throw error;
|
|
1084
|
+
}
|
|
1085
|
+
const pathname = url.pathname;
|
|
1086
|
+
let index = -1;
|
|
1087
|
+
while (++index < pathname.length) {
|
|
1088
|
+
if (pathname.codePointAt(index) === 37 /* `%` */ && pathname.codePointAt(index + 1) === 50 /* `2` */) {
|
|
1089
|
+
const third = pathname.codePointAt(index + 2);
|
|
1090
|
+
if (third === 70 /* `F` */ || third === 102 /* `f` */) {
|
|
1091
|
+
/** @type {NodeJS.ErrnoException} */
|
|
1092
|
+
const error = new TypeError('File URL path must not include encoded / characters');
|
|
1093
|
+
error.code = 'ERR_INVALID_FILE_URL_PATH';
|
|
1094
|
+
throw error;
|
|
1095
|
+
}
|
|
1096
|
+
}
|
|
1097
|
+
}
|
|
1098
|
+
return decodeURIComponent(pathname);
|
|
1099
|
+
}
|
|
1100
|
+
|
|
1101
|
+
/**
|
|
1102
|
+
* @import {Node, Point, Position} from 'unist'
|
|
1103
|
+
* @import {Options as MessageOptions} from 'vfile-message'
|
|
1104
|
+
* @import {Compatible, Data, Map, Options, Value} from 'vfile'
|
|
1105
|
+
*/
|
|
1106
|
+
|
|
1107
|
+
|
|
1108
|
+
/**
|
|
1109
|
+
* Order of setting (least specific to most), we need this because otherwise
|
|
1110
|
+
* `{stem: 'a', path: '~/b.js'}` would throw, as a path is needed before a
|
|
1111
|
+
* stem can be set.
|
|
1112
|
+
*/
|
|
1113
|
+
const order = /** @type {const} */['history', 'path', 'basename', 'stem', 'extname', 'dirname'];
|
|
1114
|
+
class VFile {
|
|
1115
|
+
/**
|
|
1116
|
+
* Create a new virtual file.
|
|
1117
|
+
*
|
|
1118
|
+
* `options` is treated as:
|
|
1119
|
+
*
|
|
1120
|
+
* * `string` or `Uint8Array` — `{value: options}`
|
|
1121
|
+
* * `URL` — `{path: options}`
|
|
1122
|
+
* * `VFile` — shallow copies its data over to the new file
|
|
1123
|
+
* * `object` — all fields are shallow copied over to the new file
|
|
1124
|
+
*
|
|
1125
|
+
* Path related fields are set in the following order (least specific to
|
|
1126
|
+
* most specific): `history`, `path`, `basename`, `stem`, `extname`,
|
|
1127
|
+
* `dirname`.
|
|
1128
|
+
*
|
|
1129
|
+
* You cannot set `dirname` or `extname` without setting either `history`,
|
|
1130
|
+
* `path`, `basename`, or `stem` too.
|
|
1131
|
+
*
|
|
1132
|
+
* @param {Compatible | null | undefined} [value]
|
|
1133
|
+
* File value.
|
|
1134
|
+
* @returns
|
|
1135
|
+
* New instance.
|
|
1136
|
+
*/
|
|
1137
|
+
constructor(value) {
|
|
1138
|
+
/** @type {Options | VFile} */
|
|
1139
|
+
let options;
|
|
1140
|
+
if (!value) {
|
|
1141
|
+
options = {};
|
|
1142
|
+
} else if (isUrl(value)) {
|
|
1143
|
+
options = {
|
|
1144
|
+
path: value
|
|
1145
|
+
};
|
|
1146
|
+
} else if (typeof value === 'string' || isUint8Array$1(value)) {
|
|
1147
|
+
options = {
|
|
1148
|
+
value
|
|
1149
|
+
};
|
|
1150
|
+
} else {
|
|
1151
|
+
options = value;
|
|
1152
|
+
}
|
|
1153
|
+
|
|
1154
|
+
/* eslint-disable no-unused-expressions */
|
|
1155
|
+
|
|
1156
|
+
/**
|
|
1157
|
+
* Base of `path` (default: `process.cwd()` or `'/'` in browsers).
|
|
1158
|
+
*
|
|
1159
|
+
* @type {string}
|
|
1160
|
+
*/
|
|
1161
|
+
// Prevent calling `cwd` (which could be expensive) if it’s not needed;
|
|
1162
|
+
// the empty string will be overridden in the next block.
|
|
1163
|
+
this.cwd = 'cwd' in options ? '' : minproc.cwd();
|
|
1164
|
+
|
|
1165
|
+
/**
|
|
1166
|
+
* Place to store custom info (default: `{}`).
|
|
1167
|
+
*
|
|
1168
|
+
* It’s OK to store custom data directly on the file but moving it to
|
|
1169
|
+
* `data` is recommended.
|
|
1170
|
+
*
|
|
1171
|
+
* @type {Data}
|
|
1172
|
+
*/
|
|
1173
|
+
this.data = {};
|
|
1174
|
+
|
|
1175
|
+
/**
|
|
1176
|
+
* List of file paths the file moved between.
|
|
1177
|
+
*
|
|
1178
|
+
* The first is the original path and the last is the current path.
|
|
1179
|
+
*
|
|
1180
|
+
* @type {Array<string>}
|
|
1181
|
+
*/
|
|
1182
|
+
this.history = [];
|
|
1183
|
+
|
|
1184
|
+
/**
|
|
1185
|
+
* List of messages associated with the file.
|
|
1186
|
+
*
|
|
1187
|
+
* @type {Array<VFileMessage>}
|
|
1188
|
+
*/
|
|
1189
|
+
this.messages = [];
|
|
1190
|
+
|
|
1191
|
+
/**
|
|
1192
|
+
* Raw value.
|
|
1193
|
+
*
|
|
1194
|
+
* @type {Value}
|
|
1195
|
+
*/
|
|
1196
|
+
this.value;
|
|
1197
|
+
|
|
1198
|
+
// The below are non-standard, they are “well-known”.
|
|
1199
|
+
// As in, used in several tools.
|
|
1200
|
+
/**
|
|
1201
|
+
* Source map.
|
|
1202
|
+
*
|
|
1203
|
+
* This type is equivalent to the `RawSourceMap` type from the `source-map`
|
|
1204
|
+
* module.
|
|
1205
|
+
*
|
|
1206
|
+
* @type {Map | null | undefined}
|
|
1207
|
+
*/
|
|
1208
|
+
this.map;
|
|
1209
|
+
|
|
1210
|
+
/**
|
|
1211
|
+
* Custom, non-string, compiled, representation.
|
|
1212
|
+
*
|
|
1213
|
+
* This is used by unified to store non-string results.
|
|
1214
|
+
* One example is when turning markdown into React nodes.
|
|
1215
|
+
*
|
|
1216
|
+
* @type {unknown}
|
|
1217
|
+
*/
|
|
1218
|
+
this.result;
|
|
1219
|
+
|
|
1220
|
+
/**
|
|
1221
|
+
* Whether a file was saved to disk.
|
|
1222
|
+
*
|
|
1223
|
+
* This is used by vfile reporters.
|
|
1224
|
+
*
|
|
1225
|
+
* @type {boolean}
|
|
1226
|
+
*/
|
|
1227
|
+
this.stored;
|
|
1228
|
+
/* eslint-enable no-unused-expressions */
|
|
1229
|
+
|
|
1230
|
+
// Set path related properties in the correct order.
|
|
1231
|
+
let index = -1;
|
|
1232
|
+
while (++index < order.length) {
|
|
1233
|
+
const field = order[index];
|
|
1234
|
+
|
|
1235
|
+
// Note: we specifically use `in` instead of `hasOwnProperty` to accept
|
|
1236
|
+
// `vfile`s too.
|
|
1237
|
+
if (field in options && options[field] !== undefined && options[field] !== null) {
|
|
1238
|
+
// @ts-expect-error: TS doesn’t understand basic reality.
|
|
1239
|
+
this[field] = field === 'history' ? [...options[field]] : options[field];
|
|
1240
|
+
}
|
|
1241
|
+
}
|
|
1242
|
+
|
|
1243
|
+
/** @type {string} */
|
|
1244
|
+
let field;
|
|
1245
|
+
|
|
1246
|
+
// Set non-path related properties.
|
|
1247
|
+
for (field in options) {
|
|
1248
|
+
// @ts-expect-error: fine to set other things.
|
|
1249
|
+
if (!order.includes(field)) {
|
|
1250
|
+
// @ts-expect-error: fine to set other things.
|
|
1251
|
+
this[field] = options[field];
|
|
1252
|
+
}
|
|
1253
|
+
}
|
|
1254
|
+
}
|
|
1255
|
+
|
|
1256
|
+
/**
|
|
1257
|
+
* Get the basename (including extname) (example: `'index.min.js'`).
|
|
1258
|
+
*
|
|
1259
|
+
* @returns {string | undefined}
|
|
1260
|
+
* Basename.
|
|
1261
|
+
*/
|
|
1262
|
+
get basename() {
|
|
1263
|
+
return typeof this.path === 'string' ? minpath.basename(this.path) : undefined;
|
|
1264
|
+
}
|
|
1265
|
+
|
|
1266
|
+
/**
|
|
1267
|
+
* Set basename (including extname) (`'index.min.js'`).
|
|
1268
|
+
*
|
|
1269
|
+
* Cannot contain path separators (`'/'` on unix, macOS, and browsers, `'\'`
|
|
1270
|
+
* on windows).
|
|
1271
|
+
* Cannot be nullified (use `file.path = file.dirname` instead).
|
|
1272
|
+
*
|
|
1273
|
+
* @param {string} basename
|
|
1274
|
+
* Basename.
|
|
1275
|
+
* @returns {undefined}
|
|
1276
|
+
* Nothing.
|
|
1277
|
+
*/
|
|
1278
|
+
set basename(basename) {
|
|
1279
|
+
assertNonEmpty(basename, 'basename');
|
|
1280
|
+
assertPart(basename, 'basename');
|
|
1281
|
+
this.path = minpath.join(this.dirname || '', basename);
|
|
1282
|
+
}
|
|
1283
|
+
|
|
1284
|
+
/**
|
|
1285
|
+
* Get the parent path (example: `'~'`).
|
|
1286
|
+
*
|
|
1287
|
+
* @returns {string | undefined}
|
|
1288
|
+
* Dirname.
|
|
1289
|
+
*/
|
|
1290
|
+
get dirname() {
|
|
1291
|
+
return typeof this.path === 'string' ? minpath.dirname(this.path) : undefined;
|
|
1292
|
+
}
|
|
1293
|
+
|
|
1294
|
+
/**
|
|
1295
|
+
* Set the parent path (example: `'~'`).
|
|
1296
|
+
*
|
|
1297
|
+
* Cannot be set if there’s no `path` yet.
|
|
1298
|
+
*
|
|
1299
|
+
* @param {string | undefined} dirname
|
|
1300
|
+
* Dirname.
|
|
1301
|
+
* @returns {undefined}
|
|
1302
|
+
* Nothing.
|
|
1303
|
+
*/
|
|
1304
|
+
set dirname(dirname) {
|
|
1305
|
+
assertPath(this.basename, 'dirname');
|
|
1306
|
+
this.path = minpath.join(dirname || '', this.basename);
|
|
1307
|
+
}
|
|
1308
|
+
|
|
1309
|
+
/**
|
|
1310
|
+
* Get the extname (including dot) (example: `'.js'`).
|
|
1311
|
+
*
|
|
1312
|
+
* @returns {string | undefined}
|
|
1313
|
+
* Extname.
|
|
1314
|
+
*/
|
|
1315
|
+
get extname() {
|
|
1316
|
+
return typeof this.path === 'string' ? minpath.extname(this.path) : undefined;
|
|
1317
|
+
}
|
|
1318
|
+
|
|
1319
|
+
/**
|
|
1320
|
+
* Set the extname (including dot) (example: `'.js'`).
|
|
1321
|
+
*
|
|
1322
|
+
* Cannot contain path separators (`'/'` on unix, macOS, and browsers, `'\'`
|
|
1323
|
+
* on windows).
|
|
1324
|
+
* Cannot be set if there’s no `path` yet.
|
|
1325
|
+
*
|
|
1326
|
+
* @param {string | undefined} extname
|
|
1327
|
+
* Extname.
|
|
1328
|
+
* @returns {undefined}
|
|
1329
|
+
* Nothing.
|
|
1330
|
+
*/
|
|
1331
|
+
set extname(extname) {
|
|
1332
|
+
assertPart(extname, 'extname');
|
|
1333
|
+
assertPath(this.dirname, 'extname');
|
|
1334
|
+
if (extname) {
|
|
1335
|
+
if (extname.codePointAt(0) !== 46 /* `.` */) {
|
|
1336
|
+
throw new Error('`extname` must start with `.`');
|
|
1337
|
+
}
|
|
1338
|
+
if (extname.includes('.', 1)) {
|
|
1339
|
+
throw new Error('`extname` cannot contain multiple dots');
|
|
1340
|
+
}
|
|
1341
|
+
}
|
|
1342
|
+
this.path = minpath.join(this.dirname, this.stem + (extname || ''));
|
|
1343
|
+
}
|
|
1344
|
+
|
|
1345
|
+
/**
|
|
1346
|
+
* Get the full path (example: `'~/index.min.js'`).
|
|
1347
|
+
*
|
|
1348
|
+
* @returns {string}
|
|
1349
|
+
* Path.
|
|
1350
|
+
*/
|
|
1351
|
+
get path() {
|
|
1352
|
+
return this.history[this.history.length - 1];
|
|
1353
|
+
}
|
|
1354
|
+
|
|
1355
|
+
/**
|
|
1356
|
+
* Set the full path (example: `'~/index.min.js'`).
|
|
1357
|
+
*
|
|
1358
|
+
* Cannot be nullified.
|
|
1359
|
+
* You can set a file URL (a `URL` object with a `file:` protocol) which will
|
|
1360
|
+
* be turned into a path with `url.fileURLToPath`.
|
|
1361
|
+
*
|
|
1362
|
+
* @param {URL | string} path
|
|
1363
|
+
* Path.
|
|
1364
|
+
* @returns {undefined}
|
|
1365
|
+
* Nothing.
|
|
1366
|
+
*/
|
|
1367
|
+
set path(path) {
|
|
1368
|
+
if (isUrl(path)) {
|
|
1369
|
+
path = urlToPath(path);
|
|
1370
|
+
}
|
|
1371
|
+
assertNonEmpty(path, 'path');
|
|
1372
|
+
if (this.path !== path) {
|
|
1373
|
+
this.history.push(path);
|
|
1374
|
+
}
|
|
1375
|
+
}
|
|
1376
|
+
|
|
1377
|
+
/**
|
|
1378
|
+
* Get the stem (basename w/o extname) (example: `'index.min'`).
|
|
1379
|
+
*
|
|
1380
|
+
* @returns {string | undefined}
|
|
1381
|
+
* Stem.
|
|
1382
|
+
*/
|
|
1383
|
+
get stem() {
|
|
1384
|
+
return typeof this.path === 'string' ? minpath.basename(this.path, this.extname) : undefined;
|
|
1385
|
+
}
|
|
1386
|
+
|
|
1387
|
+
/**
|
|
1388
|
+
* Set the stem (basename w/o extname) (example: `'index.min'`).
|
|
1389
|
+
*
|
|
1390
|
+
* Cannot contain path separators (`'/'` on unix, macOS, and browsers, `'\'`
|
|
1391
|
+
* on windows).
|
|
1392
|
+
* Cannot be nullified (use `file.path = file.dirname` instead).
|
|
1393
|
+
*
|
|
1394
|
+
* @param {string} stem
|
|
1395
|
+
* Stem.
|
|
1396
|
+
* @returns {undefined}
|
|
1397
|
+
* Nothing.
|
|
1398
|
+
*/
|
|
1399
|
+
set stem(stem) {
|
|
1400
|
+
assertNonEmpty(stem, 'stem');
|
|
1401
|
+
assertPart(stem, 'stem');
|
|
1402
|
+
this.path = minpath.join(this.dirname || '', stem + (this.extname || ''));
|
|
1403
|
+
}
|
|
1404
|
+
|
|
1405
|
+
// Normal prototypal methods.
|
|
1406
|
+
/**
|
|
1407
|
+
* Create a fatal message for `reason` associated with the file.
|
|
1408
|
+
*
|
|
1409
|
+
* The `fatal` field of the message is set to `true` (error; file not usable)
|
|
1410
|
+
* and the `file` field is set to the current file path.
|
|
1411
|
+
* The message is added to the `messages` field on `file`.
|
|
1412
|
+
*
|
|
1413
|
+
* > 🪦 **Note**: also has obsolete signatures.
|
|
1414
|
+
*
|
|
1415
|
+
* @overload
|
|
1416
|
+
* @param {string} reason
|
|
1417
|
+
* @param {MessageOptions | null | undefined} [options]
|
|
1418
|
+
* @returns {never}
|
|
1419
|
+
*
|
|
1420
|
+
* @overload
|
|
1421
|
+
* @param {string} reason
|
|
1422
|
+
* @param {Node | NodeLike | null | undefined} parent
|
|
1423
|
+
* @param {string | null | undefined} [origin]
|
|
1424
|
+
* @returns {never}
|
|
1425
|
+
*
|
|
1426
|
+
* @overload
|
|
1427
|
+
* @param {string} reason
|
|
1428
|
+
* @param {Point | Position | null | undefined} place
|
|
1429
|
+
* @param {string | null | undefined} [origin]
|
|
1430
|
+
* @returns {never}
|
|
1431
|
+
*
|
|
1432
|
+
* @overload
|
|
1433
|
+
* @param {string} reason
|
|
1434
|
+
* @param {string | null | undefined} [origin]
|
|
1435
|
+
* @returns {never}
|
|
1436
|
+
*
|
|
1437
|
+
* @overload
|
|
1438
|
+
* @param {Error | VFileMessage} cause
|
|
1439
|
+
* @param {Node | NodeLike | null | undefined} parent
|
|
1440
|
+
* @param {string | null | undefined} [origin]
|
|
1441
|
+
* @returns {never}
|
|
1442
|
+
*
|
|
1443
|
+
* @overload
|
|
1444
|
+
* @param {Error | VFileMessage} cause
|
|
1445
|
+
* @param {Point | Position | null | undefined} place
|
|
1446
|
+
* @param {string | null | undefined} [origin]
|
|
1447
|
+
* @returns {never}
|
|
1448
|
+
*
|
|
1449
|
+
* @overload
|
|
1450
|
+
* @param {Error | VFileMessage} cause
|
|
1451
|
+
* @param {string | null | undefined} [origin]
|
|
1452
|
+
* @returns {never}
|
|
1453
|
+
*
|
|
1454
|
+
* @param {Error | VFileMessage | string} causeOrReason
|
|
1455
|
+
* Reason for message, should use markdown.
|
|
1456
|
+
* @param {Node | NodeLike | MessageOptions | Point | Position | string | null | undefined} [optionsOrParentOrPlace]
|
|
1457
|
+
* Configuration (optional).
|
|
1458
|
+
* @param {string | null | undefined} [origin]
|
|
1459
|
+
* Place in code where the message originates (example:
|
|
1460
|
+
* `'my-package:my-rule'` or `'my-rule'`).
|
|
1461
|
+
* @returns {never}
|
|
1462
|
+
* Never.
|
|
1463
|
+
* @throws {VFileMessage}
|
|
1464
|
+
* Message.
|
|
1465
|
+
*/
|
|
1466
|
+
fail(causeOrReason, optionsOrParentOrPlace, origin) {
|
|
1467
|
+
// @ts-expect-error: the overloads are fine.
|
|
1468
|
+
const message = this.message(causeOrReason, optionsOrParentOrPlace, origin);
|
|
1469
|
+
message.fatal = true;
|
|
1470
|
+
throw message;
|
|
1471
|
+
}
|
|
1472
|
+
|
|
1473
|
+
/**
|
|
1474
|
+
* Create an info message for `reason` associated with the file.
|
|
1475
|
+
*
|
|
1476
|
+
* The `fatal` field of the message is set to `undefined` (info; change
|
|
1477
|
+
* likely not needed) and the `file` field is set to the current file path.
|
|
1478
|
+
* The message is added to the `messages` field on `file`.
|
|
1479
|
+
*
|
|
1480
|
+
* > 🪦 **Note**: also has obsolete signatures.
|
|
1481
|
+
*
|
|
1482
|
+
* @overload
|
|
1483
|
+
* @param {string} reason
|
|
1484
|
+
* @param {MessageOptions | null | undefined} [options]
|
|
1485
|
+
* @returns {VFileMessage}
|
|
1486
|
+
*
|
|
1487
|
+
* @overload
|
|
1488
|
+
* @param {string} reason
|
|
1489
|
+
* @param {Node | NodeLike | null | undefined} parent
|
|
1490
|
+
* @param {string | null | undefined} [origin]
|
|
1491
|
+
* @returns {VFileMessage}
|
|
1492
|
+
*
|
|
1493
|
+
* @overload
|
|
1494
|
+
* @param {string} reason
|
|
1495
|
+
* @param {Point | Position | null | undefined} place
|
|
1496
|
+
* @param {string | null | undefined} [origin]
|
|
1497
|
+
* @returns {VFileMessage}
|
|
1498
|
+
*
|
|
1499
|
+
* @overload
|
|
1500
|
+
* @param {string} reason
|
|
1501
|
+
* @param {string | null | undefined} [origin]
|
|
1502
|
+
* @returns {VFileMessage}
|
|
1503
|
+
*
|
|
1504
|
+
* @overload
|
|
1505
|
+
* @param {Error | VFileMessage} cause
|
|
1506
|
+
* @param {Node | NodeLike | null | undefined} parent
|
|
1507
|
+
* @param {string | null | undefined} [origin]
|
|
1508
|
+
* @returns {VFileMessage}
|
|
1509
|
+
*
|
|
1510
|
+
* @overload
|
|
1511
|
+
* @param {Error | VFileMessage} cause
|
|
1512
|
+
* @param {Point | Position | null | undefined} place
|
|
1513
|
+
* @param {string | null | undefined} [origin]
|
|
1514
|
+
* @returns {VFileMessage}
|
|
1515
|
+
*
|
|
1516
|
+
* @overload
|
|
1517
|
+
* @param {Error | VFileMessage} cause
|
|
1518
|
+
* @param {string | null | undefined} [origin]
|
|
1519
|
+
* @returns {VFileMessage}
|
|
1520
|
+
*
|
|
1521
|
+
* @param {Error | VFileMessage | string} causeOrReason
|
|
1522
|
+
* Reason for message, should use markdown.
|
|
1523
|
+
* @param {Node | NodeLike | MessageOptions | Point | Position | string | null | undefined} [optionsOrParentOrPlace]
|
|
1524
|
+
* Configuration (optional).
|
|
1525
|
+
* @param {string | null | undefined} [origin]
|
|
1526
|
+
* Place in code where the message originates (example:
|
|
1527
|
+
* `'my-package:my-rule'` or `'my-rule'`).
|
|
1528
|
+
* @returns {VFileMessage}
|
|
1529
|
+
* Message.
|
|
1530
|
+
*/
|
|
1531
|
+
info(causeOrReason, optionsOrParentOrPlace, origin) {
|
|
1532
|
+
// @ts-expect-error: the overloads are fine.
|
|
1533
|
+
const message = this.message(causeOrReason, optionsOrParentOrPlace, origin);
|
|
1534
|
+
message.fatal = undefined;
|
|
1535
|
+
return message;
|
|
1536
|
+
}
|
|
1537
|
+
|
|
1538
|
+
/**
|
|
1539
|
+
* Create a message for `reason` associated with the file.
|
|
1540
|
+
*
|
|
1541
|
+
* The `fatal` field of the message is set to `false` (warning; change may be
|
|
1542
|
+
* needed) and the `file` field is set to the current file path.
|
|
1543
|
+
* The message is added to the `messages` field on `file`.
|
|
1544
|
+
*
|
|
1545
|
+
* > 🪦 **Note**: also has obsolete signatures.
|
|
1546
|
+
*
|
|
1547
|
+
* @overload
|
|
1548
|
+
* @param {string} reason
|
|
1549
|
+
* @param {MessageOptions | null | undefined} [options]
|
|
1550
|
+
* @returns {VFileMessage}
|
|
1551
|
+
*
|
|
1552
|
+
* @overload
|
|
1553
|
+
* @param {string} reason
|
|
1554
|
+
* @param {Node | NodeLike | null | undefined} parent
|
|
1555
|
+
* @param {string | null | undefined} [origin]
|
|
1556
|
+
* @returns {VFileMessage}
|
|
1557
|
+
*
|
|
1558
|
+
* @overload
|
|
1559
|
+
* @param {string} reason
|
|
1560
|
+
* @param {Point | Position | null | undefined} place
|
|
1561
|
+
* @param {string | null | undefined} [origin]
|
|
1562
|
+
* @returns {VFileMessage}
|
|
1563
|
+
*
|
|
1564
|
+
* @overload
|
|
1565
|
+
* @param {string} reason
|
|
1566
|
+
* @param {string | null | undefined} [origin]
|
|
1567
|
+
* @returns {VFileMessage}
|
|
1568
|
+
*
|
|
1569
|
+
* @overload
|
|
1570
|
+
* @param {Error | VFileMessage} cause
|
|
1571
|
+
* @param {Node | NodeLike | null | undefined} parent
|
|
1572
|
+
* @param {string | null | undefined} [origin]
|
|
1573
|
+
* @returns {VFileMessage}
|
|
1574
|
+
*
|
|
1575
|
+
* @overload
|
|
1576
|
+
* @param {Error | VFileMessage} cause
|
|
1577
|
+
* @param {Point | Position | null | undefined} place
|
|
1578
|
+
* @param {string | null | undefined} [origin]
|
|
1579
|
+
* @returns {VFileMessage}
|
|
1580
|
+
*
|
|
1581
|
+
* @overload
|
|
1582
|
+
* @param {Error | VFileMessage} cause
|
|
1583
|
+
* @param {string | null | undefined} [origin]
|
|
1584
|
+
* @returns {VFileMessage}
|
|
1585
|
+
*
|
|
1586
|
+
* @param {Error | VFileMessage | string} causeOrReason
|
|
1587
|
+
* Reason for message, should use markdown.
|
|
1588
|
+
* @param {Node | NodeLike | MessageOptions | Point | Position | string | null | undefined} [optionsOrParentOrPlace]
|
|
1589
|
+
* Configuration (optional).
|
|
1590
|
+
* @param {string | null | undefined} [origin]
|
|
1591
|
+
* Place in code where the message originates (example:
|
|
1592
|
+
* `'my-package:my-rule'` or `'my-rule'`).
|
|
1593
|
+
* @returns {VFileMessage}
|
|
1594
|
+
* Message.
|
|
1595
|
+
*/
|
|
1596
|
+
message(causeOrReason, optionsOrParentOrPlace, origin) {
|
|
1597
|
+
const message = new VFileMessage(
|
|
1598
|
+
// @ts-expect-error: the overloads are fine.
|
|
1599
|
+
causeOrReason, optionsOrParentOrPlace, origin);
|
|
1600
|
+
if (this.path) {
|
|
1601
|
+
message.name = this.path + ':' + message.name;
|
|
1602
|
+
message.file = this.path;
|
|
1603
|
+
}
|
|
1604
|
+
message.fatal = false;
|
|
1605
|
+
this.messages.push(message);
|
|
1606
|
+
return message;
|
|
1607
|
+
}
|
|
1608
|
+
|
|
1609
|
+
/**
|
|
1610
|
+
* Serialize the file.
|
|
1611
|
+
*
|
|
1612
|
+
* > **Note**: which encodings are supported depends on the engine.
|
|
1613
|
+
* > For info on Node.js, see:
|
|
1614
|
+
* > <https://nodejs.org/api/util.html#whatwg-supported-encodings>.
|
|
1615
|
+
*
|
|
1616
|
+
* @param {string | null | undefined} [encoding='utf8']
|
|
1617
|
+
* Character encoding to understand `value` as when it’s a `Uint8Array`
|
|
1618
|
+
* (default: `'utf-8'`).
|
|
1619
|
+
* @returns {string}
|
|
1620
|
+
* Serialized file.
|
|
1621
|
+
*/
|
|
1622
|
+
toString(encoding) {
|
|
1623
|
+
if (this.value === undefined) {
|
|
1624
|
+
return '';
|
|
1625
|
+
}
|
|
1626
|
+
if (typeof this.value === 'string') {
|
|
1627
|
+
return this.value;
|
|
1628
|
+
}
|
|
1629
|
+
const decoder = new TextDecoder(encoding || undefined);
|
|
1630
|
+
return decoder.decode(this.value);
|
|
1631
|
+
}
|
|
1632
|
+
}
|
|
1633
|
+
|
|
1634
|
+
/**
|
|
1635
|
+
* Assert that `part` is not a path (as in, does not contain `path.sep`).
|
|
1636
|
+
*
|
|
1637
|
+
* @param {string | null | undefined} part
|
|
1638
|
+
* File path part.
|
|
1639
|
+
* @param {string} name
|
|
1640
|
+
* Part name.
|
|
1641
|
+
* @returns {undefined}
|
|
1642
|
+
* Nothing.
|
|
1643
|
+
*/
|
|
1644
|
+
function assertPart(part, name) {
|
|
1645
|
+
if (part && part.includes(minpath.sep)) {
|
|
1646
|
+
throw new Error('`' + name + '` cannot be a path: did not expect `' + minpath.sep + '`');
|
|
1647
|
+
}
|
|
1648
|
+
}
|
|
1649
|
+
|
|
1650
|
+
/**
|
|
1651
|
+
* Assert that `part` is not empty.
|
|
1652
|
+
*
|
|
1653
|
+
* @param {string | undefined} part
|
|
1654
|
+
* Thing.
|
|
1655
|
+
* @param {string} name
|
|
1656
|
+
* Part name.
|
|
1657
|
+
* @returns {asserts part is string}
|
|
1658
|
+
* Nothing.
|
|
1659
|
+
*/
|
|
1660
|
+
function assertNonEmpty(part, name) {
|
|
1661
|
+
if (!part) {
|
|
1662
|
+
throw new Error('`' + name + '` cannot be empty');
|
|
1663
|
+
}
|
|
1664
|
+
}
|
|
1665
|
+
|
|
1666
|
+
/**
|
|
1667
|
+
* Assert `path` exists.
|
|
1668
|
+
*
|
|
1669
|
+
* @param {string | undefined} path
|
|
1670
|
+
* Path.
|
|
1671
|
+
* @param {string} name
|
|
1672
|
+
* Dependency name.
|
|
1673
|
+
* @returns {asserts path is string}
|
|
1674
|
+
* Nothing.
|
|
1675
|
+
*/
|
|
1676
|
+
function assertPath(path, name) {
|
|
1677
|
+
if (!path) {
|
|
1678
|
+
throw new Error('Setting `' + name + '` requires `path` to be set too');
|
|
1679
|
+
}
|
|
1680
|
+
}
|
|
1681
|
+
|
|
1682
|
+
/**
|
|
1683
|
+
* Assert `value` is an `Uint8Array`.
|
|
1684
|
+
*
|
|
1685
|
+
* @param {unknown} value
|
|
1686
|
+
* thing.
|
|
1687
|
+
* @returns {value is Uint8Array}
|
|
1688
|
+
* Whether `value` is an `Uint8Array`.
|
|
1689
|
+
*/
|
|
1690
|
+
function isUint8Array$1(value) {
|
|
1691
|
+
return Boolean(value && typeof value === 'object' && 'byteLength' in value && 'byteOffset' in value);
|
|
1692
|
+
}
|
|
1693
|
+
|
|
1694
|
+
const CallableInstance =
|
|
1695
|
+
/**
|
|
1696
|
+
* @type {new <Parameters extends Array<unknown>, Result>(property: string | symbol) => (...parameters: Parameters) => Result}
|
|
1697
|
+
*/
|
|
1698
|
+
|
|
1699
|
+
/** @type {unknown} */
|
|
1700
|
+
|
|
1701
|
+
/**
|
|
1702
|
+
* @this {Function}
|
|
1703
|
+
* @param {string | symbol} property
|
|
1704
|
+
* @returns {(...parameters: Array<unknown>) => unknown}
|
|
1705
|
+
*/
|
|
1706
|
+
function (property) {
|
|
1707
|
+
const self = this;
|
|
1708
|
+
const constr = self.constructor;
|
|
1709
|
+
const proto = /** @type {Record<string | symbol, Function>} */
|
|
1710
|
+
// Prototypes do exist.
|
|
1711
|
+
// type-coverage:ignore-next-line
|
|
1712
|
+
constr.prototype;
|
|
1713
|
+
const value = proto[property];
|
|
1714
|
+
/** @type {(...parameters: Array<unknown>) => unknown} */
|
|
1715
|
+
const apply = function () {
|
|
1716
|
+
return value.apply(apply, arguments);
|
|
1717
|
+
};
|
|
1718
|
+
Object.setPrototypeOf(apply, proto);
|
|
1719
|
+
|
|
1720
|
+
// Not needed for us in `unified`: we only call this on the `copy`
|
|
1721
|
+
// function,
|
|
1722
|
+
// and we don't need to add its fields (`length`, `name`)
|
|
1723
|
+
// over.
|
|
1724
|
+
// See also: GH-246.
|
|
1725
|
+
// const names = Object.getOwnPropertyNames(value)
|
|
1726
|
+
//
|
|
1727
|
+
// for (const p of names) {
|
|
1728
|
+
// const descriptor = Object.getOwnPropertyDescriptor(value, p)
|
|
1729
|
+
// if (descriptor) Object.defineProperty(apply, p, descriptor)
|
|
1730
|
+
// }
|
|
1731
|
+
|
|
1732
|
+
return apply;
|
|
1733
|
+
};
|
|
1734
|
+
|
|
1735
|
+
/**
|
|
1736
|
+
* @typedef {import('trough').Pipeline} Pipeline
|
|
1737
|
+
*
|
|
1738
|
+
* @typedef {import('unist').Node} Node
|
|
1739
|
+
*
|
|
1740
|
+
* @typedef {import('vfile').Compatible} Compatible
|
|
1741
|
+
* @typedef {import('vfile').Value} Value
|
|
1742
|
+
*
|
|
1743
|
+
* @typedef {import('../index.js').CompileResultMap} CompileResultMap
|
|
1744
|
+
* @typedef {import('../index.js').Data} Data
|
|
1745
|
+
* @typedef {import('../index.js').Settings} Settings
|
|
1746
|
+
*/
|
|
1747
|
+
|
|
1748
|
+
|
|
1749
|
+
// To do: next major: drop `Compiler`, `Parser`: prefer lowercase.
|
|
1750
|
+
|
|
1751
|
+
// To do: we could start yielding `never` in TS when a parser is missing and
|
|
1752
|
+
// `parse` is called.
|
|
1753
|
+
// Currently, we allow directly setting `processor.parser`, which is untyped.
|
|
1754
|
+
|
|
1755
|
+
const own = {}.hasOwnProperty;
|
|
1756
|
+
|
|
1757
|
+
/**
|
|
1758
|
+
* @template {Node | undefined} [ParseTree=undefined]
|
|
1759
|
+
* Output of `parse` (optional).
|
|
1760
|
+
* @template {Node | undefined} [HeadTree=undefined]
|
|
1761
|
+
* Input for `run` (optional).
|
|
1762
|
+
* @template {Node | undefined} [TailTree=undefined]
|
|
1763
|
+
* Output for `run` (optional).
|
|
1764
|
+
* @template {Node | undefined} [CompileTree=undefined]
|
|
1765
|
+
* Input of `stringify` (optional).
|
|
1766
|
+
* @template {CompileResults | undefined} [CompileResult=undefined]
|
|
1767
|
+
* Output of `stringify` (optional).
|
|
1768
|
+
* @extends {CallableInstance<[], Processor<ParseTree, HeadTree, TailTree, CompileTree, CompileResult>>}
|
|
1769
|
+
*/
|
|
1770
|
+
class Processor extends CallableInstance {
|
|
1771
|
+
/**
|
|
1772
|
+
* Create a processor.
|
|
1773
|
+
*/
|
|
1774
|
+
constructor() {
|
|
1775
|
+
// If `Processor()` is called (w/o new), `copy` is called instead.
|
|
1776
|
+
super('copy');
|
|
1777
|
+
|
|
1778
|
+
/**
|
|
1779
|
+
* Compiler to use (deprecated).
|
|
1780
|
+
*
|
|
1781
|
+
* @deprecated
|
|
1782
|
+
* Use `compiler` instead.
|
|
1783
|
+
* @type {(
|
|
1784
|
+
* Compiler<
|
|
1785
|
+
* CompileTree extends undefined ? Node : CompileTree,
|
|
1786
|
+
* CompileResult extends undefined ? CompileResults : CompileResult
|
|
1787
|
+
* > |
|
|
1788
|
+
* undefined
|
|
1789
|
+
* )}
|
|
1790
|
+
*/
|
|
1791
|
+
this.Compiler = undefined;
|
|
1792
|
+
|
|
1793
|
+
/**
|
|
1794
|
+
* Parser to use (deprecated).
|
|
1795
|
+
*
|
|
1796
|
+
* @deprecated
|
|
1797
|
+
* Use `parser` instead.
|
|
1798
|
+
* @type {(
|
|
1799
|
+
* Parser<ParseTree extends undefined ? Node : ParseTree> |
|
|
1800
|
+
* undefined
|
|
1801
|
+
* )}
|
|
1802
|
+
*/
|
|
1803
|
+
this.Parser = undefined;
|
|
1804
|
+
|
|
1805
|
+
// Note: the following fields are considered private.
|
|
1806
|
+
// However, they are needed for tests, and TSC generates an untyped
|
|
1807
|
+
// `private freezeIndex` field for, which trips `type-coverage` up.
|
|
1808
|
+
// Instead, we use `@deprecated` to visualize that they shouldn’t be used.
|
|
1809
|
+
/**
|
|
1810
|
+
* Internal list of configured plugins.
|
|
1811
|
+
*
|
|
1812
|
+
* @deprecated
|
|
1813
|
+
* This is a private internal property and should not be used.
|
|
1814
|
+
* @type {Array<PluginTuple<Array<unknown>>>}
|
|
1815
|
+
*/
|
|
1816
|
+
this.attachers = [];
|
|
1817
|
+
|
|
1818
|
+
/**
|
|
1819
|
+
* Compiler to use.
|
|
1820
|
+
*
|
|
1821
|
+
* @type {(
|
|
1822
|
+
* Compiler<
|
|
1823
|
+
* CompileTree extends undefined ? Node : CompileTree,
|
|
1824
|
+
* CompileResult extends undefined ? CompileResults : CompileResult
|
|
1825
|
+
* > |
|
|
1826
|
+
* undefined
|
|
1827
|
+
* )}
|
|
1828
|
+
*/
|
|
1829
|
+
this.compiler = undefined;
|
|
1830
|
+
|
|
1831
|
+
/**
|
|
1832
|
+
* Internal state to track where we are while freezing.
|
|
1833
|
+
*
|
|
1834
|
+
* @deprecated
|
|
1835
|
+
* This is a private internal property and should not be used.
|
|
1836
|
+
* @type {number}
|
|
1837
|
+
*/
|
|
1838
|
+
this.freezeIndex = -1;
|
|
1839
|
+
|
|
1840
|
+
/**
|
|
1841
|
+
* Internal state to track whether we’re frozen.
|
|
1842
|
+
*
|
|
1843
|
+
* @deprecated
|
|
1844
|
+
* This is a private internal property and should not be used.
|
|
1845
|
+
* @type {boolean | undefined}
|
|
1846
|
+
*/
|
|
1847
|
+
this.frozen = undefined;
|
|
1848
|
+
|
|
1849
|
+
/**
|
|
1850
|
+
* Internal state.
|
|
1851
|
+
*
|
|
1852
|
+
* @deprecated
|
|
1853
|
+
* This is a private internal property and should not be used.
|
|
1854
|
+
* @type {Data}
|
|
1855
|
+
*/
|
|
1856
|
+
this.namespace = {};
|
|
1857
|
+
|
|
1858
|
+
/**
|
|
1859
|
+
* Parser to use.
|
|
1860
|
+
*
|
|
1861
|
+
* @type {(
|
|
1862
|
+
* Parser<ParseTree extends undefined ? Node : ParseTree> |
|
|
1863
|
+
* undefined
|
|
1864
|
+
* )}
|
|
1865
|
+
*/
|
|
1866
|
+
this.parser = undefined;
|
|
1867
|
+
|
|
1868
|
+
/**
|
|
1869
|
+
* Internal list of configured transformers.
|
|
1870
|
+
*
|
|
1871
|
+
* @deprecated
|
|
1872
|
+
* This is a private internal property and should not be used.
|
|
1873
|
+
* @type {Pipeline}
|
|
1874
|
+
*/
|
|
1875
|
+
this.transformers = trough();
|
|
1876
|
+
}
|
|
1877
|
+
|
|
1878
|
+
/**
|
|
1879
|
+
* Copy a processor.
|
|
1880
|
+
*
|
|
1881
|
+
* @deprecated
|
|
1882
|
+
* This is a private internal method and should not be used.
|
|
1883
|
+
* @returns {Processor<ParseTree, HeadTree, TailTree, CompileTree, CompileResult>}
|
|
1884
|
+
* New *unfrozen* processor ({@linkcode Processor}) that is
|
|
1885
|
+
* configured to work the same as its ancestor.
|
|
1886
|
+
* When the descendant processor is configured in the future it does not
|
|
1887
|
+
* affect the ancestral processor.
|
|
1888
|
+
*/
|
|
1889
|
+
copy() {
|
|
1890
|
+
// Cast as the type parameters will be the same after attaching.
|
|
1891
|
+
const destination = /** @type {Processor<ParseTree, HeadTree, TailTree, CompileTree, CompileResult>} */
|
|
1892
|
+
new Processor();
|
|
1893
|
+
let index = -1;
|
|
1894
|
+
while (++index < this.attachers.length) {
|
|
1895
|
+
const attacher = this.attachers[index];
|
|
1896
|
+
destination.use(...attacher);
|
|
1897
|
+
}
|
|
1898
|
+
destination.data(extend(true, {}, this.namespace));
|
|
1899
|
+
return destination;
|
|
1900
|
+
}
|
|
1901
|
+
|
|
1902
|
+
/**
|
|
1903
|
+
* Configure the processor with info available to all plugins.
|
|
1904
|
+
* Information is stored in an object.
|
|
1905
|
+
*
|
|
1906
|
+
* Typically, options can be given to a specific plugin, but sometimes it
|
|
1907
|
+
* makes sense to have information shared with several plugins.
|
|
1908
|
+
* For example, a list of HTML elements that are self-closing, which is
|
|
1909
|
+
* needed during all phases.
|
|
1910
|
+
*
|
|
1911
|
+
* > **Note**: setting information cannot occur on *frozen* processors.
|
|
1912
|
+
* > Call the processor first to create a new unfrozen processor.
|
|
1913
|
+
*
|
|
1914
|
+
* > **Note**: to register custom data in TypeScript, augment the
|
|
1915
|
+
* > {@linkcode Data} interface.
|
|
1916
|
+
*
|
|
1917
|
+
* @example
|
|
1918
|
+
* This example show how to get and set info:
|
|
1919
|
+
*
|
|
1920
|
+
* ```js
|
|
1921
|
+
* import {unified} from 'unified'
|
|
1922
|
+
*
|
|
1923
|
+
* const processor = unified().data('alpha', 'bravo')
|
|
1924
|
+
*
|
|
1925
|
+
* processor.data('alpha') // => 'bravo'
|
|
1926
|
+
*
|
|
1927
|
+
* processor.data() // => {alpha: 'bravo'}
|
|
1928
|
+
*
|
|
1929
|
+
* processor.data({charlie: 'delta'})
|
|
1930
|
+
*
|
|
1931
|
+
* processor.data() // => {charlie: 'delta'}
|
|
1932
|
+
* ```
|
|
1933
|
+
*
|
|
1934
|
+
* @template {keyof Data} Key
|
|
1935
|
+
*
|
|
1936
|
+
* @overload
|
|
1937
|
+
* @returns {Data}
|
|
1938
|
+
*
|
|
1939
|
+
* @overload
|
|
1940
|
+
* @param {Data} dataset
|
|
1941
|
+
* @returns {Processor<ParseTree, HeadTree, TailTree, CompileTree, CompileResult>}
|
|
1942
|
+
*
|
|
1943
|
+
* @overload
|
|
1944
|
+
* @param {Key} key
|
|
1945
|
+
* @returns {Data[Key]}
|
|
1946
|
+
*
|
|
1947
|
+
* @overload
|
|
1948
|
+
* @param {Key} key
|
|
1949
|
+
* @param {Data[Key]} value
|
|
1950
|
+
* @returns {Processor<ParseTree, HeadTree, TailTree, CompileTree, CompileResult>}
|
|
1951
|
+
*
|
|
1952
|
+
* @param {Data | Key} [key]
|
|
1953
|
+
* Key to get or set, or entire dataset to set, or nothing to get the
|
|
1954
|
+
* entire dataset (optional).
|
|
1955
|
+
* @param {Data[Key]} [value]
|
|
1956
|
+
* Value to set (optional).
|
|
1957
|
+
* @returns {unknown}
|
|
1958
|
+
* The current processor when setting, the value at `key` when getting, or
|
|
1959
|
+
* the entire dataset when getting without key.
|
|
1960
|
+
*/
|
|
1961
|
+
data(key, value) {
|
|
1962
|
+
if (typeof key === 'string') {
|
|
1963
|
+
// Set `key`.
|
|
1964
|
+
if (arguments.length === 2) {
|
|
1965
|
+
assertUnfrozen('data', this.frozen);
|
|
1966
|
+
this.namespace[key] = value;
|
|
1967
|
+
return this;
|
|
1968
|
+
}
|
|
1969
|
+
|
|
1970
|
+
// Get `key`.
|
|
1971
|
+
return own.call(this.namespace, key) && this.namespace[key] || undefined;
|
|
1972
|
+
}
|
|
1973
|
+
|
|
1974
|
+
// Set space.
|
|
1975
|
+
if (key) {
|
|
1976
|
+
assertUnfrozen('data', this.frozen);
|
|
1977
|
+
this.namespace = key;
|
|
1978
|
+
return this;
|
|
1979
|
+
}
|
|
1980
|
+
|
|
1981
|
+
// Get space.
|
|
1982
|
+
return this.namespace;
|
|
1983
|
+
}
|
|
1984
|
+
|
|
1985
|
+
/**
|
|
1986
|
+
* Freeze a processor.
|
|
1987
|
+
*
|
|
1988
|
+
* Frozen processors are meant to be extended and not to be configured
|
|
1989
|
+
* directly.
|
|
1990
|
+
*
|
|
1991
|
+
* When a processor is frozen it cannot be unfrozen.
|
|
1992
|
+
* New processors working the same way can be created by calling the
|
|
1993
|
+
* processor.
|
|
1994
|
+
*
|
|
1995
|
+
* It’s possible to freeze processors explicitly by calling `.freeze()`.
|
|
1996
|
+
* Processors freeze automatically when `.parse()`, `.run()`, `.runSync()`,
|
|
1997
|
+
* `.stringify()`, `.process()`, or `.processSync()` are called.
|
|
1998
|
+
*
|
|
1999
|
+
* @returns {Processor<ParseTree, HeadTree, TailTree, CompileTree, CompileResult>}
|
|
2000
|
+
* The current processor.
|
|
2001
|
+
*/
|
|
2002
|
+
freeze() {
|
|
2003
|
+
if (this.frozen) {
|
|
2004
|
+
return this;
|
|
2005
|
+
}
|
|
2006
|
+
|
|
2007
|
+
// Cast so that we can type plugins easier.
|
|
2008
|
+
// Plugins are supposed to be usable on different processors, not just on
|
|
2009
|
+
// this exact processor.
|
|
2010
|
+
const self = /** @type {Processor} */ /** @type {unknown} */this;
|
|
2011
|
+
while (++this.freezeIndex < this.attachers.length) {
|
|
2012
|
+
const [attacher, ...options] = this.attachers[this.freezeIndex];
|
|
2013
|
+
if (options[0] === false) {
|
|
2014
|
+
continue;
|
|
2015
|
+
}
|
|
2016
|
+
if (options[0] === true) {
|
|
2017
|
+
options[0] = undefined;
|
|
2018
|
+
}
|
|
2019
|
+
const transformer = attacher.call(self, ...options);
|
|
2020
|
+
if (typeof transformer === 'function') {
|
|
2021
|
+
this.transformers.use(transformer);
|
|
2022
|
+
}
|
|
2023
|
+
}
|
|
2024
|
+
this.frozen = true;
|
|
2025
|
+
this.freezeIndex = Number.POSITIVE_INFINITY;
|
|
2026
|
+
return this;
|
|
2027
|
+
}
|
|
2028
|
+
|
|
2029
|
+
/**
|
|
2030
|
+
* Parse text to a syntax tree.
|
|
2031
|
+
*
|
|
2032
|
+
* > **Note**: `parse` freezes the processor if not already *frozen*.
|
|
2033
|
+
*
|
|
2034
|
+
* > **Note**: `parse` performs the parse phase, not the run phase or other
|
|
2035
|
+
* > phases.
|
|
2036
|
+
*
|
|
2037
|
+
* @param {Compatible | undefined} [file]
|
|
2038
|
+
* file to parse (optional); typically `string` or `VFile`; any value
|
|
2039
|
+
* accepted as `x` in `new VFile(x)`.
|
|
2040
|
+
* @returns {ParseTree extends undefined ? Node : ParseTree}
|
|
2041
|
+
* Syntax tree representing `file`.
|
|
2042
|
+
*/
|
|
2043
|
+
parse(file) {
|
|
2044
|
+
this.freeze();
|
|
2045
|
+
const realFile = vfile(file);
|
|
2046
|
+
const parser = this.parser || this.Parser;
|
|
2047
|
+
assertParser('parse', parser);
|
|
2048
|
+
return parser(String(realFile), realFile);
|
|
2049
|
+
}
|
|
2050
|
+
|
|
2051
|
+
/**
|
|
2052
|
+
* Process the given file as configured on the processor.
|
|
2053
|
+
*
|
|
2054
|
+
* > **Note**: `process` freezes the processor if not already *frozen*.
|
|
2055
|
+
*
|
|
2056
|
+
* > **Note**: `process` performs the parse, run, and stringify phases.
|
|
2057
|
+
*
|
|
2058
|
+
* @overload
|
|
2059
|
+
* @param {Compatible | undefined} file
|
|
2060
|
+
* @param {ProcessCallback<VFileWithOutput<CompileResult>>} done
|
|
2061
|
+
* @returns {undefined}
|
|
2062
|
+
*
|
|
2063
|
+
* @overload
|
|
2064
|
+
* @param {Compatible | undefined} [file]
|
|
2065
|
+
* @returns {Promise<VFileWithOutput<CompileResult>>}
|
|
2066
|
+
*
|
|
2067
|
+
* @param {Compatible | undefined} [file]
|
|
2068
|
+
* File (optional); typically `string` or `VFile`]; any value accepted as
|
|
2069
|
+
* `x` in `new VFile(x)`.
|
|
2070
|
+
* @param {ProcessCallback<VFileWithOutput<CompileResult>> | undefined} [done]
|
|
2071
|
+
* Callback (optional).
|
|
2072
|
+
* @returns {Promise<VFile> | undefined}
|
|
2073
|
+
* Nothing if `done` is given.
|
|
2074
|
+
* Otherwise a promise, rejected with a fatal error or resolved with the
|
|
2075
|
+
* processed file.
|
|
2076
|
+
*
|
|
2077
|
+
* The parsed, transformed, and compiled value is available at
|
|
2078
|
+
* `file.value` (see note).
|
|
2079
|
+
*
|
|
2080
|
+
* > **Note**: unified typically compiles by serializing: most
|
|
2081
|
+
* > compilers return `string` (or `Uint8Array`).
|
|
2082
|
+
* > Some compilers, such as the one configured with
|
|
2083
|
+
* > [`rehype-react`][rehype-react], return other values (in this case, a
|
|
2084
|
+
* > React tree).
|
|
2085
|
+
* > If you’re using a compiler that doesn’t serialize, expect different
|
|
2086
|
+
* > result values.
|
|
2087
|
+
* >
|
|
2088
|
+
* > To register custom results in TypeScript, add them to
|
|
2089
|
+
* > {@linkcode CompileResultMap}.
|
|
2090
|
+
*
|
|
2091
|
+
* [rehype-react]: https://github.com/rehypejs/rehype-react
|
|
2092
|
+
*/
|
|
2093
|
+
process(file, done) {
|
|
2094
|
+
const self = this;
|
|
2095
|
+
this.freeze();
|
|
2096
|
+
assertParser('process', this.parser || this.Parser);
|
|
2097
|
+
assertCompiler('process', this.compiler || this.Compiler);
|
|
2098
|
+
return done ? executor(undefined, done) : new Promise(executor);
|
|
2099
|
+
|
|
2100
|
+
// Note: `void`s needed for TS.
|
|
2101
|
+
/**
|
|
2102
|
+
* @param {((file: VFileWithOutput<CompileResult>) => undefined | void) | undefined} resolve
|
|
2103
|
+
* @param {(error: Error | undefined) => undefined | void} reject
|
|
2104
|
+
* @returns {undefined}
|
|
2105
|
+
*/
|
|
2106
|
+
function executor(resolve, reject) {
|
|
2107
|
+
const realFile = vfile(file);
|
|
2108
|
+
// Assume `ParseTree` (the result of the parser) matches `HeadTree` (the
|
|
2109
|
+
// input of the first transform).
|
|
2110
|
+
const parseTree = /** @type {HeadTree extends undefined ? Node : HeadTree} */
|
|
2111
|
+
/** @type {unknown} */self.parse(realFile);
|
|
2112
|
+
self.run(parseTree, realFile, function (error, tree, file) {
|
|
2113
|
+
if (error || !tree || !file) {
|
|
2114
|
+
return realDone(error);
|
|
2115
|
+
}
|
|
2116
|
+
|
|
2117
|
+
// Assume `TailTree` (the output of the last transform) matches
|
|
2118
|
+
// `CompileTree` (the input of the compiler).
|
|
2119
|
+
const compileTree = /** @type {CompileTree extends undefined ? Node : CompileTree} */
|
|
2120
|
+
/** @type {unknown} */tree;
|
|
2121
|
+
const compileResult = self.stringify(compileTree, file);
|
|
2122
|
+
if (looksLikeAValue(compileResult)) {
|
|
2123
|
+
file.value = compileResult;
|
|
2124
|
+
} else {
|
|
2125
|
+
file.result = compileResult;
|
|
2126
|
+
}
|
|
2127
|
+
realDone(error, /** @type {VFileWithOutput<CompileResult>} */file);
|
|
2128
|
+
});
|
|
2129
|
+
|
|
2130
|
+
/**
|
|
2131
|
+
* @param {Error | undefined} error
|
|
2132
|
+
* @param {VFileWithOutput<CompileResult> | undefined} [file]
|
|
2133
|
+
* @returns {undefined}
|
|
2134
|
+
*/
|
|
2135
|
+
function realDone(error, file) {
|
|
2136
|
+
if (error || !file) {
|
|
2137
|
+
reject(error);
|
|
2138
|
+
} else if (resolve) {
|
|
2139
|
+
resolve(file);
|
|
2140
|
+
} else {
|
|
2141
|
+
done(undefined, file);
|
|
2142
|
+
}
|
|
2143
|
+
}
|
|
2144
|
+
}
|
|
2145
|
+
}
|
|
2146
|
+
|
|
2147
|
+
/**
|
|
2148
|
+
* Process the given file as configured on the processor.
|
|
2149
|
+
*
|
|
2150
|
+
* An error is thrown if asynchronous transforms are configured.
|
|
2151
|
+
*
|
|
2152
|
+
* > **Note**: `processSync` freezes the processor if not already *frozen*.
|
|
2153
|
+
*
|
|
2154
|
+
* > **Note**: `processSync` performs the parse, run, and stringify phases.
|
|
2155
|
+
*
|
|
2156
|
+
* @param {Compatible | undefined} [file]
|
|
2157
|
+
* File (optional); typically `string` or `VFile`; any value accepted as
|
|
2158
|
+
* `x` in `new VFile(x)`.
|
|
2159
|
+
* @returns {VFileWithOutput<CompileResult>}
|
|
2160
|
+
* The processed file.
|
|
2161
|
+
*
|
|
2162
|
+
* The parsed, transformed, and compiled value is available at
|
|
2163
|
+
* `file.value` (see note).
|
|
2164
|
+
*
|
|
2165
|
+
* > **Note**: unified typically compiles by serializing: most
|
|
2166
|
+
* > compilers return `string` (or `Uint8Array`).
|
|
2167
|
+
* > Some compilers, such as the one configured with
|
|
2168
|
+
* > [`rehype-react`][rehype-react], return other values (in this case, a
|
|
2169
|
+
* > React tree).
|
|
2170
|
+
* > If you’re using a compiler that doesn’t serialize, expect different
|
|
2171
|
+
* > result values.
|
|
2172
|
+
* >
|
|
2173
|
+
* > To register custom results in TypeScript, add them to
|
|
2174
|
+
* > {@linkcode CompileResultMap}.
|
|
2175
|
+
*
|
|
2176
|
+
* [rehype-react]: https://github.com/rehypejs/rehype-react
|
|
2177
|
+
*/
|
|
2178
|
+
processSync(file) {
|
|
2179
|
+
/** @type {boolean} */
|
|
2180
|
+
let complete = false;
|
|
2181
|
+
/** @type {VFileWithOutput<CompileResult> | undefined} */
|
|
2182
|
+
let result;
|
|
2183
|
+
this.freeze();
|
|
2184
|
+
assertParser('processSync', this.parser || this.Parser);
|
|
2185
|
+
assertCompiler('processSync', this.compiler || this.Compiler);
|
|
2186
|
+
this.process(file, realDone);
|
|
2187
|
+
assertDone('processSync', 'process', complete);
|
|
2188
|
+
return result;
|
|
2189
|
+
|
|
2190
|
+
/**
|
|
2191
|
+
* @type {ProcessCallback<VFileWithOutput<CompileResult>>}
|
|
2192
|
+
*/
|
|
2193
|
+
function realDone(error, file) {
|
|
2194
|
+
complete = true;
|
|
2195
|
+
bail(error);
|
|
2196
|
+
result = file;
|
|
2197
|
+
}
|
|
2198
|
+
}
|
|
2199
|
+
|
|
2200
|
+
/**
|
|
2201
|
+
* Run *transformers* on a syntax tree.
|
|
2202
|
+
*
|
|
2203
|
+
* > **Note**: `run` freezes the processor if not already *frozen*.
|
|
2204
|
+
*
|
|
2205
|
+
* > **Note**: `run` performs the run phase, not other phases.
|
|
2206
|
+
*
|
|
2207
|
+
* @overload
|
|
2208
|
+
* @param {HeadTree extends undefined ? Node : HeadTree} tree
|
|
2209
|
+
* @param {RunCallback<TailTree extends undefined ? Node : TailTree>} done
|
|
2210
|
+
* @returns {undefined}
|
|
2211
|
+
*
|
|
2212
|
+
* @overload
|
|
2213
|
+
* @param {HeadTree extends undefined ? Node : HeadTree} tree
|
|
2214
|
+
* @param {Compatible | undefined} file
|
|
2215
|
+
* @param {RunCallback<TailTree extends undefined ? Node : TailTree>} done
|
|
2216
|
+
* @returns {undefined}
|
|
2217
|
+
*
|
|
2218
|
+
* @overload
|
|
2219
|
+
* @param {HeadTree extends undefined ? Node : HeadTree} tree
|
|
2220
|
+
* @param {Compatible | undefined} [file]
|
|
2221
|
+
* @returns {Promise<TailTree extends undefined ? Node : TailTree>}
|
|
2222
|
+
*
|
|
2223
|
+
* @param {HeadTree extends undefined ? Node : HeadTree} tree
|
|
2224
|
+
* Tree to transform and inspect.
|
|
2225
|
+
* @param {(
|
|
2226
|
+
* RunCallback<TailTree extends undefined ? Node : TailTree> |
|
|
2227
|
+
* Compatible
|
|
2228
|
+
* )} [file]
|
|
2229
|
+
* File associated with `node` (optional); any value accepted as `x` in
|
|
2230
|
+
* `new VFile(x)`.
|
|
2231
|
+
* @param {RunCallback<TailTree extends undefined ? Node : TailTree>} [done]
|
|
2232
|
+
* Callback (optional).
|
|
2233
|
+
* @returns {Promise<TailTree extends undefined ? Node : TailTree> | undefined}
|
|
2234
|
+
* Nothing if `done` is given.
|
|
2235
|
+
* Otherwise, a promise rejected with a fatal error or resolved with the
|
|
2236
|
+
* transformed tree.
|
|
2237
|
+
*/
|
|
2238
|
+
run(tree, file, done) {
|
|
2239
|
+
assertNode(tree);
|
|
2240
|
+
this.freeze();
|
|
2241
|
+
const transformers = this.transformers;
|
|
2242
|
+
if (!done && typeof file === 'function') {
|
|
2243
|
+
done = file;
|
|
2244
|
+
file = undefined;
|
|
2245
|
+
}
|
|
2246
|
+
return done ? executor(undefined, done) : new Promise(executor);
|
|
2247
|
+
|
|
2248
|
+
// Note: `void`s needed for TS.
|
|
2249
|
+
/**
|
|
2250
|
+
* @param {(
|
|
2251
|
+
* ((tree: TailTree extends undefined ? Node : TailTree) => undefined | void) |
|
|
2252
|
+
* undefined
|
|
2253
|
+
* )} resolve
|
|
2254
|
+
* @param {(error: Error) => undefined | void} reject
|
|
2255
|
+
* @returns {undefined}
|
|
2256
|
+
*/
|
|
2257
|
+
function executor(resolve, reject) {
|
|
2258
|
+
const realFile = vfile(file);
|
|
2259
|
+
transformers.run(tree, realFile, realDone);
|
|
2260
|
+
|
|
2261
|
+
/**
|
|
2262
|
+
* @param {Error | undefined} error
|
|
2263
|
+
* @param {Node} outputTree
|
|
2264
|
+
* @param {VFile} file
|
|
2265
|
+
* @returns {undefined}
|
|
2266
|
+
*/
|
|
2267
|
+
function realDone(error, outputTree, file) {
|
|
2268
|
+
const resultingTree = /** @type {TailTree extends undefined ? Node : TailTree} */
|
|
2269
|
+
outputTree || tree;
|
|
2270
|
+
if (error) {
|
|
2271
|
+
reject(error);
|
|
2272
|
+
} else if (resolve) {
|
|
2273
|
+
resolve(resultingTree);
|
|
2274
|
+
} else {
|
|
2275
|
+
done(undefined, resultingTree, file);
|
|
2276
|
+
}
|
|
2277
|
+
}
|
|
2278
|
+
}
|
|
2279
|
+
}
|
|
2280
|
+
|
|
2281
|
+
/**
|
|
2282
|
+
* Run *transformers* on a syntax tree.
|
|
2283
|
+
*
|
|
2284
|
+
* An error is thrown if asynchronous transforms are configured.
|
|
2285
|
+
*
|
|
2286
|
+
* > **Note**: `runSync` freezes the processor if not already *frozen*.
|
|
2287
|
+
*
|
|
2288
|
+
* > **Note**: `runSync` performs the run phase, not other phases.
|
|
2289
|
+
*
|
|
2290
|
+
* @param {HeadTree extends undefined ? Node : HeadTree} tree
|
|
2291
|
+
* Tree to transform and inspect.
|
|
2292
|
+
* @param {Compatible | undefined} [file]
|
|
2293
|
+
* File associated with `node` (optional); any value accepted as `x` in
|
|
2294
|
+
* `new VFile(x)`.
|
|
2295
|
+
* @returns {TailTree extends undefined ? Node : TailTree}
|
|
2296
|
+
* Transformed tree.
|
|
2297
|
+
*/
|
|
2298
|
+
runSync(tree, file) {
|
|
2299
|
+
/** @type {boolean} */
|
|
2300
|
+
let complete = false;
|
|
2301
|
+
/** @type {(TailTree extends undefined ? Node : TailTree) | undefined} */
|
|
2302
|
+
let result;
|
|
2303
|
+
this.run(tree, file, realDone);
|
|
2304
|
+
assertDone('runSync', 'run', complete);
|
|
2305
|
+
return result;
|
|
2306
|
+
|
|
2307
|
+
/**
|
|
2308
|
+
* @type {RunCallback<TailTree extends undefined ? Node : TailTree>}
|
|
2309
|
+
*/
|
|
2310
|
+
function realDone(error, tree) {
|
|
2311
|
+
bail(error);
|
|
2312
|
+
result = tree;
|
|
2313
|
+
complete = true;
|
|
2314
|
+
}
|
|
2315
|
+
}
|
|
2316
|
+
|
|
2317
|
+
/**
|
|
2318
|
+
* Compile a syntax tree.
|
|
2319
|
+
*
|
|
2320
|
+
* > **Note**: `stringify` freezes the processor if not already *frozen*.
|
|
2321
|
+
*
|
|
2322
|
+
* > **Note**: `stringify` performs the stringify phase, not the run phase
|
|
2323
|
+
* > or other phases.
|
|
2324
|
+
*
|
|
2325
|
+
* @param {CompileTree extends undefined ? Node : CompileTree} tree
|
|
2326
|
+
* Tree to compile.
|
|
2327
|
+
* @param {Compatible | undefined} [file]
|
|
2328
|
+
* File associated with `node` (optional); any value accepted as `x` in
|
|
2329
|
+
* `new VFile(x)`.
|
|
2330
|
+
* @returns {CompileResult extends undefined ? Value : CompileResult}
|
|
2331
|
+
* Textual representation of the tree (see note).
|
|
2332
|
+
*
|
|
2333
|
+
* > **Note**: unified typically compiles by serializing: most compilers
|
|
2334
|
+
* > return `string` (or `Uint8Array`).
|
|
2335
|
+
* > Some compilers, such as the one configured with
|
|
2336
|
+
* > [`rehype-react`][rehype-react], return other values (in this case, a
|
|
2337
|
+
* > React tree).
|
|
2338
|
+
* > If you’re using a compiler that doesn’t serialize, expect different
|
|
2339
|
+
* > result values.
|
|
2340
|
+
* >
|
|
2341
|
+
* > To register custom results in TypeScript, add them to
|
|
2342
|
+
* > {@linkcode CompileResultMap}.
|
|
2343
|
+
*
|
|
2344
|
+
* [rehype-react]: https://github.com/rehypejs/rehype-react
|
|
2345
|
+
*/
|
|
2346
|
+
stringify(tree, file) {
|
|
2347
|
+
this.freeze();
|
|
2348
|
+
const realFile = vfile(file);
|
|
2349
|
+
const compiler = this.compiler || this.Compiler;
|
|
2350
|
+
assertCompiler('stringify', compiler);
|
|
2351
|
+
assertNode(tree);
|
|
2352
|
+
return compiler(tree, realFile);
|
|
2353
|
+
}
|
|
2354
|
+
|
|
2355
|
+
/**
|
|
2356
|
+
* Configure the processor to use a plugin, a list of usable values, or a
|
|
2357
|
+
* preset.
|
|
2358
|
+
*
|
|
2359
|
+
* If the processor is already using a plugin, the previous plugin
|
|
2360
|
+
* configuration is changed based on the options that are passed in.
|
|
2361
|
+
* In other words, the plugin is not added a second time.
|
|
2362
|
+
*
|
|
2363
|
+
* > **Note**: `use` cannot be called on *frozen* processors.
|
|
2364
|
+
* > Call the processor first to create a new unfrozen processor.
|
|
2365
|
+
*
|
|
2366
|
+
* @example
|
|
2367
|
+
* There are many ways to pass plugins to `.use()`.
|
|
2368
|
+
* This example gives an overview:
|
|
2369
|
+
*
|
|
2370
|
+
* ```js
|
|
2371
|
+
* import {unified} from 'unified'
|
|
2372
|
+
*
|
|
2373
|
+
* unified()
|
|
2374
|
+
* // Plugin with options:
|
|
2375
|
+
* .use(pluginA, {x: true, y: true})
|
|
2376
|
+
* // Passing the same plugin again merges configuration (to `{x: true, y: false, z: true}`):
|
|
2377
|
+
* .use(pluginA, {y: false, z: true})
|
|
2378
|
+
* // Plugins:
|
|
2379
|
+
* .use([pluginB, pluginC])
|
|
2380
|
+
* // Two plugins, the second with options:
|
|
2381
|
+
* .use([pluginD, [pluginE, {}]])
|
|
2382
|
+
* // Preset with plugins and settings:
|
|
2383
|
+
* .use({plugins: [pluginF, [pluginG, {}]], settings: {position: false}})
|
|
2384
|
+
* // Settings only:
|
|
2385
|
+
* .use({settings: {position: false}})
|
|
2386
|
+
* ```
|
|
2387
|
+
*
|
|
2388
|
+
* @template {Array<unknown>} [Parameters=[]]
|
|
2389
|
+
* @template {Node | string | undefined} [Input=undefined]
|
|
2390
|
+
* @template [Output=Input]
|
|
2391
|
+
*
|
|
2392
|
+
* @overload
|
|
2393
|
+
* @param {Preset | null | undefined} [preset]
|
|
2394
|
+
* @returns {Processor<ParseTree, HeadTree, TailTree, CompileTree, CompileResult>}
|
|
2395
|
+
*
|
|
2396
|
+
* @overload
|
|
2397
|
+
* @param {PluggableList} list
|
|
2398
|
+
* @returns {Processor<ParseTree, HeadTree, TailTree, CompileTree, CompileResult>}
|
|
2399
|
+
*
|
|
2400
|
+
* @overload
|
|
2401
|
+
* @param {Plugin<Parameters, Input, Output>} plugin
|
|
2402
|
+
* @param {...(Parameters | [boolean])} parameters
|
|
2403
|
+
* @returns {UsePlugin<ParseTree, HeadTree, TailTree, CompileTree, CompileResult, Input, Output>}
|
|
2404
|
+
*
|
|
2405
|
+
* @param {PluggableList | Plugin | Preset | null | undefined} value
|
|
2406
|
+
* Usable value.
|
|
2407
|
+
* @param {...unknown} parameters
|
|
2408
|
+
* Parameters, when a plugin is given as a usable value.
|
|
2409
|
+
* @returns {Processor<ParseTree, HeadTree, TailTree, CompileTree, CompileResult>}
|
|
2410
|
+
* Current processor.
|
|
2411
|
+
*/
|
|
2412
|
+
use(value, ...parameters) {
|
|
2413
|
+
const attachers = this.attachers;
|
|
2414
|
+
const namespace = this.namespace;
|
|
2415
|
+
assertUnfrozen('use', this.frozen);
|
|
2416
|
+
if (value === null || value === undefined) ; else if (typeof value === 'function') {
|
|
2417
|
+
addPlugin(value, parameters);
|
|
2418
|
+
} else if (typeof value === 'object') {
|
|
2419
|
+
if (Array.isArray(value)) {
|
|
2420
|
+
addList(value);
|
|
2421
|
+
} else {
|
|
2422
|
+
addPreset(value);
|
|
2423
|
+
}
|
|
2424
|
+
} else {
|
|
2425
|
+
throw new TypeError('Expected usable value, not `' + value + '`');
|
|
2426
|
+
}
|
|
2427
|
+
return this;
|
|
2428
|
+
|
|
2429
|
+
/**
|
|
2430
|
+
* @param {Pluggable} value
|
|
2431
|
+
* @returns {undefined}
|
|
2432
|
+
*/
|
|
2433
|
+
function add(value) {
|
|
2434
|
+
if (typeof value === 'function') {
|
|
2435
|
+
addPlugin(value, []);
|
|
2436
|
+
} else if (typeof value === 'object') {
|
|
2437
|
+
if (Array.isArray(value)) {
|
|
2438
|
+
const [plugin, ...parameters] = /** @type {PluginTuple<Array<unknown>>} */value;
|
|
2439
|
+
addPlugin(plugin, parameters);
|
|
2440
|
+
} else {
|
|
2441
|
+
addPreset(value);
|
|
2442
|
+
}
|
|
2443
|
+
} else {
|
|
2444
|
+
throw new TypeError('Expected usable value, not `' + value + '`');
|
|
2445
|
+
}
|
|
2446
|
+
}
|
|
2447
|
+
|
|
2448
|
+
/**
|
|
2449
|
+
* @param {Preset} result
|
|
2450
|
+
* @returns {undefined}
|
|
2451
|
+
*/
|
|
2452
|
+
function addPreset(result) {
|
|
2453
|
+
if (!('plugins' in result) && !('settings' in result)) {
|
|
2454
|
+
throw new Error('Expected usable value but received an empty preset, which is probably a mistake: presets typically come with `plugins` and sometimes with `settings`, but this has neither');
|
|
2455
|
+
}
|
|
2456
|
+
addList(result.plugins);
|
|
2457
|
+
if (result.settings) {
|
|
2458
|
+
namespace.settings = extend(true, namespace.settings, result.settings);
|
|
2459
|
+
}
|
|
2460
|
+
}
|
|
2461
|
+
|
|
2462
|
+
/**
|
|
2463
|
+
* @param {PluggableList | null | undefined} plugins
|
|
2464
|
+
* @returns {undefined}
|
|
2465
|
+
*/
|
|
2466
|
+
function addList(plugins) {
|
|
2467
|
+
let index = -1;
|
|
2468
|
+
if (plugins === null || plugins === undefined) ; else if (Array.isArray(plugins)) {
|
|
2469
|
+
while (++index < plugins.length) {
|
|
2470
|
+
const thing = plugins[index];
|
|
2471
|
+
add(thing);
|
|
2472
|
+
}
|
|
2473
|
+
} else {
|
|
2474
|
+
throw new TypeError('Expected a list of plugins, not `' + plugins + '`');
|
|
2475
|
+
}
|
|
2476
|
+
}
|
|
2477
|
+
|
|
2478
|
+
/**
|
|
2479
|
+
* @param {Plugin} plugin
|
|
2480
|
+
* @param {Array<unknown>} parameters
|
|
2481
|
+
* @returns {undefined}
|
|
2482
|
+
*/
|
|
2483
|
+
function addPlugin(plugin, parameters) {
|
|
2484
|
+
let index = -1;
|
|
2485
|
+
let entryIndex = -1;
|
|
2486
|
+
while (++index < attachers.length) {
|
|
2487
|
+
if (attachers[index][0] === plugin) {
|
|
2488
|
+
entryIndex = index;
|
|
2489
|
+
break;
|
|
2490
|
+
}
|
|
2491
|
+
}
|
|
2492
|
+
if (entryIndex === -1) {
|
|
2493
|
+
attachers.push([plugin, ...parameters]);
|
|
2494
|
+
}
|
|
2495
|
+
// Only set if there was at least a `primary` value, otherwise we’d change
|
|
2496
|
+
// `arguments.length`.
|
|
2497
|
+
else if (parameters.length > 0) {
|
|
2498
|
+
let [primary, ...rest] = parameters;
|
|
2499
|
+
const currentPrimary = attachers[entryIndex][1];
|
|
2500
|
+
if (isPlainObject(currentPrimary) && isPlainObject(primary)) {
|
|
2501
|
+
primary = extend(true, currentPrimary, primary);
|
|
2502
|
+
}
|
|
2503
|
+
attachers[entryIndex] = [plugin, primary, ...rest];
|
|
2504
|
+
}
|
|
2505
|
+
}
|
|
2506
|
+
}
|
|
2507
|
+
}
|
|
2508
|
+
|
|
2509
|
+
// Note: this returns a *callable* instance.
|
|
2510
|
+
// That’s why it’s documented as a function.
|
|
2511
|
+
/**
|
|
2512
|
+
* Create a new processor.
|
|
2513
|
+
*
|
|
2514
|
+
* @example
|
|
2515
|
+
* This example shows how a new processor can be created (from `remark`) and linked
|
|
2516
|
+
* to **stdin**(4) and **stdout**(4).
|
|
2517
|
+
*
|
|
2518
|
+
* ```js
|
|
2519
|
+
* import process from 'node:process'
|
|
2520
|
+
* import concatStream from 'concat-stream'
|
|
2521
|
+
* import {remark} from 'remark'
|
|
2522
|
+
*
|
|
2523
|
+
* process.stdin.pipe(
|
|
2524
|
+
* concatStream(function (buf) {
|
|
2525
|
+
* process.stdout.write(String(remark().processSync(buf)))
|
|
2526
|
+
* })
|
|
2527
|
+
* )
|
|
2528
|
+
* ```
|
|
2529
|
+
*
|
|
2530
|
+
* @returns
|
|
2531
|
+
* New *unfrozen* processor (`processor`).
|
|
2532
|
+
*
|
|
2533
|
+
* This processor is configured to work the same as its ancestor.
|
|
2534
|
+
* When the descendant processor is configured in the future it does not
|
|
2535
|
+
* affect the ancestral processor.
|
|
2536
|
+
*/
|
|
2537
|
+
const unified = new Processor().freeze();
|
|
2538
|
+
|
|
2539
|
+
/**
|
|
2540
|
+
* Assert a parser is available.
|
|
2541
|
+
*
|
|
2542
|
+
* @param {string} name
|
|
2543
|
+
* @param {unknown} value
|
|
2544
|
+
* @returns {asserts value is Parser}
|
|
2545
|
+
*/
|
|
2546
|
+
function assertParser(name, value) {
|
|
2547
|
+
if (typeof value !== 'function') {
|
|
2548
|
+
throw new TypeError('Cannot `' + name + '` without `parser`');
|
|
2549
|
+
}
|
|
2550
|
+
}
|
|
2551
|
+
|
|
2552
|
+
/**
|
|
2553
|
+
* Assert a compiler is available.
|
|
2554
|
+
*
|
|
2555
|
+
* @param {string} name
|
|
2556
|
+
* @param {unknown} value
|
|
2557
|
+
* @returns {asserts value is Compiler}
|
|
2558
|
+
*/
|
|
2559
|
+
function assertCompiler(name, value) {
|
|
2560
|
+
if (typeof value !== 'function') {
|
|
2561
|
+
throw new TypeError('Cannot `' + name + '` without `compiler`');
|
|
2562
|
+
}
|
|
2563
|
+
}
|
|
2564
|
+
|
|
2565
|
+
/**
|
|
2566
|
+
* Assert the processor is not frozen.
|
|
2567
|
+
*
|
|
2568
|
+
* @param {string} name
|
|
2569
|
+
* @param {unknown} frozen
|
|
2570
|
+
* @returns {asserts frozen is false}
|
|
2571
|
+
*/
|
|
2572
|
+
function assertUnfrozen(name, frozen) {
|
|
2573
|
+
if (frozen) {
|
|
2574
|
+
throw new Error('Cannot call `' + name + '` on a frozen processor.\nCreate a new processor first, by calling it: use `processor()` instead of `processor`.');
|
|
2575
|
+
}
|
|
2576
|
+
}
|
|
2577
|
+
|
|
2578
|
+
/**
|
|
2579
|
+
* Assert `node` is a unist node.
|
|
2580
|
+
*
|
|
2581
|
+
* @param {unknown} node
|
|
2582
|
+
* @returns {asserts node is Node}
|
|
2583
|
+
*/
|
|
2584
|
+
function assertNode(node) {
|
|
2585
|
+
// `isPlainObj` unfortunately uses `any` instead of `unknown`.
|
|
2586
|
+
// type-coverage:ignore-next-line
|
|
2587
|
+
if (!isPlainObject(node) || typeof node.type !== 'string') {
|
|
2588
|
+
throw new TypeError('Expected node, got `' + node + '`');
|
|
2589
|
+
// Fine.
|
|
2590
|
+
}
|
|
2591
|
+
}
|
|
2592
|
+
|
|
2593
|
+
/**
|
|
2594
|
+
* Assert that `complete` is `true`.
|
|
2595
|
+
*
|
|
2596
|
+
* @param {string} name
|
|
2597
|
+
* @param {string} asyncName
|
|
2598
|
+
* @param {unknown} complete
|
|
2599
|
+
* @returns {asserts complete is true}
|
|
2600
|
+
*/
|
|
2601
|
+
function assertDone(name, asyncName, complete) {
|
|
2602
|
+
if (!complete) {
|
|
2603
|
+
throw new Error('`' + name + '` finished async. Use `' + asyncName + '` instead');
|
|
2604
|
+
}
|
|
2605
|
+
}
|
|
2606
|
+
|
|
2607
|
+
/**
|
|
2608
|
+
* @param {Compatible | undefined} [value]
|
|
2609
|
+
* @returns {VFile}
|
|
2610
|
+
*/
|
|
2611
|
+
function vfile(value) {
|
|
2612
|
+
return looksLikeAVFile(value) ? value : new VFile(value);
|
|
2613
|
+
}
|
|
2614
|
+
|
|
2615
|
+
/**
|
|
2616
|
+
* @param {Compatible | undefined} [value]
|
|
2617
|
+
* @returns {value is VFile}
|
|
2618
|
+
*/
|
|
2619
|
+
function looksLikeAVFile(value) {
|
|
2620
|
+
return Boolean(value && typeof value === 'object' && 'message' in value && 'messages' in value);
|
|
2621
|
+
}
|
|
2622
|
+
|
|
2623
|
+
/**
|
|
2624
|
+
* @param {unknown} [value]
|
|
2625
|
+
* @returns {value is Value}
|
|
2626
|
+
*/
|
|
2627
|
+
function looksLikeAValue(value) {
|
|
2628
|
+
return typeof value === 'string' || isUint8Array(value);
|
|
2629
|
+
}
|
|
2630
|
+
|
|
2631
|
+
/**
|
|
2632
|
+
* Assert `value` is an `Uint8Array`.
|
|
2633
|
+
*
|
|
2634
|
+
* @param {unknown} value
|
|
2635
|
+
* thing.
|
|
2636
|
+
* @returns {value is Uint8Array}
|
|
2637
|
+
* Whether `value` is an `Uint8Array`.
|
|
2638
|
+
*/
|
|
2639
|
+
function isUint8Array(value) {
|
|
2640
|
+
return Boolean(value && typeof value === 'object' && 'byteLength' in value && 'byteOffset' in value);
|
|
2641
|
+
}
|
|
2642
|
+
|
|
2643
|
+
export { unified };
|
|
2644
|
+
//# sourceMappingURL=index-DBBNT106.js.map
|