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
use std::path::Path;

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

use crate::parse_source_to_module;

/// Far past the depth at which the Oxc parser overflows a 16 MiB stack.
const DEEP: usize = 20_000;

fn parse(path: &str, source: &str) -> ModuleInfo {
    parse_source_to_module(FileId(0), Path::new(path), source, 0, true)
}

fn assert_degraded(info: &ModuleInfo) {
    assert!(
        info.parse_panicked,
        "a too-deep file must be a failed parse"
    );
    assert!(info.parse_error_count > 0);
    assert!(info.exports.is_empty());
    assert!(info.imports.is_empty());
}

#[test]
fn deeply_nested_parens_degrade_instead_of_overflowing() {
    let source = format!(
        "import a from './a';\nexport const x = {}1{};\n",
        "(".repeat(DEEP),
        ")".repeat(DEEP)
    );
    let info = parse("src/index.mjs", &source);
    assert_degraded(&info);
    assert_eq!(
        info.line_offsets.len(),
        3,
        "line offsets still describe the source"
    );
}

#[test]
fn deeply_nested_arrays_in_a_glimmer_file_degrade() {
    let source = format!("export default {};\n", "[".repeat(DEEP));
    assert_degraded(&parse("src/App.gts", &source));
}

#[test]
fn deep_nesting_inside_a_vue_script_degrades() {
    let source = format!(
        "<script setup>\nconst x = {}{};\n</script>\n",
        "{a:".repeat(DEEP),
        "}".repeat(DEEP)
    );
    assert_degraded(&parse("src/App.vue", &source));
}

#[test]
fn closers_in_literals_do_not_hide_real_nesting() {
    // Each level opens a real paren and hides a closer in a literal. A plain
    // byte count sees depth 0 or 1 here.
    for unit in ["(')',", "(\")\",", "(`)`,", "(/)/,", "(/*)*/", "(//)\n"] {
        let source = format!("f{};\n", unit.repeat(DEEP));
        assert_degraded(&parse("src/index.js", &source));
    }
}

#[test]
fn template_substitutions_count_as_nesting() {
    let source = format!("const s = {}{};\n", "`${".repeat(DEEP), "}`".repeat(DEEP));
    assert_degraded(&parse("src/index.js", &source));
}

#[test]
fn ordinary_nesting_still_parses() {
    let depth = 200;
    let source = format!(
        "import a from './a';\nexport const x = {}a{};\n",
        "[".repeat(depth),
        "]".repeat(depth)
    );
    let info = parse("src/index.ts", &source);
    assert!(!info.parse_panicked);
    assert_eq!(info.parse_error_count, 0);
    assert_eq!(info.exports.len(), 1);
    assert_eq!(info.imports.len(), 1);
}

#[test]
fn brackets_inside_literals_do_not_count_as_nesting() {
    // Serialized parser tables and regexes carry many unbalanced openers in
    // strings. They are not nesting and must not degrade the file.
    let openers = "(".repeat(DEEP);
    let source = format!(
        "export const a = '{openers}';\nexport const b = \"{openers}\";\n\
         export const c = `{openers}`;\nexport const d = /\\({{3}}[(]/;\n\
         // {openers}\n/* {openers} */\nexport const e = 1 / (2) / 3;\n"
    );
    let info = parse("src/index.js", &source);
    assert!(!info.parse_panicked);
    assert_eq!(info.exports.len(), 5);
}

fn deep_custom_property() -> String {
    format!(".a{{--x:{}1{};}}\n", "(".repeat(DEEP), ")".repeat(DEEP))
}

#[test]
fn deep_css_after_a_protocol_relative_url_degrades() {
    // Minified CSS is one line. A `//` in `url()` is not a comment in CSS, so
    // the nesting after it must still count.
    let source = format!(
        ".b{{background:url(//cdn.example.com/a.png)}}{}",
        deep_custom_property()
    );
    assert_degraded(&parse("src/styles.css", &source));
}

#[test]
fn css_module_exports_skip_a_too_deep_stylesheet() {
    let source = format!(".keep{{color:red}}\n{}", deep_custom_property());
    let exports = crate::css::extract_css_module_exports(&source, false);
    assert!(
        exports
            .iter()
            .any(|export| export.name.to_string() == "keep")
    );
}

