Skip to content

Commit

Permalink
feat: Promote react-remove-properties (#215)
Browse files Browse the repository at this point in the history
  • Loading branch information
kdy1 authored Sep 21, 2023
1 parent ea45aa9 commit 74b5317
Show file tree
Hide file tree
Showing 30 changed files with 327 additions and 24 deletions.
46 changes: 39 additions & 7 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ members = [
"packages/jest",
"packages/loadable-components",
"packages/noop",
"packages/react-remove-properties",
"packages/relay",
"packages/remove-console",
"packages/styled-components",
Expand Down
2 changes: 1 addition & 1 deletion packages/constify/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@swc/plugin-constify",
"version": "0.1.13",
"version": "0.1.14",
"description": "SWC plugin for optimization",
"main": "swc_plugin_constify.wasm",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion packages/constify/transform/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ edition = "2021"
license = "Apache-2.0"
name = "swc_constify"
repository = "https://github.com/swc-project/plugins.git"
version = "0.12.0"
version = "0.13.0"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

Expand Down
2 changes: 1 addition & 1 deletion packages/emotion/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@swc/plugin-emotion",
"version": "2.5.83",
"version": "2.5.84",
"description": "SWC plugin for emotion css-in-js library",
"main": "swc_plugin_emotion.wasm",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion packages/emotion/transform/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ edition = "2021"
license = "Apache-2.0"
name = "swc_emotion"
repository = "https://github.com/swc-project/plugins.git"
version = "0.48.0"
version = "0.49.0"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

Expand Down
2 changes: 1 addition & 1 deletion packages/jest/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@swc/plugin-jest",
"version": "1.5.83",
"version": "1.5.84",
"description": "SWC plugin for jest",
"main": "swc_plugin_jest.wasm",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion packages/loadable-components/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@swc/plugin-loadable-components",
"version": "0.3.83",
"version": "0.3.84",
"description": "SWC plugin for `@loadable/components`",
"main": "swc_plugin_loadable_components.wasm",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion packages/noop/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@swc/plugin-noop",
"version": "1.5.81",
"version": "1.5.82",
"description": "Noop SWC plugin, for debugging",
"main": "swc_plugin_noop.wasm",
"scripts": {
Expand Down
23 changes: 23 additions & 0 deletions packages/react-remove-properties/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
[package]
authors = ["강동윤 <[email protected]>"]
description = "SWC plugin for https://www.npmjs.com/package/babel-plugin-react-remove-properties"
edition = "2021"
license = "Apache-2.0"
name = "swc_plugin_react_remove_properties"
publish = false
version = "0.13.3"

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

[dependencies]
react_remove_properties = { path = "./transform" }
serde_json = "1.0.79"
swc_cached = "0.3.17"
swc_common = { version = "0.32.1", features = ["concurrent"] }
swc_core = { version = "0.83.5", features = ["ecma_plugin_transform"] }
swc_ecma_ast = "0.109.1"
swc_ecma_utils = "0.123.0"
swc_ecma_visit = "0.95.1"
swc_plugin_macro = "0.9.15"
tracing = { version = "0.1.37", features = ["release_max_level_off"] }
22 changes: 22 additions & 0 deletions packages/react-remove-properties/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# react-remove-properties

See https://nextjs.org/docs/architecture/nextjs-compiler#remove-react-properties for more information.

## Config

```json
["react-remove-properties"]
```

or

```json
[
"react-remove-properties",
{
// The regexes defined here are processed in Rust so the syntax is different from
// JavaScript `RegExp`s. See https://docs.rs/regex.
"properties": ["^data-custom$"]
}
]
```
21 changes: 21 additions & 0 deletions packages/react-remove-properties/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "@swc/plugin-react-remove-properties",
"version": "1.5.84",
"description": "SWC plugin for https://www.npmjs.com/package/babel-plugin-react-remove-properties",
"main": "swc_plugin_react_remove_properties.wasm",
"scripts": {
"prepack": "cp ../../target/wasm32-wasi/release/swc_plugin_react_remove_properties.wasm ."
},
"homepage": "https://swc.rs",
"repository": {
"type": "git",
"url": "+https://github.com/swc-project/plugins.git"
},
"bugs": {
"url": "https://github.com/swc-project/plugins/issues"
},
"author": "강동윤 <[email protected]>",
"keywords": [],
"license": "Apache-2.0",
"preferUnplugged": true
}
20 changes: 20 additions & 0 deletions packages/react-remove-properties/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#![allow(clippy::not_unsafe_ptr_arg_deref)]
use swc_core::{
ecma::{ast::Program, visit::FoldWith},
plugin::{plugin_transform, proxies::TransformPluginProgramMetadata},
};

#[plugin_transform]
fn swc_plugin(program: Program, data: TransformPluginProgramMetadata) -> Program {
let config = serde_json::from_str::<Option<react_remove_properties::Config>>(
&data
.get_transform_plugin_config()
.expect("failed to get plugin config for react-remove-properties"),
)
.expect("invalid packages")
.unwrap_or_else(|| react_remove_properties::Config::All(true));

program.fold_with(&mut react_remove_properties::react_remove_properties(
config,
))
}
24 changes: 24 additions & 0 deletions packages/react-remove-properties/transform/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[package]
authors = ["강동윤 <[email protected]>"]
description = "AST Transforms for import modularizer"
edition = "2021"
license = "Apache-2.0"
name = "react_remove_properties"
repository = "https://github.com/swc-project/plugins.git"
version = "0.1.0"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
serde = { version = "1", features = ["derive"] }
swc_atoms = "0.5.9"
swc_cached = "0.3.17"
swc_common = "0.32.1"
swc_ecma_ast = "0.109.1"
swc_ecma_visit = "0.95.1"

[dev-dependencies]
swc_ecma_parser = "0.140.0"
swc_ecma_transforms_base = "0.133.2"
swc_ecma_transforms_testing = "0.136.1"
testing = "0.34.1"
70 changes: 70 additions & 0 deletions packages/react-remove-properties/transform/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
use serde::Deserialize;
use swc_cached::regex::CachedRegex;
use swc_ecma_ast::*;
use swc_ecma_visit::{noop_fold_type, Fold, FoldWith};

#[derive(Clone, Debug, Deserialize)]
#[serde(untagged)]
pub enum Config {
All(bool),
WithOptions(Options),
}

impl Config {
pub fn truthy(&self) -> bool {
match self {
Config::All(b) => *b,
Config::WithOptions(_) => true,
}
}
}

#[derive(Clone, Debug, Deserialize)]
pub struct Options {
#[serde(default)]
pub properties: Vec<String>,
}

pub fn react_remove_properties(config: Config) -> impl Fold {
let mut properties: Vec<CachedRegex> = match config {
Config::WithOptions(x) => x
.properties
.iter()
.map(|pattern| {
CachedRegex::new(pattern).unwrap_or_else(|e| {
panic!("error compiling property regex `{}`: {}", pattern, e);
})
})
.collect(),
_ => vec![],
};
if properties.is_empty() {
// Keep the default regex identical to `babel-plugin-react-remove-properties`.
properties.push(CachedRegex::new(r"^data-test").unwrap());
}
RemoveProperties { properties }
}

struct RemoveProperties {
properties: Vec<CachedRegex>,
}

impl RemoveProperties {
fn should_remove_property(&self, name: &str) -> bool {
self.properties.iter().any(|p| p.is_match(name))
}
}

impl Fold for RemoveProperties {
noop_fold_type!();

fn fold_jsx_opening_element(&mut self, mut el: JSXOpeningElement) -> JSXOpeningElement {
el.attrs.retain(|attr| {
!matches!(attr, JSXAttrOrSpread::JSXAttr(JSXAttr {
name: JSXAttrName::Ident(ident),
..
}) if self.should_remove_property(ident.sym.as_ref()))
});
el.fold_children_with(self)
}
}
Loading

0 comments on commit 74b5317

Please sign in to comment.