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

refactor(utils): remove utils::to_camel_case #123

Merged
merged 1 commit into from
Sep 2, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
128 changes: 0 additions & 128 deletions crates/rome_js_analyze/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use rome_js_syntax::{
JsVariableDeclaration, JsVariableDeclarator, JsVariableDeclaratorList, JsVariableStatement, T,
};
use rome_rowan::{AstNode, AstSeparatedList, BatchMutation, Direction, WalkEvent};
use std::borrow::Cow;
use std::iter;

pub mod batch;
Expand Down Expand Up @@ -45,86 +44,6 @@ pub(crate) fn escape_string(s: &str) -> Result<String, EscapeError> {
(InterpretEscapedString { s: s.chars() }).collect()
}

pub trait ToCamelCase {
/// Return the camel case form of the input parameter.
/// If it is already in camel case, nothing is done.
///
/// This method do not address abbreviations and acronyms.
fn to_camel_case(&self) -> Cow<str>;
}

impl ToCamelCase for str {
fn to_camel_case(&self) -> Cow<str> {
to_camel_case(self)
}
}

/// Return the camel case form of the input parameter.
/// If it is already in camel case, nothing is done.
///
/// This method do not address abbreviations and acronyms.
pub fn to_camel_case(input: &str) -> Cow<str> {
pub enum ForceNext {
Uppercase,
Lowercase,
}

let mut force_next = None;
let mut chars = input.char_indices();
let mut last_i = input.len() - 1;

while let Some((i, chr)) = chars.next() {
if i == 0 && chr.is_uppercase() {
chars = input.char_indices();
force_next = Some(ForceNext::Lowercase);
last_i = i;
break;
}

if !chr.is_alphanumeric() {
if i == 0 {
force_next = Some(ForceNext::Lowercase);
} else {
force_next = Some(ForceNext::Uppercase);
}
last_i = i;
break;
}
}

if last_i >= (input.len() - 1) {
Cow::Borrowed(input)
} else {
let mut output = Vec::with_capacity(input.len());
output.extend_from_slice(input[..last_i].as_bytes());
//SAFETY: bytes were already inside a valid &str
let mut output = unsafe { String::from_utf8_unchecked(output) };

for (_, chr) in chars {
if !chr.is_alphanumeric() {
force_next = Some(ForceNext::Uppercase);
continue;
}

match force_next {
Some(ForceNext::Uppercase) => {
output.extend(chr.to_uppercase());
}
Some(ForceNext::Lowercase) => {
output.extend(chr.to_lowercase());
}
None => {
output.push(chr);
}
}

force_next = None;
}

Cow::Owned(output)
}
}

/// Utility function to remove a statement node from a syntax tree, by either
/// removing the node from its parent if said parent is a statement list or
/// module item list, or by replacing the statement node with an empty statement
Expand Down Expand Up @@ -234,50 +153,3 @@ pub(crate) fn is_node_equal(a_node: &JsSyntaxNode, b_node: &JsSyntaxNode) -> boo

true
}

#[test]
fn ok_to_camel_case() {
assert_eq!(to_camel_case("camelCase"), Cow::Borrowed("camelCase"));
assert_eq!(
to_camel_case("longCamelCase"),
Cow::Borrowed("longCamelCase")
);

assert!(matches!(
to_camel_case("CamelCase"),
Cow::Owned(s) if s.as_str() == "camelCase"
));
assert!(matches!(
to_camel_case("_camelCase"),
Cow::Owned(s) if s.as_str() == "camelCase"
));
assert!(matches!(
to_camel_case("_camelCase_"),
Cow::Owned(s) if s.as_str() == "camelCase"
));
assert!(matches!(
to_camel_case("_camel_Case_"),
Cow::Owned(s) if s.as_str() == "camelCase"
));
assert!(matches!(
to_camel_case("_camel_case_"),
Cow::Owned(s) if s.as_str() == "camelCase"
));
assert!(matches!(
to_camel_case("_camel_case"),
Cow::Owned(s) if s.as_str() == "camelCase"
));
assert!(matches!(
to_camel_case("camel_case"),
Cow::Owned(s) if s.as_str() == "camelCase"
));

assert!(matches!(
to_camel_case("LongCamelCase"),
Cow::Owned(s) if s.as_str() == "longCamelCase"
));
assert!(matches!(
to_camel_case("long_camel_case"),
Cow::Owned(s) if s.as_str() == "longCamelCase"
));
}
4 changes: 2 additions & 2 deletions xtask/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
[package]
name = "xtask"
version = "0.0.0"
edition = "2021"
name = "xtask"
publish = false
version = "0.0.0"