#[test]
fn css_analytics_skip_a_too_deep_stylesheet() {
    assert!(crate::css_metrics::compute_css_analytics(&deep_custom_property()).is_none());
}

/// Depth of the nested color functions. lightningcss parses the content of an
/// unclosed `light-dark(`, `rgb(` or `hsl(` twice at each level, so at this
/// depth a parse takes far longer than the time limit of the test.
const COLOR_DEPTH: usize = 40;

/// Extract the CSS Modules classes of `source` on another thread and fail when
/// the extraction does not end in time.
fn css_module_exports_in_time(source: String) -> Vec<String> {
    let (done, finished) = std::sync::mpsc::channel();
    std::thread::spawn(move || {
        let names = crate::css::extract_css_module_exports(&source, false)
            .iter()
            .map(|export| export.name.to_string())
            .collect::<Vec<_>>();
        let _ = done.send(names);
    });
    finished
        .recv_timeout(std::time::Duration::from_secs(20))
        .expect("CSS Modules extraction must end in time")
}

/// In CSS a `}` or `]` inside an open `(` is a plain token, so it does not end
/// the color functions that lightningcss still nests.
#[test]
fn a_brace_inside_a_color_function_does_not_close_it() {
    for repeated in ["light-dark(}", "rgb(]", "light-dark(i}\n.b{x:"] {
        let source = format!(".keep{{color:red}}\n.a{{x:{}", repeated.repeat(COLOR_DEPTH));
        let names = css_module_exports_in_time(source.clone());
        assert!(names.contains(&"keep".to_string()), "{repeated}: {names:?}");
        assert!(
            crate::parse_style_bounded(&source, || ()).is_none(),
            "{repeated} nesting must skip the parser"
        );
    }
}

/// CSS reads an unquoted `url(` body up to its first `)`, so a `{` in the body
/// does not hide the color functions after it.
#[test]
fn a_brace_in_a_url_does_not_hide_color_functions() {
    let source = format!(
        ".keep{{color:red}}\n.a{{x:url(a{{b)}}\n.b{{x:{}",
        "light-dark(".repeat(COLOR_DEPTH)
    );
    let names = css_module_exports_in_time(source.clone());
    assert!(names.contains(&"keep".to_string()), "{names:?}");
    assert!(crate::parse_style_bounded(&source, || ()).is_none());
}

/// lightningcss panics when a math function where it expects a percentage,
/// such as `opacity`, `font-stretch` or a color channel, does not reduce to
/// one percentage. The declarations have no closing `}`, as in the fuzz input,
/// because a `}` inside an open `(` is a plain token.
#[test]
fn a_math_function_that_panics_the_css_parser_skips_it() {
    for declaration in [
        "o:lab(max(4,nan",
        "color:lab(1 MAX(4%, NaN) 1)",
        "opacity:max(4%,5)",
        "opacity:clamp(1%,2,3%)",
        "opacity:calc(4% + 5)",
        "opacity:calc(max(4%,5))",
        "opacity:abs(4%)",
        "color:rgb(round(up,4%,5) 1 1)",
        "opacity:m\\61 x(4%,5)",
        "font-stretch:calc(1 / 2)",
        "-webkit-text-size-adjust:max(1, 2)",
        "color:rgb(10%, 10%, calc(1/2))",
        "--x:var(--y, rgba(1%,2%,max(1,2)))",
        "--x:max(lab(max(4%,5) 1 1))",
        "color:calc(1 + lab(1 rgb(abs(4%) 1 1) 1))",
        // The tokenizer reads escapes, `url(` tokens and bad strings, so
        // they hide no math function.
        "color:r\\67 b(max(4%,5) 1 1)",
        "b:\\75rl(x( );opacity:max(4%,5)",
        "b:\"max(\n;opacity:max(4%,5)",
    ] {
        let source = format!(".keep{{color:red}}\n.a{{{declaration}");
        let names = css_module_exports_in_time(source.clone());
        assert!(
            names.contains(&"keep".to_string()),
            "{declaration}: {names:?}"
        );
        assert!(
            crate::css_metrics::compute_css_analytics(&source).is_none(),
            "{declaration} must skip the parser"
        );
    }
}

