tsv_ts 0.8.0

TypeScript/JS formatter, type stripper and acorn-compatible parser — part of tsv
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
// Type alias, function, and class declaration writers.

use super::expressions::{
    ChainState, ExprFlags, write_expression, write_expression_inner, write_expressions,
};
use super::statements::{write_block_statement, write_statement};
use super::types::{
    write_entity_name, write_index_signature, write_type, write_type_parameter_instantiation,
};
use super::{
    Ctx, JsonWriter, close_node, node_header, write_array, write_function_flags_fields,
    write_identifier_plain, write_identifier_with_optional, write_name_field, write_or_null,
    write_return_type_field, write_type_annotation_field, write_type_parameters_field,
};
use crate::ast;
use tsv_lang::Span;
use tsv_lang::source_scan::{self, TriviaProfile};

/// Emits a `Decorator` node: an unparenthesized decorator's call/member
/// spine omits `optional`; a parenthesized `@(expr)` rides the full expression
/// parser and keeps it. Parens are stripped from the expression, so the only
/// signal is the span gap.
pub(super) fn write_decorator(w: &mut JsonWriter, decorator: &ast::Decorator<'_>, ctx: &Ctx<'_>) {
    let parenthesized = decorator.span.end > decorator.expression.span().end;
    node_header(w, "Decorator", decorator.span, ctx);
    w.raw(",\"expression\":");
    write_expression_inner(
        w,
        &decorator.expression,
        ctx,
        ExprFlags {
            // A decorator expression is a fresh chain root — nothing above it
            // has resolved its spine either way.
            chain: ChainState::Unresolved,
            force_optional: false,
            strip_optional: !parenthesized,
        },
    );
    close_node(w, "Decorator", decorator.span, ctx);
}

