1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#![allow(clippy::not_unsafe_ptr_arg_deref)]
use swc_core::{
    ecma::{ast::Program, visit::FoldWith},
    plugin::{plugin_transform, proxies::TransformPluginProgramMetadata},
};

use std::str;
use swc_common::DUMMY_SP;
use swc_ecma_ast::*;
use swc_ecma_visit::{noop_fold_type, Fold};

fn get_el_name(name: JSXElementName) -> String {
    match name {
        JSXElementName::Ident(n) => n.sym.as_ref().to_string(),
        JSXElementName::JSXMemberExpr(JSXMemberExpr {
            obj: JSXObject::Ident(n),
            prop,
        }) => format!("{}.{}", n.sym.as_ref(), prop.sym.as_ref()),
       _ => "Unknown".to_string(),
    }
}

pub fn add_logging_dataset() -> impl Fold {
    let properties: Vec<String> = vec![
        "data-clickable".to_string(),
        "onClick".to_string(),
        "onClickCapture".to_string(),
        "onChange".to_string(),
        "onLongPress".to_string(),
        "onMouseDown".to_string(),
        "onKeyUp".to_string(),
    ];
    AddProperties { properties }
}

struct AddProperties {
    properties: Vec<String>,
}

impl AddProperties {
    fn should_add_property(&self, name: &str) -> bool {
        self.properties.iter().any(|p: &String| p == name)
    }
}

impl Fold for AddProperties {
    noop_fold_type!();

    fn fold_jsx_opening_element(&mut self, mut el: JSXOpeningElement) -> JSXOpeningElement {
        let mut should_add_data_tossinvest_log = false;
        for attr in &el.attrs {
            match attr {
                JSXAttrOrSpread::JSXAttr(JSXAttr {
                    name: JSXAttrName::Ident(ident),
                    ..
                }) if self.should_add_property(ident.sym.as_ref()) => {
                    should_add_data_tossinvest_log = true;
                }
                _ => {}
            }
        }

        let node_name = get_el_name(el.name.clone());

        if (should_add_data_tossinvest_log || node_name == "Tab.Item" || node_name == "Button" || node_name == "ListFooter01") && node_name != "Fragment" {
            let data_tossinvest_log_attr = JSXAttrOrSpread::JSXAttr(JSXAttr {
                span: DUMMY_SP,
                name: JSXAttrName::Ident(Ident::new(
                    "data-tossinvest-log".into(),
                    DUMMY_SP,
                )), 
                value: Some(JSXAttrValue::Lit((node_name).into())),
            });
            el.attrs.push(data_tossinvest_log_attr);
        }

        if get_el_name(el.name.clone()) == "BottomSheet" {
            let data_tossinvest_log_attr = JSXAttrOrSpread::JSXAttr(JSXAttr {
                span: DUMMY_SP,
                name: JSXAttrName::Ident(Ident::new(
                    "data-section-name".into(),
                    DUMMY_SP,
                )),
                value: Some(JSXAttrValue::Lit(("바텀 시트").into())),
            });
            el.attrs.push(data_tossinvest_log_attr);
        }

        if get_el_name(el.name.clone()) == "BottomSheet.Header" {
            let data_tossinvest_log_attr = JSXAttrOrSpread::JSXAttr(JSXAttr {
                span: DUMMY_SP,
                name: JSXAttrName::Ident(Ident::new(
                    "data-section-name".into(),
                    DUMMY_SP,
                )),
                value: Some(JSXAttrValue::Lit(("바텀시트 헤더").into())),
            });
            el.attrs.push(data_tossinvest_log_attr);
        }

       
        el.fold_children_with(self)
    }
}


#[plugin_transform]
pub fn process_transform(program: Program, _: TransformPluginProgramMetadata) -> Program {
    program.fold_with(&mut add_logging_dataset())
}