/// A number-only math function makes lightningcss panic in a keyframe
/// selector and in the `initial-value` of a `@property`.
#[test]
fn a_number_math_function_where_a_percentage_is_required_skips_the_css_parser() {
    for source in [
        ".keep{ color: red }\n@keyframes k{ calc(1 / 2) { opacity: 1 } }",
        ".keep{ color: red }\n@property --p{ syntax: '<percentage>'; inherits: false; initial-value: calc(1 / 2) }",
    ] {
        let names = css_module_exports_in_time(source.to_string());
        assert!(names.contains(&"keep".to_string()), "{source}: {names:?}");
        assert!(
            crate::parse_style_bounded(source, || ()).is_none(),
            "{source}"
        );
    }
}

/// lightningcss parses the number first where a property takes a number or a
/// percentage, so a number-only math function there is safe.
#[test]
fn a_number_math_function_where_a_number_is_allowed_still_parses() {
    let source = ".a{z-index:calc(1 + 1);line-height:calc(3 / 2);opacity:calc(1 - 0.5)}\n\
                  .b{color:hsl(calc(200 + 10) 50% 50%);background:lab(calc(50 / 2) 1 1)}\n\
                  .c{opacity:min(1, 0.5);font-weight:clamp(100, 400, 900);aspect-ratio:calc(16 / 9)}\n\
                  :root{--text-xs--line-height:calc(1 / 0.75)}\n\
                  .d:hover{transform:scale(calc(1 + 0.1))}";
    assert!(crate::parse_style_bounded(source, || ()).is_some());
    assert!(crate::css_metrics::compute_css_analytics(source).is_some());
}

/// A hex escape takes one whitespace after it, so a newline there does not
/// end a string, and the real closing quote does not start a new one.
#[test]
fn a_newline_after_a_hex_escape_does_not_end_a_string() {
    for escape in ["\\a\r", "\\a\n", "\\a\r\n", "\\1\u{c}", "\\00000a\n"] {
        let source = format!(
            "a{{--x:\"{escape}\"{}1{}}}",
            "(".repeat(DEEP),
            ")".repeat(DEEP)
        );
        assert!(
            crate::parse_style_bounded(&source, || ()).is_none(),
            "{escape:?}"
        );
    }
}

/// A bad string ends at its newline, so a `/*` in it does not hide the
/// nesting after it from the stack estimate.
#[test]
fn a_comment_opener_in_a_bad_string_does_not_hide_nesting() {
    let source = format!("a{{--x:\"/*\n;}}\n{}", "a{".repeat(DEEP));
    assert!(crate::parse_style_bounded(&source, || ()).is_none());
    assert_degraded(&parse("src/a.css", &source));
}

/// Each level of nested math functions doubles the parse time of lightningcss
/// when the innermost value is not of the expected type.
#[test]
fn deeply_nested_math_functions_skip_the_css_parser() {
    for name in ["calc(", "abs(", "round(1,", "clamp(1,", "atan2(1px,"] {
        let depth = if name.starts_with("atan2") { 3 } else { 26 };
        let source = format!(".keep{{color:red}}\n.a{{width:{}1px", name.repeat(depth));
        let names = css_module_exports_in_time(source.clone());
        assert!(names.contains(&"keep".to_string()), "{name}: {names:?}");
        assert!(
            crate::parse_style_bounded(&source, || ()).is_none(),
            "{name} nesting must skip the parser"
        );
    }
}

#[test]
fn ordinary_math_functions_still_parse() {
    let source = ".a{width:calc(100% - 2rem);font-size:clamp(1rem, 2.5vw, 2rem)}\n\
                  .e{font-size:calc(1rem * clamp(1, 1.2, 1.5));width:calc(100% * min(1, 2))}\n\
                  .b{opacity:calc(50% * 2);width:max(10%, 5px);height:calc(var(--h) / 2)}\n\
                  .c{grid-template-columns:repeat(3, calc(100% / 3));--x:min(1px, 2px)}\n\
                  .nan{animation-name:nan;width:var(--nan, 1px)}\n\
                  @media (min-width: calc(40rem + 1px)) {.d{opacity:max(50%, 20%)}}";
    assert!(crate::parse_style_bounded(source, || ()).is_some());
    assert!(crate::css_metrics::compute_css_analytics(source).is_some());
}