/// Emit a `decorators` field when the internal node carries decorators
/// (`Option<Vec>` with skip-if-none: present ⇒ emitted, even empty).
pub(super) fn write_decorators_field(
    w: &mut JsonWriter,
    decorators: Option<&[ast::Decorator<'_>]>,
    ctx: &Ctx<'_>,
) {
    if let Some(decs) = decorators {
        w.raw(",\"decorators\":");
        write_array(w, decs, |w, d| write_decorator(w, d, ctx));
    }
}

/// Emits a `TSTypeAliasDeclaration` node. Field order: `id`,
/// `typeParameters?`, `typeAnnotation`, `declare` (only when true).
pub(super) fn write_type_alias_declaration(
    w: &mut JsonWriter,
    type_alias: &ast::TSTypeAliasDeclaration<'_>,
    span: Span,
    ctx: &Ctx<'_>,
) {
    node_header(w, "TSTypeAliasDeclaration", span, ctx);
    w.raw(",\"id\":");
    write_identifier_plain(w, &type_alias.id, ctx);
    write_type_parameters_field(w, type_alias.type_parameters.as_ref(), ctx);
    w.raw(",\"typeAnnotation\":");
    write_type(w, type_alias.type_annotation, ctx);
    if type_alias.declare {
        w.raw(",\"declare\":true");
    }
    close_node(w, "TSTypeAliasDeclaration", span, ctx);
}

/// Emits a `FunctionDeclaration` node. Field order: `id` (nullable),
/// `expression` (always false), `generator`, `async`, `typeParameters?`,
/// `params`, `returnType?`, `body`.
pub(super) fn write_function_declaration(
    w: &mut JsonWriter,
    func_decl: &ast::FunctionDeclaration<'_>,
    ctx: &Ctx<'_>,
) {
    node_header(w, "FunctionDeclaration", func_decl.span, ctx);
    w.raw(",\"id\":");
    write_or_null(w, func_decl.id.as_ref(), |w, id| {
        write_identifier_plain(w, id, ctx);
    });
    write_function_flags_fields(w, func_decl.generator, func_decl.r#async);
    write_type_parameters_field(w, func_decl.type_parameters.as_ref(), ctx);
    w.raw(",\"params\":");
    write_expressions(w, func_decl.params, ctx);
    write_return_type_field(w, func_decl.return_type.as_ref(), ctx);
    w.raw(",\"body\":");
    write_block_statement(w, &func_decl.body, ctx);
    close_node(w, "FunctionDeclaration", func_decl.span, ctx);
}

/// The super-class wrap decision: when
/// `extends Base<T>` sits on a different line from the closing `>` of the type
/// parameters, acorn-typescript emits `superClass` as a
/// `TSInstantiationExpression` consuming `superTypeParameters`. Returns the
/// combined span to wrap with, or `None` for the plain shape. All offsets are
/// internal byte offsets (the same-line scan byte-indexes `source`).
fn super_class_wrap_span(
    type_params_span: Option<Span>,
    super_class: Option<&ast::Expression<'_>>,
    super_type_parameters_end: Option<u32>,
    source: &str,
) -> Option<Span> {
    let tp_span = type_params_span?;
    // The public super-class node starts at the CommentBoundPair-unwrapped inner
    // expression.
    let sc_start = super_class.map(|e| e.unwrap_comment_bound_pairs().span().start)?;
    let stp_end = super_type_parameters_end?;
    if tsv_lang::printing::is_same_line(source, tp_span.end, sc_start) {
        return None;
    }
    Some(Span::new(sc_start, stp_end))
}

/// Emit the `superClass` (nullable) and `superTypeParameters?` fields, applying
/// the wrap decision. Shared by class declarations and expressions.
fn write_super_class_fields(
    w: &mut JsonWriter,
    super_class: Option<&ast::Expression<'_>>,
    super_type_parameters: Option<&ast::TSTypeParameterInstantiation<'_>>,
    type_params_span: Option<Span>,
    ctx: &Ctx<'_>,
) {
    let wrap_span = super_class_wrap_span(
        type_params_span,
        super_class,
        super_type_parameters.map(|tp| tp.span.end),
        ctx.source,
    );
    w.raw(",\"superClass\":");
    if let (Some(e), Some(stp), Some(ws)) = (super_class, super_type_parameters, wrap_span) {
        // Wrapped: `superTypeParameters` is consumed into the wrapper.
        node_header(w, "TSInstantiationExpression", ws, ctx);
        w.raw(",\"expression\":");
        write_expression(w, e, ctx);
        w.raw(",\"typeArguments\":");
        write_type_parameter_instantiation(w, stp, ctx);
        close_node(w, "TSInstantiationExpression", ws, ctx);
    } else {
        write_or_null(w, super_class, |w, e| write_expression(w, e, ctx));
        if let Some(stp) = super_type_parameters {
            w.raw(",\"superTypeParameters\":");
            write_type_parameter_instantiation(w, stp, ctx);
        }
    }
}

/// Emits a `ClassDeclaration` node. Field order: `declare?` (statement
/// position), `abstract?`, `decorators?`, `id` (nullable), `typeParameters?`,
/// `superClass` (nullable), `superTypeParameters?`, `implements?`, `body`,
/// `declare?` (post-hoc position) — acorn-typescript's statement-level
/// `declare class` stamps `declare` before parsing the class
/// (`tsTryParseDeclare`), but `declare abstract class` (the
/// `tsParseAbstractDeclaration` route) and the `export declare` forms stamp
/// the finished node, so the field's position depends on `abstract` and on
/// *which* route consumed the `declare`. `abstract` itself is assigned at its
/// keyword, decorators attach after it.
///
/// "Is it exported" does not settle the route once decorators are in play: a
/// decorator between `export` and `declare` stops `parseExportDeclaration` from
/// eating the keyword, so `export @dec declare class D {}` reaches the
/// pre-stamping statement route while `@dec export declare class C {}` does not.
/// `declare_stamped_by_export` is therefore the enclosing export's **type-export**
/// bit, not merely "inside an export" — acorn sets `exportKind = 'type'` and does
/// the post-hoc `declare` stamp in one `if (isDeclare)` block, and a class is
/// never a type export for any other reason, so the two are the same fact.
pub(super) fn write_class_declaration(
    w: &mut JsonWriter,
    class_decl: &ast::ClassDeclaration<'_>,
    ctx: &Ctx<'_>,
    declare_stamped_by_export: bool,
) {
    node_header(w, "ClassDeclaration", class_decl.span, ctx);
    let declare_last = declare_stamped_by_export || class_decl.r#abstract;
    if class_decl.declare && !declare_last {
        w.raw(",\"declare\":true");
    }
    if class_decl.r#abstract {
        w.raw(",\"abstract\":true");
    }
    write_decorators_field(w, class_decl.decorators, ctx);
    w.raw(",\"id\":");
    write_or_null(w, class_decl.id.as_ref(), |w, id| {
        write_identifier_plain(w, id, ctx);
    });
    write_type_parameters_field(w, class_decl.type_parameters.as_ref(), ctx);
    write_super_class_fields(
        w,
        class_decl.super_class,
        class_decl.super_type_parameters.as_ref(),
        class_decl.type_parameters.as_ref().map(|tp| tp.span),
        ctx,
    );
    write_implements_field(w, class_decl.implements, ctx);
    w.raw(",\"body\":");
    write_class_body(w, &class_decl.body, ctx);
    if class_decl.declare && declare_last {
        w.raw(",\"declare\":true");
    }
    close_node(w, "ClassDeclaration", class_decl.span, ctx);
}

/// Emits a `ClassExpression` node (no `declare` field).
pub(super) fn write_class_expression(
    w: &mut JsonWriter,
    class_expr: &ast::ClassExpression<'_>,
    span: Span,
    ctx: &Ctx<'_>,
) {
    node_header(w, "ClassExpression", span, ctx);
    write_decorators_field(w, class_expr.decorators, ctx);
    if class_expr.r#abstract {
        w.raw(",\"abstract\":true");
    }
    w.raw(",\"id\":");
    write_or_null(w, class_expr.id.as_ref(), |w, id| {
        write_identifier_plain(w, id, ctx);
    });
    write_type_parameters_field(w, class_expr.type_parameters.as_ref(), ctx);
    write_super_class_fields(
        w,
        class_expr.super_class,
        class_expr.super_type_parameters.as_ref(),
        class_expr.type_parameters.as_ref().map(|tp| tp.span),
        ctx,
    );
    write_implements_field(w, class_expr.implements, ctx);
    w.raw(",\"body\":");
    write_class_body(w, &class_expr.body, ctx);
    close_node(w, "ClassExpression", span, ctx);
}

