bynk_ts/program/verbatim.rs
1#[cfg(doc)]
2use super::TsType;
3use super::{TsArrowBody, TsDecl, TsExpr, TsObjectEntry, TsProgram, TsStmt, TsStmtKind};
4
5impl TsProgram {
6 /// Every `TsStmtKind::Verbatim`/[`TsExpr::VerbatimExpr`] leaf's own
7 /// text, tagged by its [`VerbatimOrigin`] — found by walking every
8 /// statement/expression container in the tree, not just the top level.
9 /// #1538's own real gap: nothing before this walked a whole `TsProgram`
10 /// to find every opaque leaf nested inside real structured content (a
11 /// `Verbatim` inside an `if` branch, a `VerbatimExpr` inside a `Call`'s
12 /// arguments, …) — the only prior way to inspect a `Verbatim`'s content
13 /// was scanning the *whole printed document* text
14 /// ([`crate::verbatim_violations`] doesn't distinguish opaque text from
15 /// a real node's own printed output), which also flags real, structured
16 /// nodes that happen to print the substring `any` (`TsType::named("any")`
17 /// residue, tracked separately by `xtask`'s own `ts_any` probe) — a
18 /// false positive for *this* check, whose whole point is the opaque
19 /// escape hatch specifically. `TsStmtKind` is `pub(crate)`, so only code
20 /// inside this crate can walk it directly; this method is the public
21 /// surface a caller outside `bynk-ts` (`bynk`'s own
22 /// `tests/tsc_verify.rs`) uses instead.
23 ///
24 /// Every container is walked exhaustively (`match`, no wildcard arm) so
25 /// a future `TsStmt`/`TsExpr` variant fails to compile here until this
26 /// walker accounts for it — the same "compile-time construct, not a
27 /// grep" discipline [`VerbatimOrigin`]'s own doc already states for
28 /// itself. `TsStmtKind::Raw` is deliberately not walked into any
29 /// deeper than skipped: it carries no `VerbatimOrigin` at all (ADR
30 /// `arc-c-lower-rs-permanent-exclusion`'s own permanent exclusion, a
31 /// different bucket from `Verbatim`'s temporary conversion residue), so
32 /// it has nothing this method could report. [`TsType`] positions are
33 /// never walked — no `TsType` variant can hold a `Verbatim`/
34 /// `VerbatimExpr` leaf, so there is nothing to find there.
35 ///
36 /// **A real gap this walker exposed (#1538):** no `VerbatimOrigin::Emit`
37 /// leaf can currently reach a `TsProgram` returned from `bynk-emit` at
38 /// all. Every real `bynk-emit/src/emitter/emit.rs` construction site
39 /// builds a [`TsExpr::VerbatimExpr`], immediately prints it
40 /// (`crate::print_expr`/`crate::print_stmt`), and splices the resulting
41 /// text into a plain `String` a caller further up wraps in
42 /// `TsStmtKind::Raw` — not tagged with a `VerbatimOrigin` at all, and
43 /// so invisible to this walker by design (see `TsStmtKind::Raw`'s own
44 /// doc). `VerbatimExpr`'s own doc promises this content is "visible to
45 /// `crate::verbatim_violations`"; today that's only true at the instant
46 /// of construction, not once the surrounding document is returned —
47 /// closing it needs a real Arc C conversion slice, not a change here.
48 pub fn verbatim_content(&self) -> Vec<(VerbatimOrigin, &str)> {
49 let mut out = Vec::new();
50 for stmt in &self.stmts {
51 walk_stmt(stmt, &mut out);
52 }
53 out
54 }
55}
56
57fn walk_stmt<'a>(stmt: &'a TsStmt, out: &mut Vec<(VerbatimOrigin, &'a str)>) {
58 match &stmt.kind {
59 TsStmtKind::Verbatim { origin, text } => out.push((*origin, text.as_str())),
60 TsStmtKind::Decl(decl) => walk_decl(decl, out),
61 TsStmtKind::Const { init, .. } => walk_expr(init, out),
62 TsStmtKind::Let { init, .. } => {
63 if let Some(e) = init {
64 walk_expr(e, out);
65 }
66 }
67 TsStmtKind::ExprStmt(e) => walk_expr(e, out),
68 TsStmtKind::Return(e) => {
69 if let Some(e) = e {
70 walk_expr(e, out);
71 }
72 }
73 TsStmtKind::Throw(e) => walk_expr(e, out),
74 TsStmtKind::If {
75 cond,
76 then_branch,
77 else_branch,
78 same_line_else: _,
79 } => {
80 walk_expr(cond, out);
81 walk_stmt(then_branch, out);
82 if let Some(e) = else_branch {
83 walk_stmt(e, out);
84 }
85 }
86 TsStmtKind::ForOf {
87 binding: _,
88 iter,
89 body,
90 is_await: _,
91 } => {
92 walk_expr(iter, out);
93 walk_stmt(body, out);
94 }
95 TsStmtKind::While { cond, body } => {
96 walk_expr(cond, out);
97 walk_stmt(body, out);
98 }
99 TsStmtKind::Labelled { label: _, body } => walk_stmt(body, out),
100 TsStmtKind::LetList(decls) => {
101 for d in decls {
102 if let Some(e) = &d.init {
103 walk_expr(e, out);
104 }
105 }
106 }
107 TsStmtKind::For {
108 name: _,
109 init,
110 test,
111 body,
112 } => {
113 walk_expr(init, out);
114 walk_expr(test, out);
115 walk_stmt(body, out);
116 }
117 TsStmtKind::TryCatch {
118 try_block,
119 catch_param: _,
120 catch_block,
121 finally_block,
122 } => {
123 walk_stmt(try_block, out);
124 if let Some(c) = catch_block {
125 walk_stmt(c, out);
126 }
127 if let Some(f) = finally_block {
128 walk_stmt(f, out);
129 }
130 }
131 TsStmtKind::Block(stmts) | TsStmtKind::InlineBlock(stmts) => {
132 for s in stmts {
133 walk_stmt(s, out);
134 }
135 }
136 TsStmtKind::Continue(_) | TsStmtKind::Break(_) => {}
137 TsStmtKind::Assign { target, value } => {
138 walk_expr(target, out);
139 walk_expr(value, out);
140 }
141 TsStmtKind::Comment(_) | TsStmtKind::DocComment(_) | TsStmtKind::Blank => {}
142 TsStmtKind::Switch {
143 discriminant,
144 cases,
145 } => {
146 walk_expr(discriminant, out);
147 for case in cases {
148 for t in &case.labels {
149 walk_expr(t, out);
150 }
151 for s in &case.body {
152 walk_stmt(s, out);
153 }
154 }
155 }
156 TsStmtKind::Increment(e) => walk_expr(e, out),
157 TsStmtKind::Raw(..) => {}
158 }
159}
160
161fn walk_decl<'a>(decl: &'a TsDecl, out: &mut Vec<(VerbatimOrigin, &'a str)>) {
162 match decl {
163 TsDecl::Import { .. }
164 | TsDecl::ImportNamespace { .. }
165 | TsDecl::ImportDefault { .. }
166 | TsDecl::ReExport { .. }
167 | TsDecl::ExportNames { .. }
168 | TsDecl::ReExportAll { .. }
169 | TsDecl::Interface { .. }
170 | TsDecl::TypeAlias { .. }
171 | TsDecl::DeclareConst { .. } => {}
172 TsDecl::Export(inner) => walk_decl(inner, out),
173 TsDecl::ConstDecl { init, .. } => walk_expr(init, out),
174 TsDecl::Class {
175 constructor,
176 methods,
177 ..
178 } => {
179 if let Some(ctor) = constructor {
180 for s in &ctor.body {
181 walk_stmt(s, out);
182 }
183 }
184 for m in methods {
185 for s in &m.body {
186 walk_stmt(s, out);
187 }
188 }
189 }
190 TsDecl::Function { body, .. } => {
191 for s in body {
192 walk_stmt(s, out);
193 }
194 }
195 TsDecl::ExportDefault(e) => walk_expr(e, out),
196 }
197}
198
199fn walk_arrow_body<'a>(body: &'a TsArrowBody, out: &mut Vec<(VerbatimOrigin, &'a str)>) {
200 match body {
201 TsArrowBody::Expr(e) => walk_expr(e, out),
202 TsArrowBody::Block(stmts) | TsArrowBody::Multiline(stmts) | TsArrowBody::Flat(stmts) => {
203 for s in stmts {
204 walk_stmt(s, out);
205 }
206 }
207 }
208}
209
210fn walk_object_entry<'a>(entry: &'a TsObjectEntry, out: &mut Vec<(VerbatimOrigin, &'a str)>) {
211 match entry {
212 TsObjectEntry::Prop { value, .. } => walk_expr(value, out),
213 TsObjectEntry::Shorthand(_) => {}
214 TsObjectEntry::Method { body, .. } => {
215 for s in body {
216 walk_stmt(s, out);
217 }
218 }
219 TsObjectEntry::Spread(e) => walk_expr(e, out),
220 }
221}
222
223fn walk_expr<'a>(expr: &'a TsExpr, out: &mut Vec<(VerbatimOrigin, &'a str)>) {
224 match expr {
225 TsExpr::Ident(_) | TsExpr::RawUsing(..) => {}
226 TsExpr::VerbatimExpr(text, origin) => out.push((*origin, text.as_str())),
227 TsExpr::Member { object, .. } | TsExpr::OptionalMember { object, .. } => {
228 walk_expr(object, out);
229 }
230 TsExpr::TypeApp { callee, .. } => walk_expr(callee, out),
231 TsExpr::Index { object, index } | TsExpr::OptionalIndex { object, index } => {
232 walk_expr(object, out);
233 walk_expr(index, out);
234 }
235 TsExpr::Arrow { body, .. } => walk_arrow_body(body, out),
236 TsExpr::DefaultIife { default, body, .. } => {
237 walk_expr(default, out);
238 for s in body {
239 walk_stmt(s, out);
240 }
241 }
242 TsExpr::Call { callee, args } | TsExpr::New { callee, args } => {
243 walk_expr(callee, out);
244 for a in args {
245 walk_expr(a, out);
246 }
247 }
248 TsExpr::Object { entries, .. } => {
249 for e in entries {
250 walk_object_entry(e, out);
251 }
252 }
253 TsExpr::Array { items, .. } | TsExpr::LinesArray(items) => {
254 for i in items {
255 walk_expr(i, out);
256 }
257 }
258 TsExpr::TemplateLit { parts: _, exprs } => {
259 for e in exprs {
260 walk_expr(e, out);
261 }
262 }
263 TsExpr::Await(e) => walk_expr(e, out),
264 TsExpr::As { expr, ty: _ } => walk_expr(expr, out),
265 TsExpr::AsConst(expr) => walk_expr(expr, out),
266 TsExpr::Unary { op: _, expr } => walk_expr(expr, out),
267 TsExpr::Binary { op: _, left, right } => {
268 walk_expr(left, out);
269 walk_expr(right, out);
270 }
271 TsExpr::Conditional {
272 test,
273 consequent,
274 alternate,
275 }
276 | TsExpr::MultilineConditional {
277 test,
278 consequent,
279 alternate,
280 } => {
281 walk_expr(test, out);
282 walk_expr(consequent, out);
283 walk_expr(alternate, out);
284 }
285 TsExpr::Paren(e) | TsExpr::Spread(e) | TsExpr::NonNull(e) | TsExpr::Yield(e) => {
286 walk_expr(e, out)
287 }
288 TsExpr::Assign { target, value }
289 | TsExpr::AddAssign { target, value }
290 | TsExpr::NullishAssign { target, value } => {
291 walk_expr(target, out);
292 walk_expr(value, out);
293 }
294 TsExpr::Sequence(items) => {
295 for i in items {
296 walk_expr(i, out);
297 }
298 }
299 TsExpr::OptionalCall { callee, args } => {
300 walk_expr(callee, out);
301 for a in args {
302 walk_expr(a, out);
303 }
304 }
305 TsExpr::Function { body, .. } => {
306 for s in body {
307 walk_stmt(s, out);
308 }
309 }
310 TsExpr::Lit(_) => {}
311 }
312}
313
314/// Which family of residual, not-yet-converted emission a [`TsStmt::verbatim`]
315/// statement came from. A closed enum, deliberately — "makes the ratchet a
316/// compile-time construct, not a grep" (Q2's own settling text). Named
317/// file-by-file as Arc C actually needs them (`ast_importers`'s own five-file
318/// floor is the precedent for how this track names residue), not
319/// pre-populated for the whole ~19-slice Arc C schedule up front.
320///
321/// Deliberately **not** `#[non_exhaustive]`: a `match` over every variant —
322/// in this crate, or in `bynk-emit` once Arc C reads it — must fail to
323/// compile the moment a new variant is added, forcing every consumer to
324/// account for it explicitly. A non-exhaustive enum would let a wildcard arm
325/// silently absorb a new residue family instead, exactly the "grep, not a
326/// compile-time construct" Q2's own settling text rejected. P7.8's own
327/// grounding work found that `Contracts`/`Secrets`/`RuntimeUse` were seeded
328/// against files that turn out not to need `bynk-ts` conversion at all
329/// (`design/tracks/the-typescript-tree.md` §9) — recorded there, not fixed
330/// by removing the variants here, since that's separate follow-on work.
331#[derive(Debug, Clone, Copy, PartialEq, Eq)]
332pub enum VerbatimOrigin {
333 /// `bynk-emit/src/emitter/contracts.rs`.
334 Contracts,
335 /// `bynk-emit/src/emitter/secrets.rs`.
336 Secrets,
337 /// `bynk-emit/src/emitter/runtime_use.rs`.
338 RuntimeUse,
339 /// P7.6's own transitional wrap (#1309): the whole of a still-`String`-
340 /// producing `bynk-emit` document (an entry point, `compose.ts`, the
341 /// runtime module, an adapter binding, a test module, …), carried into
342 /// `Document::Ts` so `Artefacts` never stores a bare `String` for TS
343 /// output (R7.8) even before Arc C converts the function that built it.
344 /// Deliberately **not** file-specific like the three variants above —
345 /// this is Arc B's own infrastructure slice, not an Arc C conversion, so
346 /// it covers everything Arc C hasn't reached yet, one call site per
347 /// document `bynk-emit/src/project.rs`/`project/tests_emit.rs`
348 /// constructs (not funnelled through a shared helper: a shared wrap
349 /// point would collapse every one of those call sites to a single
350 /// textual `TsStmt::verbatim(` occurrence, defeating `verbatim_sites`'
351 /// own purpose of counting how much is genuinely still unconverted).
352 /// Retires site by site as Arc C converts each underlying emitter
353 /// function to return a real `TsProgram` directly — at which point that
354 /// document's own construction site stops calling `TsStmt::verbatim` at
355 /// all, not by this variant being deleted first.
356 NotYetConverted,
357 /// `bynk-emit/src/emitter/emit.rs`'s own residual [`TsExpr::VerbatimExpr`]
358 /// leaves (#1539) — a generic-typed callee, a printer-unsafe nested `As`
359 /// chain, a block-bodied ICU IIFE, and a pre-escaped/unescaped predicate
360 /// message, none reducible to a real node without expanding this crate's
361 /// own type algebra past what #1539 asks for. Expression-level, not
362 /// statement-level like the three file-specific variants above (this is
363 /// the first `VerbatimOrigin` a `TsExpr` carries, not a `TsStmt`), but
364 /// named the same file-by-file way.
365 Emit,
366 /// The `__formDecode` / `__formString` field-spec table (1A.35a part C, #3736 Decision E): the
367 /// array literal of `{ name, kind, list, option }` records the router's `@form` path reads a
368 /// body by, which `workers_entry::field_spec_source` returns as TypeScript *source text*. The
369 /// text is foreign (it is the router's own spelling, shared with the entry point), so a
370 /// `Form.decode[T]` or `Form.encode` call carries it as one expression leaf rather than
371 /// re-spelling it as nodes. Built at the one site `form_field_specs`
372 /// (`bynk-emit/src/emitter/lower_call/codec_statics.rs`).
373 FormFieldSpecs,
374}