Skip to content

Commit 5f7a58d

Browse files
committed
chore
1 parent e62c31c commit 5f7a58d

File tree

5 files changed

+47
-40
lines changed

5 files changed

+47
-40
lines changed

crates/swc_ecma_transforms_react/src/jsx/automatic.rs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ use std::iter::once;
44

55
use swc_atoms::Atom;
66
use swc_common::{
7-
errors::HANDLER, util::take::Take, BytePos, Mark, Spanned, SyntaxContext, DUMMY_SP,
7+
comments::Comments, errors::HANDLER, sync::Lrc, util::take::Take, BytePos, Mark, Spanned,
8+
SyntaxContext, DUMMY_SP,
89
};
910
use swc_ecma_ast::*;
1011
use swc_ecma_utils::{prepend_stmt, private_ident, quote_ident, ExprFactory, StmtLike};
@@ -21,12 +22,22 @@ use crate::{
2122
/// automatically and JSX elements are converted to jsx() and jsxs() calls.
2223
///
2324
/// https://github.com/reactjs/rfcs/blob/createlement-rfc/text/0000-create-element-changes.md
24-
pub fn automatic(
25+
pub fn automatic<C>(
2526
options: AutomaticConfig,
2627
common: CommonConfig,
2728
unresolved_mark: Mark,
28-
add_pure_comment: Box<dyn Fn(BytePos)>,
29-
) -> impl Pass + VisitMut {
29+
comments: Option<C>,
30+
) -> impl Pass + VisitMut
31+
where
32+
C: Comments + 'static,
33+
{
34+
let add_pure_comment: Lrc<dyn Fn(BytePos)> = match comments {
35+
Some(c) => Lrc::new(move |pos: BytePos| {
36+
c.add_pure_comment(pos);
37+
}),
38+
None => Lrc::new(|_pos| {}),
39+
};
40+
3041
visit_mut_pass(Automatic {
3142
unresolved_mark,
3243
import_source: options.import_source,
@@ -53,7 +64,7 @@ struct Automatic {
5364
development: bool,
5465
throw_if_namespace: bool,
5566

56-
add_pure_comment: Box<dyn Fn(BytePos)>,
67+
add_pure_comment: Lrc<dyn Fn(BytePos)>,
5768
}
5869

5970
impl Automatic {

crates/swc_ecma_transforms_react/src/jsx/classic.rs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ use bytes_str::BytesStr;
99
use once_cell::sync::Lazy;
1010
use rustc_hash::FxHashMap;
1111
use swc_common::{
12-
errors::HANDLER, sync::Lrc, util::take::Take, BytePos, FileName, Mark, SourceMap, Spanned,
13-
DUMMY_SP,
12+
comments::Comments, errors::HANDLER, sync::Lrc, util::take::Take, BytePos, FileName, Mark,
13+
SourceMap, Spanned, DUMMY_SP,
1414
};
1515
use swc_ecma_ast::*;
1616
use swc_ecma_parser::{parse_file_as_expr, Syntax};
@@ -82,13 +82,23 @@ fn apply_mark(e: &mut Expr, mark: Mark) {
8282
/// ```js
8383
/// import React from 'react';
8484
/// ```
85-
pub fn classic(
85+
pub fn classic<C>(
8686
options: ClassicConfig,
8787
common: CommonConfig,
8888
pragma_mark: Mark,
89-
add_pure_comment: Box<dyn Fn(BytePos)>,
89+
comments: Option<C>,
9090
cm: Lrc<SourceMap>,
91-
) -> impl Pass + VisitMut {
91+
) -> impl Pass + VisitMut
92+
where
93+
C: Comments + 'static,
94+
{
95+
let add_pure_comment: Lrc<dyn Fn(BytePos)> = match comments {
96+
Some(c) => Lrc::new(move |pos: BytePos| {
97+
c.add_pure_comment(pos);
98+
}),
99+
None => Lrc::new(|_pos| {}),
100+
};
101+
92102
let pragma = parse_expr_for_jsx(&cm, "pragma", options.pragma, pragma_mark);
93103
let pragma = Lrc::new(pragma);
94104

@@ -110,7 +120,7 @@ struct Classic {
110120
pragma_frag: Lrc<Box<Expr>>,
111121
throw_if_namespace: bool,
112122

113-
add_pure_comment: Box<dyn Fn(BytePos)>,
123+
add_pure_comment: Lrc<dyn Fn(BytePos)>,
114124
}
115125

116126
#[cfg(feature = "concurrent")]

crates/swc_ecma_transforms_react/src/jsx/mod.rs

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -264,26 +264,15 @@ where
264264
runtime, common, ..
265265
} = options;
266266

267-
type AddPureCommentFn = Box<dyn Fn(swc_common::BytePos)>;
268-
let create_add_pure_comment = || -> AddPureCommentFn {
269-
match comments.as_ref() {
270-
Some(c) => {
271-
let c = c.clone();
272-
Box::new(move |pos: swc_common::BytePos| {
273-
c.add_pure_comment(pos);
274-
})
275-
}
276-
None => Box::new(|_pos| {}),
277-
}
278-
};
267+
279268

280269
match runtime {
281270
Runtime::Automatic(config) => (
282271
Some(automatic(
283272
config,
284273
common,
285274
unresolved_mark,
286-
create_add_pure_comment(),
275+
comments.clone(),
287276
)),
288277
None,
289278
),
@@ -293,7 +282,7 @@ where
293282
config,
294283
common,
295284
top_level_mark,
296-
create_add_pure_comment(),
285+
comments.clone(),
297286
cm.clone(),
298287
)),
299288
),

crates/swc_ecma_transforms_react/src/lib.rs

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -58,19 +58,6 @@ where
5858

5959
let refresh_options = options.refresh;
6060

61-
type AddPureCommentFn = Box<dyn Fn(swc_common::BytePos)>;
62-
let create_add_pure_comment = || -> AddPureCommentFn {
63-
match comments.as_ref() {
64-
Some(c) => {
65-
let c = c.clone();
66-
Box::new(move |pos: swc_common::BytePos| {
67-
c.add_pure_comment(pos);
68-
})
69-
}
70-
None => Box::new(|_pos| {}),
71-
}
72-
};
73-
7461
(
7562
jsx_src(development, cm.clone()),
7663
jsx_self(development),
@@ -86,15 +73,15 @@ where
8673
config,
8774
options.common,
8875
unresolved_mark,
89-
create_add_pure_comment(),
76+
comments.clone(),
9077
)
9178
}),
9279
classic_config.map(|config| {
9380
classic(
9481
config,
9582
options.common,
9683
top_level_mark,
97-
create_add_pure_comment(),
84+
comments.clone(),
9885
cm.clone(),
9986
)
10087
}),

crates/swc_plugin_proxy/src/comments/plugin_comments_proxy.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,12 @@ impl Comments for PluginCommentsProxy {
150150
c.add_pure_comment(pos);
151151
});
152152
}
153+
154+
fn for_each(&self, f: &mut dyn FnMut(&Comment)) {
155+
swc_common::comments::COMMENTS.with(|c| {
156+
c.for_each(f);
157+
});
158+
}
153159
}
154160

155161
#[cfg(all(feature = "__plugin_mode", target_arch = "wasm32"))]
@@ -276,4 +282,8 @@ impl Comments for PluginCommentsProxy {
276282
}
277283
}
278284
}
285+
286+
fn for_each(&self, f: &mut dyn FnMut(&Comment)) {
287+
unimplemented!()
288+
}
279289
}

0 commit comments

Comments
 (0)