/// The `implements` field is skip-if-empty — an empty internal list emits no
/// field.
fn write_implements_field(
    w: &mut JsonWriter,
    implements: &[ast::TSInterfaceHeritage<'_>],
    ctx: &Ctx<'_>,
) {
    if implements.is_empty() {
        return;
    }
    w.raw(",\"implements\":");
    write_array(w, implements, |w, h| {
        write_expression_with_type_arguments(w, h, ctx);
    });
}

/// Emits a `ClassBody` node (dispatching each `ClassMember`).
fn write_class_body(w: &mut JsonWriter, body: &ast::ClassBody<'_>, ctx: &Ctx<'_>) {
    node_header(w, "ClassBody", body.span, ctx);
    w.raw(",\"body\":");
    write_array(w, body.body, |w, m| match m {
        ast::ClassMember::MethodDefinition(method) => {
            write_method_definition(w, method, ctx);
        }
        ast::ClassMember::PropertyDefinition(prop) => {
            write_property_definition(w, prop, ctx);
        }
        ast::ClassMember::StaticBlock(block) => {
            node_header(w, "StaticBlock", block.span, ctx);
            w.raw(",\"body\":");
            write_array(w, block.body, |w, s| write_statement(w, s, ctx));
            // acorn-typescript attaches a member's decorators once it has built it,
            // so a static block's list follows its `body`.
            write_decorators_field(w, block.decorators, ctx);
            close_node(w, "StaticBlock", block.span, ctx);
        }
        ast::ClassMember::IndexSignature(sig) => {
            write_index_signature(w, sig, ctx);
        }
    });
    close_node(w, "ClassBody", body.span, ctx);
}

/// A present class-member modifier: its source keyword and the exact field
/// fragment to emit. See `write_modifiers_in_source_order`.
struct MemberModifier {
    keyword: &'static str,
    fragment: &'static str,
}

const fn accessibility_modifier(acc: ast::Accessibility) -> MemberModifier {
    MemberModifier {
        keyword: acc.as_str(),
        fragment: match acc {
            ast::Accessibility::Public => ",\"accessibility\":\"public\"",
            ast::Accessibility::Private => ",\"accessibility\":\"private\"",
            ast::Accessibility::Protected => ",\"accessibility\":\"protected\"",
        },
    }
}

/// Emits a class member's modifier fields (everything before `computed`) in
/// source order, `static` always included.
///
/// acorn-typescript's `tsParseModifiers` assigns each modifier field as it
/// parses the keyword, so the wire order follows the member's source order;
/// `static` is (re-)assigned right after the loop, so when absent from source
/// it still emits (`false`) after the source modifiers. With at most one
/// keyword present there is nothing to order and no scan runs; otherwise the
/// keywords are re-read from the scan window (member start past any
/// decorators, up to the key) with the trivia-aware cursor — comments legally
/// separate modifiers from names (`static /* c */ a = 1`). Every present
/// modifier's keyword precedes the key, so the pending set empties before the
/// walk can reach it; anything unmatched (malformed input) still emits after
/// the walk, in declaration order, so output stays deterministic.
fn write_modifiers_in_source_order(
    w: &mut JsonWriter,
    mods: &mut [Option<MemberModifier>],
    is_static: bool,
    scan_start: u32,
    scan_end: u32,
    ctx: &Ctx<'_>,
) {
    let mut pending = mods.iter().flatten().count();
    let mut static_pending = true;
    if pending + usize::from(is_static) > 1 {
        let bytes = ctx.source.as_bytes();
        let mut i = scan_start as usize;
        let end = (scan_end as usize).min(bytes.len());
        while (pending > 0 || (is_static && static_pending)) && i < end {
            if bytes[i].is_ascii_whitespace() {
                i += 1;
                continue;
            }
            if let Some(next) = source_scan::skip_trivia(bytes, i, end, TriviaProfile::JS) {
                i = next;
                continue;
            }
            let word_start = i;
            while i < end
                && (bytes[i].is_ascii_alphanumeric() || bytes[i] == b'_' || bytes[i] == b'$')
            {
                i += 1;
            }
            if i == word_start {
                break; // non-word byte — the key or punctuation
            }
            let word = &ctx.source[word_start..i];
            if is_static && static_pending && word == "static" {
                w.raw(",\"static\":true");
                static_pending = false;
            } else if let Some(m) = mods.iter_mut().find_map(|slot| {
                slot.as_ref()
                    .is_some_and(|m| m.keyword == word)
                    .then(|| slot.take())
                    .flatten()
            }) {
                w.raw(m.fragment);
                pending -= 1;
            } else {
                break; // not a pending modifier — `async`, `get`, the key, …
            }
        }
    }
    for m in mods.iter_mut().filter_map(Option::take) {
        w.raw(m.fragment);
    }
    if static_pending {
        w.raw(",\"static\":");
        w.bool(is_static);
    }
}

/// The modifier scan window's start: past the decorators when present (they
/// precede the modifiers), else the member's own start.
fn modifier_scan_start(decorators: Option<&[ast::Decorator<'_>]>, span: Span) -> u32 {
    decorators
        .and_then(<[_]>::last)
        .map_or(span.start, |d| d.span.end)
}

/// Emits a `MethodDefinition` node. Field order: source-order modifiers
/// (`abstract?`, `accessibility?`, `override?`, `static` — see
/// `write_modifiers_in_source_order`), `computed`, `key`, `optional?`, `kind`,
/// `typeParameters?` (moved here from the FunctionExpression, acorn
/// convention), `value`, `decorators?` (attached to the finished member, so
/// they serialize last).
fn write_method_definition(w: &mut JsonWriter, method: &ast::MethodDefinition<'_>, ctx: &Ctx<'_>) {
    node_header(w, "MethodDefinition", method.span, ctx);
    let mut mods = [
        method.r#abstract.then_some(MemberModifier {
            keyword: "abstract",
            fragment: ",\"abstract\":true",
        }),
        method.accessibility.map(accessibility_modifier),
        method.r#override.then_some(MemberModifier {
            keyword: "override",
            fragment: ",\"override\":true",
        }),
    ];
    write_modifiers_in_source_order(
        w,
        &mut mods,
        method.is_static,
        modifier_scan_start(method.decorators, method.span),
        method.key.span().start,
        ctx,
    );
    w.raw(",\"computed\":");
    w.bool(method.computed);
    w.raw(",\"key\":");
    write_expression(w, &method.key, ctx);
    if method.optional {
        w.raw(",\"optional\":true");
    }
    w.raw(",\"kind\":");
    w.token(method.kind.as_str());
    let func = &method.value;
    write_type_parameters_field(w, func.type_parameters.as_ref(), ctx);
    // Abstract methods and overload signatures emit TSDeclareMethod (no
    // body): abstract flag OR an empty body with a zero-width (synthetic)
    // span. The value span starts at the `(`, not at the method keyword.
    // Both value shapes are otherwise identical — typeParameters stays on
    // the MethodDefinition, never on the value node.
    let is_bodyless = method.r#abstract
        || (func.body.body.is_empty() && func.body.span.start == func.body.span.end);
    let (value_type, body) = if is_bodyless {
        ("TSDeclareMethod", None)
    } else {
        ("FunctionExpression", Some(&func.body))
    };
    w.raw(",\"value\":");
    let value_span = Span::new(func.params_start, func.span.end);
    node_header(w, value_type, value_span, ctx);
    w.raw(",\"id\":");
    write_or_null(w, func.id.as_ref(), |w, id| {
        write_identifier_with_optional(w, id, ctx);
    });
    write_function_flags_fields(w, func.generator, func.r#async);
    w.raw(",\"params\":");
    write_expressions(w, func.params, ctx);
    write_return_type_field(w, func.return_type.as_ref(), ctx);
    if let Some(body) = body {
        w.raw(",\"body\":");
        write_block_statement(w, body, ctx);
    }
    // Close the `value` function node, then the `MethodDefinition`.
    close_node(w, value_type, value_span, ctx);
    write_decorators_field(w, method.decorators, ctx);
    close_node(w, "MethodDefinition", method.span, ctx);
}

