[−][src]Function minifier::js::get_variable_name_and_value_positions
pub fn get_variable_name_and_value_positions<'a>(
tokens: &'a Tokens<'a>,
pos: usize
) -> Option<(usize, Option<usize>)>
When looping over Tokens
, if you encounter Keyword::Var
, Keyword::Let
or
Token::Other
using this function will allow you to get the variable name's
position and the variable value's position (if any).
Note
It'll return the value only if there is an Operation::Equal
found.
Examples
extern crate minifier; use minifier::js::{Keyword, get_variable_name_and_value_positions, simple_minify}; fn main() { let source = r#"var x = 1;var z;var y = "2";"#; let mut result = Vec::new(); let tokens = simple_minify(source); for pos in 0..tokens.len() { match tokens[pos].get_keyword() { Some(k) if k == Keyword::Let || k == Keyword::Var => { if let Some(x) = get_variable_name_and_value_positions(&tokens, pos) { result.push(x); } } _ => {} } } assert_eq!(result, vec![(2, Some(6)), (10, None), (14, Some(22))]); }