swc_ecma_quote/
lib.rs

1/// Not a public API.
2#[doc(hidden)]
3pub extern crate swc_common;
4/// Not a public API.
5#[doc(hidden)]
6pub extern crate swc_ecma_ast;
7/// Not a public API.
8#[doc(hidden)]
9pub extern crate swc_ecma_quote_macros;
10
11#[doc(hidden)]
12pub use self::clone::ImplicitClone;
13
14mod clone;
15
16/// # Supported output types
17///
18///  - `Expr`
19///  - `Pat`
20///  - `AssignTarget`
21///  - `Stmt`
22///  - `ModuleItem`
23///
24///  - Option<T> where T is supported type
25///  - Box<T> where T is supported type
26///
27/// For example, `Box<Expr>` and `Option<Box<Expr>>` are supported.
28///
29/// # Variable substitution
30///
31/// If an identifier starts with `$`, it is substituted with the value of the
32/// parameter passed.
33///
34/// e.g.
35///
36/// ```rust
37/// use swc_common::DUMMY_SP;
38/// use swc_ecma_ast::Ident;
39/// use swc_ecma_quote::quote;
40///
41/// // This will return ast for `const ref = 4;`
42/// let _stmt = quote!("const $name = 4;" as Stmt, name = Ident::new("ref".into(), DUMMY_SP));
43///
44/// // Tip: Use private_ident!("ref") for real identifiers.
45/// ```
46///
47/// ## Typed variables
48///
49/// As this macro generates static AST, it can't substitute variables if an
50/// identifier is not allowed in such position. In other words, this macro only
51/// supports substituting
52///
53///  - [swc_ecma_ast::Ident]
54///  - [swc_ecma_ast::Expr]
55///  - [swc_ecma_ast::Pat]
56///  - [swc_ecma_ast::Str]
57///
58/// You can use it like
59///
60/// ```rust
61/// use swc_common::DUMMY_SP;
62/// use swc_ecma_ast::Ident;
63/// use swc_ecma_quote::quote;
64///
65/// // This will return ast for `const ref = 4;`
66/// let _stmt = quote!(
67///                 "const $name = $val;" as Stmt,
68///                 name = Ident::new("ref".into(), DUMMY_SP),
69///                 val: Expr = 4.into(),
70///             );
71/// ```
72///
73/// # Examples
74///
75/// ## Quote a variable declaration
76/// ```rust
77/// use swc_common::DUMMY_SP;
78/// use swc_ecma_ast::Ident;
79/// use swc_ecma_quote::quote;
80///
81/// // This will return ast for `const ref = 4;`
82/// let _stmt = quote!("const $name = 4;" as Stmt, name =
83/// Ident::new("ref".into(), DUMMY_SP));
84///
85/// // Tip: Use private_ident!("ref") for real identifiers.
86/// ```
87///
88/// ## Using `Str`
89///
90/// The grammar is `"$var_name"`.
91///
92/// ```rust
93/// use swc_common::DUMMY_SP;
94/// use swc_ecma_ast::Str;
95/// use swc_ecma_quote::quote;
96///
97/// // This will return ast for `import thing from "foo";`
98/// let _stmt = quote!(
99///                 "import thing from \"$thing\";" as ModuleItem,
100///                 thing: Str = "foo".into(),
101///             );
102/// ```
103#[macro_export]
104macro_rules! quote {
105    ($($tt:tt)*) => {{
106        $crate::swc_ecma_quote_macros::internal_quote!($($tt)*)
107    }};
108}
109
110/// Creates a `Box<Expr>` from the source code.
111///
112/// This is an alias for [quote], but without `as Box<Expr>`.
113#[macro_export]
114macro_rules! quote_expr {
115    ($src:tt) => {{
116        $crate::quote!($src as Box<Expr>)
117    }};
118
119    ($src:tt, $($tt2:tt)*) => {{
120        $crate::quote!($src as Box<Expr>, $($tt2)*)
121    }};
122}