/// The scan reads each escape once, so a long run of escapes stays linear.
#[test]
fn a_long_escape_chain_scans_in_time() {
    let source = format!(".keep{{color:red}}\n.a{{x:({}", "\\!".repeat(500_000));
    let names = css_module_exports_in_time(source);
    assert!(names.contains(&"keep".to_string()));
}

#[test]
fn deeply_nested_color_functions_skip_the_css_parser() {
    for name in [
        "light-dark(",
        "rgb(from red r g b / ",
        "HSL(",
        "light-dark(red, ",
    ] {
        let source = format!(".keep{{color:red}}\n.a{{x:{}]}}", name.repeat(COLOR_DEPTH));
        let names = css_module_exports_in_time(source.clone());
        assert!(names.contains(&"keep".to_string()), "{name}: {names:?}");
        assert!(
            crate::parse_style_bounded(&source, || ()).is_none(),
            "{name} nesting must skip the parser"
        );
        assert!(crate::css_metrics::compute_css_analytics(&source).is_none());
    }
}

#[test]
fn ordinary_color_function_nesting_still_parses() {
    let source = ".a{color:color-mix(in srgb, light-dark(rgb(from var(--x) r g b / 50%), \
                  hsl(0 0% 0%)), red)}\n.b{--c:light-dark(rgb(0 0 0), hsl(0 0% 100%))}";
    assert!(crate::parse_style_bounded(source, || ()).is_some());
    assert!(crate::css_metrics::compute_css_analytics(source).is_some());
}

/// Open color functions stop only the lightningcss parse. The bracket depth
/// of a stylesheet must still count every bracket after them.
#[test]
fn color_functions_do_not_hide_bracket_nesting() {
    let source = format!(".a{{x:{}{}", "rgb(".repeat(COLOR_DEPTH), "(".repeat(5000));
    assert!(crate::nesting::parse_file_bounded(Path::new("a.css"), &source, || ()).is_none());
    assert_degraded(&parse("a.css", &source));
}

#[test]
fn a_hash_or_at_url_prefix_does_not_hide_nesting() {
    for prefix in ["#url(", "@url(", "url("] {
        let source = format!(".a{{--x:{prefix}{}}}", "(".repeat(DEEP));
        assert!(
            crate::parse_style_bounded(&source, || ()).is_none(),
            "{prefix} hid the nesting"
        );
    }
}

/// Depth of the nested destructure defaults. Work that doubles at each level
/// does not end in the time limit at this depth.
const DEFAULT_DEPTH: usize = 40;

/// Extract `source` on another thread and fail when it does not end in time.
fn assert_extracts_in_time(source: String) {
    let (done, finished) = std::sync::mpsc::channel();
    std::thread::spawn(move || {
        let info = parse("src/index.ts", &source);
        let _ = done.send(info.parse_error_count);
    });
    let errors = finished
        .recv_timeout(std::time::Duration::from_secs(20))
        .expect("extraction must end in time");
    assert_eq!(errors, 0);
}

#[test]
fn nested_array_destructure_defaults_cost_linear_work() {
    let mut pattern = String::from("{ m }");
    for _ in 0..DEFAULT_DEPTH {
        pattern = format!("[{pattern} = [s]]");
    }
    assert_extracts_in_time(format!(
        "declare const s: any;\nconst {pattern} = [s];\nfor (const {pattern} of [[s]]) {{}}\n"
    ));
}

#[test]
fn nested_object_destructure_assignment_defaults_cost_linear_work() {
    let mut pattern = String::from("{ m }");
    for _ in 0..DEFAULT_DEPTH {
        pattern = format!("{{ k: {pattern} = s }}");
    }
    assert_extracts_in_time(format!(
        "declare const s: any;\nlet m: unknown;\n({pattern} = s);\n"
    ));
}

