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::BlockStmt,
8    typescript::{TsParamProp, 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    pub params: Vec<Param>,
18
19    #[cfg_attr(feature = "serde-impl", serde(default))]
20    pub decorators: Vec<Decorator>,
21
22    pub span: Span,
23
24    pub ctxt: SyntaxContext,
25
26    #[cfg_attr(feature = "serde-impl", serde(default))]
27    #[cfg_attr(
28        feature = "encoding-impl",
29        encoding(with = "cbor4ii::core::types::Maybe")
30    )]
31    pub body: Option<BlockStmt>,
32
33    /// if it's a generator.
34    #[cfg_attr(feature = "serde-impl", serde(default, rename = "generator"))]
35    pub is_generator: bool,
36
37    /// if it's an async function.
38    #[cfg_attr(feature = "serde-impl", serde(default, rename = "async"))]
39    pub is_async: bool,
40
41    #[cfg_attr(feature = "serde-impl", serde(default, rename = "typeParameters"))]
42    #[cfg_attr(
43        feature = "encoding-impl",
44        encoding(with = "cbor4ii::core::types::Maybe")
45    )]
46    pub type_params: Option<Box<TsTypeParamDecl>>,
47
48    #[cfg_attr(feature = "serde-impl", serde(default))]
49    #[cfg_attr(
50        feature = "encoding-impl",
51        encoding(with = "cbor4ii::core::types::Maybe")
52    )]
53    pub return_type: Option<Box<TsTypeAnn>>,
54}
55
56impl Take for Function {
57    fn dummy() -> Self {
58        Function {
59            ..Default::default()
60        }
61    }
62}
63
64#[ast_node("Parameter")]
65#[derive(Eq, Hash, EqIgnoreSpan)]
66#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
67#[cfg_attr(feature = "shrink-to-fit", derive(shrink_to_fit::ShrinkToFit))]
68pub struct Param {
69    pub span: Span,
70    #[cfg_attr(feature = "serde-impl", serde(default))]
71    pub decorators: Vec<Decorator>,
72    pub pat: Pat,
73}
74
75impl From<Pat> for Param {
76    fn from(pat: Pat) -> Self {
77        Self {
78            span: DUMMY_SP,
79            decorators: Default::default(),
80            pat,
81        }
82    }
83}
84
85#[ast_node]
86#[derive(Eq, Hash, Is, EqIgnoreSpan)]
87#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
88#[cfg_attr(feature = "shrink-to-fit", derive(shrink_to_fit::ShrinkToFit))]
89pub enum ParamOrTsParamProp {
90    #[tag("TsParameterProperty")]
91    TsParamProp(TsParamProp),
92    #[tag("Parameter")]
93    Param(Param),
94}