/// Emits a `PropertyDefinition` node. Field order: source-order modifiers
/// (`declare?`, `abstract?`, `accessor?`, `accessibility?`, `readonly?`,
/// `override?`, `static` — see `write_modifiers_in_source_order`), `computed`,
/// `key`, `optional?`, `definite?`, `typeAnnotation?`, `value` (nullable),
/// `decorators?` (attached to the finished member, so they serialize last).
fn write_property_definition(
    w: &mut JsonWriter,
    prop: &ast::PropertyDefinition<'_>,
    ctx: &Ctx<'_>,
) {
    node_header(w, "PropertyDefinition", prop.span, ctx);
    let mut mods = [
        prop.declare.then_some(MemberModifier {
            keyword: "declare",
            fragment: ",\"declare\":true",
        }),
        prop.r#abstract.then_some(MemberModifier {
            keyword: "abstract",
            fragment: ",\"abstract\":true",
        }),
        prop.accessor.then_some(MemberModifier {
            keyword: "accessor",
            fragment: ",\"accessor\":true",
        }),
        prop.accessibility.map(accessibility_modifier),
        prop.readonly.then_some(MemberModifier {
            keyword: "readonly",
            fragment: ",\"readonly\":true",
        }),
        prop.r#override.then_some(MemberModifier {
            keyword: "override",
            fragment: ",\"override\":true",
        }),
    ];
    write_modifiers_in_source_order(
        w,
        &mut mods,
        prop.is_static,
        modifier_scan_start(prop.decorators, prop.span),
        prop.key.span().start,
        ctx,
    );
    w.raw(",\"computed\":");
    w.bool(prop.computed);
    w.raw(",\"key\":");
    write_expression(w, &prop.key, ctx);
    if matches!(prop.modifier, ast::PropertyModifier::Optional) {
        w.raw(",\"optional\":true");
    }
    if matches!(prop.modifier, ast::PropertyModifier::Definite) {
        w.raw(",\"definite\":true");
    }
    write_type_annotation_field(w, prop.type_annotation.as_ref(), ctx);
    w.raw(",\"value\":");
    write_or_null(w, prop.value.as_ref(), |w, v| write_expression(w, v, ctx));
    write_decorators_field(w, prop.decorators, ctx);
    close_node(w, "PropertyDefinition", prop.span, ctx);
}

