mirror of
https://github.com/MercuryWorkshop/scramjet.git
synced 2025-05-15 23:30:00 -04:00
clippy + refactor out config parsing
This commit is contained in:
parent
aa218770c7
commit
3d1b5b4c09
2 changed files with 22 additions and 56 deletions
|
@ -29,31 +29,26 @@ fn create_encode_function(encode: Function) -> EncodeFn {
|
|||
})
|
||||
}
|
||||
|
||||
fn get_str(config: &Object, k: &str) -> String {
|
||||
Reflect::get(config, &k.into())
|
||||
.unwrap()
|
||||
.as_string()
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
fn get_config(config: Object) -> Config {
|
||||
Config {
|
||||
prefix: get_str(&config, "prefix"),
|
||||
encode: create_encode_function(Reflect::get(&config, &"encode".into()).unwrap().into()),
|
||||
wrapfn: get_str(&config, "wrapfn"),
|
||||
importfn: get_str(&config, "importfn"),
|
||||
rewritefn: get_str(&config, "rewritefn"),
|
||||
}
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub fn rewrite_js(js: &str, url: &str, config: Object) -> Vec<u8> {
|
||||
rewrite(
|
||||
js,
|
||||
Url::from_str(url).unwrap(),
|
||||
Config {
|
||||
prefix: Reflect::get(&config, &"prefix".into())
|
||||
.unwrap()
|
||||
.as_string()
|
||||
.unwrap(),
|
||||
encode: create_encode_function(Reflect::get(&config, &"encode".into()).unwrap().into()),
|
||||
wrapfn: Reflect::get(&config, &"wrapfn".into())
|
||||
.unwrap()
|
||||
.as_string()
|
||||
.unwrap(),
|
||||
importfn: Reflect::get(&config, &"importfn".into())
|
||||
.unwrap()
|
||||
.as_string()
|
||||
.unwrap(),
|
||||
rewritefn: Reflect::get(&config, &"rewritefn".into())
|
||||
.unwrap()
|
||||
.as_string()
|
||||
.unwrap(),
|
||||
},
|
||||
)
|
||||
rewrite(js, Url::from_str(url).unwrap(), get_config(config))
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
|
@ -61,27 +56,5 @@ pub fn rewrite_js_from_arraybuffer(js: &[u8], url: &str, config: Object) -> Vec<
|
|||
// we know that this is a valid utf-8 string
|
||||
let js = unsafe { std::str::from_utf8_unchecked(js) };
|
||||
|
||||
rewrite(
|
||||
js,
|
||||
Url::from_str(url).unwrap(),
|
||||
Config {
|
||||
prefix: Reflect::get(&config, &"prefix".into())
|
||||
.unwrap()
|
||||
.as_string()
|
||||
.unwrap(),
|
||||
encode: create_encode_function(Reflect::get(&config, &"encode".into()).unwrap().into()),
|
||||
wrapfn: Reflect::get(&config, &"wrapfn".into())
|
||||
.unwrap()
|
||||
.as_string()
|
||||
.unwrap(),
|
||||
importfn: Reflect::get(&config, &"importfn".into())
|
||||
.unwrap()
|
||||
.as_string()
|
||||
.unwrap(),
|
||||
rewritefn: Reflect::get(&config, &"rewritefn".into())
|
||||
.unwrap()
|
||||
.as_string()
|
||||
.unwrap(),
|
||||
},
|
||||
)
|
||||
rewrite(js, Url::from_str(url).unwrap(), get_config(config))
|
||||
}
|
||||
|
|
|
@ -1,9 +1,6 @@
|
|||
use oxc_allocator::Allocator;
|
||||
use oxc_ast::{
|
||||
ast::{
|
||||
AssignmentTarget, BindingPattern, BindingPatternKind, Expression, IdentifierReference,
|
||||
ObjectPropertyKind,
|
||||
},
|
||||
ast::{AssignmentTarget, Expression, IdentifierReference, ObjectPropertyKind},
|
||||
visit::walk,
|
||||
Visit,
|
||||
};
|
||||
|
@ -201,9 +198,8 @@ impl<'a> Visit<'a> for Rewriter {
|
|||
walk::walk_statement(self, &it.body);
|
||||
}
|
||||
|
||||
fn visit_update_expression(&mut self, it: &oxc_ast::ast::UpdateExpression<'a>) {
|
||||
fn visit_update_expression(&mut self, _it: &oxc_ast::ast::UpdateExpression<'a>) {
|
||||
// then no, don't walk it, we don't care
|
||||
return;
|
||||
}
|
||||
|
||||
fn visit_assignment_expression(&mut self, it: &oxc_ast::ast::AssignmentExpression<'a>) {
|
||||
|
@ -334,7 +330,6 @@ pub fn rewrite(js: &str, url: Url, config: Config) -> Vec<u8> {
|
|||
op: _,
|
||||
} => entirespan.start,
|
||||
JsChange::DebugInject { span } => span.start,
|
||||
_ => 0,
|
||||
};
|
||||
let b = match b {
|
||||
JsChange::GenericChange { span, text: _ } => span.start,
|
||||
|
@ -345,7 +340,6 @@ pub fn rewrite(js: &str, url: Url, config: Config) -> Vec<u8> {
|
|||
op: _,
|
||||
} => entirespan.start,
|
||||
JsChange::DebugInject { span } => span.start,
|
||||
_ => 0,
|
||||
};
|
||||
a.cmp(&b)
|
||||
});
|
||||
|
@ -392,7 +386,7 @@ pub fn rewrite(js: &str, url: Url, config: Config) -> Vec<u8> {
|
|||
let start = entirespan.start as usize;
|
||||
buffer.extend_from_slice(js[offset..start].as_bytes());
|
||||
|
||||
let opstr = buffer.extend_from_slice(
|
||||
buffer.extend_from_slice(
|
||||
format!(
|
||||
"((t)=>$scramjet$tryset({},\"{}\",t)||{}{}t)({})",
|
||||
name,
|
||||
|
@ -412,7 +406,6 @@ pub fn rewrite(js: &str, url: Url, config: Config) -> Vec<u8> {
|
|||
|
||||
offset = span.end as usize;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
buffer.extend_from_slice(js[offset..].as_bytes());
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue