Skip to main content

swc_ecma_transforms_react/
lib.rs

1#![deny(clippy::all)]
2#![allow(clippy::mutable_key_type)]
3#![allow(clippy::arc_with_non_send_sync)]
4#![allow(rustc::untranslatable_diagnostic_trivial)]
5#![cfg_attr(not(feature = "concurrent"), allow(unused))]
6
7use swc_common::{comments::Comments, sync::Lrc, Mark, SourceMap};
8use swc_ecma_ast::Pass;
9use swc_ecma_hooks::{CompositeHook, VisitMutWithHook};
10use swc_ecma_visit::visit_mut_pass;
11
12pub use self::{jsx::*, refresh::options::RefreshOptions};
13
14mod display_name;
15pub mod jsx;
16mod jsx_self;
17mod jsx_src;
18mod pure_annotations;
19mod refresh;
20
21// Re-export old function names for compatibility
22pub fn display_name() -> impl Pass {
23    visit_mut_pass(VisitMutWithHook {
24        hook: display_name::hook(),
25        context: (),
26    })
27}
28
29pub fn jsx_self(dev: bool) -> impl Pass {
30    visit_mut_pass(VisitMutWithHook {
31        hook: jsx_self::hook(dev),
32        context: (),
33    })
34}
35
36pub fn jsx_src(dev: bool, cm: Lrc<SourceMap>) -> impl Pass {
37    visit_mut_pass(VisitMutWithHook {
38        hook: jsx_src::hook(dev, cm),
39        context: (),
40    })
41}
42
43pub fn pure_annotations<C>(comments: Option<C>) -> impl Pass
44where
45    C: Comments,
46{
47    visit_mut_pass(VisitMutWithHook {
48        hook: pure_annotations::hook(comments),
49        context: (),
50    })
51}
52
53pub fn refresh<C: Comments>(
54    dev: bool,
55    options: Option<RefreshOptions>,
56    cm: Lrc<SourceMap>,
57    comments: Option<C>,
58    global_mark: Mark,
59) -> impl Pass {
60    visit_mut_pass(VisitMutWithHook {
61        hook: refresh::hook(dev, options, cm, comments, global_mark),
62        context: (),
63    })
64}
65
66/// `@babel/preset-react`
67///
68/// Preset for all React plugins.
69///
70///
71/// `top_level_mark` should be [Mark] passed to
72/// [swc_ecma_transforms_base::resolver::resolver_with_mark].
73///
74///
75///
76/// # Note
77///
78/// This pass uses [swc_ecma_utils::HANDLER].
79pub fn react<C>(
80    cm: Lrc<SourceMap>,
81    comments: Option<C>,
82    mut options: Options,
83    top_level_mark: Mark,
84    unresolved_mark: Mark,
85) -> impl Pass
86where
87    C: Comments + Clone,
88{
89    let Options { development, .. } = options;
90    let development = development.unwrap_or(false);
91
92    let refresh_options = options.refresh.take();
93
94    let hook = CompositeHook {
95        first: jsx_src::hook(development, cm.clone()),
96        second: CompositeHook {
97            first: jsx_self::hook(development),
98            second: CompositeHook {
99                first: refresh::hook(
100                    development,
101                    refresh_options.clone(),
102                    cm.clone(),
103                    comments.clone(),
104                    top_level_mark,
105                ),
106                second: CompositeHook {
107                    first: jsx::hook(
108                        cm.clone(),
109                        comments.clone(),
110                        options,
111                        top_level_mark,
112                        unresolved_mark,
113                    ),
114                    second: CompositeHook {
115                        first: display_name::hook(),
116                        second: pure_annotations::hook(comments.clone()),
117                    },
118                },
119            },
120        },
121    };
122
123    visit_mut_pass(VisitMutWithHook { hook, context: () })
124}