mobx-state-tree 7.2.0 → 7.3.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/dist/core/type/errorFormatting.d.ts +60 -0
- package/dist/index.d.ts +1 -1
- package/dist/internal.d.ts +1 -0
- package/dist/mobx-state-tree.js +205 -13
- package/dist/mobx-state-tree.min.js +1 -1
- package/dist/mobx-state-tree.module.js +204 -14
- package/dist/mobx-state-tree.umd.js +205 -13
- package/dist/mobx-state-tree.umd.min.js +1 -1
- package/package.json +1 -1
|
@@ -3535,30 +3535,207 @@ function isActionContextThisOrChildOf(actionContext, parentOrThis) {
|
|
|
3535
3535
|
return _isActionContextThisOrChildOf(actionContext, parentOrThis, true);
|
|
3536
3536
|
}
|
|
3537
3537
|
|
|
3538
|
-
|
|
3538
|
+
var defaultOptions = {
|
|
3539
|
+
enabled: false,
|
|
3540
|
+
indent: 2,
|
|
3541
|
+
maxStringLength: 100,
|
|
3542
|
+
maxArrayLength: 10,
|
|
3543
|
+
maxPropertyCount: 30,
|
|
3544
|
+
maxDepth: 5
|
|
3545
|
+
};
|
|
3546
|
+
var currentOptions = __assign({}, defaultOptions);
|
|
3547
|
+
/**
|
|
3548
|
+
* Configures how snapshots/values and type shapes are formatted inside
|
|
3549
|
+
* type-checking error messages. This is **opt-in**: by default MST keeps its
|
|
3550
|
+
* original single-line error formatting, so enabling this does not change
|
|
3551
|
+
* behavior for anyone who doesn't call it.
|
|
3552
|
+
*
|
|
3553
|
+
* The given options are merged over the current ones, so you can set only the
|
|
3554
|
+
* fields you care about.
|
|
3555
|
+
*
|
|
3556
|
+
* @example
|
|
3557
|
+
* // pretty-print large snapshots across multiple lines and truncate them
|
|
3558
|
+
* setErrorFormatting({ enabled: true })
|
|
3559
|
+
*
|
|
3560
|
+
* @example
|
|
3561
|
+
* // only bound the message size, without reflowing it onto multiple lines
|
|
3562
|
+
* setErrorFormatting({ enabled: true, indent: 0 })
|
|
3563
|
+
*
|
|
3564
|
+
* @example
|
|
3565
|
+
* // tweak the truncation limits
|
|
3566
|
+
* setErrorFormatting({ enabled: true, maxArrayLength: 3, maxStringLength: 40 })
|
|
3567
|
+
*
|
|
3568
|
+
* @param options A partial set of {@link ErrorFormattingOptions} to apply.
|
|
3569
|
+
*/
|
|
3570
|
+
function setErrorFormatting(options) {
|
|
3571
|
+
currentOptions = __assign(__assign({}, currentOptions), options);
|
|
3572
|
+
}
|
|
3573
|
+
/**
|
|
3574
|
+
* Returns a copy of the current error formatting options (see
|
|
3575
|
+
* {@link setErrorFormatting}). Useful for temporarily overriding and then
|
|
3576
|
+
* restoring the configuration.
|
|
3577
|
+
*
|
|
3578
|
+
* @returns The current {@link ErrorFormattingOptions}.
|
|
3579
|
+
*/
|
|
3580
|
+
function getErrorFormatting() {
|
|
3581
|
+
return __assign({}, currentOptions);
|
|
3582
|
+
}
|
|
3583
|
+
|
|
3584
|
+
function safeStringify(value, indent) {
|
|
3539
3585
|
try {
|
|
3540
|
-
return JSON.stringify(value);
|
|
3586
|
+
return JSON.stringify(value, null, indent);
|
|
3541
3587
|
}
|
|
3542
3588
|
catch (e) {
|
|
3543
3589
|
// istanbul ignore next
|
|
3544
3590
|
return "<Unserializable: ".concat(e, ">");
|
|
3545
3591
|
}
|
|
3546
3592
|
}
|
|
3593
|
+
function shortenPrintValue(valueInString) {
|
|
3594
|
+
return valueInString.length < 280
|
|
3595
|
+
? valueInString
|
|
3596
|
+
: "".concat(valueInString.substring(0, 272), "......").concat(valueInString.substring(valueInString.length - 8));
|
|
3597
|
+
}
|
|
3598
|
+
/**
|
|
3599
|
+
* Returns a clone of `value` in which overly long strings are clipped and large
|
|
3600
|
+
* arrays/objects (or values nested too deeply) are summarized, so the result is
|
|
3601
|
+
* safe to print in an error message without flooding the screen.
|
|
3602
|
+
*/
|
|
3603
|
+
function truncateForDisplay(value, depth, options) {
|
|
3604
|
+
if (typeof value === "string") {
|
|
3605
|
+
return value.length > options.maxStringLength
|
|
3606
|
+
? "".concat(value.slice(0, options.maxStringLength), "\u2026 (").concat(value.length - options.maxStringLength, " more characters)")
|
|
3607
|
+
: value;
|
|
3608
|
+
}
|
|
3609
|
+
if (Array.isArray(value)) {
|
|
3610
|
+
if (depth >= options.maxDepth)
|
|
3611
|
+
return "[…]";
|
|
3612
|
+
var items = value
|
|
3613
|
+
.slice(0, options.maxArrayLength)
|
|
3614
|
+
.map(function (item) { return truncateForDisplay(item, depth + 1, options); });
|
|
3615
|
+
if (value.length > options.maxArrayLength) {
|
|
3616
|
+
items.push("\u2026 ".concat(value.length - options.maxArrayLength, " more items"));
|
|
3617
|
+
}
|
|
3618
|
+
return items;
|
|
3619
|
+
}
|
|
3620
|
+
if (isPlainObject(value)) {
|
|
3621
|
+
if (depth >= options.maxDepth)
|
|
3622
|
+
return "{…}";
|
|
3623
|
+
var result_1 = {};
|
|
3624
|
+
var keys = Object.keys(value);
|
|
3625
|
+
keys.slice(0, options.maxPropertyCount).forEach(function (key) {
|
|
3626
|
+
result_1[key] = truncateForDisplay(value[key], depth + 1, options);
|
|
3627
|
+
});
|
|
3628
|
+
if (keys.length > options.maxPropertyCount) {
|
|
3629
|
+
result_1["…"] = "".concat(keys.length - options.maxPropertyCount, " more keys");
|
|
3630
|
+
}
|
|
3631
|
+
return result_1;
|
|
3632
|
+
}
|
|
3633
|
+
return value;
|
|
3634
|
+
}
|
|
3547
3635
|
/**
|
|
3548
3636
|
* @internal
|
|
3549
3637
|
* @hidden
|
|
3550
3638
|
*/
|
|
3551
3639
|
function prettyPrintValue(value) {
|
|
3552
|
-
|
|
3553
|
-
|
|
3554
|
-
|
|
3555
|
-
|
|
3556
|
-
|
|
3640
|
+
if (typeof value === "function") {
|
|
3641
|
+
return "<function".concat(value.name ? " " + value.name : "", ">");
|
|
3642
|
+
}
|
|
3643
|
+
if (isStateTreeNode(value)) {
|
|
3644
|
+
return "<".concat(value, ">");
|
|
3645
|
+
}
|
|
3646
|
+
var options = getErrorFormatting();
|
|
3647
|
+
if (!options.enabled) {
|
|
3648
|
+
// Default behavior: serialize the value compactly on a single line.
|
|
3649
|
+
// JSON.stringify returns `undefined` for values like `undefined` itself,
|
|
3650
|
+
// which the template literal coerces back to a string.
|
|
3651
|
+
return "`".concat(safeStringify(value), "`");
|
|
3652
|
+
}
|
|
3653
|
+
// Opt-in behavior: clip long strings, big arrays/objects and deep nesting, and
|
|
3654
|
+
// (when indent > 0) pretty-print the result across multiple lines.
|
|
3655
|
+
var truncated = truncateForDisplay(value, 0, options);
|
|
3656
|
+
return "`".concat(safeStringify(truncated, options.indent || undefined), "`");
|
|
3557
3657
|
}
|
|
3558
|
-
|
|
3559
|
-
|
|
3560
|
-
|
|
3561
|
-
|
|
3658
|
+
/**
|
|
3659
|
+
* Re-indents a type description (as produced by `IType.describe()`) across
|
|
3660
|
+
* multiple lines, by breaking after the `{`, `}` and `;` separators used in
|
|
3661
|
+
* model shapes (while leaving union `|` and array `[]` parts inline).
|
|
3662
|
+
* Characters inside string literals (e.g. literal types like `"a;b"`) are left
|
|
3663
|
+
* untouched.
|
|
3664
|
+
*
|
|
3665
|
+
* `indentSize` is the number of spaces per nesting level; when it is `0` (or
|
|
3666
|
+
* negative) the description is returned unchanged on a single line. As this runs
|
|
3667
|
+
* while formatting an error that is already being thrown, it must never throw
|
|
3668
|
+
* itself: any unexpected input falls back to the original, unformatted
|
|
3669
|
+
* description.
|
|
3670
|
+
*
|
|
3671
|
+
* @internal
|
|
3672
|
+
* @hidden
|
|
3673
|
+
*/
|
|
3674
|
+
function prettyPrintDescription(description, indentSize) {
|
|
3675
|
+
if (indentSize <= 0) {
|
|
3676
|
+
return description;
|
|
3677
|
+
}
|
|
3678
|
+
try {
|
|
3679
|
+
var step_1 = " ".repeat(indentSize);
|
|
3680
|
+
var result_2 = "";
|
|
3681
|
+
var depth_1 = 0;
|
|
3682
|
+
var stringDelimiter = null;
|
|
3683
|
+
var escaped = false;
|
|
3684
|
+
var newline = function () {
|
|
3685
|
+
// drop any trailing spaces (e.g. the "{ " / "; " separators) before breaking,
|
|
3686
|
+
// and never let a malformed (over-closed) shape produce a negative indent
|
|
3687
|
+
result_2 = result_2.replace(/[ \t]+$/, "");
|
|
3688
|
+
result_2 += "\n" + step_1.repeat(Math.max(0, depth_1));
|
|
3689
|
+
};
|
|
3690
|
+
for (var i = 0; i < description.length; i++) {
|
|
3691
|
+
var char = description[i];
|
|
3692
|
+
if (stringDelimiter) {
|
|
3693
|
+
result_2 += char;
|
|
3694
|
+
if (escaped) {
|
|
3695
|
+
escaped = false;
|
|
3696
|
+
}
|
|
3697
|
+
else if (char === "\\") {
|
|
3698
|
+
escaped = true;
|
|
3699
|
+
}
|
|
3700
|
+
else if (char === stringDelimiter) {
|
|
3701
|
+
stringDelimiter = null;
|
|
3702
|
+
}
|
|
3703
|
+
continue;
|
|
3704
|
+
}
|
|
3705
|
+
switch (char) {
|
|
3706
|
+
case '"':
|
|
3707
|
+
case "'":
|
|
3708
|
+
stringDelimiter = char;
|
|
3709
|
+
result_2 += char;
|
|
3710
|
+
break;
|
|
3711
|
+
case "{":
|
|
3712
|
+
depth_1++;
|
|
3713
|
+
result_2 += "{";
|
|
3714
|
+
newline();
|
|
3715
|
+
while (description[i + 1] === " ")
|
|
3716
|
+
i++;
|
|
3717
|
+
break;
|
|
3718
|
+
case "}":
|
|
3719
|
+
depth_1--;
|
|
3720
|
+
newline();
|
|
3721
|
+
result_2 += "}";
|
|
3722
|
+
break;
|
|
3723
|
+
case ";":
|
|
3724
|
+
result_2 += ";";
|
|
3725
|
+
newline();
|
|
3726
|
+
while (description[i + 1] === " ")
|
|
3727
|
+
i++;
|
|
3728
|
+
break;
|
|
3729
|
+
default:
|
|
3730
|
+
result_2 += char;
|
|
3731
|
+
}
|
|
3732
|
+
}
|
|
3733
|
+
return result_2;
|
|
3734
|
+
}
|
|
3735
|
+
catch (e) {
|
|
3736
|
+
// istanbul ignore next - defensive: never let formatting hide the real error
|
|
3737
|
+
return description;
|
|
3738
|
+
}
|
|
3562
3739
|
}
|
|
3563
3740
|
function toErrorString(error) {
|
|
3564
3741
|
var value = error.value;
|
|
@@ -3577,12 +3754,18 @@ function toErrorString(error) {
|
|
|
3577
3754
|
? "value"
|
|
3578
3755
|
: "snapshot";
|
|
3579
3756
|
var isSnapshotCompatible = type && isStateTreeNode(value) && type.is(getStateTreeNode(value).snapshot);
|
|
3757
|
+
// When error formatting is enabled, the type shape is re-indented using the
|
|
3758
|
+
// same indent setting as values; otherwise it's left on a single line.
|
|
3759
|
+
var formatting = getErrorFormatting();
|
|
3760
|
+
var describeType = function (t) {
|
|
3761
|
+
return formatting.enabled ? prettyPrintDescription(t.describe(), formatting.indent) : t.describe();
|
|
3762
|
+
};
|
|
3580
3763
|
return ("".concat(pathPrefix).concat(currentTypename, " ").concat(prettyPrintValue(value), " is not assignable ").concat(type ? "to type: `".concat(type.name, "`") : "") +
|
|
3581
3764
|
(error.message ? " (".concat(error.message, ")") : "") +
|
|
3582
3765
|
(type
|
|
3583
3766
|
? isPrimitiveType(type) || isPrimitive(value)
|
|
3584
3767
|
? "."
|
|
3585
|
-
: ", expected an instance of `".concat(type.name, "` or a snapshot like `").concat(type
|
|
3768
|
+
: ", expected an instance of `".concat(type.name, "` or a snapshot like `").concat(describeType(type), "` instead.") +
|
|
3586
3769
|
(isSnapshotCompatible
|
|
3587
3770
|
? " (Note that a snapshot of the provided value is compatible with the targeted type)"
|
|
3588
3771
|
: "")
|
|
@@ -3645,7 +3828,14 @@ function validationErrorsToString(type, value, errors) {
|
|
|
3645
3828
|
if (errors.length === 0) {
|
|
3646
3829
|
return undefined;
|
|
3647
3830
|
}
|
|
3648
|
-
|
|
3831
|
+
// When formatting is disabled, keep the original behavior of capping the
|
|
3832
|
+
// header value's length; when enabled, truncation already bounds its size.
|
|
3833
|
+
var printedValue = prettyPrintValue(value);
|
|
3834
|
+
var headerValue = getErrorFormatting().enabled
|
|
3835
|
+
? printedValue
|
|
3836
|
+
: shortenPrintValue(printedValue);
|
|
3837
|
+
return ("Error while converting ".concat(headerValue, " to `").concat(type.name, "`:\n\n ") +
|
|
3838
|
+
errors.map(toErrorString).join("\n "));
|
|
3649
3839
|
}
|
|
3650
3840
|
|
|
3651
3841
|
var identifierCacheId = 0;
|
|
@@ -9007,4 +9197,4 @@ var types = {
|
|
|
9007
9197
|
snapshotProcessor: snapshotProcessor
|
|
9008
9198
|
};
|
|
9009
9199
|
|
|
9010
|
-
export { addDisposer, addMiddleware, applyAction, applyPatch, applySnapshot, cast, castFlowReturn, castToReferenceSnapshot, castToSnapshot, clone, createActionTrackingMiddleware, createActionTrackingMiddleware2, decorate, destroy, detach, escapeJsonPath, flow, getChildType, getEnv, getIdentifier, getLivelinessChecking, getMembers, getNodeId, getParent, getParentOfType, getPath, getPathParts, getPropertyMembers, getRelativePath, getRoot, getRunningActionContext, getSnapshot, getType, hasEnv, hasParent, hasParentOfType, isActionContextChildOf, isActionContextThisOrChildOf, isAlive, isArrayType, isFrozenType, isIdentifierType, isLateType, isLiteralType, isMapType, isModelType, isOptionalType, isPrimitiveType, isProtected, isReferenceType, isRefinementType, isRoot, isStateTreeNode, isType, isUnionType, isValidReference, joinJsonPath, onAction, onPatch, onSnapshot, process$1 as process, protect, recordActions, recordPatches, resolveIdentifier, resolvePath, setLivelinessChecking, setLivelynessChecking, splitJsonPath, types as t, toGenerator, toGeneratorFunction, tryReference, tryResolve, typecheck, types, unescapeJsonPath, unprotect, walk };
|
|
9200
|
+
export { addDisposer, addMiddleware, applyAction, applyPatch, applySnapshot, cast, castFlowReturn, castToReferenceSnapshot, castToSnapshot, clone, createActionTrackingMiddleware, createActionTrackingMiddleware2, decorate, destroy, detach, escapeJsonPath, flow, getChildType, getEnv, getErrorFormatting, getIdentifier, getLivelinessChecking, getMembers, getNodeId, getParent, getParentOfType, getPath, getPathParts, getPropertyMembers, getRelativePath, getRoot, getRunningActionContext, getSnapshot, getType, hasEnv, hasParent, hasParentOfType, isActionContextChildOf, isActionContextThisOrChildOf, isAlive, isArrayType, isFrozenType, isIdentifierType, isLateType, isLiteralType, isMapType, isModelType, isOptionalType, isPrimitiveType, isProtected, isReferenceType, isRefinementType, isRoot, isStateTreeNode, isType, isUnionType, isValidReference, joinJsonPath, onAction, onPatch, onSnapshot, process$1 as process, protect, recordActions, recordPatches, resolveIdentifier, resolvePath, setErrorFormatting, setLivelinessChecking, setLivelynessChecking, splitJsonPath, types as t, toGenerator, toGeneratorFunction, tryReference, tryResolve, typecheck, types, unescapeJsonPath, unprotect, walk };
|
|
@@ -3539,30 +3539,207 @@
|
|
|
3539
3539
|
return _isActionContextThisOrChildOf(actionContext, parentOrThis, true);
|
|
3540
3540
|
}
|
|
3541
3541
|
|
|
3542
|
-
|
|
3542
|
+
var defaultOptions = {
|
|
3543
|
+
enabled: false,
|
|
3544
|
+
indent: 2,
|
|
3545
|
+
maxStringLength: 100,
|
|
3546
|
+
maxArrayLength: 10,
|
|
3547
|
+
maxPropertyCount: 30,
|
|
3548
|
+
maxDepth: 5
|
|
3549
|
+
};
|
|
3550
|
+
var currentOptions = __assign({}, defaultOptions);
|
|
3551
|
+
/**
|
|
3552
|
+
* Configures how snapshots/values and type shapes are formatted inside
|
|
3553
|
+
* type-checking error messages. This is **opt-in**: by default MST keeps its
|
|
3554
|
+
* original single-line error formatting, so enabling this does not change
|
|
3555
|
+
* behavior for anyone who doesn't call it.
|
|
3556
|
+
*
|
|
3557
|
+
* The given options are merged over the current ones, so you can set only the
|
|
3558
|
+
* fields you care about.
|
|
3559
|
+
*
|
|
3560
|
+
* @example
|
|
3561
|
+
* // pretty-print large snapshots across multiple lines and truncate them
|
|
3562
|
+
* setErrorFormatting({ enabled: true })
|
|
3563
|
+
*
|
|
3564
|
+
* @example
|
|
3565
|
+
* // only bound the message size, without reflowing it onto multiple lines
|
|
3566
|
+
* setErrorFormatting({ enabled: true, indent: 0 })
|
|
3567
|
+
*
|
|
3568
|
+
* @example
|
|
3569
|
+
* // tweak the truncation limits
|
|
3570
|
+
* setErrorFormatting({ enabled: true, maxArrayLength: 3, maxStringLength: 40 })
|
|
3571
|
+
*
|
|
3572
|
+
* @param options A partial set of {@link ErrorFormattingOptions} to apply.
|
|
3573
|
+
*/
|
|
3574
|
+
function setErrorFormatting(options) {
|
|
3575
|
+
currentOptions = __assign(__assign({}, currentOptions), options);
|
|
3576
|
+
}
|
|
3577
|
+
/**
|
|
3578
|
+
* Returns a copy of the current error formatting options (see
|
|
3579
|
+
* {@link setErrorFormatting}). Useful for temporarily overriding and then
|
|
3580
|
+
* restoring the configuration.
|
|
3581
|
+
*
|
|
3582
|
+
* @returns The current {@link ErrorFormattingOptions}.
|
|
3583
|
+
*/
|
|
3584
|
+
function getErrorFormatting() {
|
|
3585
|
+
return __assign({}, currentOptions);
|
|
3586
|
+
}
|
|
3587
|
+
|
|
3588
|
+
function safeStringify(value, indent) {
|
|
3543
3589
|
try {
|
|
3544
|
-
return JSON.stringify(value);
|
|
3590
|
+
return JSON.stringify(value, null, indent);
|
|
3545
3591
|
}
|
|
3546
3592
|
catch (e) {
|
|
3547
3593
|
// istanbul ignore next
|
|
3548
3594
|
return "<Unserializable: ".concat(e, ">");
|
|
3549
3595
|
}
|
|
3550
3596
|
}
|
|
3597
|
+
function shortenPrintValue(valueInString) {
|
|
3598
|
+
return valueInString.length < 280
|
|
3599
|
+
? valueInString
|
|
3600
|
+
: "".concat(valueInString.substring(0, 272), "......").concat(valueInString.substring(valueInString.length - 8));
|
|
3601
|
+
}
|
|
3602
|
+
/**
|
|
3603
|
+
* Returns a clone of `value` in which overly long strings are clipped and large
|
|
3604
|
+
* arrays/objects (or values nested too deeply) are summarized, so the result is
|
|
3605
|
+
* safe to print in an error message without flooding the screen.
|
|
3606
|
+
*/
|
|
3607
|
+
function truncateForDisplay(value, depth, options) {
|
|
3608
|
+
if (typeof value === "string") {
|
|
3609
|
+
return value.length > options.maxStringLength
|
|
3610
|
+
? "".concat(value.slice(0, options.maxStringLength), "\u2026 (").concat(value.length - options.maxStringLength, " more characters)")
|
|
3611
|
+
: value;
|
|
3612
|
+
}
|
|
3613
|
+
if (Array.isArray(value)) {
|
|
3614
|
+
if (depth >= options.maxDepth)
|
|
3615
|
+
return "[…]";
|
|
3616
|
+
var items = value
|
|
3617
|
+
.slice(0, options.maxArrayLength)
|
|
3618
|
+
.map(function (item) { return truncateForDisplay(item, depth + 1, options); });
|
|
3619
|
+
if (value.length > options.maxArrayLength) {
|
|
3620
|
+
items.push("\u2026 ".concat(value.length - options.maxArrayLength, " more items"));
|
|
3621
|
+
}
|
|
3622
|
+
return items;
|
|
3623
|
+
}
|
|
3624
|
+
if (isPlainObject(value)) {
|
|
3625
|
+
if (depth >= options.maxDepth)
|
|
3626
|
+
return "{…}";
|
|
3627
|
+
var result_1 = {};
|
|
3628
|
+
var keys = Object.keys(value);
|
|
3629
|
+
keys.slice(0, options.maxPropertyCount).forEach(function (key) {
|
|
3630
|
+
result_1[key] = truncateForDisplay(value[key], depth + 1, options);
|
|
3631
|
+
});
|
|
3632
|
+
if (keys.length > options.maxPropertyCount) {
|
|
3633
|
+
result_1["…"] = "".concat(keys.length - options.maxPropertyCount, " more keys");
|
|
3634
|
+
}
|
|
3635
|
+
return result_1;
|
|
3636
|
+
}
|
|
3637
|
+
return value;
|
|
3638
|
+
}
|
|
3551
3639
|
/**
|
|
3552
3640
|
* @internal
|
|
3553
3641
|
* @hidden
|
|
3554
3642
|
*/
|
|
3555
3643
|
function prettyPrintValue(value) {
|
|
3556
|
-
|
|
3557
|
-
|
|
3558
|
-
|
|
3559
|
-
|
|
3560
|
-
|
|
3644
|
+
if (typeof value === "function") {
|
|
3645
|
+
return "<function".concat(value.name ? " " + value.name : "", ">");
|
|
3646
|
+
}
|
|
3647
|
+
if (isStateTreeNode(value)) {
|
|
3648
|
+
return "<".concat(value, ">");
|
|
3649
|
+
}
|
|
3650
|
+
var options = getErrorFormatting();
|
|
3651
|
+
if (!options.enabled) {
|
|
3652
|
+
// Default behavior: serialize the value compactly on a single line.
|
|
3653
|
+
// JSON.stringify returns `undefined` for values like `undefined` itself,
|
|
3654
|
+
// which the template literal coerces back to a string.
|
|
3655
|
+
return "`".concat(safeStringify(value), "`");
|
|
3656
|
+
}
|
|
3657
|
+
// Opt-in behavior: clip long strings, big arrays/objects and deep nesting, and
|
|
3658
|
+
// (when indent > 0) pretty-print the result across multiple lines.
|
|
3659
|
+
var truncated = truncateForDisplay(value, 0, options);
|
|
3660
|
+
return "`".concat(safeStringify(truncated, options.indent || undefined), "`");
|
|
3561
3661
|
}
|
|
3562
|
-
|
|
3563
|
-
|
|
3564
|
-
|
|
3565
|
-
|
|
3662
|
+
/**
|
|
3663
|
+
* Re-indents a type description (as produced by `IType.describe()`) across
|
|
3664
|
+
* multiple lines, by breaking after the `{`, `}` and `;` separators used in
|
|
3665
|
+
* model shapes (while leaving union `|` and array `[]` parts inline).
|
|
3666
|
+
* Characters inside string literals (e.g. literal types like `"a;b"`) are left
|
|
3667
|
+
* untouched.
|
|
3668
|
+
*
|
|
3669
|
+
* `indentSize` is the number of spaces per nesting level; when it is `0` (or
|
|
3670
|
+
* negative) the description is returned unchanged on a single line. As this runs
|
|
3671
|
+
* while formatting an error that is already being thrown, it must never throw
|
|
3672
|
+
* itself: any unexpected input falls back to the original, unformatted
|
|
3673
|
+
* description.
|
|
3674
|
+
*
|
|
3675
|
+
* @internal
|
|
3676
|
+
* @hidden
|
|
3677
|
+
*/
|
|
3678
|
+
function prettyPrintDescription(description, indentSize) {
|
|
3679
|
+
if (indentSize <= 0) {
|
|
3680
|
+
return description;
|
|
3681
|
+
}
|
|
3682
|
+
try {
|
|
3683
|
+
var step_1 = " ".repeat(indentSize);
|
|
3684
|
+
var result_2 = "";
|
|
3685
|
+
var depth_1 = 0;
|
|
3686
|
+
var stringDelimiter = null;
|
|
3687
|
+
var escaped = false;
|
|
3688
|
+
var newline = function () {
|
|
3689
|
+
// drop any trailing spaces (e.g. the "{ " / "; " separators) before breaking,
|
|
3690
|
+
// and never let a malformed (over-closed) shape produce a negative indent
|
|
3691
|
+
result_2 = result_2.replace(/[ \t]+$/, "");
|
|
3692
|
+
result_2 += "\n" + step_1.repeat(Math.max(0, depth_1));
|
|
3693
|
+
};
|
|
3694
|
+
for (var i = 0; i < description.length; i++) {
|
|
3695
|
+
var char = description[i];
|
|
3696
|
+
if (stringDelimiter) {
|
|
3697
|
+
result_2 += char;
|
|
3698
|
+
if (escaped) {
|
|
3699
|
+
escaped = false;
|
|
3700
|
+
}
|
|
3701
|
+
else if (char === "\\") {
|
|
3702
|
+
escaped = true;
|
|
3703
|
+
}
|
|
3704
|
+
else if (char === stringDelimiter) {
|
|
3705
|
+
stringDelimiter = null;
|
|
3706
|
+
}
|
|
3707
|
+
continue;
|
|
3708
|
+
}
|
|
3709
|
+
switch (char) {
|
|
3710
|
+
case '"':
|
|
3711
|
+
case "'":
|
|
3712
|
+
stringDelimiter = char;
|
|
3713
|
+
result_2 += char;
|
|
3714
|
+
break;
|
|
3715
|
+
case "{":
|
|
3716
|
+
depth_1++;
|
|
3717
|
+
result_2 += "{";
|
|
3718
|
+
newline();
|
|
3719
|
+
while (description[i + 1] === " ")
|
|
3720
|
+
i++;
|
|
3721
|
+
break;
|
|
3722
|
+
case "}":
|
|
3723
|
+
depth_1--;
|
|
3724
|
+
newline();
|
|
3725
|
+
result_2 += "}";
|
|
3726
|
+
break;
|
|
3727
|
+
case ";":
|
|
3728
|
+
result_2 += ";";
|
|
3729
|
+
newline();
|
|
3730
|
+
while (description[i + 1] === " ")
|
|
3731
|
+
i++;
|
|
3732
|
+
break;
|
|
3733
|
+
default:
|
|
3734
|
+
result_2 += char;
|
|
3735
|
+
}
|
|
3736
|
+
}
|
|
3737
|
+
return result_2;
|
|
3738
|
+
}
|
|
3739
|
+
catch (e) {
|
|
3740
|
+
// istanbul ignore next - defensive: never let formatting hide the real error
|
|
3741
|
+
return description;
|
|
3742
|
+
}
|
|
3566
3743
|
}
|
|
3567
3744
|
function toErrorString(error) {
|
|
3568
3745
|
var value = error.value;
|
|
@@ -3581,12 +3758,18 @@
|
|
|
3581
3758
|
? "value"
|
|
3582
3759
|
: "snapshot";
|
|
3583
3760
|
var isSnapshotCompatible = type && isStateTreeNode(value) && type.is(getStateTreeNode(value).snapshot);
|
|
3761
|
+
// When error formatting is enabled, the type shape is re-indented using the
|
|
3762
|
+
// same indent setting as values; otherwise it's left on a single line.
|
|
3763
|
+
var formatting = getErrorFormatting();
|
|
3764
|
+
var describeType = function (t) {
|
|
3765
|
+
return formatting.enabled ? prettyPrintDescription(t.describe(), formatting.indent) : t.describe();
|
|
3766
|
+
};
|
|
3584
3767
|
return ("".concat(pathPrefix).concat(currentTypename, " ").concat(prettyPrintValue(value), " is not assignable ").concat(type ? "to type: `".concat(type.name, "`") : "") +
|
|
3585
3768
|
(error.message ? " (".concat(error.message, ")") : "") +
|
|
3586
3769
|
(type
|
|
3587
3770
|
? isPrimitiveType(type) || isPrimitive(value)
|
|
3588
3771
|
? "."
|
|
3589
|
-
: ", expected an instance of `".concat(type.name, "` or a snapshot like `").concat(type
|
|
3772
|
+
: ", expected an instance of `".concat(type.name, "` or a snapshot like `").concat(describeType(type), "` instead.") +
|
|
3590
3773
|
(isSnapshotCompatible
|
|
3591
3774
|
? " (Note that a snapshot of the provided value is compatible with the targeted type)"
|
|
3592
3775
|
: "")
|
|
@@ -3649,7 +3832,14 @@
|
|
|
3649
3832
|
if (errors.length === 0) {
|
|
3650
3833
|
return undefined;
|
|
3651
3834
|
}
|
|
3652
|
-
|
|
3835
|
+
// When formatting is disabled, keep the original behavior of capping the
|
|
3836
|
+
// header value's length; when enabled, truncation already bounds its size.
|
|
3837
|
+
var printedValue = prettyPrintValue(value);
|
|
3838
|
+
var headerValue = getErrorFormatting().enabled
|
|
3839
|
+
? printedValue
|
|
3840
|
+
: shortenPrintValue(printedValue);
|
|
3841
|
+
return ("Error while converting ".concat(headerValue, " to `").concat(type.name, "`:\n\n ") +
|
|
3842
|
+
errors.map(toErrorString).join("\n "));
|
|
3653
3843
|
}
|
|
3654
3844
|
|
|
3655
3845
|
var identifierCacheId = 0;
|
|
@@ -9008,6 +9198,7 @@
|
|
|
9008
9198
|
exports.flow = flow;
|
|
9009
9199
|
exports.getChildType = getChildType;
|
|
9010
9200
|
exports.getEnv = getEnv;
|
|
9201
|
+
exports.getErrorFormatting = getErrorFormatting;
|
|
9011
9202
|
exports.getIdentifier = getIdentifier;
|
|
9012
9203
|
exports.getLivelinessChecking = getLivelinessChecking;
|
|
9013
9204
|
exports.getMembers = getMembers;
|
|
@@ -9055,6 +9246,7 @@
|
|
|
9055
9246
|
exports.recordPatches = recordPatches;
|
|
9056
9247
|
exports.resolveIdentifier = resolveIdentifier;
|
|
9057
9248
|
exports.resolvePath = resolvePath;
|
|
9249
|
+
exports.setErrorFormatting = setErrorFormatting;
|
|
9058
9250
|
exports.setLivelinessChecking = setLivelinessChecking;
|
|
9059
9251
|
exports.setLivelynessChecking = setLivelynessChecking;
|
|
9060
9252
|
exports.splitJsonPath = splitJsonPath;
|