fallow-extract 3.46.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
use std::path::Path;

use fallow_types::discover::FileId;

use crate::parse_source_to_module;

#[test]
fn extracts_astro_frontmatter_imports() {
    let info = parse_source_to_module(
        FileId(0),
        Path::new("Layout.astro"),
        r#"---
import Layout from '../layouts/Layout.astro';
import { Card } from '../components/Card';
const title = "Hello";
---
<Layout title={title}>
  <Card />
</Layout>
"#,
        0,
        false,
    );
    assert_eq!(info.imports.len(), 2);
    assert!(
        info.imports
            .iter()
            .any(|i| i.source == "../layouts/Layout.astro")
    );
    assert!(
        info.imports
            .iter()
            .any(|i| i.source == "../components/Card")
    );
}

#[test]
fn astro_template_only_rendered_component_is_not_unused() {
    // `Header` is imported in frontmatter and used ONLY as a `<Header/>` tag in
    // the markup; the frontmatter semantic pass alone would call it unused, but
    // the template-used credit must keep it out of `unused_import_bindings` so
    // `referenced_import_bindings` (imports minus unused) includes it.
    let info = parse_source_to_module(
        FileId(0),
        Path::new("Page.astro"),
        r"---
import Header from '../components/Header.astro';
import { fmt } from '../lib/util.ts';
const title = fmt('hi');
---
<html><body><h1>{title}</h1><Header /></body></html>
",
        0,
        false,
    );
    assert!(
        !info.unused_import_bindings.contains(&"Header".to_string()),
        "template-rendered component must not be unused: {:?}",
        info.unused_import_bindings
    );
    // `fmt` is used in the frontmatter script, so it is a real value reference.
    assert!(
        info.value_referenced_import_bindings
            .contains(&"fmt".to_string()),
        "frontmatter-used import must be value-referenced: {:?}",
        info.value_referenced_import_bindings
    );
}

#[test]
fn astro_template_expression_binding_is_credited() {
    // `tmplOnly` is consumed only inside a `{tmplOnly()}` markup expression.
    let info = parse_source_to_module(
        FileId(0),
        Path::new("Page.astro"),
        r"---
import { tmplOnly } from '../lib/util.ts';
---
<p>{tmplOnly()}</p>
",
        0,
        false,
    );
    assert!(
        !info
            .unused_import_bindings
            .contains(&"tmplOnly".to_string()),
        "expression-binding import must not be unused: {:?}",
        info.unused_import_bindings
    );
}

#[test]
fn astro_frontmatter_import_used_nowhere_is_unused() {
    let info = parse_source_to_module(
        FileId(0),
        Path::new("Page.astro"),
        r"---
import Dead from '../components/Dead.astro';
---
<div>nothing here references Dead</div>
",
        0,
        false,
    );
    assert!(
        info.unused_import_bindings.contains(&"Dead".to_string()),
        "import used nowhere must be unused: {:?}",
        info.unused_import_bindings
    );
}

#[test]
fn astro_identifier_inside_script_block_does_not_credit_frontmatter_import() {
    // A frontmatter import whose name appears ONLY inside a `<script>` block (a
    // client-script local) is not a markup use and stays unused.
    let info = parse_source_to_module(
        FileId(0),
        Path::new("Page.astro"),
        r"---
import Widget from '../components/Widget.astro';
---
<div>no markup use</div>
<script>const Widget = 1; console.log(Widget);</script>
",
        0,
        false,
    );
    assert!(
        info.unused_import_bindings.contains(&"Widget".to_string()),
        "script-block identifier must not credit a frontmatter import: {:?}",
        info.unused_import_bindings
    );
}

#[test]
fn astro_unused_prop_harvested_dead_used_prop_credited() {
    let info = parse_source_to_module(
        FileId(0),
        Path::new("Card.astro"),
        r"---
interface Props { title: string; unused: string }
const { title } = Astro.props;
---
<h1>{title}</h1>
",
        0,
        false,
    );
    let title = info
        .component_props
        .iter()
        .find(|p| p.name == "title")
        .expect("title harvested");
    let unused = info
        .component_props
        .iter()
        .find(|p| p.name == "unused")
        .expect("unused harvested");
    assert!(title.used_in_template, "title used in {{title}}: {title:?}");
    assert!(
        !unused.used_in_script && !unused.used_in_template,
        "unused prop must be dead: {unused:?}"
    );
    assert!(!info.has_unharvestable_props);
    assert!(!info.has_props_attrs_fallthrough);
}

#[test]
fn astro_props_member_access_credits_prop() {
    let info = parse_source_to_module(
        FileId(0),
        Path::new("Card.astro"),
        r"---
interface Props { title: string }
const heading = Astro.props.title;
---
<h1>{heading}</h1>
",
        0,
        false,
    );
    let title = info
        .component_props
        .iter()
        .find(|p| p.name == "title")
        .expect("title harvested");
    assert!(
        title.used_in_script,
        "Astro.props.title must credit title: {title:?}"
    );
}

