Skip to main content

fallow_extract/
module_parse.rs

1//! Public entry points that parse one source into a [`ModuleInfo`] on a stack
2//! that is large enough for it.
3
4use std::path::Path;
5
6use fallow_types::discover::FileId;
7use fallow_types::extract::ModuleInfo;
8
9use crate::parse::parse_source_to_module_unbounded;
10use fallow_types::extract::FlagPatterns;
11
12/// Parse source text into a [`ModuleInfo`].
13///
14/// When `need_complexity` is false the per-function complexity visitor is
15/// skipped, saving one full AST walk per file.  The dead-code analysis
16/// pipeline never consumes complexity data, so callers that only need
17/// imports/exports should pass `false`.
18///
19/// Flag detection uses the built-in patterns only; see
20/// [`parse_source_to_module_with_flags`].
21pub fn parse_source_to_module(
22    file_id: FileId,
23    path: &Path,
24    source: &str,
25    content_hash: u64,
26    need_complexity: bool,
27) -> ModuleInfo {
28    parse_source_to_module_with_flags(
29        file_id,
30        path,
31        source,
32        content_hash,
33        need_complexity,
34        &FlagPatterns::default(),
35    )
36}
37
38/// Parse source text into a [`ModuleInfo`], with the user flag patterns
39/// applied on top of the built-in ones.
40pub fn parse_source_to_module_with_flags(
41    file_id: FileId,
42    path: &Path,
43    source: &str,
44    content_hash: u64,
45    need_complexity: bool,
46    flag_patterns: &FlagPatterns,
47) -> ModuleInfo {
48    let bounded = crate::nesting::parse_file_bounded(path, crate::strip_bom(source), || {
49        let module = parse_source_to_module_unbounded(
50            file_id,
51            path,
52            source,
53            content_hash,
54            need_complexity,
55            flag_patterns,
56        );
57        // The CSS comment mask counts on the thread that parsed. Carry the
58        // count back in case that was a sized thread.
59        (module, crate::css_masked_bytes::take())
60    });
61    match bounded {
62        Some((module, masked_bytes)) => {
63            crate::css_masked_bytes::add(masked_bytes);
64            module
65        }
66        None => crate::nesting::too_deep_module(file_id, source, content_hash),
67    }
68}