[dependencies]
anyhow = "1.0.52"
46 changes: 23 additions & 23 deletions xtask/bench/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,39 +1,39 @@
[package]
name = "xtask_bench"
version = "0.0.0"
edition = "2021"
name = "xtask_bench"
publish = false
version = "0.0.0"

[dependencies]
xtask = { path = '../', version = "0.0" }
rome_js_syntax = { path = "../../crates/rome_js_syntax" }
rome_console = { path = "../../crates/rome_console" }
rome_js_parser = { path = "../../crates/rome_js_parser" }
rome_analyze = { path = "../../crates/rome_analyze" }
rome_console = { path = "../../crates/rome_console" }
rome_diagnostics = { path = "../../crates/rome_diagnostics" }
rome_formatter = { path = "../../crates/rome_formatter" }
rome_js_analyze = { path = "../../crates/rome_js_analyze" }
rome_js_formatter = { path = "../../crates/rome_js_formatter" }
rome_js_parser = { path = "../../crates/rome_js_parser" }
rome_js_syntax = { path = "../../crates/rome_js_syntax" }
rome_json_formatter = { path = "../../crates/rome_json_formatter" }
rome_json_parser = { path = "../../crates/rome_json_parser" }
rome_json_syntax = { path = "../../crates/rome_json_syntax" }
rome_parser = { path = "../../crates/rome_parser" }
rome_diagnostics = { path = "../../crates/rome_diagnostics" }
rome_formatter = { path = "../../crates/rome_formatter" }
rome_js_formatter = { path = "../../crates/rome_js_formatter" }
rome_analyze = { path = "../../crates/rome_analyze" }
rome_js_analyze = { path = "../../crates/rome_js_analyze" }
rome_rowan = { path = "../../crates/rome_rowan" }
rome_json_parser = { path = "../../crates/rome_json_parser" }
rome_json_syntax = { path = "../../crates/rome_json_syntax" }
rome_parser = { path = "../../crates/rome_parser" }
rome_rowan = { path = "../../crates/rome_rowan" }
xtask = { path = '../', version = "0.0" }


pico-args = { version = "0.5.0", features = ["eq-separator"] }
timing = "0.2.3"
ansi_rgb = "0.2.0"
criterion = "0.5.1"
regex = "1.5.5"
ureq = "2.7.1"
url = "2.2.2"
itertools = "0.11.0"
ansi_rgb = "0.2.0"
pico-args = { version = "0.5.0", features = ["eq-separator"] }
regex = "1.5.5"
timing = "0.2.3"
ureq = "2.7.1"
url = "2.2.2"

countme = { workspace = true }

# dhat-on
dhat = { version = "0.3.0", optional = true }
dhat = { version = "0.3.0", optional = true }
humansize = { version = "2.1.2", optional = true }

[target.'cfg(target_os = "windows")'.dependencies]
Expand All @@ -43,5 +43,5 @@ mimalloc = "0.1.29"
tikv-jemallocator = "0.5.0"

[features]
count = ["countme/print_at_exit"]
dhat-heap = ["dhat", "humansize"]
count = ["countme/print_at_exit"]
66 changes: 32 additions & 34 deletions xtask/codegen/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,44 +1,42 @@
[package]
name = "xtask_codegen"
version = "0.0.0"
edition = "2021"
name = "xtask_codegen"
publish = false
version = "0.0.0"

[dependencies]
xtask = { path = '../', version = "0.0" }
anyhow = "1.0.52"
pico-args = { version = "0.5.0", features = ["eq-separator"] }
quote = "1.0.14"
proc-macro2 = { version = "1.0.63", features = ["span-locations"] }
ungrammar = "1.14.9"
walkdir = "2.3.2"
ureq = "2.4.0"
git2 = { version = "0.17.1", default-features = false }
filetime = "0.2.15"
case = "1.0.0"
convert_case = "0.6.0"
anyhow = "1.0.52"
case = "1.0.0"
convert_case = "0.6.0"
filetime = "0.2.15"
fs_extra = "1.3.0"
git2 = { version = "0.17.1", default-features = false }
pico-args = { version = "0.5.0", features = ["eq-separator"] }
proc-macro2 = { version = "1.0.63", features = ["span-locations"] }
pulldown-cmark = { version = "0.9", default-features = false, optional = true }
fs_extra = "1.3.0"
quote = "1.0.14"
ungrammar = "1.14.9"
ureq = "2.4.0"
walkdir = "2.3.2"
xtask = { path = '../', version = "0.0" }

