|
| 1 | +import { onCleanup, type Children } from "@alloy-js/core"; |
| 2 | +import { useTSNamePolicy } from "../name-policy.js"; |
| 3 | +import type { ParameterDescriptor } from "../parameter-descriptor.js"; |
| 4 | +import { createValueSymbol, TSSymbolFlags } from "../symbols/index.js"; |
| 5 | +import { BlockScope } from "./BlockScope.jsx"; |
| 6 | + |
| 7 | +export interface TryStatementProps { |
| 8 | + children: Children; |
| 9 | +} |
| 10 | + |
| 11 | +/** |
| 12 | + * A try statement. |
| 13 | + * |
| 14 | + * @example |
| 15 | + * ```tsx |
| 16 | + * <TryStatement> |
| 17 | + * // risky operation |
| 18 | + * </TryStatement> |
| 19 | + * <CatchClause parameter="error"> |
| 20 | + * console.error(error); |
| 21 | + * </CatchClause> |
| 22 | + * ``` |
| 23 | + * renders to |
| 24 | + * ```ts |
| 25 | + * try { |
| 26 | + * // risky operation |
| 27 | + * } catch (error) { |
| 28 | + * console.error(error); |
| 29 | + * } |
| 30 | + * ``` |
| 31 | + */ |
| 32 | +export function TryStatement(props: TryStatementProps) { |
| 33 | + return ( |
| 34 | + <> |
| 35 | + try <BlockScope>{props.children}</BlockScope> |
| 36 | + </> |
| 37 | + ); |
| 38 | +} |
| 39 | + |
| 40 | +export interface CatchClauseProps { |
| 41 | + parameter?: ParameterDescriptor | string; |
| 42 | + children: Children; |
| 43 | +} |
| 44 | + |
| 45 | +/** |
| 46 | + * A catch clause of a try statement. |
| 47 | + * |
| 48 | + * @example |
| 49 | + * ```tsx |
| 50 | + * <CatchClause parameter={{ name: "error", type: "unknown" }}> |
| 51 | + * console.error(error); |
| 52 | + * </CatchClause> |
| 53 | + * ``` |
| 54 | + * renders to |
| 55 | + * ```ts |
| 56 | + * catch (error: unknown) { |
| 57 | + * console.error(error); |
| 58 | + * } |
| 59 | + * ``` |
| 60 | + * |
| 61 | + * @remarks |
| 62 | + * The parameter can be omitted for a catch-all clause, or provided as a string |
| 63 | + * or {@link ParameterDescriptor}. When using a descriptor, a refkey can be provided |
| 64 | + * to reference the parameter within the catch block. Note that TypeScript only allows |
| 65 | + * `any` or `unknown` as catch parameter types. |
| 66 | + */ |
| 67 | +export function CatchClause(props: CatchClauseProps) { |
| 68 | + if (!props.parameter) { |
| 69 | + return ( |
| 70 | + <> |
| 71 | + {" "} |
| 72 | + catch <BlockScope>{props.children}</BlockScope> |
| 73 | + </> |
| 74 | + ); |
| 75 | + } |
| 76 | + |
| 77 | + const namePolicy = useTSNamePolicy(); |
| 78 | + const paramDesc = |
| 79 | + typeof props.parameter === "string" ? |
| 80 | + { name: props.parameter } |
| 81 | + : props.parameter; |
| 82 | + |
| 83 | + const symbol = createValueSymbol(paramDesc.name, { |
| 84 | + refkeys: paramDesc.refkey, |
| 85 | + tsFlags: TSSymbolFlags.ParameterSymbol, |
| 86 | + metadata: paramDesc.metadata, |
| 87 | + namePolicy: namePolicy.for("parameter"), |
| 88 | + }); |
| 89 | + |
| 90 | + onCleanup(() => { |
| 91 | + symbol.delete(); |
| 92 | + }); |
| 93 | + |
| 94 | + const param = ( |
| 95 | + <> |
| 96 | + {symbol.name} |
| 97 | + {paramDesc.type && <>: {paramDesc.type}</>} |
| 98 | + </> |
| 99 | + ); |
| 100 | + |
| 101 | + return ( |
| 102 | + <> |
| 103 | + {" "} |
| 104 | + catch ({param}) <BlockScope>{props.children}</BlockScope> |
| 105 | + </> |
| 106 | + ); |
| 107 | +} |
| 108 | + |
| 109 | +export interface FinallyClauseProps { |
| 110 | + children: Children; |
| 111 | +} |
| 112 | + |
| 113 | +/** |
| 114 | + * A finally clause of a try statement. |
| 115 | + * |
| 116 | + * @example |
| 117 | + * ```tsx |
| 118 | + * <TryStatement> |
| 119 | + * // risky operation |
| 120 | + * </TryStatement> |
| 121 | + * <FinallyClause> |
| 122 | + * // cleanup |
| 123 | + * </FinallyClause> |
| 124 | + * ``` |
| 125 | + * renders to |
| 126 | + * ```ts |
| 127 | + * try { |
| 128 | + * // risky operation |
| 129 | + * } finally { |
| 130 | + * // cleanup |
| 131 | + * } |
| 132 | + * ``` |
| 133 | + */ |
| 134 | +export function FinallyClause(props: FinallyClauseProps) { |
| 135 | + return ( |
| 136 | + <> |
| 137 | + {" "} |
| 138 | + finally <BlockScope>{props.children}</BlockScope> |
| 139 | + </> |
| 140 | + ); |
| 141 | +} |
0 commit comments