Skip to main content

swc_ecma_ast/
function.rs

1use is_macro::Is;
2use swc_common::{ast_node, util::take::Take, EqIgnoreSpan, Span, SyntaxContext, DUMMY_SP};
3
4use crate::{
5    class::Decorator,
6    pat::Pat,
7    stmt::Stmt,
8    typescript::{TsParamProp, TsThisParam, TsTypeAnn, TsTypeParamDecl},
9};
10
11/// Common parts of function and method.
12#[ast_node]
13#[derive(Eq, Hash, EqIgnoreSpan, Default)]
14#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
15#[cfg_attr(feature = "shrink-to-fit", derive(shrink_to_fit::ShrinkToFit))]
16pub struct Function {
17    /// TypeScript or Flow `this` parameter, which is not part of the runtime
18    /// parameter list.
19    #[cfg_attr(
20        feature = "serde-impl",
21        serde(default, rename = "thisParam", skip_serializing_if = "Option::is_none")
22    )]
23    #[cfg_attr(
24        feature = "encoding-impl",
25        encoding(with = "cbor4ii::core::types::Maybe")
26    )]
27    pub this_param: Option<Box<TsThisParam>>,
28
29    pub params: Vec<Param>,
30
31    #[cfg_attr(feature = "serde-impl", serde(default))]
32    pub decorators: Vec<Decorator>,
33
34    pub span: Span,
35
36    pub ctxt: SyntaxContext,
37
38    #[cfg_attr(feature = "serde-impl", serde(default))]
39    #[cfg_attr(
40        feature = "encoding-impl",
41        encoding(with = "cbor4ii::core::types::Maybe")
42    )]
43    pub body: Option<FunctionBody>,
44
45    /// if it's a generator.
46    #[cfg_attr(feature = "serde-impl", serde(default, rename = "generator"))]
47    pub is_generator: bool,
48
49    /// if it's an async function.
50    #[cfg_attr(feature = "serde-impl", serde(default, rename = "async"))]
51    pub is_async: bool,
52
53    #[cfg_attr(feature = "serde-impl", serde(default, rename = "typeParameters"))]
54    #[cfg_attr(
55        feature = "encoding-impl",
56        encoding(with = "cbor4ii::core::types::Maybe")
57    )]
58    pub type_params: Option<Box<TsTypeParamDecl>>,
59
60    #[cfg_attr(feature = "serde-impl", serde(default))]
61    #[cfg_attr(
62        feature = "encoding-impl",
63        encoding(with = "cbor4ii::core::types::Maybe")
64    )]
65    pub return_type: Option<Box<TsTypeAnn>>,
66}
67
68impl Take for Function {
69    fn dummy() -> Self {
70        Function {
71            ..Default::default()
72        }
73    }
74}
75
76/// The braced body of a function, method, constructor, or block-bodied arrow
77/// function.
78///
79/// Unlike [`crate::BlockStmt`], a function body does not introduce an
80/// additional block scope. Its statements are evaluated in the scope created
81/// by the owning function-like node.
82#[ast_node("FunctionBody")]
83#[derive(Eq, Hash, EqIgnoreSpan, Default)]
84#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
85#[cfg_attr(feature = "shrink-to-fit", derive(shrink_to_fit::ShrinkToFit))]
86pub struct FunctionBody {
87    /// Span including the braces.
88    pub span: Span,
89
90    pub stmts: Vec<Stmt>,
91}
92
93impl Take for FunctionBody {
94    fn dummy() -> Self {
95        FunctionBody {
96            span: DUMMY_SP,
97            stmts: Vec::new(),
98        }
99    }
100}
101
102#[ast_node("Parameter")]
103#[derive(Eq, Hash, EqIgnoreSpan)]
104#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
105#[cfg_attr(feature = "shrink-to-fit", derive(shrink_to_fit::ShrinkToFit))]
106pub struct Param {
107    pub span: Span,
108    #[cfg_attr(feature = "serde-impl", serde(default))]
109    pub decorators: Vec<Decorator>,
110    pub pat: Pat,
111}
112
113impl From<Pat> for Param {
114    fn from(pat: Pat) -> Self {
115        Self {
116            span: DUMMY_SP,
117            decorators: Default::default(),
118            pat,
119        }
120    }
121}
122
123#[ast_node]
124#[derive(Eq, Hash, Is, EqIgnoreSpan)]
125#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
126#[cfg_attr(feature = "shrink-to-fit", derive(shrink_to_fit::ShrinkToFit))]
127pub enum ParamOrTsParamProp {
128    #[tag("TsParameterProperty")]
129    TsParamProp(TsParamProp),
130    #[tag("Parameter")]
131    Param(Param),
132}