rome_js_parser = { workspace = true, optional = true }
rome_rowan = { path = "../../crates/rome_rowan", optional = true }
rome_cli = { workspace = true, optional = true }
rome_analyze = { path = "../../crates/rome_analyze", optional = true }
rome_js_analyze = { path = "../../crates/rome_js_analyze", optional = true }
rome_json_analyze = { workspace = true, optional = true }
rome_js_syntax = { path = "../../crates/rome_js_syntax", optional = true }
rome_json_syntax = { workspace = true, optional = true }
rome_js_factory = { path = "../../crates/rome_js_factory", optional = true }
rome_js_formatter = { path = "../../crates/rome_js_formatter", optional = true }
rome_analyze = { path = "../../crates/rome_analyze", optional = true }
rome_aria = { path = "../../crates/rome_aria", optional = true }
rome_cli = { workspace = true, optional = true }
rome_diagnostics = { path = "../../crates/rome_diagnostics", optional = true }
rome_js_analyze = { path = "../../crates/rome_js_analyze", optional = true }
rome_js_factory = { path = "../../crates/rome_js_factory", optional = true }
rome_js_formatter = { path = "../../crates/rome_js_formatter", optional = true }
rome_js_parser = { workspace = true, optional = true }
rome_js_syntax = { path = "../../crates/rome_js_syntax", optional = true }
rome_json_analyze = { workspace = true, optional = true }
rome_json_formatter = { path = "../../crates/rome_json_formatter", optional = true }
rome_json_parser = { path = "../../crates/rome_json_parser", optional = true }
rome_diagnostics = { path = "../../crates/rome_diagnostics", optional = true }
rome_aria = { path = "../../crates/rome_aria", optional = true }
rome_service = { path = "../../crates/rome_service", features = [
"schema",
], optional = true }
schemars = { version = "0.8.10", optional = true }
serde_json = { version = "1.0.74", optional = true }
rome_json_parser = { path = "../../crates/rome_json_parser", optional = true }
rome_json_syntax = { workspace = true, optional = true }
rome_rowan = { path = "../../crates/rome_rowan", optional = true }
rome_service = { path = "../../crates/rome_service", features = ["schema"], optional = true }
schemars = { version = "0.8.10", optional = true }
serde_json = { version = "1.0.74", optional = true }

[features]
configuration = [
Expand All @@ -49,7 +47,6 @@ configuration = [
"rome_json_syntax",
"pulldown-cmark",
]
website = ["rome_service", "rome_cli/docgen", "rome_js_parser", "rome_js_formatter", "rome_js_syntax"]
schema = [
"schemars",
"serde_json",
Expand All @@ -62,3 +59,4 @@ schema = [
"rome_json_parser",
"rome_diagnostics",
]
website = ["rome_service", "rome_cli/docgen", "rome_js_parser", "rome_js_formatter", "rome_js_syntax"]
8 changes: 4 additions & 4 deletions xtask/codegen/src/generate_analyzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ fn generate_category(
generate_group(name, file_name, base_path.clone())?;

let module_name = format_ident!("{}", file_name);
let group_name = format_ident!("{}", to_camel_case(file_name)?);
let group_name = format_ident!("{}", to_pascal_case(file_name)?);

groups.insert(
file_name.to_string(),
Expand All @@ -96,7 +96,7 @@ fn generate_category(
let key = name;
let module_name = format_ident!("{name}");

let category_name = to_camel_case(name).unwrap();
let category_name = to_pascal_case(name).unwrap();
let category_name = format_ident!("{category_name}");

let kind = match name {
Expand Down Expand Up @@ -162,7 +162,7 @@ fn generate_group(category: &'static str, group: &str, base_path: PathBuf) -> Re
);
}

let group_name = format_ident!("{}", to_camel_case(group)?);
let group_name = format_ident!("{}", to_pascal_case(group)?);

let (rule_imports, rule_names): (Vec<_>, Vec<_>) = rules.into_values().unzip();

Expand All @@ -189,7 +189,7 @@ fn generate_group(category: &'static str, group: &str, base_path: PathBuf) -> Re
Ok(())
}

fn to_camel_case(input: &str) -> Result<String> {
fn to_pascal_case(input: &str) -> Result<String> {
let mut result = String::new();
let mut chars = input.char_indices();

Expand Down
14 changes: 7 additions & 7 deletions xtask/contributors/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
[package]
name = "xtask_contributors"
version = "0.0.0"
edition = "2021"
name = "xtask_contributors"
publish = false
version = "0.0.0"

[dependencies]
serde = { version = "1.0.133", features = ["derive"] }
serde_json = { version = "1.0.74" }
xtask = { path = '../', version = "0.0" }
ureq = { version = "2.4.0", features = ["json"] }
pico-args = "0.5.0"
html-escape = "0.2.11"
pico-args = "0.5.0"
serde = { version = "1.0.133", features = ["derive"] }
serde_json = { version = "1.0.74" }
ureq = { version = "2.4.0", features = ["json"] }
xtask = { path = '../', version = "0.0" }
Loading