pub fn extract_scope_names(src: &str) -> Result<Scopes, ParseError>
Expand description
Extracts function scopes from the given JS-like src
.
The returned Vec includes the Range
of the function scope, in byte offsets
inside the src
, and the corresponding function name. None
in this case
denotes a function scope for which no name could be inferred from the
surrounding code, which can mostly happen for anonymous or arrow functions
used as immediate callbacks.
The range includes the whole range of the function expression, including the
leading function
keyword, function argument parentheses and trailing brace
in case there is one.
The returned vector does not have a guaranteed sorting order, and is
implementation dependent.
ยงExamples
let src = "const arrowFnExpr = (a) => a; function namedFnDecl() {}";
// arrowFnExpr -^------^ ^------namedFnDecl------^
let mut scopes: Vec<_> = js_source_scopes::extract_scope_names(src)
.unwrap()
.into_iter()
.map(|res| {
let components = res.1.map(|n| n.components().map(|c| {
(c.text().to_string(), c.range())
}).collect::<Vec<_>>());
(res.0, components)
}).collect();
scopes.sort_by_key(|s| s.0.start);
let expected = vec![
(20..28, Some(vec![(String::from("arrowFnExpr"), Some(6..17))])),
(30..55, Some(vec![(String::from("namedFnDecl"),Some(39..50))])),
];
assert_eq!(scopes, expected);