Unverified Commit 40f0eb12 authored by Noel Kim (김민혁)'s avatar Noel Kim (김민혁) Committed by GitHub

feat(next-swc/modularize_imports): Add Kebab case (#38583)

* Feat: Add Kebab case

* Add document

* Feat: Add Kebab case

* Add document

* Add Test cases
parent 4e6d0557
...@@ -358,7 +358,7 @@ This transform uses [handlebars](https://docs.rs/handlebars) to template the rep ...@@ -358,7 +358,7 @@ This transform uses [handlebars](https://docs.rs/handlebars) to template the rep
1. `matches`: Has type `string[]`. All groups matched by the regular expression. `matches.[0]` is the full match. 1. `matches`: Has type `string[]`. All groups matched by the regular expression. `matches.[0]` is the full match.
2. `member`: Has type `string`. The name of the member import. 2. `member`: Has type `string`. The name of the member import.
3. `lowerCase`, `upperCase`, `camelCase`: Helper functions to convert a string to lower, upper or camel cases. 3. `lowerCase`, `upperCase`, `camelCase`, `kebabCase`: Helper functions to convert a string to lower, upper, camel or kebab cases.
### SWC Trace profiling ### SWC Trace profiling
......
...@@ -343,6 +343,12 @@ version = "0.1.5" ...@@ -343,6 +343,12 @@ version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "245097e9a4535ee1e3e3931fcfcd55a796a44c643e8596ff6566d68f09b87bbc" checksum = "245097e9a4535ee1e3e3931fcfcd55a796a44c643e8596ff6566d68f09b87bbc"
[[package]]
name = "convert_case"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fb4a24b1aaf0fd0ce8b45161144d6f42cd91677fd5940fd431183eb023b3a2b8"
[[package]] [[package]]
name = "core-foundation" name = "core-foundation"
version = "0.9.3" version = "0.9.3"
...@@ -1477,6 +1483,7 @@ dependencies = [ ...@@ -1477,6 +1483,7 @@ dependencies = [
name = "modularize_imports" name = "modularize_imports"
version = "0.14.0" version = "0.14.0"
dependencies = [ dependencies = [
"convert_case",
"handlebars", "handlebars",
"once_cell", "once_cell",
"regex", "regex",
......
...@@ -10,6 +10,7 @@ version = "0.14.0" ...@@ -10,6 +10,7 @@ version = "0.14.0"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
convert_case = "0.5.0"
handlebars = "4.2.1" handlebars = "4.2.1"
once_cell = "1.13.0" once_cell = "1.13.0"
regex = "1.5" regex = "1.5"
......
use std::borrow::Cow;
use std::collections::HashMap; use std::collections::HashMap;
use convert_case::{Case, Casing};
use handlebars::{Context, Handlebars, Helper, HelperResult, Output, RenderContext}; use handlebars::{Context, Handlebars, Helper, HelperResult, Output, RenderContext};
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
use regex::{Captures, Regex}; use regex::{Captures, Regex};
...@@ -175,6 +175,9 @@ pub fn modularize_imports(config: Config) -> impl Fold { ...@@ -175,6 +175,9 @@ pub fn modularize_imports(config: Config) -> impl Fold {
folder folder
.renderer .renderer
.register_helper("camelCase", Box::new(helper_camel_case)); .register_helper("camelCase", Box::new(helper_camel_case));
folder
.renderer
.register_helper("kebabCase", Box::new(helper_kebab_case));
for (mut k, v) in config.packages { for (mut k, v) in config.packages {
// XXX: Should we keep this hack? // XXX: Should we keep this hack?
if !k.starts_with('^') && !k.ends_with('$') { if !k.starts_with('^') && !k.ends_with('$') {
...@@ -223,13 +226,21 @@ fn helper_camel_case( ...@@ -223,13 +226,21 @@ fn helper_camel_case(
) -> HelperResult { ) -> HelperResult {
// get parameter from helper or throw an error // get parameter from helper or throw an error
let param = h.param(0).and_then(|v| v.value().as_str()).unwrap_or(""); let param = h.param(0).and_then(|v| v.value().as_str()).unwrap_or("");
let value = if param.is_empty() || param.chars().next().unwrap().is_lowercase() {
Cow::Borrowed(param) out.write(param.to_case(Case::Camel).as_ref())?;
} else { Ok(())
let mut it = param.chars(); }
let fst = it.next().unwrap();
Cow::Owned(fst.to_lowercase().chain(it).collect::<String>()) fn helper_kebab_case(
}; h: &Helper<'_, '_>,
out.write(value.as_ref())?; _: &Handlebars<'_>,
_: &Context,
_: &mut RenderContext<'_, '_>,
out: &mut dyn Output,
) -> HelperResult {
// get parameter from helper or throw an error
let param = h.param(0).and_then(|v| v.value().as_str()).unwrap_or("");
out.write(param.to_case(Case::Kebab).as_ref())?;
Ok(()) Ok(())
} }
...@@ -44,6 +44,14 @@ fn modularize_imports_fixture(input: PathBuf) { ...@@ -44,6 +44,14 @@ fn modularize_imports_fixture(input: PathBuf) {
skip_default_conversion: true, skip_default_conversion: true,
}, },
), ),
(
"my-library-3".to_string(),
PackageConfig {
transform: "my-library-3/{{ kebabCase member }}".into(),
prevent_full_import: false,
skip_default_conversion: true,
},
),
] ]
.into_iter() .into_iter()
.collect(), .collect(),
......
import { Grid, Row, Col as Col1 } from 'react-bootstrap'; import { Grid, Row, Col as Col1 } from 'react-bootstrap';
import { MyModule, Widget } from 'my-library-2'; import { MyModule, Widget } from 'my-library-2';
import { MyModule } from 'my-library-3';
...@@ -3,3 +3,4 @@ import Row from "react-bootstrap/lib/Row"; ...@@ -3,3 +3,4 @@ import Row from "react-bootstrap/lib/Row";
import Col1 from "react-bootstrap/lib/Col"; import Col1 from "react-bootstrap/lib/Col";
import { MyModule } from "my-library-2/myModule"; import { MyModule } from "my-library-2/myModule";
import { Widget } from "my-library-2/widget"; import { Widget } from "my-library-2/widget";
import { MyModule } from "my-library-3/my-module";
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment