Skip to content

new plugin: remove-side-effect #17

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
[workspace]
resolver = "2"
members = [
"packages/remove-export",
"packages/keep-platform",
"packages/keep-export",
"packages/node-transform"
"packages/node-transform",
"packages/remove-side-effect"
]

[workspace.dependencies]
Expand Down
4 changes: 4 additions & 0 deletions packages/remove-side-effect/.cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# These command aliases are not final, may change
[alias]
# Alias to build actual plugin binary for the specified target.
prepublish = "build --target wasm32-wasi"
26 changes: 26 additions & 0 deletions packages/remove-side-effect/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
[package]
name = "swc_plugin_remove_side_effect"
version = "0.0.1"
edition = "2021"

[lib]
crate-type = ["cdylib", "rlib"]

[dependencies]
serde = { workspace = true }
fxhash= { workspace = true }
tracing = { workspace = true, features = ["release_max_level_info"] }
swc_core = { workspace = true, features = [
"ecma_plugin_transform",
"ecma_utils",
"ecma_visit",
"ecma_ast",
"common",
"ecma_codegen",
"ecma_parser",
]}
swc_common = { workspace = true, features = ["concurrent"] }
serde_json = { workspace = true, features = ["unbounded_depth"]}

[dev-dependencies]
testing = { workspace = true }
13 changes: 13 additions & 0 deletions packages/remove-side-effect/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "@ice/swc-plugin-remove-side-effect",
"version": "0.0.1",
"license": "MIT",
"keywords": ["swc-plugin"],
"main": "swc_plugin_remove_side_effect.wasm",
"scripts": {
"prepublishOnly": "cargo prepublish --release && cp ../../target/wasm32-wasi/release/swc_plugin_remove_side_effect.wasm ."
},
"publishConfig": {
"access": "public"
}
}
250 changes: 250 additions & 0 deletions packages/remove-side-effect/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,250 @@
use swc_core::ecma::{
ast::*,
visit::{as_folder, FoldWith, VisitMut, VisitMutWith},
};
use swc_core::plugin::{plugin_transform, proxies::TransformPluginProgramMetadata};

#[derive(Default)]
pub struct TransformVisitor {
// Track React imports (e.g., import React from 'react')
react_imports: Vec<String>,
// Track imported hooks (e.g., import { useEffect } from 'react')
imported_hooks: Vec<String>,
// Stack of scopes for tracking variable declarations
scope_stack: Vec<Vec<String>>,
}

impl TransformVisitor {
pub fn new() -> Self {
Self {
react_imports: Vec::new(),
imported_hooks: Vec::new(),
scope_stack: vec![Vec::new()],
}
}

fn target_hooks() -> Vec<&'static str> {
vec!["useEffect", "useLayoutEffect"]
}

fn is_target_hook(name: &str) -> bool {
Self::target_hooks().contains(&name)
}

fn enter_scope(&mut self) {
self.scope_stack.push(Vec::new());
}

fn exit_scope(&mut self) {
self.scope_stack.pop();
}

fn add_to_current_scope(&mut self, name: String) {
if let Some(scope) = self.scope_stack.last_mut() {
scope.push(name);
}
}

fn is_local_variable(&self, name: &str) -> bool {
for scope in self.scope_stack.iter().rev() {
if scope.contains(&name.to_string()) {
return true;
}
}
false
}

fn is_removable_effect(&self, expr: &Expr) -> bool {
match expr {
Expr::Ident(ident) => {
let name = ident.sym.to_string();
if Self::is_target_hook(&name) {
if self.is_local_variable(&name) {
return false;
}
return self.imported_hooks.contains(&name);
}
}
Expr::Member(member) => {
if let Expr::Ident(obj) = &*member.obj {
if let Some(prop) = &member.prop.as_ident() {
return self.react_imports.contains(&obj.sym.to_string())
&& Self::is_target_hook(&prop.sym.to_string());
}
}
}
_ => {}
}
false
}
}

