ember-repl 7.1.0 → 7.1.1

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