|
| 1 | +use crate::component::ExportVariableKind; |
| 2 | +use crate::css_utils::{css_to_style, keyframes_to_keyframes_style, optimize_css_block}; |
| 3 | +use crate::extract_style::ExtractStyleProperty; |
| 4 | +use crate::extract_style::extract_css::ExtractCss; |
| 5 | +use crate::extract_style::extract_keyframes::ExtractKeyframes; |
| 6 | +use crate::extractor::KeyframesExtractResult; |
| 7 | +use crate::extractor::extract_keyframes_from_expression::extract_keyframes_from_expression; |
| 8 | +use crate::extractor::{ |
| 9 | + ExtractResult, GlobalExtractResult, |
| 10 | + extract_global_style_from_expression::extract_global_style_from_expression, |
| 11 | + extract_style_from_expression::extract_style_from_expression, |
| 12 | + extract_style_from_jsx::extract_style_from_jsx, |
| 13 | +}; |
| 14 | +use crate::gen_class_name::gen_class_names; |
| 15 | +use crate::prop_modify_utils::{modify_prop_object, modify_props}; |
| 16 | +use crate::util_type::UtilType; |
| 17 | +use crate::utils::get_string_by_literal_expression; |
| 18 | +use crate::{ExtractStyleProp, ExtractStyleValue}; |
| 19 | +use css::disassemble_property; |
| 20 | +use css::is_special_property::is_special_property; |
| 21 | +use oxc_allocator::{Allocator, CloneIn}; |
| 22 | +use oxc_ast::ast::ImportDeclarationSpecifier::{self, ImportSpecifier}; |
| 23 | +use oxc_ast::ast::JSXAttributeItem::Attribute; |
| 24 | +use oxc_ast::ast::JSXAttributeName::Identifier; |
| 25 | +use oxc_ast::ast::{ |
| 26 | + Argument, BindingPatternKind, CallExpression, Expression, ImportDeclaration, |
| 27 | + ImportOrExportKind, JSXAttributeValue, JSXElement, Program, PropertyKey, Statement, |
| 28 | + VariableDeclarator, WithClause, |
| 29 | +}; |
| 30 | +use oxc_ast_visit::VisitMut; |
| 31 | +use oxc_ast_visit::walk_mut::{ |
| 32 | + walk_call_expression, walk_expression, walk_expression_statement, walk_import_declaration, |
| 33 | + walk_jsx_element, walk_object_property, walk_program, walk_string_literal, |
| 34 | + walk_variable_declarator, walk_variable_declarators, |
| 35 | +}; |
| 36 | +use strum::IntoEnumIterator; |
| 37 | + |
| 38 | +use oxc_ast::AstBuilder; |
| 39 | +use oxc_span::SPAN; |
| 40 | +use std::collections::{HashMap, HashSet}; |
| 41 | + |
| 42 | +pub struct AsVisitor<'a> { |
| 43 | + ast: AstBuilder<'a>, |
| 44 | + element: JSXElement<'a>, |
| 45 | +} |
| 46 | + |
| 47 | +impl<'a> AsVisitor<'a> { |
| 48 | + pub fn new(allocator: &'a Allocator, element: JSXElement<'a>) -> Self { |
| 49 | + Self { |
| 50 | + ast: AstBuilder::new(allocator), |
| 51 | + element, |
| 52 | + } |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +fn change_element_name<'a>(ast: &AstBuilder<'a>, element: &mut JSXElement<'a>, element_name: &str) { |
| 57 | + let element_name = ast.jsx_element_name_identifier(SPAN, ast.atom(element_name)); |
| 58 | + element.opening_element.name = element_name.clone_in(ast.allocator); |
| 59 | + if let Some(el) = &mut element.closing_element { |
| 60 | + el.name = element_name; |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +impl<'a> VisitMut<'a> for AsVisitor<'a> { |
| 65 | + fn visit_expression(&mut self, it: &mut oxc_ast::ast::Expression<'a>) { |
| 66 | + if let Some(element_name) = get_string_by_literal_expression(it) { |
| 67 | + let mut element = self.element.clone_in(self.ast.allocator); |
| 68 | + change_element_name(&self.ast, &mut element, &element_name); |
| 69 | + *it = Expression::JSXElement(self.ast.alloc(element)); |
| 70 | + return; |
| 71 | + } else if let Expression::Identifier(ident) = it { |
| 72 | + let element_name = ident.name.to_string(); |
| 73 | + if element_name == "undefined" { |
| 74 | + return; |
| 75 | + } |
| 76 | + let mut element = self.element.clone_in(self.ast.allocator); |
| 77 | + change_element_name(&self.ast, &mut element, &element_name); |
| 78 | + *it = Expression::JSXElement(self.ast.alloc(element)); |
| 79 | + return; |
| 80 | + } else if let Expression::ConditionalExpression(conditional) = it { |
| 81 | + self.visit_expression(&mut conditional.consequent); |
| 82 | + self.visit_expression(&mut conditional.alternate); |
| 83 | + return; |
| 84 | + } else if let Expression::ComputedMemberExpression(member) = it { |
| 85 | + self.visit_expression(&mut member.object); |
| 86 | + return; |
| 87 | + } |
| 88 | + walk_expression(self, it); |
| 89 | + } |
| 90 | + |
| 91 | + fn visit_object_property(&mut self, it: &mut oxc_ast::ast::ObjectProperty<'a>) { |
| 92 | + self.visit_expression(&mut it.value); |
| 93 | + } |
| 94 | + |
| 95 | + fn visit_spread_element(&mut self, _: &mut oxc_ast::ast::SpreadElement<'a>) { |
| 96 | + // spread be mantained |
| 97 | + } |
| 98 | +} |
0 commit comments