Skip to content

Commit

Permalink
fix(es/transforms): Do not add PURE comment to BytePos(0) (#8207)
Browse files Browse the repository at this point in the history
**Related issue:**

 - vercel/next.js#57904 (CI failed)
  • Loading branch information
kdy1 committed Nov 2, 2023
1 parent aefa701 commit c061356
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
18 changes: 11 additions & 7 deletions crates/swc_ecma_compat_es2015/src/classes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,9 +243,9 @@ where
fn visit_mut_expr(&mut self, n: &mut Expr) {
match n {
Expr::Class(e) => {
let class = self.fold_class(e.ident.take(), e.class.take());
if let Expr::Call(call) = &class {
self.add_pure_comments(call.span.lo)
let mut class = self.fold_class(e.ident.take(), e.class.take());
if let Expr::Call(call) = &mut class {
self.add_pure_comments(&mut call.span.lo)
}
*n = class;
n.visit_mut_children_with(self)
Expand Down Expand Up @@ -359,9 +359,13 @@ impl<C> Classes<C>
where
C: Comments,
{
fn add_pure_comments(&mut self, start: BytePos) {
fn add_pure_comments(&mut self, start: &mut BytePos) {
if start.is_dummy() {
*start = Span::dummy_with_cmt().lo;
}

if let Some(comments) = &self.comments {
comments.add_pure_comment(start);
comments.add_pure_comment(*start);
}
}

Expand All @@ -375,12 +379,12 @@ where

// let VarDecl take every comments except pure
if let Expr::Call(call) = &mut rhs {
let span = Span {
let mut span = Span {
// after class
lo: span.hi + BytePos(5),
..span
};
self.add_pure_comments(span.lo);
self.add_pure_comments(&mut span.lo);
call.span = span;
}

Expand Down
12 changes: 10 additions & 2 deletions crates/swc_ecma_transforms_react/src/jsx/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -436,11 +436,15 @@ where
}

fn jsx_frag_to_expr(&mut self, el: JSXFragment) -> Expr {
let span = el.span();
let mut span = el.span();

let use_jsxs = count_children(&el.children) > 1;

if let Some(comments) = &self.comments {
if span.lo.is_dummy() {
span.lo = Span::dummy_with_cmt().lo;
}

comments.add_pure_comment(span.lo);
}

Expand Down Expand Up @@ -543,13 +547,17 @@ where
/// <div></div> => React.createElement('div', null);
fn jsx_elem_to_expr(&mut self, el: JSXElement) -> Expr {
let top_level_node = self.top_level_node;
let span = el.span();
let mut span = el.span();
let use_create_element = should_use_create_element(&el.opening.attrs);
self.top_level_node = false;

let name = self.jsx_name(el.opening.name);

if let Some(comments) = &self.comments {
if span.lo.is_dummy() {
span.lo = Span::dummy_with_cmt().lo;
}

comments.add_pure_comment(span.lo);
}

Expand Down
6 changes: 5 additions & 1 deletion crates/swc_ecma_transforms_react/src/pure_annotations/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use swc_atoms::JsWord;
use swc_common::{collections::AHashMap, comments::Comments};
use swc_common::{collections::AHashMap, comments::Comments, Span};
use swc_ecma_ast::*;
use swc_ecma_visit::{as_folder, noop_visit_mut_type, Fold, VisitMut, VisitMutWith};

Expand Down Expand Up @@ -108,6 +108,10 @@ where

if is_react_call {
if let Some(comments) = &self.comments {
if call.span.lo.is_dummy() {
call.span.lo = Span::dummy_with_cmt().lo;
}

comments.add_pure_comment(call.span.lo);
}
}
Expand Down

0 comments on commit c061356

Please sign in to comment.