fix(custom-widgets): finish v2 hardening
This commit is contained in:
97 files changed
+2833
-12393
No files matched your search
@@ -0,0 +1,36 @@
|
||||
export interface CustomJsxTemplateDiagnostic {
|
||||
severity: "error" | "warning";
|
||||
message: string;
|
||||
index: number;
|
||||
line: number;
|
||||
column: number;
|
||||
}
|
||||
|
||||
export function createCustomJsxParseDiagnostic(template: string, error: unknown): CustomJsxTemplateDiagnostic {
|
||||
const parseError = error as Error & { pos?: number; loc?: { line?: number; column?: number } };
|
||||
const parseIndex = Math.max(0, (parseError.pos ?? 2) - 2);
|
||||
const missingInitializer = [...template.matchAll(/(?:\bconst|,)\s+([A-Za-z_$][\w$]*)\s*;/gu)].find((match) => {
|
||||
const semicolon = (match.index ?? 0) + match[0].length - 1;
|
||||
return Math.abs(semicolon - parseIndex) <= 1;
|
||||
});
|
||||
const duplicateBinding = /Identifier '([^']+)' has already been declared/iu.exec(parseError.message);
|
||||
const duplicateArgument = /Argument name clash/iu.test(parseError.message);
|
||||
const message = missingInitializer
|
||||
? `LOCAL_BINDING_REQUIRES_INITIALIZER: '${missingInitializer[1]}' requires an initializer`
|
||||
: duplicateBinding
|
||||
? `DUPLICATE_LOCAL_BINDING: '${duplicateBinding[1]}' is already declared`
|
||||
: duplicateArgument
|
||||
? "DUPLICATE_LOCAL_BINDING: Callback parameter names must be unique"
|
||||
: parseError.message;
|
||||
|
||||
return {
|
||||
severity: "error",
|
||||
message,
|
||||
index: parseIndex,
|
||||
line: parseError.loc?.line ?? 1,
|
||||
column: Math.max(
|
||||
1,
|
||||
(parseError.loc?.column ?? 0) + (parseError.loc?.line === 1 || parseError.loc?.line === undefined ? -1 : 1),
|
||||
),
|
||||
};
|
||||
}
|
||||
@@ -118,13 +118,19 @@ function analyzeAttributeValue(
|
||||
}
|
||||
if (
|
||||
attributeName === "maxDepth" &&
|
||||
(typeof literalValue.value !== "number" || literalValue.value < 1 || literalValue.value > 32)
|
||||
(typeof literalValue.value !== "number" ||
|
||||
!Number.isInteger(literalValue.value) ||
|
||||
literalValue.value < 1 ||
|
||||
literalValue.value > 32)
|
||||
) {
|
||||
context.add(attribute, "INVALID_PROP_VALUE: 'maxDepth' must be between 1 and 32");
|
||||
}
|
||||
if (
|
||||
attributeName === "maxNodes" &&
|
||||
(typeof literalValue.value !== "number" || literalValue.value < 1 || literalValue.value > 2_000)
|
||||
(typeof literalValue.value !== "number" ||
|
||||
!Number.isInteger(literalValue.value) ||
|
||||
literalValue.value < 1 ||
|
||||
literalValue.value > 2_000)
|
||||
) {
|
||||
context.add(attribute, "INVALID_PROP_VALUE: 'maxNodes' must be between 1 and 2000");
|
||||
}
|
||||
|
||||
@@ -102,6 +102,30 @@ export function isStaticallyCallableBinding(node: AstNode): boolean {
|
||||
return Boolean(propertyName && callableRootMembers[String(object.name)]?.has(propertyName));
|
||||
}
|
||||
|
||||
export function canStaticallyProduceCallable(node: AstNode): boolean {
|
||||
if (isStaticallyCallableBinding(node) || ["ArrowFunctionExpression", "FunctionExpression"].includes(node.type)) {
|
||||
return true;
|
||||
}
|
||||
if (node.type === "ConditionalExpression") {
|
||||
const consequent = nodeOf(node.consequent);
|
||||
const alternate = nodeOf(node.alternate);
|
||||
return Boolean(
|
||||
(consequent && canStaticallyProduceCallable(consequent)) ||
|
||||
(alternate && canStaticallyProduceCallable(alternate)),
|
||||
);
|
||||
}
|
||||
if (node.type === "LogicalExpression") {
|
||||
const left = nodeOf(node.left);
|
||||
const right = nodeOf(node.right);
|
||||
return Boolean((left && canStaticallyProduceCallable(left)) || (right && canStaticallyProduceCallable(right)));
|
||||
}
|
||||
if (node.type === "ChainExpression") {
|
||||
const expression = nodeOf(node.expression);
|
||||
return Boolean(expression && canStaticallyProduceCallable(expression));
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
function editDistance(left: string, right: string): number {
|
||||
const previous = Array.from({ length: right.length + 1 }, (_, index) => index);
|
||||
for (let leftIndex = 1; leftIndex <= left.length; leftIndex += 1) {
|
||||
|
||||
@@ -9,23 +9,54 @@ import {
|
||||
} from "./policy";
|
||||
import type { AstNode } from "./analyzer-ast";
|
||||
import { nodeOf, nodesOf, staticPropertyName } from "./analyzer-ast";
|
||||
import { createCustomJsxParseDiagnostic } from "./analyzer-diagnostics";
|
||||
import type { CustomJsxTemplateDiagnostic } from "./analyzer-diagnostics";
|
||||
import { analyzeCustomJsxElement } from "./analyzer-jsx";
|
||||
import { isStaticallyCallableBinding, RESERVED_LOCAL_BINDINGS, ROOT_BINDINGS } from "./analyzer-language";
|
||||
import {
|
||||
canStaticallyProduceCallable,
|
||||
isStaticallyCallableBinding,
|
||||
RESERVED_LOCAL_BINDINGS,
|
||||
ROOT_BINDINGS,
|
||||
} from "./analyzer-language";
|
||||
import {
|
||||
CUSTOM_JSX_BINARY_OPERATORS,
|
||||
CUSTOM_JSX_CALLBACK_METHODS,
|
||||
CUSTOM_JSX_SAFE_VALUE_METHODS,
|
||||
} from "./safe-language-policy";
|
||||
|
||||
export interface CustomJsxTemplateDiagnostic {
|
||||
severity: "error" | "warning";
|
||||
message: string;
|
||||
index: number;
|
||||
line: number;
|
||||
column: number;
|
||||
}
|
||||
export type { CustomJsxTemplateDiagnostic } from "./analyzer-diagnostics";
|
||||
|
||||
const JsxParser = Parser.extend(jsx());
|
||||
const requestStatusLabels = new Set(["loading", "success", "error"]);
|
||||
|
||||
function directRequestStatusId(node: AstNode | null): string | null {
|
||||
if (node?.type !== "MemberExpression") return null;
|
||||
const object = nodeOf(node.object);
|
||||
if (object?.type !== "Identifier" || object.name !== "status") return null;
|
||||
const property = nodeOf(node.property);
|
||||
return node.computed
|
||||
? (staticPropertyName(property) ?? null)
|
||||
: String(property?.name ?? property?.value ?? "") || null;
|
||||
}
|
||||
|
||||
function invalidRequestStatusComparison(node: AstNode) {
|
||||
if (!["===", "==", "!==", "!="].includes(String(node.operator))) return null;
|
||||
const left = nodeOf(node.left);
|
||||
const right = nodeOf(node.right);
|
||||
const leftStatus = directRequestStatusId(left);
|
||||
const rightStatus = directRequestStatusId(right);
|
||||
const leftLabel = left?.type === "Literal" && typeof left.value === "string" ? left.value : null;
|
||||
const rightLabel = right?.type === "Literal" && typeof right.value === "string" ? right.value : null;
|
||||
const requestId =
|
||||
leftStatus && rightLabel && requestStatusLabels.has(rightLabel)
|
||||
? leftStatus
|
||||
: rightStatus && leftLabel && requestStatusLabels.has(leftLabel)
|
||||
? rightStatus
|
||||
: null;
|
||||
return requestId
|
||||
? `INVALID_STATUS_COMPARISON: status.${requestId} is an object. Use status.${requestId}?.loading, status.${requestId}?.ok === true, or status.${requestId}?.ok === false`
|
||||
: null;
|
||||
}
|
||||
|
||||
export function validateCustomJsxTemplate(template: string): CustomJsxTemplateDiagnostic[] {
|
||||
const diagnostics: CustomJsxTemplateDiagnostic[] = [];
|
||||
@@ -85,7 +116,9 @@ export function validateCustomJsxTemplate(template: string): CustomJsxTemplateDi
|
||||
const body = nodeOf(node.body);
|
||||
if (!body) return;
|
||||
if (body.type === "BlockStatement") visitSafeBlock(body, depth + 1, callbackBindings);
|
||||
else visit(body, depth + 1, callbackBindings);
|
||||
else if (canStaticallyProduceCallable(body)) {
|
||||
add(body, "CALLBACK_VALUE_NOT_ALLOWED: Authored callbacks cannot return callable values");
|
||||
} else visit(body, depth + 1, callbackBindings);
|
||||
};
|
||||
|
||||
const visitSafeBlock = (block: AstNode, depth: number, bindings: ReadonlySet<string>) => {
|
||||
@@ -110,7 +143,9 @@ export function validateCustomJsxTemplate(template: string): CustomJsxTemplateDi
|
||||
return;
|
||||
}
|
||||
const argument = nodeOf(statement.argument);
|
||||
if (argument) visit(argument, depth + 1, scopedBindings);
|
||||
if (argument && canStaticallyProduceCallable(argument)) {
|
||||
add(argument, "CALLBACK_VALUE_NOT_ALLOWED: Authored callbacks cannot return callable values");
|
||||
} else if (argument) visit(argument, depth + 1, scopedBindings);
|
||||
return;
|
||||
}
|
||||
if (statement.type !== "VariableDeclaration") {
|
||||
@@ -146,11 +181,7 @@ export function validateCustomJsxTemplate(template: string): CustomJsxTemplateDi
|
||||
add(declaration, `LOCAL_BINDING_REQUIRES_INITIALIZER: '${name}' requires an initializer`);
|
||||
continue;
|
||||
}
|
||||
if (
|
||||
initializer.type === "ArrowFunctionExpression" ||
|
||||
initializer.type === "FunctionExpression" ||
|
||||
isStaticallyCallableBinding(initializer)
|
||||
) {
|
||||
if (canStaticallyProduceCallable(initializer)) {
|
||||
add(initializer, "CALLBACK_VALUE_NOT_ALLOWED: Functions cannot be stored in local bindings");
|
||||
continue;
|
||||
}
|
||||
@@ -240,6 +271,10 @@ export function validateCustomJsxTemplate(template: string): CustomJsxTemplateDi
|
||||
if (node.type === "BinaryExpression" && !CUSTOM_JSX_BINARY_OPERATORS.has(String(node.operator))) {
|
||||
add(node, `Binary operator '${String(node.operator)}' is not supported`);
|
||||
}
|
||||
if (node.type === "BinaryExpression") {
|
||||
const statusDiagnostic = invalidRequestStatusComparison(node);
|
||||
if (statusDiagnostic) add(node, statusDiagnostic);
|
||||
}
|
||||
const left = nodeOf(node.left);
|
||||
const right = nodeOf(node.right);
|
||||
if (left) visit(left, depth + 1, bindings);
|
||||
@@ -268,6 +303,9 @@ export function validateCustomJsxTemplate(template: string): CustomJsxTemplateDi
|
||||
case "CallExpression": {
|
||||
const callee = nodeOf(node.callee);
|
||||
const arguments_ = nodesOf(node.arguments);
|
||||
if (node.optional) {
|
||||
add(node, "CALL_TARGET_NOT_ALLOWED: Optional calls are not supported");
|
||||
}
|
||||
if (callee?.type === "ArrowFunctionExpression") {
|
||||
if (nodesOf(callee.params).length > 0) {
|
||||
add(callee, "CALLBACK_VALUE_NOT_ALLOWED: Inline derived-value functions cannot declare parameters");
|
||||
@@ -275,6 +313,9 @@ export function validateCustomJsxTemplate(template: string): CustomJsxTemplateDi
|
||||
if (arguments_.length > 0) {
|
||||
add(node, "CALLBACK_VALUE_NOT_ALLOWED: Inline derived-value functions must be called without arguments");
|
||||
}
|
||||
if (nodeOf(callee.body)?.type !== "BlockStatement") {
|
||||
add(callee, "BLOCK_REQUIRES_FINAL_RETURN: Inline derived-value functions require a safe const block");
|
||||
}
|
||||
visitArrow(callee, depth + 1, bindings);
|
||||
arguments_.forEach((argument) => visit(argument, depth + 1, bindings));
|
||||
return;
|
||||
@@ -352,29 +393,7 @@ export function validateCustomJsxTemplate(template: string): CustomJsxTemplateDi
|
||||
}) as unknown as AstNode;
|
||||
visit(program, 0, ROOT_BINDINGS);
|
||||
} catch (error) {
|
||||
const parseError = error as Error & { pos?: number; loc?: { line?: number; column?: number } };
|
||||
const parseIndex = Math.max(0, (parseError.pos ?? 2) - 2);
|
||||
const missingInitializer = [...template.matchAll(/(?:\bconst|,)\s+([A-Za-z_$][\w$]*)\s*;/gu)].find((match) => {
|
||||
const semicolon = (match.index ?? 0) + match[0].length - 1;
|
||||
return Math.abs(semicolon - parseIndex) <= 1;
|
||||
});
|
||||
const duplicateCandidate = /Identifier '([^']+)' has already been declared/iu.exec(parseError.message);
|
||||
const duplicateBinding = duplicateCandidate;
|
||||
const message = missingInitializer
|
||||
? `LOCAL_BINDING_REQUIRES_INITIALIZER: '${missingInitializer[1]}' requires an initializer`
|
||||
: duplicateBinding
|
||||
? `DUPLICATE_LOCAL_BINDING: '${duplicateBinding[1]}' is already declared`
|
||||
: parseError.message;
|
||||
diagnostics.push({
|
||||
severity: "error",
|
||||
message,
|
||||
index: Math.max(0, (parseError.pos ?? 2) - 2),
|
||||
line: parseError.loc?.line ?? 1,
|
||||
column: Math.max(
|
||||
1,
|
||||
(parseError.loc?.column ?? 0) + (parseError.loc?.line === 1 || parseError.loc?.line === undefined ? -1 : 1),
|
||||
),
|
||||
});
|
||||
diagnostics.push(createCustomJsxParseDiagnostic(template, error));
|
||||
}
|
||||
|
||||
return diagnostics;
|
||||
|
||||
@@ -7,6 +7,7 @@ import {
|
||||
SafeJsxError,
|
||||
} from "./interpreter-foundation";
|
||||
import type { AstNode, Budget, InterpreterCallback } from "./interpreter-foundation";
|
||||
import { RESERVED_LOCAL_BINDINGS } from "./analyzer-language";
|
||||
import { isBlockedCustomJsxLexicalBinding } from "./policy";
|
||||
|
||||
interface InterpreterCallbackContext {
|
||||
@@ -14,20 +15,20 @@ interface InterpreterCallbackContext {
|
||||
evaluate(node: AstNode, environment: Environment, depth: number): unknown;
|
||||
}
|
||||
|
||||
const reservedLocalBindings = new Set(["data", "status", "options", "inputs"]);
|
||||
|
||||
export function createInterpreterCallback(node: AstNode, environment: Environment): InterpreterCallback {
|
||||
if (node.async || node.generator) {
|
||||
throw new SafeJsxError("CALLBACK_VALUE_NOT_ALLOWED: Async and generator callbacks are not supported");
|
||||
}
|
||||
const parameterNames = new Set<string>();
|
||||
const params = asNodeArray(node.params, "callback parameters").map((param) => {
|
||||
if (param.type !== "Identifier") throw new SafeJsxError("Callback parameters must be identifiers");
|
||||
if (param.type !== "Identifier") {
|
||||
throw new SafeJsxError("INVALID_LOCAL_DECLARATION: Callback parameters must be identifiers");
|
||||
}
|
||||
const name = String(param.name);
|
||||
if (isBlockedCustomJsxLexicalBinding(name)) {
|
||||
throw new SafeJsxError(`INVALID_LOCAL_DECLARATION: '${name}' is not a safe callback parameter name`);
|
||||
}
|
||||
if (reservedLocalBindings.has(name)) {
|
||||
if (RESERVED_LOCAL_BINDINGS.has(name)) {
|
||||
throw new SafeJsxError(`RESERVED_LOCAL_BINDING: '${name}' cannot be shadowed`);
|
||||
}
|
||||
if (parameterNames.has(name)) {
|
||||
@@ -55,9 +56,11 @@ export function runInterpreterCallback(
|
||||
values[name] = args[index];
|
||||
});
|
||||
const environment = new Environment(values, callback.environment);
|
||||
return callback.body.type === "BlockStatement"
|
||||
? executeSafeBlock(callback.body, environment, depth + 1, context)
|
||||
: context.evaluate(callback.body, environment, depth + 1);
|
||||
const result =
|
||||
callback.body.type === "BlockStatement"
|
||||
? executeSafeBlock(callback.body, environment, depth + 1, context)
|
||||
: context.evaluate(callback.body, environment, depth + 1);
|
||||
return ensureNonCallableResult(result);
|
||||
}
|
||||
|
||||
function executeSafeBlock(
|
||||
@@ -91,7 +94,9 @@ function executeSafeBlock(
|
||||
if (!isFinal) {
|
||||
throw new SafeJsxError("BLOCK_REQUIRES_FINAL_RETURN: Return is only allowed as the final statement");
|
||||
}
|
||||
return context.evaluate(asNode(statement.argument, "safe block return value"), scopedEnvironment, depth + 1);
|
||||
return ensureNonCallableResult(
|
||||
context.evaluate(asNode(statement.argument, "safe block return value"), scopedEnvironment, depth + 1),
|
||||
);
|
||||
}
|
||||
if (statement.type !== "VariableDeclaration") {
|
||||
throw new SafeJsxError(`UNSUPPORTED_BLOCK_STATEMENT: '${statement.type}' is not allowed in a safe block`);
|
||||
@@ -109,7 +114,7 @@ function executeSafeBlock(
|
||||
if (isBlockedCustomJsxLexicalBinding(name)) {
|
||||
throw new SafeJsxError(`INVALID_LOCAL_DECLARATION: '${name}' is not a safe local binding name`);
|
||||
}
|
||||
if (reservedLocalBindings.has(name)) {
|
||||
if (RESERVED_LOCAL_BINDINGS.has(name)) {
|
||||
throw new SafeJsxError(`RESERVED_LOCAL_BINDING: '${name}' cannot be shadowed`);
|
||||
}
|
||||
if (localNames.has(name)) {
|
||||
@@ -129,3 +134,10 @@ function executeSafeBlock(
|
||||
}
|
||||
throw new SafeJsxError("BLOCK_REQUIRES_FINAL_RETURN: Safe block did not return a value");
|
||||
}
|
||||
|
||||
function ensureNonCallableResult(value: unknown): unknown {
|
||||
if (typeof value === "function" || isInterpreterCallback(value)) {
|
||||
throw new SafeJsxError("CALLBACK_VALUE_NOT_ALLOWED: Authored callbacks cannot return callable values");
|
||||
}
|
||||
return value;
|
||||
}
|
||||
@@ -20,7 +20,12 @@ export function parseCustomJsxTemplate(template: string): AstNode {
|
||||
allowReturnOutsideFunction: false,
|
||||
}) as unknown as AstNode;
|
||||
} catch (error) {
|
||||
throw new SafeJsxError(error instanceof Error ? error.message : "Unable to parse JSX template");
|
||||
const message = error instanceof Error ? error.message : "Unable to parse JSX template";
|
||||
throw new SafeJsxError(
|
||||
/Argument name clash/iu.test(message)
|
||||
? "DUPLICATE_LOCAL_BINDING: Callback parameter names must be unique"
|
||||
: message,
|
||||
);
|
||||
}
|
||||
const body = asNodeArray(program.body, "program body");
|
||||
if (body.length !== 1 || body[0]?.type !== "ExpressionStatement") {
|
||||
|
||||
@@ -264,6 +264,11 @@ class Interpreter {
|
||||
const callee = asNode(node.callee, "call target");
|
||||
const argumentNodes = asNodeArray(node.arguments, "call arguments");
|
||||
if (callee.type === "ArrowFunctionExpression") {
|
||||
if (callee.body && asNode(callee.body, "inline derived-value body").type !== "BlockStatement") {
|
||||
throw new SafeJsxError(
|
||||
"BLOCK_REQUIRES_FINAL_RETURN: Inline derived-value functions require a safe const block",
|
||||
);
|
||||
}
|
||||
if (asNodeArray(callee.params, "inline derived-value parameters").length > 0) {
|
||||
throw new SafeJsxError("CALLBACK_VALUE_NOT_ALLOWED: Inline derived-value functions cannot declare parameters");
|
||||
}
|
||||
|
||||
@@ -59,7 +59,7 @@ export function buildTrustedRecursiveList(options: RecursiveListBuildOptions): T
|
||||
return [diagnosticNode("diagnostic:root-data", "RecursiveList data must be an object or an array")];
|
||||
}
|
||||
|
||||
const willExceedNodeLimit = exceedsRecursiveListNodeLimit(roots, childrenPath, maxDepth, maxNodes);
|
||||
const willExceedNodeLimit = exceedsRecursiveListNodeLimit(roots, childrenPath, maxDepth, maxNodes, options.budget);
|
||||
let renderedRowCount = 0;
|
||||
let diagnosticCount = 0;
|
||||
let limitSentinelRendered = false;
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { SafeJsxError } from "./interpreter-foundation";
|
||||
import type { Budget } from "./interpreter-foundation";
|
||||
import { CUSTOM_JSX_BLOCKED_PROPERTIES, normalizeCustomJsxProperty } from "./policy";
|
||||
import { ownProperty } from "./safe-properties";
|
||||
|
||||
@@ -12,25 +13,27 @@ export function exceedsRecursiveListNodeLimit(
|
||||
childrenPath: readonly string[],
|
||||
maxDepth: number,
|
||||
maxNodes: number,
|
||||
budget: Budget,
|
||||
) {
|
||||
let rows = 0;
|
||||
const charge = () => {
|
||||
const charge = (depth: number) => {
|
||||
budget.operation(depth + 1);
|
||||
rows += 1;
|
||||
return rows > maxNodes;
|
||||
};
|
||||
const visit = (values: readonly unknown[], depth: number, ancestors: ReadonlySet<object>): boolean => {
|
||||
for (const value of values) {
|
||||
if (charge()) return true;
|
||||
if (charge(depth)) return true;
|
||||
if (!isObject(value) || ancestors.has(value)) continue;
|
||||
|
||||
const rawChildren = readRestrictedPath(value, childrenPath);
|
||||
if (rawChildren !== undefined && rawChildren !== null && !Array.isArray(rawChildren)) {
|
||||
if (charge()) return true;
|
||||
if (charge(depth + 1)) return true;
|
||||
continue;
|
||||
}
|
||||
if (!Array.isArray(rawChildren) || rawChildren.length === 0) continue;
|
||||
if (depth + 1 >= maxDepth) {
|
||||
if (charge()) return true;
|
||||
if (charge(depth + 1)) return true;
|
||||
continue;
|
||||
}
|
||||
if (visit(rawChildren, depth + 1, new Set(ancestors).add(value))) return true;
|
||||
@@ -74,7 +77,7 @@ export function clampWithWarning(
|
||||
const validNumber = typeof value === "number" && Number.isFinite(value);
|
||||
const number = validNumber ? Math.trunc(value) : fallback;
|
||||
const clamped = Math.min(maximum, Math.max(minimum, number));
|
||||
if (value !== undefined && (!validNumber || number !== clamped)) {
|
||||
if (value !== undefined && (!validNumber || value !== number || number !== clamped)) {
|
||||
warnings.add(`RECURSIVE_LIST_LIMIT_CLAMPED: ${name} was clamped to ${clamped}`);
|
||||
}
|
||||
return clamped;
|
||||
|
||||
@@ -18,7 +18,7 @@ export type {
|
||||
|
||||
export function TrustedRecursiveList({ ...props }: TrustedRecursiveListProps) {
|
||||
return (
|
||||
<RecursiveListErrorBoundary resetKey={props.nodes}>
|
||||
<RecursiveListErrorBoundary resetKey={props.nodes} code="RECURSIVE_LIST_RENDER_ERROR">
|
||||
<TrustedRecursiveListInner {...props} />
|
||||
</RecursiveListErrorBoundary>
|
||||
);
|
||||
@@ -51,16 +51,13 @@ function TrustedRecursiveListInner({
|
||||
}
|
||||
|
||||
class RecursiveListErrorBoundary extends Component<
|
||||
{ children: ReactNode; resetKey: readonly TreeNodeData[] },
|
||||
{ error: Error | null; resetKey: readonly TreeNodeData[] }
|
||||
{ children: ReactNode; resetKey: unknown; code: string },
|
||||
{ error: Error | null; resetKey: unknown }
|
||||
> {
|
||||
public state = { error: null, resetKey: this.props.resetKey } as {
|
||||
error: Error | null;
|
||||
resetKey: readonly TreeNodeData[];
|
||||
};
|
||||
public state = { error: null, resetKey: this.props.resetKey } as { error: Error | null; resetKey: unknown };
|
||||
public static getDerivedStateFromProps(
|
||||
props: Readonly<{ children: ReactNode; resetKey: readonly TreeNodeData[] }>,
|
||||
state: Readonly<{ error: Error | null; resetKey: readonly TreeNodeData[] }>,
|
||||
props: Readonly<{ children: ReactNode; resetKey: unknown; code: string }>,
|
||||
state: Readonly<{ error: Error | null; resetKey: unknown }>,
|
||||
) {
|
||||
if (props.resetKey === state.resetKey) return null;
|
||||
return { error: null, resetKey: props.resetKey };
|
||||
@@ -72,7 +69,7 @@ class RecursiveListErrorBoundary extends Component<
|
||||
public render() {
|
||||
return this.state.error ? (
|
||||
<Text size="xs" c="red">
|
||||
RECURSIVE_LIST_RENDER_ERROR: {this.state.error.message}
|
||||
{this.props.code}: {this.state.error.message}
|
||||
</Text>
|
||||
) : (
|
||||
this.props.children
|
||||
@@ -88,7 +85,7 @@ function TrustedRecursiveListNode({
|
||||
gap,
|
||||
}: RenderTreeNodePayload & { gap: MantineSpacing }) {
|
||||
return (
|
||||
<Group {...elementProps} gap={gap} wrap="nowrap" py={2}>
|
||||
<Group {...elementProps} aria-expanded={hasChildren ? expanded : undefined} gap={gap} wrap="nowrap" py={2}>
|
||||
<Box w={16} h={16} style={{ flex: "0 0 16px" }} aria-hidden>
|
||||
{hasChildren && (
|
||||
<IconChevronRight
|
||||
@@ -97,7 +94,11 @@ function TrustedRecursiveListNode({
|
||||
/>
|
||||
)}
|
||||
</Box>
|
||||
<Box style={{ minWidth: 0, flex: 1 }}>{node.label}</Box>
|
||||
<Box style={{ minWidth: 0, flex: 1 }}>
|
||||
<RecursiveListErrorBoundary resetKey={node.label} code="RECURSIVE_LIST_BRANCH_RENDER_ERROR">
|
||||
{node.label}
|
||||
</RecursiveListErrorBoundary>
|
||||
</Box>
|
||||
</Group>
|
||||
);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user