fallow-extract 2.79.0

AST extraction engine for fallow codebase intelligence (parser, complexity, SFC / Astro / MDX / CSS)
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
mod astro;
mod css;
mod graphql;
mod js_ts;
mod mdx;
mod regex_compile;
mod sfc;

use std::path::Path;

use fallow_types::discover::FileId;
use fallow_types::extract::ModuleInfo;

use crate::parse::parse_source_to_module;

/// Shared test helper: parse TypeScript source and return `ModuleInfo`.
pub fn parse_ts(source: &str) -> ModuleInfo {
    parse_source_to_module(FileId(0), Path::new("test.ts"), source, 0, false)
}

/// Shared test helper: parse TypeScript source with complexity metrics.
pub fn parse_ts_with_complexity(source: &str) -> ModuleInfo {
    parse_source_to_module(FileId(0), Path::new("test.ts"), source, 0, true)
}

/// Shared test helper: parse TSX source and return `ModuleInfo`.
pub fn parse_tsx(source: &str) -> ModuleInfo {
    parse_source_to_module(FileId(0), Path::new("test.tsx"), source, 0, false)
}

#[test]
fn parses_glimmer_typescript_as_typescript() {
    let info = parse_source_to_module(
        FileId(0),
        Path::new("component.gts"),
        "import type Service from './service';\nexport type ServiceRef = Service;\n",
        0,
        false,
    );

    assert_eq!(info.imports.len(), 1);
    assert_eq!(info.imports[0].source, "./service");
    assert!(info.imports[0].is_type_only);
    assert!(
        info.exports
            .iter()
            .any(|export| export.name.matches_str("ServiceRef"))
    );
}

/// Regression test for issue #375: a `.gts` file containing both a
/// module-level template expression (assigned to const) and a class-body
/// template must still parse all imports and the default export.
///
/// Before the context-aware stripping fix, the module-level template was
/// blanked to spaces, leaving `const Wrapper: TOC<...> = ;` which is a
/// TypeScript syntax error. oxc bailed and returned zero imports, causing
/// every referenced component to be reported as unused.
#[test]
fn parses_gts_with_multi_template_blocks() {
    let source = "import type {TOC} from '@ember/component/template-only';\n\
                  import Component from '@glimmer/component';\n\
                  import BillingInfo from 'my-app/components/billing-info';\n\
                  \n\
                  const Wrapper: TOC<{ Blocks: { default: [] } }> = <template>\n  <div class=\"wrapper\">{{yield}}</div>\n</template>;\n\
                  \n\
                  export default class InvoiceDetails extends Component {\n  <template>\n    <Wrapper>\n      <BillingInfo />\n    </Wrapper>\n  </template>\n}\n";

    let info = parse_source_to_module(
        FileId(0),
        Path::new("invoice-details.gts"),
        source,
        0,
        false,
    );

    assert_eq!(
        info.imports.len(),
        3,
        "all three import statements should be extracted; got {:?}",
        info.imports.iter().map(|i| &i.source).collect::<Vec<_>>()
    );
    assert!(
        info.imports
            .iter()
            .any(|i| i.source == "@ember/component/template-only"),
    );
    assert!(
        info.imports
            .iter()
            .any(|i| i.source == "@glimmer/component")
    );
    assert!(
        info.imports
            .iter()
            .any(|i| i.source == "my-app/components/billing-info"),
    );
    assert!(
        info.exports
            .iter()
            .any(|e| matches!(e.name, fallow_types::extract::ExportName::Default)),
        "default export should be extracted",
    );
}

/// Regression test for issue #379: a `.gts` file that uses the canonical
/// template-only-component shape (`export default <template>...</template>`
/// with no `const` wrapper) must still parse the import statement and the
/// default export.
///
/// Before the keyword-aware `is_expression_position` fix, the previous
/// non-whitespace byte before `<template>` was `t` (end of `default`),
/// which fell through to blank-out and left `export default ;`, a
/// TypeScript syntax error that made oxc bail and drop every import.
#[test]
fn parses_gts_with_standalone_default_template() {
    let source = "import Icon from 'my-app/components/icon';\n\
                  \n\
                  export default <template>\n  <span class=\"badge\"><Icon /> badge</span>\n</template>\n";

    let info = parse_source_to_module(FileId(0), Path::new("badge.gts"), source, 0, false);

    assert_eq!(
        info.imports.len(),
        1,
        "import statement should be extracted; got {:?}",
        info.imports.iter().map(|i| &i.source).collect::<Vec<_>>()
    );
    assert_eq!(info.imports[0].source, "my-app/components/icon");
    assert!(
        info.exports
            .iter()
            .any(|e| matches!(e.name, fallow_types::extract::ExportName::Default)),
        "default export should be extracted",
    );
}

