Skip to content
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

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

Merged
merged 9 commits into from Aug 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/advanced-features/compiler.md
Expand Up @@ -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.
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

Expand Down
7 changes: 7 additions & 0 deletions packages/next-swc/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 packages/next-swc/crates/modularize_imports/Cargo.toml
Expand Up @@ -10,6 +10,7 @@ version = "0.14.0"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
convert_case = "0.5.0"
handlebars = "4.2.1"
once_cell = "1.13.0"
regex = "1.5"
Expand Down
29 changes: 20 additions & 9 deletions packages/next-swc/crates/modularize_imports/src/lib.rs
@@ -1,6 +1,6 @@
use std::borrow::Cow;
use std::collections::HashMap;

use convert_case::{Case, Casing};
use handlebars::{Context, Handlebars, Helper, HelperResult, Output, RenderContext};
use once_cell::sync::Lazy;
use regex::{Captures, Regex};
Expand Down Expand Up @@ -175,6 +175,9 @@ pub fn modularize_imports(config: Config) -> impl Fold {
folder
.renderer
.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 {
// XXX: Should we keep this hack?
if !k.starts_with('^') && !k.ends_with('$') {
Expand Down Expand Up @@ -223,13 +226,21 @@ fn helper_camel_case(
) -> HelperResult {
// get parameter from helper or throw an error
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)
} else {
let mut it = param.chars();
let fst = it.next().unwrap();
Cow::Owned(fst.to_lowercase().chain(it).collect::<String>())
};
out.write(value.as_ref())?;

out.write(param.to_case(Case::Camel).as_ref())?;
Ok(())
}

fn helper_kebab_case(
h: &Helper<'_, '_>,
_: &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(())
}
Expand Up @@ -44,6 +44,14 @@ fn modularize_imports_fixture(input: PathBuf) {
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()
.collect(),
Expand Down
@@ -1,2 +1,3 @@
import { Grid, Row, Col as Col1 } from 'react-bootstrap';
import { MyModule, Widget } from 'my-library-2';
import { MyModule } from 'my-library-3';
Expand Up @@ -3,3 +3,4 @@ import Row from "react-bootstrap/lib/Row";
import Col1 from "react-bootstrap/lib/Col";
import { MyModule } from "my-library-2/myModule";
import { Widget } from "my-library-2/widget";
import { MyModule } from "my-library-3/my-module";