#[test]
fn astro_props_rest_spread_abstains() {
    let info = parse_source_to_module(
        FileId(0),
        Path::new("Card.astro"),
        r"---
interface Props { a: string; b: string }
const { a, ...rest } = Astro.props;
---
<div>{a}{rest.b}</div>
",
        0,
        false,
    );
    assert!(
        info.has_props_attrs_fallthrough,
        "a rest element in the Astro.props destructure must abstain"
    );
}

#[test]
fn astro_props_interface_extends_abstains() {
    let info = parse_source_to_module(
        FileId(0),
        Path::new("Card.astro"),
        r"---
import type { Base } from './base';
interface Props extends Base { title: string }
const { title } = Astro.props;
---
<h1>{title}</h1>
",
        0,
        false,
    );
    assert!(
        info.has_unharvestable_props,
        "interface Props extends Base must abstain (names unresolvable)"
    );
}

#[test]
fn astro_props_whole_object_use_abstains() {
    let info = parse_source_to_module(
        FileId(0),
        Path::new("Card.astro"),
        r"---
interface Props { a: string }
const all = Astro.props;
doSomething(all);
---
<div>x</div>
",
        0,
        false,
    );
    assert!(
        info.has_props_attrs_fallthrough,
        "a whole-object Astro.props binding use must abstain"
    );
}

#[test]
fn astro_props_template_spread_abstains() {
    let info = parse_source_to_module(
        FileId(0),
        Path::new("Card.astro"),
        r"---
interface Props { a: string; b: string }
const { a } = Astro.props;
---
<Child {...Astro.props}>{a}</Child>
",
        0,
        false,
    );
    assert!(
        info.has_props_attrs_fallthrough,
        "a {{...Astro.props}} template spread must abstain"
    );
}

#[test]
fn astro_no_frontmatter_returns_empty() {
    let info = parse_source_to_module(
        FileId(0),
        Path::new("Simple.astro"),
        "<div>No frontmatter here</div>",
        0,
        false,
    );
    assert!(info.imports.is_empty());
    assert!(info.exports.is_empty());
}

#[test]
fn astro_empty_frontmatter() {
    let info = parse_source_to_module(
        FileId(0),
        Path::new("Empty.astro"),
        "---\n---\n<div>Content</div>",
        0,
        false,
    );
    assert!(info.imports.is_empty());
}

#[test]
fn astro_frontmatter_with_dynamic_import() {
    let info = parse_source_to_module(
        FileId(0),
        Path::new("Dynamic.astro"),
        r"---
const mod = await import('../utils/helper');
---
<div>{mod.value}</div>
",
        0,
        false,
    );
    assert_eq!(info.dynamic_imports.len(), 1);
    assert_eq!(info.dynamic_imports[0].source, "../utils/helper");
}

#[test]
fn astro_frontmatter_with_reexport() {
    let info = parse_source_to_module(
        FileId(0),
        Path::new("ReExport.astro"),
        r"---
export { default as Layout } from '../layouts/Layout.astro';
---
<div>Content</div>
",
        0,
        false,
    );
    assert_eq!(info.re_exports.len(), 1);
}

#[test]
fn astro_template_script_src_followed() {
    let info = parse_source_to_module(
        FileId(0),
        Path::new("Page.astro"),
        r#"---
const title = "Hello";
---
<html>
  <body>
    <h1>{title}</h1>
    <script src="../scripts/foo.ts"></script>
  </body>
</html>
"#,
        0,
        false,
    );
    let sources: Vec<&str> = info.imports.iter().map(|i| i.source.as_str()).collect();
    assert!(
        sources.contains(&"../scripts/foo.ts"),
        "expected ../scripts/foo.ts in {sources:?}"
    );
}

#[test]
fn astro_template_script_src_has_source_span() {
    let source = r#"---
---
<html>
  <script src="../scripts/foo.ts"></script>
</html>
"#;
    let info = parse_source_to_module(FileId(0), Path::new("Page.astro"), source, 0, false);
    let import = info
        .imports
        .iter()
        .find(|i| i.source == "../scripts/foo.ts")
        .expect("script src import extracted");
    let (line, _col) = fallow_types::extract::byte_offset_to_line_col(
        &info.line_offsets,
        import.source_span.start,
    );
    assert_eq!(line, 4);
    assert_eq!(
        &source[import.source_span.start as usize..import.source_span.end as usize],
        "../scripts/foo.ts"
    );
}

#[test]
fn astro_template_inline_script_imports_followed() {
    let info = parse_source_to_module(
        FileId(0),
        Path::new("Page.astro"),
        r"---
---
<html>
  <body>
    <script>
      import '../scripts/bar';
    </script>
  </body>
</html>
",
        0,
        false,
    );
    let sources: Vec<&str> = info.imports.iter().map(|i| i.source.as_str()).collect();
    assert!(
        sources.contains(&"../scripts/bar"),
        "expected ../scripts/bar in {sources:?}"
    );
}

