fix(es/transforms): Do not add PURE comment to BytePos(0) (#8207)

**Related issue:**

 - https://github.com/vercel/next.js/pull/57904 (CI failed)
This commit is contained in:
Donny/강동윤 2023-11-02 12:29:59 +09:00 committed by GitHub
parent aefa70159a
commit c061356b63
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 10 deletions

View File

@ -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)
@ -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);
}
}
@ -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;
}

View File

@ -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);
}
@ -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);
}

View File

@ -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};
@ -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);
}
}