/// Emits a `TSTypeParameterDeclaration` node. Field order: `params`, `extra?`
/// (`{"trailingComma":N}`, emitted like `start`/`end` in the mapper's output
/// space).
pub(super) fn write_type_parameter_declaration(
    w: &mut JsonWriter,
    params: &ast::TSTypeParameterDeclaration<'_>,
    ctx: &Ctx<'_>,
) {
    node_header(w, "TSTypeParameterDeclaration", params.span, ctx);
    w.raw(",\"params\":");
    write_array(w, params.params, |w, p| write_type_parameter(w, p, ctx));
    if let Some(pos) = params.trailing_comma {
        w.raw(",\"extra\":{\"trailingComma\":");
        w.u32(ctx.pos(pos));
        w.raw("}");
    }
    close_node(w, "TSTypeParameterDeclaration", params.span, ctx);
}

/// Emits a `TSTypeParameter` node. Field order: the modifiers present, **in the
/// order the source spells them**, then `name`, `constraint?`, `default?`.
///
/// acorn-typescript's `tsParseModifiers` stamps each flag as it consumes the
/// keyword, so `<in const T>` and `<const in T>` differ on the wire by key order
/// alone. Both parse (the ordering rule is a tsc *grammar-checker* error, not a
/// parse one), and both formatters normalize the printed form to `const in out` —
/// so this order is reachable only from source neither tool would emit, which is
/// exactly why nothing else guards it.
pub(super) fn write_type_parameter(
    w: &mut JsonWriter,
    param: &ast::TSTypeParameter<'_>,
    ctx: &Ctx<'_>,
) {
    node_header(w, "TSTypeParameter", param.span, ctx);
    for modifier in param.modifiers.iter() {
        w.raw(",\"");
        w.raw(modifier.as_str());
        w.raw("\":true");
    }
    write_name_field(w, param.name.ident_name(), param.name.span.start, ctx);
    if let Some(c) = &param.constraint {
        w.raw(",\"constraint\":");
        write_type(w, c, ctx);
    }
    if let Some(d) = &param.default {
        w.raw(",\"default\":");
        write_type(w, d, ctx);
    }
    close_node(w, "TSTypeParameter", param.span, ctx);
}

/// Emits a `TSExpressionWithTypeArguments` node (an implements clause): the
/// `expression` is the heritage entity name (an `Identifier` or a
/// `TSQualifiedName`, matching `extends`), plus `typeParameters?`.
fn write_expression_with_type_arguments(
    w: &mut JsonWriter,
    heritage: &ast::TSInterfaceHeritage<'_>,
    ctx: &Ctx<'_>,
) {
    node_header(w, "TSExpressionWithTypeArguments", heritage.span, ctx);
    w.raw(",\"expression\":");
    write_entity_name(w, &heritage.expression, ctx);
    if let Some(ta) = &heritage.type_arguments {
        w.raw(",\"typeParameters\":");
        write_type_parameter_instantiation(w, ta, ctx);
    }
    close_node(w, "TSExpressionWithTypeArguments", heritage.span, ctx);
}