skif-js-syntax 0.2.0

oxc-based JS/TS module facts and static config extraction for skif; never executes code.
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
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
//! Static ESM extraction for `.mdx` files.
//!
//! This is not an MDX compiler (ADR 0001 rules out running one): it finds the top-level `import` and `export`
//! paragraphs the way MDX itself delimits them, outside YAML frontmatter and fenced code blocks, and blanks
//! everything else. The result has the byte length and line breaks of the original, so the JavaScript parser's spans
//! point straight into the `.mdx` file. The remaining prose is the markup, scanned for the components and
//! expressions it uses (`<Card/>`, `<ui.Button/>`, `{format(x)}`).

/// The ESM of an MDX document and where its markup lies.
pub(crate) struct Extracted {
    /// The source with every byte that is not ESM replaced by a space; line breaks are kept.
    pub esm: String,
    /// Byte ranges of the markup: prose and JSX outside ESM, frontmatter and fenced code.
    pub markup: Vec<(usize, usize)>,
}

#[derive(Clone, Copy, PartialEq, Eq)]
enum Region {
    Markup,
    Esm,
    Skipped,
}

/// One line of `source`: its byte range without the line break, and the range including it.
struct Line {
    start: usize,
    end: usize,
    next: usize,
}

fn lines(source: &str) -> Vec<Line> {
    let mut out = Vec::new();
    let mut start = 0;
    while start < source.len() {
        let next = source[start..].find('\n').map_or(source.len(), |at| start + at + 1);
        let mut end = next;
        if source[start..end].ends_with('\n') {
            end -= 1;
        }
        if source[start..end].ends_with('\r') {
            end -= 1;
        }
        out.push(Line { start, end, next });
        start = next;
    }
    out
}

/// The fence marker that opens a fenced code block on `line` (up to three spaces of indentation, then three or more
/// backticks or tildes): its character and length.
fn fence_open(line: &str) -> Option<(u8, usize)> {
    let trimmed = line.trim_start_matches(' ');
    if line.len() - trimmed.len() > 3 {
        return None;
    }
    let marker = *trimmed.as_bytes().first()?;
    if marker != b'`' && marker != b'~' {
        return None;
    }
    let run = trimmed.bytes().take_while(|&b| b == marker).count();
    (run >= 3).then_some((marker, run))
}

fn fence_closes(line: &str, marker: u8, length: usize) -> bool {
    let trimmed = line.trim_start_matches(' ');
    if line.len() - trimmed.len() > 3 {
        return false;
    }
    let run = trimmed.bytes().take_while(|&b| b == marker).count();
    run >= length && trimmed[run..].trim().is_empty()
}

/// Whether `line` starts an ESM paragraph: `import` or `export` at the first column, followed by a space, `{` or `*`.
fn starts_esm(line: &str) -> bool {
    ["import", "export"].iter().any(|keyword| {
        line.strip_prefix(keyword)
            .and_then(|rest| rest.bytes().next())
            .is_some_and(|next| next.is_ascii_whitespace() || next == b'{' || next == b'*')
    })
}

/// What the scan of an ESM paragraph is inside of.
#[derive(Clone, Copy, PartialEq, Eq)]
enum Frame {
    /// A `(`, `[` or `{` of code, and the `${` of a template literal or the `{` of a JSX attribute or child.
    Bracket,
    /// The text of a template literal.
    Template,
    /// A JSX tag between its `<` and `>`; `closing` for `</a>`.
    Tag { closing: bool },
    /// The children of a JSX element: text, in which quotes, slashes and backticks mean nothing.
    Children,
}

/// The words after which an expression starts, so that `return <p/>` opens JSX.
const EXPRESSION_KEYWORDS: [&str; 15] = [
    "await",
    "case",
    "default",
    "delete",
    "do",
    "else",
    "in",
    "instanceof",
    "new",
    "of",
    "return",
    "throw",
    "typeof",
    "void",
    "yield",
];

