-
Notifications
You must be signed in to change notification settings - Fork 30.5k
Add support for removing React properties. #31606
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
Merged
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
7da9cc1
Add remove-properties on Rust side
losfair df33628
Unit tests for remove-properties
losfair 88d815c
Use the name `react_remove_properties`.
losfair 17d9012
JS side of react-remove-properties.
losfair 7ac45ee
Do not go through JsWord->&[u8]->&str.
losfair 7e62085
Fix rebase conflicts.
losfair d69d1c8
Add the missing `fold_children_with`.
losfair File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,34 @@ | ||
| # See https://help.github.com/articles/ignoring-files/ for more about ignoring files. | ||
|
|
||
| # dependencies | ||
| /node_modules | ||
| /.pnp | ||
| .pnp.js | ||
|
|
||
| # testing | ||
| /coverage | ||
|
|
||
| # next.js | ||
| /.next/ | ||
| /out/ | ||
|
|
||
| # production | ||
| /build | ||
|
|
||
| # misc | ||
| .DS_Store | ||
| *.pem | ||
|
|
||
| # debug | ||
| npm-debug.log* | ||
| yarn-debug.log* | ||
| yarn-error.log* | ||
|
|
||
| # local env files | ||
| .env.local | ||
| .env.development.local | ||
| .env.test.local | ||
| .env.production.local | ||
|
|
||
| # vercel | ||
| .vercel |
This file contains hidden or 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,27 @@ | ||
| # React Remove Properties Example | ||
|
|
||
| This example shows how to use the `reactRemoveProperties` config option to remove React properties. | ||
|
|
||
| ## Preview | ||
|
|
||
| Preview the example live on [StackBlitz](http://stackblitz.com/): | ||
|
|
||
| [](https://stackblitz.com/github/vercel/next.js/tree/canary/examples/react-remove-properties) | ||
|
|
||
| ## Deploy your own | ||
|
|
||
| Deploy the example using [Vercel](https://vercel.com?utm_source=github&utm_medium=readme&utm_campaign=next-example): | ||
|
|
||
| [](https://vercel.com/new/git/external?repository-url=https://github.com/vercel/next.js/tree/canary/examples/react-remove-properties&project-name=react-remove-properties&repository-name=react-remove-properties) | ||
|
|
||
| ## How to use | ||
|
|
||
| Execute [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app) with [npm](https://docs.npmjs.com/cli/init) or [Yarn](https://yarnpkg.com/lang/en/docs/cli/create/) to bootstrap the example: | ||
|
|
||
| ```bash | ||
| npx create-next-app --example react-remove-properties react-remove-properties-app | ||
| # or | ||
| yarn create next-app --example react-remove-properties react-remove-properties-app | ||
| ``` | ||
|
|
||
| Deploy it to the cloud with [Vercel](https://vercel.com/new?utm_source=github&utm_medium=readme&utm_campaign=next-example) ([Documentation](https://nextjs.org/docs/deployment)). |
This file contains hidden or 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,9 @@ | ||
| module.exports = { | ||
| experimental: { | ||
| reactRemoveProperties: true, | ||
| // Or, specify a custom list of regular expressions to match properties to remove. | ||
| // The regexes defined here are processed in Rust so the syntax is different from | ||
| // JavaScript `RegExp`s. See https://docs.rs/regex. | ||
| // reactRemoveProperties: { properties: ['^data-custom$'] }, | ||
| }, | ||
| } |
This file contains hidden or 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,13 @@ | ||
| { | ||
| "private": true, | ||
| "scripts": { | ||
| "dev": "next dev", | ||
| "build": "next build", | ||
| "start": "next start" | ||
| }, | ||
| "dependencies": { | ||
| "next": "latest", | ||
| "react": "^17.0.2", | ||
| "react-dom": "^17.0.2" | ||
| } | ||
| } |
This file contains hidden or 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,9 @@ | ||
| const Index = () => ( | ||
| <div data-test-id="1" data-custom="1a"> | ||
| <div data-custom="2"> | ||
| <h1 data-testid="3">Hello World!</h1> | ||
| </div> | ||
| </div> | ||
| ) | ||
|
|
||
| export default Index |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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
72 changes: 72 additions & 0 deletions
72
packages/next-swc/crates/core/src/react_remove_properties.rs
This file contains hidden or 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,72 @@ | ||
| use regex::Regex; | ||
| use serde::Deserialize; | ||
| use swc_ecmascript::ast::*; | ||
| use swc_ecmascript::visit::{noop_fold_type, Fold}; | ||
|
|
||
| #[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>, | ||
| } | ||
|
|
||
| struct RemoveProperties { | ||
| properties: Vec<Regex>, | ||
| } | ||
|
|
||
| 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| match attr { | ||
| JSXAttrOrSpread::JSXAttr(JSXAttr { | ||
| name: JSXAttrName::Ident(ident), | ||
| .. | ||
| }) if self.should_remove_property(ident.sym.as_ref()) => false, | ||
| _ => true, | ||
| }); | ||
| el | ||
| } | ||
| } | ||
|
|
||
| pub fn remove_properties(config: Config) -> impl Fold { | ||
| let mut properties: Vec<Regex> = match config { | ||
| Config::WithOptions(x) => x | ||
| .properties | ||
| .iter() | ||
| .map(|pattern| { | ||
| Regex::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(Regex::new(r"^data-test").unwrap()); | ||
| } | ||
| let remover = RemoveProperties { properties }; | ||
| remover | ||
| } | ||
This file contains hidden or 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
7 changes: 7 additions & 0 deletions
7
packages/next-swc/crates/core/tests/fixture/react-remove-properties/custom/simple/input.js
This file contains hidden or 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,7 @@ | ||
| export default function Home() { | ||
| return <div data-test-id="1" data-custom="1a"> | ||
| <div data-custom="2"> | ||
| <h1 data-testid="3">Hello World!</h1> | ||
| </div> | ||
| </div> | ||
| } |
11 changes: 11 additions & 0 deletions
11
packages/next-swc/crates/core/tests/fixture/react-remove-properties/custom/simple/output.js
This file contains hidden or 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,11 @@ | ||
| export default function Home() { | ||
| return <div data-test-id="1"> | ||
|
|
||
| <div > | ||
|
|
||
| <h1 data-testid="3">Hello World!</h1> | ||
|
|
||
| </div> | ||
|
|
||
| </div>; | ||
| }; |
7 changes: 7 additions & 0 deletions
7
packages/next-swc/crates/core/tests/fixture/react-remove-properties/default/simple/input.js
This file contains hidden or 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,7 @@ | ||
| export default function Home() { | ||
| return <div data-test-id="1" data-custom="1a"> | ||
| <div data-custom="2"> | ||
| <h1 data-testid="3">Hello World!</h1> | ||
| </div> | ||
| </div> | ||
| } |
11 changes: 11 additions & 0 deletions
11
packages/next-swc/crates/core/tests/fixture/react-remove-properties/default/simple/output.js
This file contains hidden or 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,11 @@ | ||
| export default function Home() { | ||
| return <div data-custom="1a"> | ||
|
|
||
| <div data-custom="2"> | ||
|
|
||
| <h1 >Hello World!</h1> | ||
|
|
||
| </div> | ||
|
|
||
| </div>; | ||
| }; |
This file contains hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.