From 66ed8ed595d34f7c01729d54dddd044e9892658a Mon Sep 17 00:00:00 2001 From: overlookmotel Date: Mon, 10 Jun 2024 17:33:27 +0100 Subject: [PATCH] fix(transformer): do not add `__source` for generated nodes --- crates/oxc_span/src/span.rs | 15 +++++++++++++++ crates/oxc_transformer/src/react/jsx_source.rs | 5 +++++ 2 files changed, 20 insertions(+) diff --git a/crates/oxc_span/src/span.rs b/crates/oxc_span/src/span.rs index 0ae456649e76ab..eeae24a3d3f357 100644 --- a/crates/oxc_span/src/span.rs +++ b/crates/oxc_span/src/span.rs @@ -118,6 +118,21 @@ impl Span { self.start == self.end } + /// Returns `true` if `self` is not a real span. + /// i.e. `SPAN` which is used for generated nodes which are not in source code. + /// + /// # Example + /// ``` + /// use oxc_span::{Span, SPAN}; + /// + /// assert!(SPAN.is_unspanned()); + /// assert!(!Span::new(0, 5).is_unspanned()); + /// assert!(!Span::new(5, 5).is_unspanned()); + /// ``` + pub const fn is_unspanned(&self) -> bool { + self.start == SPAN.start && self.end == SPAN.end + } + /// Create a [`Span`] covering the maximum range of two [`Span`]s. /// /// # Example diff --git a/crates/oxc_transformer/src/react/jsx_source.rs b/crates/oxc_transformer/src/react/jsx_source.rs index 2703476d9ffc09..fd553837bf05e1 100644 --- a/crates/oxc_transformer/src/react/jsx_source.rs +++ b/crates/oxc_transformer/src/react/jsx_source.rs @@ -65,6 +65,11 @@ impl<'a> ReactJsxSource<'a> { elem: &mut JSXOpeningElement<'a>, ctx: &mut TraverseCtx<'a>, ) { + // Don't add `__source` if this node was generated + if elem.span.is_unspanned() { + return; + } + // Check if `__source` attribute already exists for item in &elem.attributes { if let JSXAttributeItem::Attribute(attribute) = item {