/// The lexical state of an ESM paragraph between lines: the brackets, template literals and JSX elements and the block
/// comment it is inside of. A blank line ends the paragraph only outside all of them, so `export const meta = {`, a
/// multi-line template literal or JSX element may span blank lines. JSX text is told from code by where a `<` stands,
/// so `<a>http://x</a>` and `<p>don't</p>` hold no comment and no string.
struct Scan {
    /// What the scan is inside of, innermost last; empty at the top level of the paragraph.
    frames: Vec<Frame>,
    /// Whether an expression may start here in the code, so a `<` opens JSX and a `/` a regular expression, rather
    /// than comparing or dividing.
    expression_start: bool,
    in_block_comment: bool,
    /// The source offset of the last `*/` in the file: a `/*` opens a comment only before it (see
    /// [`esm_paragraph_end`]).
    last_close: Option<usize>,
}

impl Scan {
    const fn new(last_close: Option<usize>) -> Self {
        Self {
            frames: Vec::new(),
            expression_start: true,
            in_block_comment: false,
            last_close,
        }
    }

    fn is_open(&self) -> bool {
        !self.frames.is_empty() || self.in_block_comment
    }

    /// Scans one line, which starts at `offset` in the source.
    fn line(&mut self, text: &str, offset: usize) {
        let bytes = text.as_bytes();
        let mut i = 0;
        while i < bytes.len() {
            i = if self.in_block_comment {
                match bytes[i..].windows(2).position(|pair| pair == b"*/") {
                    Some(at) => {
                        self.in_block_comment = false;
                        i + at + 2
                    }
                    None => return,
                }
            } else {
                match self.frames.last().copied() {
                    Some(Frame::Template) => self.template(bytes, i),
                    Some(Frame::Tag { closing }) => self.tag(bytes, i, offset, closing),
                    Some(Frame::Children) => self.children(bytes, i),
                    Some(Frame::Bracket) | None => match self.code(bytes, i, offset) {
                        Some(next) => next,
                        None => return,
                    },
                }
            };
        }
    }

    /// Opens a block comment at `i` when a `*/` follows it somewhere in the file; returns whether it did.
    fn block_comment(&mut self, bytes: &[u8], i: usize, offset: usize) -> bool {
        let opens = bytes.get(i + 1) == Some(&b'*') && self.last_close.is_some_and(|close| close >= offset + i + 2);
        self.in_block_comment |= opens;
        opens
    }

    /// Scans the code at `i` and returns where the scan goes on, or `None` at a `//` comment, which ends the line. A
    /// `'` or `"` that does not close on its line is skipped alone, as JSX text the scan did not recognise.
    fn code(&mut self, bytes: &[u8], i: usize, offset: usize) -> Option<usize> {
        let byte = bytes[i];
        if is_ident(byte) {
            let end = i + bytes[i..].iter().take_while(|&&b| is_ident(b)).count();
            let member = i > 0 && bytes[i - 1] == b'.';
            self.expression_start = !member && EXPRESSION_KEYWORDS.iter().any(|word| word.as_bytes() == &bytes[i..end]);
            return Some(end);
        }
        if byte.is_ascii_whitespace() {
            return Some(i + 1);
        }
        let mut next = i + 1;
        let mut expression_start = true;
        match byte {
            b'"' | b'\'' => {
                next = string_end(bytes, i).unwrap_or(next);
                expression_start = false;
            }
            b'`' => self.frames.push(Frame::Template),
            b'/' if bytes.get(i + 1) == Some(&b'/') => return None,
            b'/' if self.block_comment(bytes, i, offset) => return Some(i + 2),
            b'/' if self.expression_start && bytes.get(i + 1) != Some(&b'*') => {
                if let Some(end) = regex_end(bytes, i) {
                    next = end;
                    expression_start = false;
                }
            }
            b'(' | b'[' | b'{' => self.frames.push(Frame::Bracket),
            b')' | b']' | b'}' => {
                if self.frames.last() == Some(&Frame::Bracket) {
                    self.frames.pop();
                }
                expression_start = false;
            }
            b'<' if self.expression_start && bytes.get(i + 1).is_some_and(|&b| b == b'>' || is_ident(b)) => {
                self.frames.push(Frame::Tag { closing: false });
            }
            _ if !byte.is_ascii() => expression_start = false,
            _ => {}
        }
        self.expression_start = expression_start;
        Some(next)
    }

