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