-
-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Promote
react-remove-properties
(#215)
- Loading branch information
Showing
30 changed files
with
327 additions
and
24 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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$"] | ||
} | ||
] | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
Oops, something went wrong.