/// Elements before the class instance in the wide destructure sources. Far
/// past any per-walk step count, so a shared step budget loses the last one.
const WIDE_ELEMENTS: usize = 1_500;

/// Whether `info` reads `member` of the local class `Service`.
fn reads_service_member(info: &ModuleInfo, member: &str) -> bool {
    info.member_accesses
        .iter()
        .any(|access| access.object == "Service" && access.member == member)
}

#[test]
fn wide_for_of_destructure_credits_the_last_element() {
    let others = "{ viaWideForOf: () => {} }, ".repeat(WIDE_ELEMENTS);
    let source = format!(
        "export class Service {{ viaWideForOf() {{}} }}\nconst service = new Service();\n\
         for (const {{ viaWideForOf }} of [{others}service]) viaWideForOf();\n"
    );
    let info = parse("src/index.ts", &source);
    assert!(reads_service_member(&info, "viaWideForOf"));
}

#[test]
fn wide_array_destructure_credits_the_last_element() {
    let holes = ", ".repeat(WIDE_ELEMENTS);
    let others = "{}, ".repeat(WIDE_ELEMENTS);
    let wrapped = "[{}], ".repeat(WIDE_ELEMENTS);
    let source = format!(
        "export class Service {{ viaWideArray() {{}} viaWideNested() {{}} }}\n\
         const service = new Service();\n\
         const [{holes}{{ viaWideArray }}] = [{others}service];\n\
         for (const [{{ viaWideNested }}] of [{wrapped}[service]]) viaWideNested();\n\
         viaWideArray();\n"
    );
    let info = parse("src/index.ts", &source);
    assert!(reads_service_member(&info, "viaWideArray"));
    assert!(reads_service_member(&info, "viaWideNested"));
}

/// Nesting levels of the spread source.
const SPREAD_DEPTH: usize = 20;

#[test]
fn nested_spread_sources_cost_bounded_work() {
    let mut pattern = String::from("{ m }");
    let mut source = String::from("s");
    for _ in 0..SPREAD_DEPTH {
        pattern = format!("{{ k: {pattern} }}");
        source = format!("{{ ...a, ...b, k: {source} }}");
    }
    assert_extracts_in_time(format!(
        "declare const s: any, a: any, b: any;\nconst {pattern} = {source};\n"
    ));
}

/// Elements and pattern properties of the wide-by-wide destructure.
const WIDE_BY_WIDE: usize = 2_000;

#[test]
fn wide_pattern_over_wide_source_records_bounded_entries() {
    let keys = (0..WIDE_BY_WIDE)
        .map(|index| format!("m{index}"))
        .collect::<Vec<_>>()
        .join(", ");
    let elements = "service, ".repeat(WIDE_BY_WIDE);
    let source = format!(
        "export class Service {{}}\nconst service = new Service();\n\
         for (const {{ {keys} }} of [{elements}]) {{}}\n"
    );
    let info = parse("src/index.ts", &source);
    assert!(
        info.member_accesses.len() < WIDE_BY_WIDE * WIDE_BY_WIDE / 4,
        "one destructure recorded {} member accesses",
        info.member_accesses.len()
    );
}

#[test]
fn deep_inline_html_script_skips_its_body() {
    // The quotes on the first line make the whole-file scan read the deep
    // script as one string, so only the guard of the inline parse sees the
    // depth.
    let source = format!(
        "<p>'</p><script type=\"module\">import \"./deep.js\"; const x = {}1{};</script><p>'</p>\n\
         <script type=\"module\">import \"./ok.js\";</script>\n",
        "[".repeat(DEEP),
        "]".repeat(DEEP)
    );
    let info = parse("index.html", &source);
    assert!(!info.parse_panicked, "the page itself must still parse");
    let sources: Vec<&str> = info
        .imports
        .iter()
        .map(|import| import.source.as_str())
        .collect();
    assert!(sources.contains(&"./ok.js"), "{sources:?}");
    assert!(!sources.contains(&"./deep.js"), "{sources:?}");
}