/// Issue #475: the same source bytes with and without a leading UTF-8 BOM
/// must produce identical extraction results. `parse_source_to_module` strips
/// the BOM as a defense-in-depth step before any line-offset computation or
/// oxc parse, so the byte spans on every export entry must match.
#[test]
fn parse_source_to_module_strips_bom_defense_in_depth() {
    let body = "import { foo } from './foo';\nexport const bar = 1;\n";
    let with_bom = format!("\u{FEFF}{body}");
    let info_plain = parse_ts(body);
    let info_bom = parse_ts(&with_bom);

    assert_eq!(info_plain.imports.len(), info_bom.imports.len());
    assert_eq!(info_plain.exports.len(), info_bom.exports.len());
    // Compare byte spans on every export entry: identical post-strip source
    // must produce identical spans.
    let plain_spans: Vec<(u32, u32)> = info_plain
        .exports
        .iter()
        .map(|e| (e.span.start, e.span.end))
        .collect();
    let bom_spans: Vec<(u32, u32)> = info_bom
        .exports
        .iter()
        .map(|e| (e.span.start, e.span.end))
        .collect();
    assert_eq!(
        plain_spans, bom_spans,
        "BOM-bearing source must produce identical export byte spans (no shift by the BOM codepoint)",
    );
}

/// Issue #475: confirm `strip_bom` + hash invariant: the post-strip bytes
/// of a BOM-bearing source equal the post-strip bytes of the same source
/// without BOM, so the cache `content_hash` (xxh3 over post-strip bytes)
/// matches and the cache hits on both shapes.
#[test]
fn bom_stripped_before_hash_so_with_and_without_bom_yield_same_hash() {
    let body = "export const x = 1;\n";
    let plain = body;
    let bom = format!("\u{FEFF}{body}");

    let plain_hash = xxhash_rust::xxh3::xxh3_64(crate::strip_bom(plain).as_bytes());
    let bom_hash = xxhash_rust::xxh3::xxh3_64(crate::strip_bom(&bom).as_bytes());
    assert_eq!(
        plain_hash, bom_hash,
        "post-strip hashes must match so the extraction cache hits regardless of BOM presence",
    );

    // The `ModuleInfo.content_hash` field carried by `parse_source_to_module`
    // is set by the caller; confirm the caller's invariant by hashing the
    // same post-strip bytes we'd pass to the parser.
    let plain_info = parse_ts(plain);
    let bom_info = parse_ts(&bom);
    // Both calls go through `parse_source_to_module`, which now strips BOM
    // defense-in-depth. The exported byte spans + member layout must match.
    assert_eq!(
        plain_info.exports.len(),
        bom_info.exports.len(),
        "BOM-bearing and BOM-free source must yield the same number of exports",
    );
}

/// Issue #475: `compute_line_offsets` runs against the post-BOM source, so
/// line numbers for symbols on line 1 are not shifted by the BOM codepoint.
/// This is the user-visible fix: the first reported export of a BOM-bearing
/// file lands on line 1 col 0 (not line 1 col 3).
#[test]
fn bom_stripped_before_line_offsets_so_line_numbers_align() {
    use fallow_types::extract::{byte_offset_to_line_col, compute_line_offsets};

    let body = "export const first = 1;\nexport const second = 2;\n";
    let with_bom = format!("\u{FEFF}{body}");
    let info_plain = parse_ts(body);
    let info_bom = parse_ts(&with_bom);

    let plain_first = info_plain
        .exports
        .iter()
        .find(|e| e.name.matches_str("first"))
        .expect("plain source exports `first`");
    let bom_first = info_bom
        .exports
        .iter()
        .find(|e| e.name.matches_str("first"))
        .expect("BOM-bearing source exports `first`");

    // The byte spans must be identical (the BOM is stripped before parse),
    // and the line/col mapping on the post-strip source produces (1, 0).
    let plain_offsets = compute_line_offsets(body);
    let bom_offsets = compute_line_offsets(crate::strip_bom(&with_bom));
    let plain_pos = byte_offset_to_line_col(&plain_offsets, plain_first.span.start);
    let bom_pos = byte_offset_to_line_col(&bom_offsets, bom_first.span.start);
    assert_eq!(
        plain_pos, bom_pos,
        "line/col must align across BOM presence"
    );
    assert_eq!(
        plain_pos.0, 1,
        "the first export sits on line 1 in both views",
    );
}

