fallow-extract 3.40.0

AST extraction engine for fallow codebase intelligence (parser, complexity, SFC / Astro / MDX / CSS)
Documentation
//! A `new URL("./node_modules/<pkg>/...", import.meta.url)` path locates a file
//! or a directory of an installed package, so the package is in use. Only a
//! relative string literal with a `node_modules/` segment credits a package.

use crate::tests::parse_ts as parse_source;

fn package_references(source: &str) -> Vec<String> {
    parse_source(source).package_path_references.to_vec()
}

#[test]
fn new_url_into_node_modules_credits_the_package() {
    let source = "export const a = new URL('./node_modules/icon-pack/svgs/', import.meta.url).pathname;\n\
         export const b = new URL('../node_modules/@scope/assets/logo.svg', import.meta.url);\n\
         export const c = new URL('./node_modules/.pnpm/x@1/node_modules/nested-pkg/a.js', import.meta.url);\n\
         export const d = new URL('./node_modules/bare-dir', import.meta.url);\n";
    assert_eq!(
        package_references(source),
        ["icon-pack", "@scope/assets", "nested-pkg", "bare-dir"]
    );
}

#[test]
fn new_url_without_a_package_segment_credits_nothing() {
    let source = "export const a = new URL('./node_modules/', import.meta.url);\n\
         export const b = new URL('./node_modules/.bin/tool', import.meta.url);\n\
         export const c = new URL('./node_modules/@scope/', import.meta.url);\n\
         export const d = new URL('./src/my_node_modules/pkg/a.js', import.meta.url);\n\
         export const e = new URL('/node_modules/pkg/a.js', import.meta.url);\n\
         export const f = new URL('./node_modules/pkg/a.js', 'https://example.com');\n\
         export const g = new URL('./node_modules/pkg/a.js');\n";
    assert!(package_references(source).is_empty());
}

#[test]
fn new_url_bare_relative_path_into_node_modules_credits_the_package() {
    let source = "export const a = new URL('node_modules/icon-pack/svgs/', import.meta.url);\n";
    assert_eq!(package_references(source), ["icon-pack"]);
}

#[test]
fn new_url_template_with_a_static_package_segment_credits_the_package() {
    let source = "const name = 'x';\n\
         export const a = new URL(`./node_modules/icon-pack/svgs/${name}.svg`, import.meta.url);\n\
         export const b = new URL(`./node_modules/@scope/assets/${name}`, import.meta.url);\n\
         export const c = new URL(`./node_modules/static-pkg/a.svg`, import.meta.url);\n";
    assert_eq!(
        package_references(source),
        ["icon-pack", "@scope/assets", "static-pkg"]
    );
}

#[test]
fn new_url_template_with_a_dynamic_package_segment_credits_nothing() {
    let source = "const name = 'x';\n\
         export const a = new URL(`./node_modules/${name}/a.svg`, import.meta.url);\n\
         export const b = new URL(`./node_modules/pkg-${name}/a.svg`, import.meta.url);\n\
         export const c = new URL(`./node_modules/@scope/${name}/a.svg`, import.meta.url);\n";
    assert!(package_references(source).is_empty());
}

#[test]
fn new_url_query_or_fragment_is_not_part_of_the_package_name() {
    let source = "export const a = new URL('./node_modules/icon-pack?raw', import.meta.url);\n\
         export const b = new URL('./node_modules/@scope/assets#x', import.meta.url);\n";
    assert_eq!(package_references(source), ["icon-pack", "@scope/assets"]);
}