pub mod emit;
pub mod lex;
pub mod map;
pub mod rename;
pub mod stream;
pub use emit::{Mapping, Options};
use rhai::{Engine, Token};
#[derive(Debug)]
pub enum Error {
InvalidInput(Box<rhai::ParseError>),
Lex(lex::LexFailure),
TokenMismatch {
index: usize,
expected: String,
found: String,
},
OutputInvalid(Box<rhai::ParseError>),
}
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::InvalidInput(e) => write!(f, "input script does not parse: {e}"),
Self::Lex(e) => write!(f, "{e}"),
Self::TokenMismatch {
index,
expected,
found,
} => write!(
f,
"token {index} of the output is {found} but should be {expected} \
(this is a bug in rhaiper)"
),
Self::OutputInvalid(e) => {
write!(
f,
"minified output does not parse: {e} (this is a bug in rhaiper)"
)
}
}
}
}
impl std::error::Error for Error {}
impl From<lex::LexFailure> for Error {
fn from(e: lex::LexFailure) -> Self {
Self::Lex(e)
}
}
#[derive(Debug)]
pub struct Minified {
pub text: String,
pub mappings: Vec<Mapping>,
pub renamed_bindings: usize,
pub rename_declined: Vec<String>,
}
impl Minified {
pub fn to_source_map(&self, src: &str, opts: &map::MapOptions) -> String {
map::to_json(src, &self.text, &self.mappings, opts)
}
}
pub(crate) fn parsing_engine(opts: &Options) -> Engine {
#[allow(unused_mut)]
let mut engine = Engine::new_raw();
#[cfg(not(feature = "no_optimize"))]
engine.set_optimization_level(rhai::OptimizationLevel::None);
#[cfg(not(feature = "unchecked"))]
if let Some((global, _function)) = opts.max_expr_depth {
#[cfg(not(feature = "no_function"))]
engine.set_max_expr_depths(global, _function);
#[cfg(feature = "no_function")]
engine.set_max_expr_depths(global);
}
let _ = opts;
engine
}
pub fn minify(src: &str, opts: &Options) -> Result<Minified, Error> {
minify_with_engine(&parsing_engine(opts), src, opts)
}
pub fn minify_with_engine(engine: &Engine, src: &str, opts: &Options) -> Result<Minified, Error> {
engine
.compile(src)
.map_err(|e| Error::InvalidInput(Box::new(e)))?;
let toks = lex::tokenize(src)?;
let renaming = if opts.rename {
rename::plan(engine, src, &toks)
} else {
rename::Outcome::default()
};
let emitted = emit::emit(src, &toks, opts, &renaming.renames);
let expected: Vec<(&Token, &str)> = emit::significant(&toks)
.iter()
.map(|t| {
let text = renaming
.renames
.get(&t.span.start)
.map_or_else(|| t.text(src), String::as_str);
(&t.token, text)
})
.collect();
let round_trip = lex::tokenize(&emitted.text)?;
let found: Vec<(&Token, &str)> = emit::significant(&round_trip)
.iter()
.map(|t| (&t.token, t.text(&emitted.text)))
.collect();
verify_tokens(&expected, &found)?;
engine
.compile(&emitted.text)
.map_err(|e| Error::OutputInvalid(Box::new(e)))?;
Ok(Minified {
text: emitted.text,
mappings: emitted.mappings,
renamed_bindings: renaming.renamed_bindings,
rename_declined: renaming.declined,
})
}
fn verify_tokens(expected: &[(&Token, &str)], found: &[(&Token, &str)]) -> Result<(), Error> {
if expected.len() != found.len() {
return Err(Error::TokenMismatch {
index: expected.len().min(found.len()),
expected: format!("{} tokens", expected.len()),
found: format!("{} tokens", found.len()),
});
}
for (index, ((want, want_text), (got, got_text))) in
expected.iter().zip(found.iter()).enumerate()
{
let ok = match (want, got) {
(Token::Identifier(..), Token::Identifier(..)) => want_text == got_text,
_ => want == got,
};
if !ok {
return Err(Error::TokenMismatch {
index,
expected: format!("{want_text:?} ({want:?})"),
found: format!("{got_text:?} ({got:?})"),
});
}
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::lex::tokenize;
use super::{Options, minify};
fn tiles(src: &str) -> Vec<String> {
match tokenize(src) {
Ok(toks) => toks.iter().map(|t| t.text(src).to_string()).collect(),
Err(e) => panic!("tokenize({src:?}) failed: {e}"),
}
}
fn min(src: &str) -> String {
match minify(src, &Options::default()) {
Ok(m) => m.text,
Err(e) => panic!("minify({src:?}) failed: {e}"),
}
}
#[test]
fn plain_statements() {
assert_eq!(tiles("let x = 1;"), ["let", "x", "=", "1", ";"]);
}
#[test]
fn comments_are_tokens() {
assert_eq!(
tiles("let x = 1; // trailing\nx"),
["let", "x", "=", "1", ";", "// trailing\n", "x"]
);
assert_eq!(
tiles("a /* one /* two */ */ b"),
["a", "/* one /* two */ */", "b"]
);
}
#[test]
fn string_literals_round_trip_verbatim() {
assert_eq!(tiles(r#""a\nb""#), [r#""a\nb""#]);
assert_eq!(
tiles(r#""quote\" and \\ back""#),
[r#""quote\" and \\ back""#]
);
assert_eq!(
tiles(r##"#"raw \n not escaped"#"##),
[r##"#"raw \n not escaped"#"##]
);
assert_eq!(
tiles(r###"##"nested "# inside"##"###),
[r###"##"nested "# inside"##"###]
);
assert_eq!(tiles(r"'\n'"), [r"'\n'"]);
assert_eq!(tiles(r"'\x41'"), [r"'\x41'"]);
}
#[test]
fn numeric_literals_keep_their_written_form() {
assert_eq!(tiles("0xFF"), ["0xFF"]);
assert_eq!(tiles("1_000_000"), ["1_000_000"]);
assert_eq!(tiles("1.5e-3"), ["1.5e-3"]);
assert_eq!(tiles("0b1010"), ["0b1010"]);
assert_eq!(tiles("0o777"), ["0o777"]);
}
#[test]
fn negative_literal_folds_the_sign_into_the_token() {
assert_eq!(tiles("a - -1"), ["a", "-", "-1"]);
}
#[test]
fn non_ascii_positions_resolve() {
assert_eq!(
tiles(r#"let s = "héllo 🎉"; s"#),
["let", "s", "=", r#""héllo 🎉""#, ";", "s"]
);
}
#[test]
fn interpolated_strings() {
assert_eq!(tiles("`a${x}b`"), ["`a$", "{", "x", "}", "b`"]);
assert_eq!(tiles("`${1+2}`"), ["`$", "{", "1", "+", "2", "}", "`"]);
assert_eq!(
tiles("`m ${ #{a:1}.a } z`"),
["`m $", "{", "#{", "a", ":", "1", "}", ".", "a", "}", " z`"]
);
}
#[test]
fn nested_interpolation() {
assert_eq!(
tiles("`outer ${ `inner ${y}` } tail`"),
[
"`outer $", "{", "`inner $", "{", "y", "}", "`", "}", " tail`"
]
);
}
#[test]
fn interpolation_containing_a_block() {
assert_eq!(
tiles("`v ${ if a { 1 } else { 2 } }`"),
[
"`v $", "{", "if", "a", "{", "1", "}", "else", "{", "2", "}", "}", "`"
]
);
}
#[test]
fn minifies_whitespace_and_comments() {
assert_eq!(
min("let count = 0; // running total\nfn step(x) {\n count += x;\n}\n"),
"let count=0;fn step(x){count+=x}"
);
}
#[test]
fn keeps_required_separators() {
assert_eq!(min("let x = 1 ; x"), "let x=1;x");
assert_eq!(min("if a in b { 1 }"), "if a in b{1}");
assert_eq!(min("let y = 1 . abs();"), "let y=1.abs()");
assert_eq!(min("let y = 1 . abs ( ) ;"), "let y=1.abs( )");
}
#[test]
fn separates_signs_that_would_form_reserved_symbols() {
assert_eq!(min("let x = a - -1;"), "let x=a- -1");
assert_eq!(min("let x = a + +b;"), "let x=a+ +b");
assert_eq!(min("let x = a - -b;"), "let x=a- -b");
}
#[test]
fn comments_do_not_disturb_unary_lexing() {
assert_eq!(min("let x = /*c*/ -1;"), "let x=-1");
assert_eq!(min("let x = 1 + /*c*/ -2;"), "let x=1+-2");
}
#[test]
fn drops_redundant_semicolons() {
assert_eq!(min("let x = 1;;;\nx;"), "let x=1;x");
assert_eq!(min("fn f() { let a = 1; a; }"), "fn f(){let a=1;a}");
assert_eq!(min("let m = #{a:1};\nm"), "let m=#{a:1};m");
assert_eq!(min("let b = { 1 };\nb"), "let b={1};b");
}
#[test]
fn preserves_empty_block() {
assert_eq!(min("fn f() { ; }"), "fn f(){;}");
}
#[test]
fn wraps_long_output() {
let src = (0..80)
.map(|i| format!("let variable_number_{i} = {i};\n"))
.collect::<String>();
let out = minify(
&src,
&Options {
max_line_len: 60,
..Default::default()
},
)
.expect("minify");
for line in out.text.lines() {
assert!(
line.chars().count() <= 60,
"line of {} chars exceeds the budget: {line:?}",
line.chars().count()
);
}
assert!(
out.text.lines().count() > 5,
"expected several wrapped lines"
);
}
}