#[test]
fn glimmer_template_only_pascal_tag_credits_import() {
    let info = parse_source_to_module(
        FileId(0),
        Path::new("app.gts"),
        "import HelloWorld from './hello-world';\nimport { greeting } from './lib';\n\
         <template><HelloWorld @msg={{greeting}} /></template>\n",
        0,
        false,
    );

    assert!(
        info.unused_import_bindings.is_empty(),
        "expected HelloWorld and greeting to be credited via the <template> block, \
         but unused_import_bindings = {:?}",
        info.unused_import_bindings,
    );
}

#[test]
fn glimmer_dotted_template_reference_emits_member_access() {
    let info = parse_source_to_module(
        FileId(0),
        Path::new("app.gts"),
        "import * as utils from './utils';\n<template>{{utils.formatDate value}}</template>\n",
        0,
        false,
    );

    assert!(info.unused_import_bindings.is_empty());
    assert!(
        info.member_accesses
            .iter()
            .any(|access| access.object == "utils" && access.member == "formatDate")
    );
}

#[test]
fn glimmer_import_used_only_inside_template_is_not_flagged() {
    // Regression for the original symptom: an import that is referenced ONLY
    // inside the template block was previously surfaced as `unused-import`
    // because the template body is blanked before the JS parse.
    let info = parse_source_to_module(
        FileId(0),
        Path::new("counter.gts"),
        "import { capitalize } from './helpers';\n\
         <template>{{capitalize name}}</template>\n",
        0,
        false,
    );

    assert!(info.unused_import_bindings.is_empty());
}

// -- negative cases: confirm the scanner does NOT over-credit ------------
//
// The trio above only proves the credit path works. These tests fail the
// suite if the scanner regresses into crediting bindings it shouldn't (or
// if `info.unused_import_bindings` ever gets stubbed back to an empty set
// by mistake): each one declares an import that is genuinely unreachable
// and asserts it surfaces in `unused_import_bindings`.

fn assert_unused(info: &ModuleInfo, expected: &[&str]) {
    let mut actual: Vec<&str> = info
        .unused_import_bindings
        .iter()
        .map(String::as_str)
        .collect();
    actual.sort_unstable();
    let mut expected = expected.to_vec();
    expected.sort_unstable();
    assert_eq!(
        actual, expected,
        "unused_import_bindings did not match expected set"
    );
}

#[test]
fn glimmer_import_referenced_nowhere_is_flagged_unused() {
    let info = parse_source_to_module(
        FileId(0),
        Path::new("app.gts"),
        "import { unused } from './lib';\n\
         <template>hello world</template>\n",
        0,
        false,
    );
    assert_unused(&info, &["unused"]);
}

#[test]
fn glimmer_import_referenced_only_via_this_dot_in_template_is_flagged() {
    // `this.greeting` reads a class property. Even when `greeting` happens
    // to also be an imported binding name, the template scanner must not
    // credit the import.
    let info = parse_source_to_module(
        FileId(0),
        Path::new("app.gts"),
        "import { greeting } from './lib';\n\
         <template>{{this.greeting}}</template>\n",
        0,
        false,
    );
    assert_unused(&info, &["greeting"]);
}

#[test]
fn glimmer_import_referenced_only_via_arg_in_template_is_flagged() {
    // `@name` is a template argument, not a module-scope binding. An import
    // named `name` is genuinely unused here.
    let info = parse_source_to_module(
        FileId(0),
        Path::new("app.gts"),
        "import { name } from './lib';\n\
         <template>{{@name}}</template>\n",
        0,
        false,
    );
    assert_unused(&info, &["name"]);
}

#[test]
fn glimmer_import_shadowing_builtin_helper_is_flagged() {
    // `each` is a Glimmer built-in helper keyword; the scanner must NEVER
    // resolve it to an import binding, even if the user did `import { each }`.
    // (Built-ins like `if` / `let` are reserved words and can't be import
    // identifiers, so `each` is the realistic regression to lock in.)
    let info = parse_source_to_module(
        FileId(0),
        Path::new("app.gts"),
        "import { each } from './lib';\n\
         <template>{{#each items as |x|}}{{x}}{{/each}}</template>\n",
        0,
        false,
    );
    assert_unused(&info, &["each"]);
}

