Skip to content

Commit c061356

Browse files
authoredNov 2, 2023
fix(es/transforms): Do not add PURE comment to BytePos(0) (#8207)
**Related issue:** - vercel/next.js#57904 (CI failed)
1 parent aefa701 commit c061356

File tree

3 files changed

+26
-10
lines changed
  • crates
    • swc_ecma_compat_es2015/src/classes
    • swc_ecma_transforms_react/src

3 files changed

+26
-10
lines changed
 

‎crates/swc_ecma_compat_es2015/src/classes/mod.rs

+11-7
Original file line numberDiff line numberDiff line change
@@ -243,9 +243,9 @@ where
243243
fn visit_mut_expr(&mut self, n: &mut Expr) {
244244
match n {
245245
Expr::Class(e) => {
246-
let class = self.fold_class(e.ident.take(), e.class.take());
247-
if let Expr::Call(call) = &class {
248-
self.add_pure_comments(call.span.lo)
246+
let mut class = self.fold_class(e.ident.take(), e.class.take());
247+
if let Expr::Call(call) = &mut class {
248+
self.add_pure_comments(&mut call.span.lo)
249249
}
250250
*n = class;
251251
n.visit_mut_children_with(self)
@@ -359,9 +359,13 @@ impl<C> Classes<C>
359359
where
360360
C: Comments,
361361
{
362-
fn add_pure_comments(&mut self, start: BytePos) {
362+
fn add_pure_comments(&mut self, start: &mut BytePos) {
363+
if start.is_dummy() {
364+
*start = Span::dummy_with_cmt().lo;
365+
}
366+
363367
if let Some(comments) = &self.comments {
364-
comments.add_pure_comment(start);
368+
comments.add_pure_comment(*start);
365369
}
366370
}
367371

@@ -375,12 +379,12 @@ where
375379

376380
// let VarDecl take every comments except pure
377381
if let Expr::Call(call) = &mut rhs {
378-
let span = Span {
382+
let mut span = Span {
379383
// after class
380384
lo: span.hi + BytePos(5),
381385
..span
382386
};
383-
self.add_pure_comments(span.lo);
387+
self.add_pure_comments(&mut span.lo);
384388
call.span = span;
385389
}
386390

‎crates/swc_ecma_transforms_react/src/jsx/mod.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -436,11 +436,15 @@ where
436436
}
437437

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

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

443443
if let Some(comments) = &self.comments {
444+
if span.lo.is_dummy() {
445+
span.lo = Span::dummy_with_cmt().lo;
446+
}
447+
444448
comments.add_pure_comment(span.lo);
445449
}
446450

@@ -543,13 +547,17 @@ where
543547
/// <div></div> => React.createElement('div', null);
544548
fn jsx_elem_to_expr(&mut self, el: JSXElement) -> Expr {
545549
let top_level_node = self.top_level_node;
546-
let span = el.span();
550+
let mut span = el.span();
547551
let use_create_element = should_use_create_element(&el.opening.attrs);
548552
self.top_level_node = false;
549553

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

552556
if let Some(comments) = &self.comments {
557+
if span.lo.is_dummy() {
558+
span.lo = Span::dummy_with_cmt().lo;
559+
}
560+
553561
comments.add_pure_comment(span.lo);
554562
}
555563

‎crates/swc_ecma_transforms_react/src/pure_annotations/mod.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use swc_atoms::JsWord;
2-
use swc_common::{collections::AHashMap, comments::Comments};
2+
use swc_common::{collections::AHashMap, comments::Comments, Span};
33
use swc_ecma_ast::*;
44
use swc_ecma_visit::{as_folder, noop_visit_mut_type, Fold, VisitMut, VisitMutWith};
55

@@ -108,6 +108,10 @@ where
108108

109109
if is_react_call {
110110
if let Some(comments) = &self.comments {
111+
if call.span.lo.is_dummy() {
112+
call.span.lo = Span::dummy_with_cmt().lo;
113+
}
114+
111115
comments.add_pure_comment(call.span.lo);
112116
}
113117
}

0 commit comments

Comments
 (0)
Please sign in to comment.