From 34c3cc5094b9a4d25d7fc5e8a6040d791fe01954 Mon Sep 17 00:00:00 2001 From: velzie Date: Sun, 28 Jul 2024 10:48:05 -0400 Subject: [PATCH] fix a bunch of edge cases in the rewriter --- rewriter/src/rewrite.rs | 53 +++++++++++++++++++++++++++++++++++++---- src/types.d.ts | 2 ++ 2 files changed, 50 insertions(+), 5 deletions(-) diff --git a/rewriter/src/rewrite.rs b/rewriter/src/rewrite.rs index 59a9101..cd5a121 100644 --- a/rewriter/src/rewrite.rs +++ b/rewriter/src/rewrite.rs @@ -15,9 +15,8 @@ enum JsChange { span: Span, text: String, }, - UrlRewrite { + DebugInject { span: Span, - url: String, }, Assignment { name: String, @@ -48,6 +47,13 @@ impl Rewriter { impl<'a> Visit<'a> for Rewriter { fn visit_identifier_reference(&mut self, it: &IdentifierReference<'a>) { + // self.jschanges.push(JsChange::GenericChange { + // span: it.span, + // text: format!( + // "({}(typeof {} == 'undefined' || {}, (()=>{{ try {{return arguments}} catch(_){{}} }})()))", + // self.wrapfn, it.name, it.name + // ), + // }); if UNSAFE_GLOBALS.contains(&it.name.to_string().as_str()) { self.jschanges.push(JsChange::GenericChange { span: it.span, @@ -97,7 +103,7 @@ impl<'a> Visit<'a> for Rewriter { text, }); } - walk::walk_export_named_declaration(self, it); + // do not walk further, we don't want to rewrite the identifiers } fn visit_object_expression(&mut self, it: &oxc_ast::ast::ObjectExpression<'a>) { @@ -123,6 +129,26 @@ impl<'a> Visit<'a> for Rewriter { walk::walk_object_expression(self, it); } + fn visit_return_statement(&mut self, it: &oxc_ast::ast::ReturnStatement<'a>) { + self.jschanges.push(JsChange::DebugInject { + span: Span::new(it.span.start + 6, it.span.start + 6), + }); + walk::walk_return_statement(self, it); + } + + // we don't want to rewrite the identifiers here because of a very specific edge case + fn visit_for_in_statement(&mut self, it: &oxc_ast::ast::ForInStatement<'a>) { + walk::walk_statement(self, &it.body); + } + fn visit_for_of_statement(&mut self, it: &oxc_ast::ast::ForOfStatement<'a>) { + walk::walk_statement(self, &it.body); + } + + 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>) { #[allow(clippy::single_match)] match &it.left { @@ -140,9 +166,18 @@ impl<'a> Visit<'a> for Rewriter { return; } } - _ => {} + AssignmentTarget::ArrayAssignmentTarget(_) => { + // [location] = ["https://example.com"] + // this is such a ridiculously specific edge case. just ignore it + return; + } + _ => { + // only walk the left side if it isn't an identifier, we can't replace the + // identifier with a function obviously + walk::walk_assignment_target(self, &it.left); + } } - walk::walk_assignment_expression(self, &it); + walk::walk_expression(self, &it.right); } } @@ -250,6 +285,7 @@ pub fn rewrite( rhsspan: _, op: _, } => entirespan.start, + JsChange::DebugInject { span } => span.start, _ => 0, }; let b = match b { @@ -260,6 +296,7 @@ pub fn rewrite( rhsspan: _, op: _, } => entirespan.start, + JsChange::DebugInject { span } => span.start, _ => 0, }; a.cmp(&b) @@ -321,6 +358,12 @@ pub fn rewrite( offset = entirespan.end as usize; } + JsChange::DebugInject { span } => { + let start = span.start as usize; + buffer.extend_from_slice(unsafe { js.get_unchecked(offset..start) }.as_bytes()); + + offset = span.end as usize; + } _ => {} } } diff --git a/src/types.d.ts b/src/types.d.ts index 8a16769..3a3ac3b 100644 --- a/src/types.d.ts +++ b/src/types.d.ts @@ -51,3 +51,5 @@ declare global { WASM: string; } } + +type Self = Window & typeof globalThis;