#[test]
fn glimmer_import_shadowed_by_block_param_is_flagged() {
    // `as |item|` introduces a template-scope local. References to `item`
    // inside the block resolve to the local, NOT to the same-named import,
    // so the import must surface as unused.
    let info = parse_source_to_module(
        FileId(0),
        Path::new("app.gts"),
        "import { item } from './lib';\n\
         <template>{{#each items as |item|}}{{item}}{{/each}}</template>\n",
        0,
        false,
    );
    assert_unused(&info, &["item"]);
}

#[test]
fn glimmer_mix_of_used_and_unused_imports_flags_only_the_unused() {
    let info = parse_source_to_module(
        FileId(0),
        Path::new("app.gts"),
        "import HelloWorld from './hello-world';\n\
         import { greeting } from './lib';\n\
         import { stale } from './lib';\n\
         <template><HelloWorld @msg={{greeting}} /></template>\n",
        0,
        false,
    );
    assert_unused(&info, &["stale"]);
}

#[test]
fn glimmer_strict_mode_helper_imports_from_ember_helper_are_credited() {
    // Strict-mode `.gts` requires `hash`, `array`, `concat`, `fn`, `mut`,
    // `get` to be imported from `@ember/helper`; using them in `<template>`
    // must keep the import credited. Regression for the case where these
    // names were misclassified as ambient built-ins and never credited.
    let info = parse_source_to_module(
        FileId(0),
        Path::new("form.gts"),
        "import { hash, array, concat, fn, get } from '@ember/helper';\n\
         <template>\n  \
           {{#let (hash a=(array 1 2) label=(concat \"x\" \"y\")) as |opts|}}\n    \
             <button {{on \"click\" (fn this.save opts)}}>{{get opts \"label\"}}</button>\n  \
           {{/let}}\n\
         </template>\n",
        0,
        false,
    );
    assert_unused(&info, &[]);
}

#[test]
fn glimmer_template_this_dot_member_emits_member_access() {
    // Common pattern: a component class declares arrow-function fields
    // whose ONLY call-site is `<Child @prop={{this.field}} />` in the
    // surrounding `<template>` block. Without member-access emission for
    // `this.field`, `unused-class-members` flags those fields as unused
    // even though the template wires them into a child component.
    let info = parse_source_to_module(
        FileId(0),
        Path::new("toolbar.gts"),
        "import Component from '@glimmer/component';\n\
         export class Toolbar extends Component {\n  \
           handleSelect = (x) => { void x; };\n  \
           changeTab = (t) => { void t; };\n  \
           <template>\n    \
             <Child @onSelect={{this.handleSelect}} \
                    @changeTab={{this.changeTab}} />\n  \
           </template>\n\
         }\n",
        0,
        false,
    );
    let access_keys: Vec<(&str, &str)> = info
        .member_accesses
        .iter()
        .map(|a| (a.object.as_str(), a.member.as_str()))
        .collect();
    assert!(
        access_keys.contains(&("this", "handleSelect")),
        "expected this.handleSelect member-access from <template>; \
         got {access_keys:?}"
    );
    assert!(
        access_keys.contains(&("this", "changeTab")),
        "expected this.changeTab member-access from <template>; got {access_keys:?}"
    );
}

#[test]
fn glimmer_template_this_dot_member_records_access_with_zero_imports() {
    // Edge case the previous post-construction `apply_glimmer_template_usage`
    // got wrong: a `.gts` file with NO module-scope imports but a
    // `{{this.foo}}` template reference must still record the member access
    // so unused-class-members tracking sees template-only `this.*` uses.
    // The Angular-shaped fold-into-extractor path runs the scan even when
    // imports are empty (only the binding-credit branch is a no-op).
    let info = parse_source_to_module(
        FileId(0),
        Path::new("no-imports.gts"),
        "export class Widget {\n  \
           handleClick = () => {};\n  \
           <template>\n    \
             <button {{on \"click\" this.handleClick}}>x</button>\n  \
           </template>\n\
         }\n",
        0,
        false,
    );
    let access_keys: Vec<(&str, &str)> = info
        .member_accesses
        .iter()
        .map(|a| (a.object.as_str(), a.member.as_str()))
        .collect();
    assert!(
        access_keys.contains(&("this", "handleClick")),
        "this.handleClick must still be recorded as a member access when \
         the file has zero module-scope imports; got {access_keys:?}",
    );
}

