Skip to content

Commit

Permalink
style: 优化目录结构
Browse files Browse the repository at this point in the history
  • Loading branch information
ZEJIA-LIU committed Sep 24, 2024
1 parent 96fabe3 commit 2bfeac8
Show file tree
Hide file tree
Showing 14 changed files with 85 additions and 12 deletions.
2 changes: 1 addition & 1 deletion crates/swc_plugin_compile_mode_pre_process/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use serde::Deserialize;
use std::collections::HashMap;
use swc_core::{
ecma::{
ast::Program,
Expand All @@ -9,6 +8,7 @@ use swc_core::{
};

mod tests;
mod utils;
mod visitors;
use visitors::entry::EntryVisitor;
struct SerdeDefault;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub const COMPILE_MODE: &str = "compileMode";
pub const DEFAULT_COMPONENT: &str = "Default_Component";
pub const COMPILE_MODE_SUB_COMPONENT: &str = "compileModeSubComponent";
3 changes: 3 additions & 0 deletions crates/swc_plugin_compile_mode_pre_process/src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub mod constant;
pub mod react_component;
pub mod render_fn;
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
use swc_core::ecma::{ast::BlockStmt, visit::VisitMutWith};

use crate::visitors::is_compile_mode_component::IsCompileModeVisitor;

pub struct ReactComponent {
pub name: String,
pub block_stmt: Option<BlockStmt>,
}

impl Clone for ReactComponent {
fn clone(&self) -> Self {
ReactComponent {
name: self.name.clone(),
block_stmt: self.block_stmt.clone(),
}
}
}

impl ReactComponent {
pub fn new(name: String, block_stmt: Option<BlockStmt>) -> Self {
ReactComponent { name, block_stmt }
}

pub fn get_name(&self) -> String {
self.name.clone()
}

pub fn is_valid(&mut self) -> bool {
// 1. 名称是否是大写字母开头
let is_first_char_uppercase = self.get_name().chars().next().unwrap().is_uppercase();
// 2. 返回的 JSX 里面有没有 compilerMode
let mut is_compile_mode_component: IsCompileModeVisitor = IsCompileModeVisitor::new();
if let Some(block_stmt) = &mut self.block_stmt {
block_stmt.visit_mut_with(&mut is_compile_mode_component);
}
is_first_char_uppercase && is_compile_mode_component.valid
}
}
24 changes: 24 additions & 0 deletions crates/swc_plugin_compile_mode_pre_process/src/utils/render_fn.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use swc_core::ecma::ast::*;

pub struct RenderFn {
pub params: Vec<Pat>,
pub jsx_element: JSXElement,
}

impl RenderFn {
pub fn new(params: Vec<Pat>, jsx_element: JSXElement) -> Self {
RenderFn {
params,
jsx_element,
}
}
}

impl Clone for RenderFn {
fn clone(&self) -> Self {
RenderFn {
params: self.params.clone(),
jsx_element: self.jsx_element.clone(),
}
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use crate::visitors::common::COMPILE_MODE_SUB_COMPONENT;
use std::collections::HashMap;
use swc_core::ecma::{
ast::*,
visit::{VisitMut, VisitMutWith},
};

use super::common::RenderFn;
use crate::utils::{constant::COMPILE_MODE_SUB_COMPONENT, render_fn::RenderFn};

pub struct CollectRenderFnVisitor {
pub raw_render_fn_map: HashMap<String, RenderFn>,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
use std::collections::{HashMap, HashSet};

use crate::PluginConfig;
use crate::{
utils::{react_component::ReactComponent, render_fn::RenderFn},
PluginConfig,
};
use swc_core::ecma::{
ast::*,
visit::{VisitMut, VisitMutWith},
};

use super::collect_render_fn::CollectRenderFnVisitor;
use super::find_react_component::FindReactComponentVisitor;
use super::generate_deps::GenerateDepsVisitor;
use super::transform::component_entry::ComponentEntryVisitor;
use super::{common::*, generate_deps::GenerateDepsVisitor};
pub struct EntryVisitor {
visitor_context: VisitorContext,
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use swc_core::ecma::{ast::*, visit::VisitMut};

use super::common::{ReactComponent, DEFAULT_COMPONENT};
use crate::utils::{constant::DEFAULT_COMPONENT, react_component::ReactComponent};

pub struct FindReactComponentVisitor<'a> {
react_component_list: &'a mut Vec<ReactComponent>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use swc_core::ecma::{
visit::{VisitMut, VisitMutWith},
};

use super::common::RenderFn;
use crate::utils::render_fn::RenderFn;

pub struct GenerateDepsVisitor<'a> {
raw_fn_map: &'a HashMap<String, RenderFn>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use swc_core::ecma::{
visit::{VisitMut, VisitMutWith},
};

use super::common::COMPILE_MODE;
use crate::utils::constant::COMPILE_MODE;

pub struct IsCompileModeVisitor {
pub valid: bool,
Expand Down Expand Up @@ -46,7 +46,6 @@ impl VisitMut for IsCompileModeVisitor {
if let JSXAttrName::Ident(jsx_attr_name) = &jsx_attr.name {
if jsx_attr_name.sym == COMPILE_MODE {
self.valid = true;
println!("valid component!\n");
break;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
pub mod collect_render_fn;
pub mod common;
pub mod entry;
pub mod find_react_component;
pub mod generate_deps;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use swc_core::ecma::{
visit::{VisitMut, VisitMutWith},
};

use crate::visitors::common::{RenderFn, DEFAULT_COMPONENT};
use crate::utils::{constant::DEFAULT_COMPONENT, render_fn::RenderFn};

use super::process::TransformProcessVisitor;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ use swc_core::ecma::{
visit::{VisitMut, VisitMutWith},
};

use crate::visitors::common::{RenderFn, COMPILE_MODE, COMPILE_MODE_SUB_COMPONENT};
use crate::utils::{
constant::{COMPILE_MODE, COMPILE_MODE_SUB_COMPONENT},
render_fn::RenderFn,
};

pub struct TransformProcessVisitor<'a> {
render_fn_map: &'a HashMap<String, RenderFn>,
in_compile_mode_jsx: bool,
Expand Down
1 change: 1 addition & 0 deletions rustfmt.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
tab_spaces = 2

edition = "2021"

0 comments on commit 2bfeac8

Please sign in to comment.