    fn template(&mut self, bytes: &[u8], i: usize) -> usize {
        match bytes[i] {
            b'\\' => return i + 2,
            b'`' => {
                self.frames.pop();
                self.expression_start = false;
            }
            b'$' if bytes.get(i + 1) == Some(&b'{') => {
                self.frames.push(Frame::Bracket);
                self.expression_start = true;
                return i + 2;
            }
            _ => {}
        }
        i + 1
    }

    fn tag(&mut self, bytes: &[u8], i: usize, offset: usize, closing: bool) -> usize {
        match bytes[i] {
            b'"' | b'\'' => return string_end(bytes, i).unwrap_or(i + 1),
            b'{' => {
                self.frames.push(Frame::Bracket);
                self.expression_start = true;
            }
            b'/' if bytes.get(i + 1) == Some(&b'>') => {
                self.frames.pop();
                self.expression_start = false;
                return i + 2;
            }
            b'/' if self.block_comment(bytes, i, offset) => return i + 2,
            b'>' => {
                self.frames.pop();
                if !closing {
                    self.frames.push(Frame::Children);
                } else if self.frames.last() == Some(&Frame::Children) {
                    self.frames.pop();
                }
                self.expression_start = false;
            }
            _ => {}
        }
        i + 1
    }

    fn children(&mut self, bytes: &[u8], i: usize) -> usize {
        match bytes[i] {
            b'{' => {
                self.frames.push(Frame::Bracket);
                self.expression_start = true;
            }
            b'<' if bytes.get(i + 1) == Some(&b'/') => {
                self.frames.push(Frame::Tag { closing: true });
                return i + 2;
            }
            b'<' => self.frames.push(Frame::Tag { closing: false }),
            _ => {}
        }
        i + 1
    }
}

const fn is_ident(byte: u8) -> bool {
    byte.is_ascii_alphanumeric() || matches!(byte, b'_' | b'$')
}

/// The end of the `'` or `"` string that starts at `i`, past its closing quote, or `None` when it does not close on
/// its line.
fn string_end(bytes: &[u8], i: usize) -> Option<usize> {
    let quote = bytes[i];
    let mut j = i + 1;
    while j < bytes.len() && bytes[j] != quote {
        j += if bytes[j] == b'\\' { 2 } else { 1 };
    }
    (j < bytes.len()).then_some(j + 1)
}

/// The end of the regular expression literal that starts at `i`, past its closing `/`, or `None` when it does not
/// close on its line: then the `/` divides.
fn regex_end(bytes: &[u8], i: usize) -> Option<usize> {
    let mut class = false;
    let mut j = i + 1;
    while let Some(&b) = bytes.get(j) {
        match b {
            b'\\' => j += 1,
            b'[' => class = true,
            b']' => class = false,
            b'/' if !class => return Some(j + 1),
            _ => {}
        }
        j += 1;
    }
    None
}

/// The end of a YAML frontmatter block at the very start of `source`, if there is one.
fn frontmatter_end(all: &[Line], source: &str) -> Option<usize> {
    let first = all.first()?;
    if &source[first.start..first.end] != "---" {
        return None;
    }
    all.iter()
        .skip(1)
        .position(|line| &source[line.start..line.end] == "---")
        .map(|index| index + 2)
}