#[test]
fn glimmer_file_with_two_class_components_credits_all_template_imports() {
    // Glimmer allows multiple class components in one `.gts` file, each
    // with its own class-body `<template>` block. Imports used by EITHER
    // template must be credited, and `this.<member>` accesses from one
    // class's template must still be recorded so unused-class-members can
    // see them. The scanner doesn't know which class a `<template>` block
    // belongs to (templates are accumulated file-wide), so the union of
    // credited bindings is what we assert on.
    let info = parse_source_to_module(
        FileId(0),
        Path::new("layout.gts"),
        "import Component from '@glimmer/component';\n\
         import { on } from '@ember/modifier';\n\
         import HelloWorld from './hello-world';\n\
         \n\
         export class Header extends Component {\n  \
           greet = (e) => { void e; };\n  \
           <template>\n    \
             <h1>Header</h1>\n    \
             <button {{on \"click\" this.greet}}>greet</button>\n  \
           </template>\n\
         }\n\
         \n\
         export class Footer extends Component {\n  \
           <template>\n    \
             <HelloWorld @msg=\"bye\" />\n  \
           </template>\n\
         }\n",
        0,
        false,
    );
    assert_unused(&info, &[]);
    let access_keys: Vec<(&str, &str)> = info
        .member_accesses
        .iter()
        .map(|a| (a.object.as_str(), a.member.as_str()))
        .collect();
    assert!(
        access_keys.contains(&("this", "greet")),
        "Header.greet referenced via `{{{{on \"click\" this.greet}}}}` must \
         emit a `this.greet` member access; got {access_keys:?}",
    );
}

#[test]
fn glimmer_file_with_two_template_only_components_credits_all_imports() {
    // Top-level template-only components are a first-class Glimmer pattern:
    // `const Foo = <template>...</template>;` defines a component without a
    // backing class. A `.gts` file may export several such components and
    // each can pull from distinct module-scope imports, so they must all be
    // credited (the JS parser sees the template bodies as blank padding, so
    // without the template scanner each import would surface as unused).
    let info = parse_source_to_module(
        FileId(0),
        Path::new("greetings.gts"),
        "import HelloWorld from './hello-world';\n\
         import { formatDate } from './utils';\n\
         \n\
         export const Greeting = <template>\n  \
           <HelloWorld @msg=\"hi\" />\n\
         </template>;\n\
         \n\
         export const Stamp = <template>\n  \
           {{formatDate this}}\n\
         </template>;\n",
        0,
        false,
    );
    assert_unused(&info, &[]);
}

#[test]
fn glimmer_file_mixing_class_and_template_only_components_credits_all_imports() {
    // Mixed shape: one class component and one top-level template-only
    // component in the same file. Imports used by either should be
    // credited regardless of which template-host shape consumes them.
    let info = parse_source_to_module(
        FileId(0),
        Path::new("mixed.gts"),
        "import Component from '@glimmer/component';\n\
         import { capitalize } from './utils';\n\
         \n\
         export class Heading extends Component {\n  \
           <template><h1>{{capitalize \"hello\"}}</h1></template>\n\
         }\n\
         \n\
         export const Spacer = <template><hr /></template>;\n",
        0,
        false,
    );
    assert_unused(&info, &[]);
}

#[test]
fn glimmer_file_with_two_components_flags_only_genuinely_unused_imports() {
    // Sanity guard against the multi-component path accidentally over-
    // crediting: a third import that no template references must STILL
    // surface as unused. Locks in that the union-of-templates scan does
    // not silently credit every module-scope binding name.
    let info = parse_source_to_module(
        FileId(0),
        Path::new("layout.gts"),
        "import Component from '@glimmer/component';\n\
         import HelloWorld from './hello-world';\n\
         import { stale } from './lib';\n\
         \n\
         export class Header extends Component {\n  \
           <template><h1>Header</h1></template>\n\
         }\n\
         \n\
         export class Footer extends Component {\n  \
           <template><HelloWorld @msg=\"bye\" /></template>\n\
         }\n",
        0,
        false,
    );
    assert_unused(&info, &["stale"]);
}

#[test]
fn glimmer_file_without_template_still_flags_unused_imports() {
    // Sanity: the scanner only ever ADDS credit; on a `.gts` file with no
    // `<template>` block at all, an unused import must still surface.
    let info = parse_source_to_module(
        FileId(0),
        Path::new("plain.gts"),
        "import { unused } from './lib';\nexport const x = 1;\n",
        0,
        false,
    );
    assert_unused(&info, &["unused"]);
}