This commit is contained in:
Toshit Chawda 2024-07-26 21:42:45 -07:00
parent 695310fce9
commit 544134f800
No known key found for this signature in database
GPG key ID: 91480ED99E2B3D9D
3 changed files with 26 additions and 44 deletions

View file

@ -1,11 +1,7 @@
pub mod rewrite;
use std::{
panic,
str::{from_utf8, FromStr},
};
use std::{panic, str::FromStr};
use js_sys::Uint8Array;
use rewrite::rewrite;
use url::Url;
use wasm_bindgen::prelude::*;

View file

@ -5,16 +5,6 @@ use std::{
str::{from_utf8, FromStr},
};
use oxc_allocator::Allocator;
use oxc_ast::{
ast::{AssignmentTarget, Class, Function, IdentifierReference, MemberExpression, TSImportType},
visit::walk,
Visit,
};
use oxc_parser::Parser;
use oxc_span::{SourceType, Span};
use oxc_syntax::scope::ScopeFlags;
pub mod rewrite;
use rewrite::rewrite;
@ -40,7 +30,6 @@ fn main() -> std::io::Result<()> {
.as_slice()
)
.unwrap()
.to_string()
);
Ok(())

View file

@ -1,15 +1,12 @@
use oxc_allocator::Allocator;
use oxc_ast::{
ast::{
AssignmentTarget, Class, Expression, Function, IdentifierReference, MemberExpression,
ObjectExpression, ObjectProperty, ObjectPropertyKind, TSImportType,
},
ast::{AssignmentTarget, Expression, IdentifierReference, ObjectPropertyKind},
visit::walk,
Visit,
};
use oxc_parser::Parser;
use oxc_span::{SourceType, Span};
use oxc_syntax::{operator::AssignmentOperator, scope::ScopeFlags};
use oxc_syntax::operator::AssignmentOperator;
use url::Url;
use urlencoding::encode;
@ -42,7 +39,7 @@ impl Rewriter {
let urlencoded = encode(url.as_str());
return format!("\"/scramjet/{}\"", urlencoded);
format!("\"/scramjet/{}\"", urlencoded)
}
}
@ -102,11 +99,11 @@ impl<'a> Visit<'a> for Rewriter {
fn visit_object_expression(&mut self, it: &oxc_ast::ast::ObjectExpression<'a>) {
for prop in &it.properties {
#[allow(clippy::single_match)]
match prop {
ObjectPropertyKind::ObjectProperty(p) => match &p.value {
Expression::Identifier(s) => {
if UNSAFE_GLOBALS.contains(&s.name.to_string().as_str()) {
if p.shorthand {
if UNSAFE_GLOBALS.contains(&s.name.to_string().as_str()) && p.shorthand {
self.jschanges.push(JsChange::GenericChange {
span: s.span,
text: format!("{}: (globalThis.$s({}))", s.name, s.name),
@ -114,7 +111,6 @@ impl<'a> Visit<'a> for Rewriter {
return;
}
}
}
_ => {}
},
_ => {}
@ -125,6 +121,7 @@ impl<'a> Visit<'a> for Rewriter {
}
fn visit_assignment_expression(&mut self, it: &oxc_ast::ast::AssignmentExpression<'a>) {
#[allow(clippy::single_match)]
match &it.left {
AssignmentTarget::AssignmentTargetIdentifier(s) => {
if ["location"].contains(&s.name.to_string().as_str()) {
@ -210,7 +207,7 @@ const UNSAFE_GLOBALS: [&str; 8] = [
pub fn rewrite(js: &str, url: Url) -> Vec<u8> {
let allocator = Allocator::default();
let source_type = SourceType::default();
let ret = Parser::new(&allocator, &js, source_type).parse();
let ret = Parser::new(&allocator, js, source_type).parse();
for error in ret.errors {
let cloned = js.to_string();
@ -232,22 +229,22 @@ pub fn rewrite(js: &str, url: Url) -> Vec<u8> {
// sorrt changse
ast_pass.jschanges.sort_by(|a, b| {
let a = match a {
JsChange::GenericChange { span, text } => span.start,
JsChange::GenericChange { span, text: _ } => span.start,
JsChange::Assignment {
name,
name: _,
entirespan,
rhsspan,
op,
rhsspan: _,
op: _,
} => entirespan.start,
_ => 0,
};
let b = match b {
JsChange::GenericChange { span, text } => span.start,
JsChange::GenericChange { span, text: _ } => span.start,
JsChange::Assignment {
name,
name: _,
entirespan,
rhsspan,
op,
rhsspan: _,
op: _,
} => entirespan.start,
_ => 0,
};
@ -265,8 +262,8 @@ pub fn rewrite(js: &str, url: Url) -> Vec<u8> {
JsChange::Assignment {
name,
entirespan,
rhsspan,
op,
rhsspan: _,
op: _,
} => difference += entirespan.size() as i32 + name.len() as i32 + 10,
_ => {}
}
@ -282,7 +279,7 @@ pub fn rewrite(js: &str, url: Url) -> Vec<u8> {
let start = span.start as usize;
let end = span.end as usize;
buffer.extend_from_slice(unsafe { js.slice_unchecked(offset, start) }.as_bytes());
buffer.extend_from_slice(unsafe { js.get_unchecked(offset..start) }.as_bytes());
buffer.extend_from_slice(text.as_bytes());
offset = end;
@ -294,7 +291,7 @@ pub fn rewrite(js: &str, url: Url) -> Vec<u8> {
op,
} => {
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 = match op {
AssignmentOperator::Assign => "=",
@ -333,5 +330,5 @@ pub fn rewrite(js: &str, url: Url) -> Vec<u8> {
}
buffer.extend_from_slice(js[offset..].as_bytes());
return buffer;
buffer
}