/// The index of the line after the ESM paragraph that starts at line `first`: the first blank line outside brackets,
/// template literals and block comments, or the end.
///
/// MDX parses the paragraph with acorn at each blank line and goes on past it when the code is incomplete, which
/// includes an unterminated comment: a block comment may span blank lines. A `/*` with no `*/` anywhere after it
/// cannot be one, since MDX rejects the document then; it is JSX text (`<code>dist/*</code>`) or a regular
/// expression (`/\/*/`) the scan does not tell apart. Such a `/*` is not a comment, so a blank line ends the
/// paragraph as MDX does. Whether a `*/` follows is one comparison with the last `*/` of the file, which keeps the scan
/// linear.
///
/// MDX parses ESM with acorn, which has no TypeScript: TypeScript syntax is not supported here either. A generic arrow
/// function such as `<T,>(x: T) => x` reads as an unclosed JSX element and keeps the paragraph open to the end.
fn esm_paragraph_end(source: &str, all: &[Line], first: usize) -> usize {
    let mut scan = Scan::new(source.rfind("*/"));
    let mut index = first;
    while index < all.len() {
        let line = &source[all[index].start..all[index].end];
        if line.trim().is_empty() && !scan.is_open() {
            return index;
        }
        scan.line(line, all[index].start);
        index += 1;
    }
    index
}

/// Splits `source` into ESM, skipped and markup lines.
pub(crate) fn extract(source: &str) -> Extracted {
    let all = lines(source);
    let mut regions = vec![Region::Markup; all.len()];
    let mut index = 0;
    if let Some(end) = frontmatter_end(&all, source) {
        regions[..end].fill(Region::Skipped);
        index = end;
    }
    while index < all.len() {
        let text = &source[all[index].start..all[index].end];
        if let Some((marker, length)) = fence_open(text) {
            regions[index] = Region::Skipped;
            index += 1;
            while index < all.len() {
                regions[index] = Region::Skipped;
                let closes = fence_closes(&source[all[index].start..all[index].end], marker, length);
                index += 1;
                if closes {
                    break;
                }
            }
        } else if starts_esm(text) {
            let end = esm_paragraph_end(source, &all, index);
            regions[index..end].fill(Region::Esm);
            index = end;
        } else {
            index += 1;
        }
    }

    let mut esm = vec![b' '; source.len()];
    let mut markup: Vec<(usize, usize)> = Vec::new();
    for (line, region) in all.iter().zip(&regions) {
        let bytes = source.as_bytes();
        match region {
            Region::Esm => esm[line.start..line.next].copy_from_slice(&bytes[line.start..line.next]),
            Region::Markup => match markup.last_mut() {
                Some(last) if last.1 == line.start => last.1 = line.next,
                _ => markup.push((line.start, line.next)),
            },
            Region::Skipped => {}
        }
        for (offset, byte) in bytes[line.end..line.next].iter().enumerate() {
            esm[line.end + offset] = *byte;
        }
    }
    // Whole lines are copied, and every other byte is an ASCII space, so the buffer is valid UTF-8.
    let esm = String::from_utf8(esm).unwrap_or_default();
    Extracted { esm, markup }
}

/// `markup` with its inline code spans (`` `<Card/>` ``, ``` `` a`b `` ```) blanked, so that code quoted in prose is
/// not taken for a component or expression it uses. A span opens with a run of backticks and closes at the next run of
/// the same length in its paragraph (spans do not cross paragraphs); a run that does not close, and a
/// backslash-escaped backtick, are literal text. In a `{}` expression a backtick opens a template literal, not a span;
/// an expression's `{` does not reach past its paragraph either, so a stray `{` in prose does not hide later spans.
/// Line breaks and the byte length are kept.
pub(crate) fn blank_inline_code(markup: &str) -> String {
    let bytes = markup.as_bytes();
    let mut out = bytes.to_vec();
    let mut depth = 0_usize;
    let mut i = 0;
    while i < bytes.len() {
        match bytes[i] {
            b'\\' => i += 1,
            b'\n' if blank_line_follows(bytes, i) => depth = 0,
            b'{' => depth += 1,
            b'}' => depth = depth.saturating_sub(1),
            b'`' if depth > 0 => {
                i = template_literal_end(bytes, i);
                continue;
            }
            b'`' => {
                let open = backtick_run(bytes, i);
                if let Some(end) = code_span_end(bytes, i + open, open) {
                    for byte in &mut out[i..end] {
                        if !matches!(*byte, b'\n' | b'\r') {
                            *byte = b' ';
                        }
                    }
                    i = end;
                    continue;
                }
                i += open - 1;
            }
            _ => {}
        }
        i += 1;
    }
    // Whole spans between ASCII backticks are blanked, so the buffer is valid UTF-8.
    String::from_utf8(out).unwrap_or_default()
}

fn backtick_run(bytes: &[u8], at: usize) -> usize {
    bytes[at..].iter().take_while(|&&b| b == b'`').count()
}

/// The end of the code span whose opening run of `open` backticks ends at `from`: past its closing run, or `None` when
/// a blank line, which ends the paragraph, or the end of the markup comes first.
fn code_span_end(bytes: &[u8], from: usize, open: usize) -> Option<usize> {
    let mut j = from;
    while j < bytes.len() {
        match bytes[j] {
            b'`' => {
                let run = backtick_run(bytes, j);
                if run == open {
                    return Some(j + run);
                }
                j += run;
            }
            b'\n' if blank_line_follows(bytes, j) => return None,
            _ => j += 1,
        }
    }
    None
}

/// Whether the line after the line break at `at` is blank, which ends the paragraph.
fn blank_line_follows(bytes: &[u8], at: usize) -> bool {
    bytes[at + 1..]
        .iter()
        .find(|&&b| !matches!(b, b' ' | b'\t' | b'\r'))
        .is_some_and(|&b| b == b'\n')
}

/// The end of the template literal whose opening backtick is at `i`, past its closing backtick, or the end of `bytes`.
/// A `${}` substitution holds code, so its braces, strings and nested template literals are followed; an explicit stack
/// keeps deep nesting off the call stack.
fn template_literal_end(bytes: &[u8], i: usize) -> usize {
    // `None` is the text of a template literal, `Some(depth)` a substitution `depth` braces deep.
    let mut frames: Vec<Option<usize>> = vec![None];
    let mut j = i + 1;
    while j < bytes.len() {
        let Some(top) = frames.last_mut() else {
            return j;
        };
        match (top, bytes[j]) {
            (_, b'\\') => j += 1,
            (None, b'`') | (Some(0), b'}') => {
                frames.pop();
            }
            (None, b'$') if bytes.get(j + 1) == Some(&b'{') => {
                frames.push(Some(0));
                j += 1;
            }
            (Some(_), b'`') => frames.push(None),
            (Some(_), quote @ (b'\'' | b'"')) => {
                j += bytes[j + 1..].iter().take_while(|&&b| b != quote && b != b'\n').count() + 1;
            }
            (Some(depth), b'{') => *depth += 1,
            (Some(depth), b'}') => *depth -= 1,
            _ => {}
        }
        j += 1;
    }
    if frames.is_empty() { j } else { bytes.len() }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn kept(source: &str) -> Vec<String> {
        extract(source)
            .esm
            .lines()
            .map(str::trim_end)
            .filter(|line| !line.is_empty())
            .map(str::to_owned)
            .collect()
    }

    #[test]
    fn top_level_import_and_export_paragraphs_are_kept() {
        let source = "---\ntitle: import x from 'y'\n---\nimport A from './a'\nimport {\n  B,\n} from './b'\n\n# Title\n\nexport const meta = {\n\n  a: 1,\n}\n\n<A/> import later\n";
        assert_eq!(
            kept(source),
            [
                "import A from './a'",
                "import {",
                "  B,",
                "} from './b'",
                "export const meta = {",
                "  a: 1,",
                "}"
            ]
        );
        assert_eq!(extract(source).esm.len(), source.len());
    }

    #[test]
    fn an_apostrophe_in_jsx_text_does_not_keep_the_paragraph_open() {
        let source = "export const Note = () => (<p>don't</p>)\n\n# Title\n\n<Note/>\n";
        assert_eq!(kept(source), ["export const Note = () => (<p>don't</p>)"]);
    }

    #[test]
    fn a_multi_line_template_literal_spans_blank_lines_and_ends() {
        let source =
            "export const css = `\n  a :( {\n\n  b ${x({ y: '`' })} c\n`\n\n# Title :(\n\nimport A from './a'\n";
        assert_eq!(
            kept(source),
            [
                "export const css = `",
                "  a :( {",
                "  b ${x({ y: '`' })} c",
                "`",
                "import A from './a'"
            ]
        );
    }

    #[test]
    fn a_comment_opener_that_never_closes_does_not_swallow_the_document() {
        let source = "export const X = () => <code>dist/*</code>\n\n# Title\n\n<Card/>\n";
        assert_eq!(kept(source), ["export const X = () => <code>dist/*</code>"]);
        let source = "export const re = (/\\/*/)\n\n# Title\n\n<Card/>\n";
        assert_eq!(kept(source), ["export const re = (/\\/*/)"]);
        let source = "export const n = 1 /* not closed\n\n# Title\n";
        assert_eq!(kept(source), ["export const n = 1 /* not closed"]);
    }

    #[test]
    fn unclosed_comment_openers_are_scanned_in_linear_time() {
        let repeats = 20_000;
        let source = format!("export const a = 1 /* x\n\n{}<A/>\n", "/* x\n\n".repeat(repeats));
        assert_eq!(kept(&source), ["export const a = 1 /* x"]);
        let source = format!("export const a = 1\n{}\n<A/>\n", "/* x\n".repeat(repeats));
        assert_eq!(kept(&source).len(), repeats + 1);
        let source = format!("export const a = 1 /* x\n{}*/\n\n<A/>\n", "\ny\n".repeat(repeats));
        let kept = kept(&source);
        assert_eq!((kept.len(), kept.last().map(String::as_str)), (repeats + 2, Some("*/")));
    }

    #[test]
    fn a_block_comment_that_closes_later_spans_blank_lines() {
        let source = "export const a = 1 /* x\n\ny */\nexport const b = 2\n\n<B/>\n";
        assert_eq!(kept(source), ["export const a = 1 /* x", "y */", "export const b = 2"]);
    }

    #[test]
    fn jsx_text_and_regular_expressions_hold_no_comment() {
        let source = "export const L = () => (<a>http://x</a>)\n\n# Title\n\n<Card/>\n";
        assert_eq!(kept(source), ["export const L = () => (<a>http://x</a>)"]);
        let source = "export const G = () => (<code>dist/*</code>)\n\n# Paths\n\nUse `src/*/index.js`.\n";
        assert_eq!(kept(source), ["export const G = () => (<code>dist/*</code>)"]);
        let source = "export const isUrl = (s) => (/:\\/\\//).test(s)\n\n# Title\n";
        assert_eq!(kept(source), ["export const isUrl = (s) => (/:\\/\\//).test(s)"]);
    }

    #[test]
    fn comparisons_divisions_and_strings_are_not_jsx_or_regexes() {
        for line in [
            "export const lt = a < b",
            "export const q = x / y / z",
            "export const t = 1 < 2",
            "export const close = '</div>'",
        ] {
            let source = format!("{line}\n\n# Title\n\n<Card/>\n");
            assert_eq!(kept(&source), [line]);
        }
    }

    #[test]
    fn a_jsx_element_spans_blank_lines() {
        let source = "export function Note() {\n  return <p a=\"{\" b={1 < 2}>\n\n    don't <b>stop</b> {`${x}`}\n  </p>\n}\n\n<Note/>\n";
        assert_eq!(
            kept(source),
            [
                "export function Note() {",
                "  return <p a=\"{\" b={1 < 2}>",
                "    don't <b>stop</b> {`${x}`}",
                "  </p>",
                "}"
            ]
        );
    }

    #[test]
    fn code_fences_and_indented_lines_are_not_esm() {
        let source = "```js\nimport x from 'fenced'\n```\n~~~~\nimport y from 'tilde'\n~~~~\n  import z from 'indented'\nimportant prose\nexport default Layout\n";
        assert_eq!(kept(source), ["export default Layout"]);
    }

    #[test]
    fn markup_excludes_esm_and_code() {
        let source = "import A from './a'\n\n<A/>\n```\n<B/>\n```\n{c}\n";
        let markup: Vec<&str> = extract(source)
            .markup
            .iter()
            .map(|&(start, end)| &source[start..end])
            .collect();
        assert_eq!(markup, ["\n<A/>\n", "{c}\n"]);
    }

    #[test]
    fn inline_code_is_blanked() {
        let markup = "Use `<Card/>` or ``a ` <B/>`` but <C/>, and a lone ` <D/>\n";
        let blanked = blank_inline_code(markup);
        assert_eq!(blanked.len(), markup.len());
        assert!(!blanked.contains("Card") && !blanked.contains("<B/>"), "{blanked}");
        assert!(blanked.contains("<C/>") && blanked.contains("<D/>"), "{blanked}");
    }

    #[test]
    fn escaped_backticks_open_no_inline_code() {
        let blanked = blank_inline_code("Use \\` then <A/> and `<B/>`\n");
        assert!(blanked.contains("<A/>") && !blanked.contains("<B/>"), "{blanked}");
    }

    #[test]
    fn inline_code_does_not_cross_a_blank_line() {
        let markup = "A lone ` here\n \t\n<A/> and ` there\n";
        assert_eq!(blank_inline_code(markup), markup);
    }

    #[test]
    fn template_literals_in_expressions_are_not_inline_code() {
        let blanked = blank_inline_code("{ui.f(`x ${ui.y}`)} and `<B/>`\n");
        assert!(blanked.contains("${ui.y}") && !blanked.contains("<B/>"), "{blanked}");
    }

    #[test]
    fn a_stray_brace_in_prose_ends_at_its_paragraph() {
        let blanked = blank_inline_code("<A b=\"{\" />\n\nSee `<B/>` and <C/>\n");
        assert!(!blanked.contains("<B/>") && blanked.contains("<C/>"), "{blanked}");
        let blanked = blank_inline_code("A { here\r\n \t\r\nSee `<B/>`\r\n");
        assert!(!blanked.contains("<B/>"), "{blanked}");
    }

    #[test]
    fn template_literals_nest_in_substitutions() {
        let blanked = blank_inline_code("{`${`{`}`} and `<B/>`\n");
        assert!(!blanked.contains("<B/>"), "{blanked}");
        let blanked = blank_inline_code("{`${f('`', \"}\")}`} and `<B/>`\n");
        assert!(!blanked.contains("<B/>"), "{blanked}");
    }

    #[test]
    fn crlf_line_breaks_are_kept() {
        let markup = "a `<A/>`\r\n\r\n` b <B/>\r\n";
        let blanked = blank_inline_code(markup);
        assert_eq!(blanked, "a       \r\n\r\n` b <B/>\r\n");
        let source = "import A from './a'\r\nexport const L = () => (<a>http://x</a>)\r\n\r\n# Title\r\n\r\n<A/>\r\n";
        let extracted = extract(source);
        assert_eq!(
            kept(source),
            ["import A from './a'", "export const L = () => (<a>http://x</a>)"]
        );
        assert_eq!(extracted.esm.len(), source.len());
        assert_eq!(extracted.esm.matches("\r\n").count(), 6);
        let markup: Vec<&str> = extracted
            .markup
            .iter()
            .map(|&(start, end)| &source[start..end])
            .collect();
        assert_eq!(markup, ["\r\n# Title\r\n\r\n<A/>\r\n"]);
    }

    #[test]
    fn non_ascii_prose_keeps_the_byte_length() {
        let source = "Grüße — ünïcode\nimport A from './a'\n";
        let extracted = extract(source);
        assert_eq!(extracted.esm.len(), source.len());
        assert!(extracted.esm.contains("import A from './a'"));
    }
}