impl VisitMut for TransformVisitor {
fn visit_mut_stmts(&mut self, stmts: &mut Vec<Stmt>) {
self.enter_scope();

// First, process all variable declarations to build the scope
for stmt in stmts.iter_mut() {
if let Stmt::Decl(Decl::Var(var_decl)) = stmt {
for decl in &var_decl.decls {
if let Pat::Ident(ident) = &decl.name {
self.add_to_current_scope(ident.id.sym.to_string());
}
}
}
}

// Process statements and remove React hooks
stmts.retain(|stmt| {
if let Stmt::Expr(expr_stmt) = stmt {
if let Expr::Call(call_expr) = &*expr_stmt.expr {
if let Callee::Expr(callee) = &call_expr.callee {
if let Expr::Ident(ident) = &**callee {
let name = ident.sym.to_string();
// If it's a local variable, keep the call
if self.is_local_variable(&name) {
return true;
}
// If it's an imported hook, remove the call
if self.imported_hooks.contains(&name) {
return false;
}
}
return !self.is_removable_effect(callee);
}
}
}
true
});

// Process child nodes
for stmt in stmts.iter_mut() {
stmt.visit_mut_children_with(self);
}

self.exit_scope();
}

fn visit_mut_block_stmt(&mut self, block: &mut BlockStmt) {
self.enter_scope();
// Process all statements in the block
self.visit_mut_stmts(&mut block.stmts);
self.exit_scope();
}

fn visit_mut_function(&mut self, func: &mut Function) {
self.enter_scope();

// Add function parameters to scope
for param in &func.params {
if let Pat::Ident(ident) = &param.pat {
self.add_to_current_scope(ident.id.sym.to_string());
}
}

// Process function body
if let Some(body) = &mut func.body {
self.visit_mut_stmts(&mut body.stmts);
}

self.exit_scope();
}

fn visit_mut_arrow_expr(&mut self, arrow: &mut ArrowExpr) {
self.enter_scope();

// Add arrow function parameters to scope
for param in &arrow.params {
if let Pat::Ident(ident) = param {
self.add_to_current_scope(ident.sym.to_string());
}
}

// Process arrow function body
match &mut *arrow.body {
BlockStmtOrExpr::BlockStmt(block) => {
self.visit_mut_block_stmt(block);
}
BlockStmtOrExpr::Expr(expr) => {
expr.visit_mut_with(self);
}
}

self.exit_scope();
}

fn visit_mut_var_decl(&mut self, var_decl: &mut VarDecl) {
for decl in &var_decl.decls {
if let Pat::Ident(ident) = &decl.name {
self.add_to_current_scope(ident.id.sym.to_string());
}
}
var_decl.visit_mut_children_with(self);
}

fn visit_mut_module(&mut self, module: &mut Module) {
for item in &module.body {
if let ModuleItem::ModuleDecl(ModuleDecl::Import(import)) = item {
if import.src.value.to_string() == "react" {
for spec in &import.specifiers {
match spec {
ImportSpecifier::Named(named) => {
let original_name = match &named.imported {
Some(imported) => match imported {
// eg: import { useEffect as myEffect } from "react";
ModuleExportName::Ident(ident) => ident.sym.to_string(),
// eg: import { 'use-effect' as myEffect } from 'react';
ModuleExportName::Str(str) => str.value.to_string(),
},
None => named.local.sym.to_string(),
};

if Self::is_target_hook(&original_name) {
self.imported_hooks.push(named.local.sym.to_string());
}
}
ImportSpecifier::Default(default_import) => {
self.react_imports
.push(default_import.local.sym.to_string());
}
ImportSpecifier::Namespace(namespace) => {
self.react_imports.push(namespace.local.sym.to_string());
}
}
}
}
}
}

module.visit_mut_children_with(self);
}

fn visit_mut_try_stmt(&mut self, try_stmt: &mut TryStmt) {
self.enter_scope();
try_stmt.block.visit_mut_children_with(self);
self.exit_scope();

// catch
if let Some(catch) = &mut try_stmt.handler {
self.enter_scope();
// add catch params to context
if let Some(Pat::Ident(ident)) = &catch.param {
self.add_to_current_scope(ident.sym.to_string());
}
catch.body.visit_mut_children_with(self);
self.exit_scope();
}

// finally
if let Some(finally) = &mut try_stmt.finalizer {
self.enter_scope();
finally.visit_mut_children_with(self);
self.exit_scope();
}
}
}

#[plugin_transform]
pub fn process_transform(program: Program, _metadata: TransformPluginProgramMetadata) -> Program {
program.fold_with(&mut as_folder(TransformVisitor::new()))
}
22 changes: 22 additions & 0 deletions packages/remove-side-effect/tests/fixtrue.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use std::path::PathBuf;
use swc_core::ecma::parser::{Syntax, EsSyntax};
use swc_core::ecma::transforms::testing::test_fixture;
use swc_core::ecma::visit::as_folder;
use swc_plugin_remove_side_effect::TransformVisitor;
use testing::fixture;

#[fixture("tests/fixture/**/input.js")]
fn fixture_test(input: PathBuf) {
let output = input.parent().unwrap().join("output.js");
test_fixture(
Syntax::Es(EsSyntax {
jsx: true,
decorators: true,
..Default::default()
}),
&|_| as_folder(TransformVisitor::new()),
&input,
&output,
Default::default(),
);
}
29 changes: 29 additions & 0 deletions packages/remove-side-effect/tests/fixture/base/input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { useEffect, useLayoutEffect } from "react";

const Component = () => {
useEffect(() => {
console.log("Hello");
}, []);

useLayoutEffect(() => {
console.log("Hello Layout");
}, []);

return <div>Hello</div>
}

function Component2() {
useEffect(() => {
console.log("Hello");
}, []);

useLayoutEffect(() => {
console.log("Hello Layout");
}, []);

return <div>Hello</div>
}

export { Component2 };

export default Component;
Loading
Loading