rhaiper 0.2.0

Minifier for Rhai scripts, with Source Map v3 output
Documentation
//! Source map round-tripping, and validation against an independent Source Map v3 implementation.

use rhaiper::map::{MapOptions, SourceMap};
use rhaiper::{Options, minify};

const SCRIPTS: &[&str] = &[
    "let x = 1;\nlet y = 2;\nx + y\n",
    "// header\nfn add(a, b) {\n    a + b   // sum\n}\nadd(1, 2)\n",
    "let total = 0;\nfor i in 0..10 {\n    total += i;\n}\ntotal\n",
    "let s = `a ${1 + 2} b`;\ns.len\n",
    "let a = 1;\nlet b = a - -1;\nb\n",
];

/// Invariant 5: every token's position in the output must map back to where it came from.
#[test]
fn every_mapping_round_trips() {
    for src in SCRIPTS {
        let out = minify(src, &Options::default()).expect("minify");
        let json = out.to_source_map(src, &MapOptions::default());
        let map = SourceMap::from_json(&json).expect("parse map");

        for m in &out.mappings {
            let got = map
                .resolve(&out.text, m.gen_line + 1, m.gen_col + 1)
                .unwrap_or_else(|| {
                    panic!(
                        "no mapping for generated {}:{} in {src:?}",
                        m.gen_line + 1,
                        m.gen_col + 1
                    )
                });
            assert_eq!(
                (got.line, got.column),
                (m.src_line + 1, m.src_col + 1),
                "wrong original position for generated {}:{} in {src:?}",
                m.gen_line + 1,
                m.gen_col + 1
            );
        }
    }
}

/// Resolving a position that falls *inside* a token should land on that token's start, which is what
/// makes a runtime error position useful rather than merely exact.
#[test]
fn interior_positions_resolve_to_the_enclosing_token() {
    let src = "let alpha = 1;\nlet beta = alpha + 2;\nbeta\n";
    let out = minify(src, &Options::default()).expect("minify");
    let json = out.to_source_map(src, &MapOptions::default());
    let map = SourceMap::from_json(&json).expect("parse map");

    // `alpha` on line 2 of the source; find where it landed in the output.
    let col = out
        .text
        .find("alpha+2")
        .expect("expected `alpha+2` in output");
    let mapping_col = out.text[..col].chars().count() as u32;

    // A position one character into the identifier still resolves to the identifier's start.
    let got = map.resolve(&out.text, 1, mapping_col + 2).expect("resolve");
    assert_eq!(got.line, 2);
    assert_eq!(
        got.column, 12,
        "should point at `alpha` in `let beta = alpha + 2;`"
    );
}

/// The map we write must be readable by an independent implementation, and agree with it.
#[test]
fn agrees_with_the_sourcemap_crate() {
    for src in SCRIPTS {
        let out = minify(src, &Options::default()).expect("minify");
        let json = out.to_source_map(src, &MapOptions::default());

        let theirs = sourcemap::SourceMap::from_slice(json.as_bytes())
            .unwrap_or_else(|e| panic!("sourcemap crate rejected our map for {src:?}: {e}"));

        for m in &out.mappings {
            let token = theirs
                .lookup_token(m.gen_line, m.gen_col)
                .unwrap_or_else(|| panic!("sourcemap crate found no token for {m:?}"));
            assert_eq!(
                (token.get_src_line(), token.get_src_col()),
                (m.src_line, m.src_col),
                "disagreement for {m:?} in {src:?}"
            );
        }
    }
}

#[test]
fn embeds_sources_content_when_asked() {
    let src = "let x = 1; x\n";
    let out = minify(src, &Options::default()).expect("minify");

    let with = out.to_source_map(src, &MapOptions::default());
    assert!(with.contains("sourcesContent"));
    assert_eq!(
        SourceMap::from_json(&with).unwrap().sources_content[0].as_deref(),
        Some(src)
    );

    let without = out.to_source_map(
        src,
        &MapOptions {
            include_sources_content: false,
            ..Default::default()
        },
    );
    assert!(!without.contains("sourcesContent"));
}

/// Columns are stored as UTF-16 code units per the spec, but reported in characters to match how
/// rhai counts them. The two differ only past an astral-plane character.
#[test]
fn astral_characters_convert_between_column_units() {
    // The emoji is one character but two UTF-16 code units, so every column after it differs.
    let src = "let flag = \"🎉🎉\";\nlet after = flag.len;\nafter\n";
    let out = minify(src, &Options::default()).expect("minify");
    let json = out.to_source_map(src, &MapOptions::default());
    let map = SourceMap::from_json(&json).expect("parse map");

    for m in &out.mappings {
        let got = map
            .resolve(&out.text, m.gen_line + 1, m.gen_col + 1)
            .expect("resolve");
        assert_eq!(
            (got.line, got.column),
            (m.src_line + 1, m.src_col + 1),
            "astral conversion broke for {m:?}"
        );
    }

    // Independently confirm the stored columns really are UTF-16 while reported ones are characters.
    let theirs = sourcemap::SourceMap::from_slice(json.as_bytes()).expect("parse");

    let semi = out.text.find(';').expect("a semicolon");
    let gen_chars = out.text[..semi].chars().count() as u32;
    let gen_utf16 = out.text[..semi].encode_utf16().count() as u32;
    assert_ne!(
        gen_chars, gen_utf16,
        "this test is only meaningful with an astral character before the semicolon"
    );

    let first_line = src.lines().next().unwrap();
    let semi_in_src = first_line.find(';').unwrap();
    let src_chars = first_line[..semi_in_src].chars().count() as u32;
    let src_utf16 = first_line[..semi_in_src].encode_utf16().count() as u32;
    assert_ne!(src_chars, src_utf16);

    // The `sourcemap` crate takes and returns UTF-16 columns.
    let token = theirs.lookup_token(0, gen_utf16).expect("token");
    assert_eq!(token.get_src_col(), src_utf16);

    // Ours takes and returns rhai's 1-based character columns.
    assert_eq!(
        map.resolve(&out.text, 1, gen_chars + 1).unwrap().column,
        src_chars + 1
    );
}