clippy + refactor out config parsing

This commit is contained in:
Toshit Chawda 2024-07-29 22:17:44 -07:00
parent aa218770c7
commit 3d1b5b4c09
No known key found for this signature in database
GPG key ID: 91480ED99E2B3D9D
2 changed files with 22 additions and 56 deletions

View file

@ -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] #[wasm_bindgen]
pub fn rewrite_js(js: &str, url: &str, config: Object) -> Vec<u8> { pub fn rewrite_js(js: &str, url: &str, config: Object) -> Vec<u8> {
rewrite( rewrite(js, Url::from_str(url).unwrap(), get_config(config))
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(),
},
)
} }
#[wasm_bindgen] #[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 // we know that this is a valid utf-8 string
let js = unsafe { std::str::from_utf8_unchecked(js) }; let js = unsafe { std::str::from_utf8_unchecked(js) };
rewrite( rewrite(js, Url::from_str(url).unwrap(), get_config(config))
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(),
},
)
} }

View file

@ -1,9 +1,6 @@
use oxc_allocator::Allocator; use oxc_allocator::Allocator;
use oxc_ast::{ use oxc_ast::{
ast::{ ast::{AssignmentTarget, Expression, IdentifierReference, ObjectPropertyKind},
AssignmentTarget, BindingPattern, BindingPatternKind, Expression, IdentifierReference,
ObjectPropertyKind,
},
visit::walk, visit::walk,
Visit, Visit,
}; };
@ -201,9 +198,8 @@ impl<'a> Visit<'a> for Rewriter {
walk::walk_statement(self, &it.body); 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 // then no, don't walk it, we don't care
return;
} }
fn visit_assignment_expression(&mut self, it: &oxc_ast::ast::AssignmentExpression<'a>) { 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: _, op: _,
} => entirespan.start, } => entirespan.start,
JsChange::DebugInject { span } => span.start, JsChange::DebugInject { span } => span.start,
_ => 0,
}; };
let b = match b { let b = match b {
JsChange::GenericChange { span, text: _ } => span.start, JsChange::GenericChange { span, text: _ } => span.start,
@ -345,7 +340,6 @@ pub fn rewrite(js: &str, url: Url, config: Config) -> Vec<u8> {
op: _, op: _,
} => entirespan.start, } => entirespan.start,
JsChange::DebugInject { span } => span.start, JsChange::DebugInject { span } => span.start,
_ => 0,
}; };
a.cmp(&b) a.cmp(&b)
}); });
@ -392,7 +386,7 @@ pub fn rewrite(js: &str, url: Url, config: Config) -> Vec<u8> {
let start = entirespan.start as usize; let start = entirespan.start as usize;
buffer.extend_from_slice(js[offset..start].as_bytes()); buffer.extend_from_slice(js[offset..start].as_bytes());
let opstr = buffer.extend_from_slice( buffer.extend_from_slice(
format!( format!(
"((t)=>$scramjet$tryset({},\"{}\",t)||{}{}t)({})", "((t)=>$scramjet$tryset({},\"{}\",t)||{}{}t)({})",
name, name,
@ -412,7 +406,6 @@ pub fn rewrite(js: &str, url: Url, config: Config) -> Vec<u8> {
offset = span.end as usize; offset = span.end as usize;
} }
_ => {}
} }
} }
buffer.extend_from_slice(js[offset..].as_bytes()); buffer.extend_from_slice(js[offset..].as_bytes());