#[test]
fn astro_inline_script_import_spans_are_original_source_offsets() {
    let source = r"---
---
<html>
  <body>
    <script>
      import '../scripts/bar';
    </script>
  </body>
</html>
";
    let info = parse_source_to_module(FileId(0), Path::new("Page.astro"), source, 0, false);
    let import = info
        .imports
        .iter()
        .find(|i| i.source == "../scripts/bar")
        .expect("inline script import extracted");
    let (line, _col) =
        fallow_types::extract::byte_offset_to_line_col(&info.line_offsets, import.span.start);
    assert_eq!(line, 6);
    assert_eq!(
        &source[import.source_span.start as usize..import.source_span.end as usize],
        "'../scripts/bar'"
    );
}

#[test]
fn astro_template_combines_frontmatter_and_template_imports() {
    let info = parse_source_to_module(
        FileId(0),
        Path::new("Page.astro"),
        r#"---
import Layout from '../layouts/Layout.astro';
---
<Layout>
  <script src="../scripts/foo.ts"></script>
  <script>
    import '../scripts/bar';
  </script>
</Layout>
"#,
        0,
        false,
    );
    let sources: Vec<&str> = info.imports.iter().map(|i| i.source.as_str()).collect();
    assert!(sources.contains(&"../layouts/Layout.astro"));
    assert!(sources.contains(&"../scripts/foo.ts"));
    assert!(sources.contains(&"../scripts/bar"));
}

#[test]
fn astro_frontmatter_top_level_await_import_stays_lazy() {
    let info = parse_source_to_module(
        FileId(0),
        Path::new("Page.astro"),
        r#"---
const { data } = await import("./data");
---
<p>{data}</p>
"#,
        0,
        false,
    );
    assert!(
        info.dynamic_imports
            .iter()
            .any(|import| import.source == "./data")
    );
    assert!(crate::tests::eager_dynamic_import_sources(&info).is_empty());
}

fn parse_astro_with_complexity(source: &str) -> fallow_types::extract::ModuleInfo {
    parse_source_to_module(FileId(0), Path::new("Page.astro"), source, 0, true)
}

#[test]
fn astro_script_block_functions_are_scored_at_file_lines() {
    let info = parse_astro_with_complexity(
        r##"---
---
<button id="b">Run</button>
<script>
  function hard(items) {
    let n = 0;
    for (const a of items) {
      if (a.x) {
        for (const b of a.y) {
          if (b.z) {
            for (const c of b.w) {
              if (c.k && c.l) n++;
              else if (c.m || c.o) n--;
            }
          }
        }
      }
    }
    return n;
  }
  document.querySelector("#b").addEventListener("click", () => hard([]));
</script>
"##,
    );

    let hard = info
        .complexity
        .iter()
        .find(|fc| fc.name == "hard")
        .expect("the <script> function must be scored");
    assert_eq!(hard.line, 5);
    assert_eq!(hard.cyclomatic, 10);
    assert_eq!(hard.cognitive, 24);
}

#[test]
fn astro_frontmatter_and_every_script_block_are_scored() {
    let info = parse_astro_with_complexity(
        r"---
const pick = (flag: boolean) => (flag ? 1 : 0);
---
<p>Text</p>
<script is:inline>
function inlineHelper(x) { if (x) return 1; return 0; }
</script>
<script define:vars={{ limit: 3 }}>const check = (n) => { if (n > limit) return 1; return 0; };</script>
<script>
const processed = (v) => v ?? 0;
</script>
",
    );

    let find = |name: &str| {
        info.complexity
            .iter()
            .find(|fc| fc.name == name)
            .unwrap_or_else(|| panic!("{name} must be scored: {:?}", info.complexity))
    };
    assert_eq!(find("pick").line, 2);
    assert_eq!(find("inlineHelper").line, 6);
    assert_eq!(find("processed").line, 10);
    let check = find("check");
    assert_eq!(check.line, 8);
    // `<script define:vars={{ limit: 3 }}>const check = ` is 49 bytes, so the
    // arrow function starts at column 49 of the file line.
    assert_eq!(check.col, 49);
}

#[test]
fn astro_non_code_script_blocks_add_no_complexity() {
    let info = parse_astro_with_complexity(
        r#"<script src="/scripts/app.js"></script>
<script type="application/ld+json">{ "@context": "https://schema.org" }</script>
<script type="text/x-template">function notCode(a) { if (a) return 1; return 0; }</script>
<!-- <script>function commented(a) { if (a) return 1; return 0; }</script> -->
<script>   </script>
"#,
    );

    assert!(
        info.complexity.iter().all(|fc| fc.name == "<template>"),
        "{:?}",
        info.complexity
    );
}

#[test]
fn astro_module_script_block_is_scored() {
    let info = parse_astro_with_complexity(
        "<p>Text</p>\n<script type=\"module\">function f(a) { if (a) return 1; return 0; }</script>\n",
    );
    let f = info
        .complexity
        .iter()
        .find(|fc| fc.name == "f")
        .expect("a module script must be scored");
    assert_eq!(f.line, 2);
    assert_eq!(f.cyclomatic, 2);
}

#[test]
fn astro_script_block_complexity_is_skipped_without_need_complexity() {
    let info = parse_source_to_module(
        FileId(0),
        Path::new("Page.astro"),
        "<script>function f(a) { if (a) return 1; return 0; }</script>\n",
        0,
        false,
    );
    assert!(info.complexity.is_empty());
}