Skip to main content

pred_condition_and_message

Function pred_condition_and_message 

Source
pub fn pred_condition_and_message(
    pred: &PredKind,
    receiver: &str,
) -> (TsExpr, String)
Expand description

#661 (Decision D)/#70 review: the one PredKind → runtime-check mapping, shared by the owner-side check (emit::emit_pred_check, over a value binding) and the boundary-side inline check (serialisation::emit_inline_pred_check, over a json binding) — the two used to hand-roll this mapping independently, pinned identical only by a comment, so amending one (e.g. the Matches regex’s ^(?:…)$ anchoring) could silently drift from the other. receiver is the bound name the generated condition reads (value or json); the returned message is the same either side of the boundary by construction.

#1471: cond is a real crate::TsExpr, not opaque text — the blocker (crate::TsBinaryOp had no >=/<=) is resolved by that enum’s own new GreaterThanEq/LessThanEq variants. Both real callers wrap the returned expression in their own Unary::Not/Paren unchanged; only this function’s own arms changed, from format!ing condition text to building the equivalent node tree.

Review of #1336: both real callers (emit::emit_pred_check, serialisation::emit_inline_pred_check) splice the returned message straight into a TypeScript string literal unescaped — safe today only because every arm’s own message is either static English text or already escape_ts_literal-escaped (the Matches arm’s own pattern). A future arm returning raw, unescaped text (a predicate carrying a user-supplied string operand, say) would emit a malformed or injectable literal at both call sites, and nothing in the existing fixture corpus would catch it. This invariant must match at every arm added here, not just the ones that exist today — return plain text or already-escape_ts_literal-escaped text only. msg stays exactly this opaque String, unchanged by #1471: only the cond side of the pair became a real node (see the Matches arm below for why the message keeps its own already-escaped copy of the pattern separate from the condition’s raw, unescaped one).