From 486dc6756b2c5f8a5167887d2defbf68eeba7def Mon Sep 17 00:00:00 2001 From: Victorien Elvinger Date: Sat, 2 Sep 2023 18:03:33 +0200 Subject: [PATCH] refactor(utils): remove utils::to_camel_case (#123) --- crates/rome_js_analyze/src/utils.rs | 128 ---------------------- xtask/Cargo.toml | 4 +- xtask/bench/Cargo.toml | 46 ++++---- xtask/codegen/Cargo.toml | 66 ++++++----- xtask/codegen/src/generate_analyzer.rs | 8 +- xtask/contributors/Cargo.toml | 14 +-- xtask/coverage/Cargo.toml | 48 ++++---- xtask/libs_bench/Cargo.toml | 32 +++--- xtask/libs_bench/benches/to_camel_case.rs | 32 ------ xtask/lintdoc/Cargo.toml | 30 ++--- 10 files changed, 121 insertions(+), 287 deletions(-) delete mode 100644 xtask/libs_bench/benches/to_camel_case.rs diff --git a/crates/rome_js_analyze/src/utils.rs b/crates/rome_js_analyze/src/utils.rs index 7b34a6ae8243..4e5297f48211 100644 --- a/crates/rome_js_analyze/src/utils.rs +++ b/crates/rome_js_analyze/src/utils.rs @@ -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; @@ -45,86 +44,6 @@ pub(crate) fn escape_string(s: &str) -> Result { (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; -} - -impl ToCamelCase for str { - fn to_camel_case(&self) -> Cow { - 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 { - 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 @@ -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" - )); -} diff --git a/xtask/Cargo.toml b/xtask/Cargo.toml index 6f8dc7c84bcb..57d8c2b3c841 100644 --- a/xtask/Cargo.toml +++ b/xtask/Cargo.toml @@ -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" diff --git a/xtask/bench/Cargo.toml b/xtask/bench/Cargo.toml index 2b5deb7f3407..f13ae2f8be90 100644 --- a/xtask/bench/Cargo.toml +++ b/xtask/bench/Cargo.toml @@ -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] @@ -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"] diff --git a/xtask/codegen/Cargo.toml b/xtask/codegen/Cargo.toml index f9ab96dd588b..3ba95088a153 100644 --- a/xtask/codegen/Cargo.toml +++ b/xtask/codegen/Cargo.toml @@ -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 = [ @@ -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", @@ -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"] diff --git a/xtask/codegen/src/generate_analyzer.rs b/xtask/codegen/src/generate_analyzer.rs index d731cd98b45c..2e13fa586c9c 100644 --- a/xtask/codegen/src/generate_analyzer.rs +++ b/xtask/codegen/src/generate_analyzer.rs @@ -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(), @@ -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 { @@ -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(); @@ -189,7 +189,7 @@ fn generate_group(category: &'static str, group: &str, base_path: PathBuf) -> Re Ok(()) } -fn to_camel_case(input: &str) -> Result { +fn to_pascal_case(input: &str) -> Result { let mut result = String::new(); let mut chars = input.char_indices(); diff --git a/xtask/contributors/Cargo.toml b/xtask/contributors/Cargo.toml index 2be232fa65cc..3ac3087bfddd 100644 --- a/xtask/contributors/Cargo.toml +++ b/xtask/contributors/Cargo.toml @@ -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" } diff --git a/xtask/coverage/Cargo.toml b/xtask/coverage/Cargo.toml index ebe58368e46b..c96998f313f9 100644 --- a/xtask/coverage/Cargo.toml +++ b/xtask/coverage/Cargo.toml @@ -1,30 +1,30 @@ [package] -name = "xtask_coverage" -version = "0.0.0" edition = "2021" +name = "xtask_coverage" publish = false +version = "0.0.0" [dependencies] -xtask = { path = '../', version = "0.0" } -rome_rowan = { path = "../../crates/rome_rowan" } -rome_console = { path = "../../crates/rome_console" } -rome_js_syntax = { path = "../../crates/rome_js_syntax" } -rome_js_parser = { path = "../../crates/rome_js_parser" } -rome_js_semantic = { path = "../../crates/rome_js_semantic" } -rome_diagnostics = { path = "../../crates/rome_diagnostics" } -rome_parser = { path = "../../crates/rome_parser" } -pico-args = { version = "0.5.0", features = ["eq-separator"] } -ascii_table = "4.0.2" -colored = "2.0.0" -yastl = "0.1.2" -indicatif = { version = "0.17.0", features = ["improved_unicode"] } -serde = { version = "1.0.133", features = ["derive"] } -serde_json = "1.0.74" -serde_yaml = "0.9.9" -regex = "1.5.5" -once_cell = "1.9.0" -walkdir = "2.3.2" -atty = { workspace = true } -tracing = { workspace = true } +ascii_table = "4.0.2" +atty = { workspace = true } +backtrace = "0.3.65" +colored = "2.0.0" +indicatif = { version = "0.17.0", features = ["improved_unicode"] } +once_cell = "1.9.0" +pico-args = { version = "0.5.0", features = ["eq-separator"] } +regex = "1.5.5" +rome_console = { path = "../../crates/rome_console" } +rome_diagnostics = { path = "../../crates/rome_diagnostics" } +rome_js_parser = { path = "../../crates/rome_js_parser" } +rome_js_semantic = { path = "../../crates/rome_js_semantic" } +rome_js_syntax = { path = "../../crates/rome_js_syntax" } +rome_parser = { path = "../../crates/rome_parser" } +rome_rowan = { path = "../../crates/rome_rowan" } +serde = { version = "1.0.133", features = ["derive"] } +serde_json = "1.0.74" +serde_yaml = "0.9.9" +tracing = { workspace = true } tracing-subscriber = { version = "0.3.11", features = ["env-filter", "std"] } -backtrace = "0.3.65" +walkdir = "2.3.2" +xtask = { path = '../', version = "0.0" } +yastl = "0.1.2" diff --git a/xtask/libs_bench/Cargo.toml b/xtask/libs_bench/Cargo.toml index e23737ab59d4..70049b1351d7 100644 --- a/xtask/libs_bench/Cargo.toml +++ b/xtask/libs_bench/Cargo.toml @@ -1,36 +1,32 @@ [package] -name = "xtask_libs_bench" -version = "0.0.0" edition = "2021" +name = "xtask_libs_bench" publish = false +version = "0.0.0" [dependencies] regex = { version = "1.6.0" } [dev-dependencies] +case = "1.0.0" +criterion = "0.4.0" +fastbloom-rs = "0.3.0" +fst = "0.4.7" +iai = "0.1.1" +memchr = "2.5.0" +qp-trie = "0.8.0" rome_js_analyze = { path = "../../crates/rome_js_analyze" } -case = "1.0.0" -fastbloom-rs = "0.3.0" -qp-trie = "0.8.0" -fst = "0.4.7" -criterion = "0.4.0" -memchr = "2.5.0" -iai = "0.1.1" - -[[bench]] -name = "to_camel_case" -harness = false [[bench]] -name = "contains_iai" harness = false +name = "contains_iai" [[bench]] -name = "contains_criterion" harness = false +name = "contains_criterion" [[bin]] -name = "contains_iai" -path = "bins/contains_iai.rs" -test = false bench = false +name = "contains_iai" +path = "bins/contains_iai.rs" +test = false diff --git a/xtask/libs_bench/benches/to_camel_case.rs b/xtask/libs_bench/benches/to_camel_case.rs deleted file mode 100644 index fa6fb3b43799..000000000000 --- a/xtask/libs_bench/benches/to_camel_case.rs +++ /dev/null @@ -1,32 +0,0 @@ -fn to_camel_case_rome_all_lowercase() { - let _ = rome_js_analyze::utils::to_camel_case(iai::black_box("lowercase")); -} - -fn to_camel_case_case_all_lowercase() { - let _ = case::CaseExt::to_camel(iai::black_box("lowercase")); -} - -fn to_camel_case_rome_already_camel_case() { - let _ = rome_js_analyze::utils::to_camel_case(iai::black_box("camelCase")); -} - -fn to_camel_case_case_already_camel_case() { - let _ = case::CaseExt::to_camel(iai::black_box("camelCase")); -} - -fn to_camel_case_rome_pascal_case() { - let _ = rome_js_analyze::utils::to_camel_case(iai::black_box("CamelCase")); -} - -fn to_camel_case_case_pascal_case() { - let _ = case::CaseExt::to_camel(iai::black_box("CamelCase")); -} - -iai::main!( - to_camel_case_rome_all_lowercase, - to_camel_case_case_all_lowercase, - to_camel_case_rome_already_camel_case, - to_camel_case_case_already_camel_case, - to_camel_case_rome_pascal_case, - to_camel_case_case_pascal_case, -); diff --git a/xtask/lintdoc/Cargo.toml b/xtask/lintdoc/Cargo.toml index 7e5cff85719d..0338f2acc247 100644 --- a/xtask/lintdoc/Cargo.toml +++ b/xtask/lintdoc/Cargo.toml @@ -1,21 +1,21 @@ [package] -name = "xtask_lintdoc" -version = "0.0.0" edition = "2021" +name = "xtask_lintdoc" publish = false +version = "0.0.0" [dependencies] -xtask = { path = '../', version = "0.0" } -rome_analyze = { path = "../../crates/rome_analyze" } -rome_console = { path = "../../crates/rome_console" } -rome_diagnostics = { path = "../../crates/rome_diagnostics" } -rome_js_analyze = { path = "../../crates/rome_js_analyze" } +convert_case = "0.6.0" +pulldown-cmark = { version = "0.9", default-features = false } +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_parser = { path = "../../crates/rome_js_parser" } +rome_js_syntax = { path = "../../crates/rome_js_syntax" } rome_json_analyze = { path = "../../crates/rome_json_analyze" } -rome_js_parser = { path = "../../crates/rome_js_parser" } -rome_js_syntax = { path = "../../crates/rome_js_syntax" } -rome_json_parser = { path = "../../crates/rome_json_parser" } -rome_json_syntax = { path = "../../crates/rome_json_syntax" } -rome_service = { path = "../../crates/rome_service" } -rome_formatter = { path = "../../crates/rome_formatter" } -pulldown-cmark = { version = "0.9", default-features = false } -convert_case = "0.6.0" +rome_json_parser = { path = "../../crates/rome_json_parser" } +rome_json_syntax = { path = "../../crates/rome_json_syntax" } +rome_service = { path = "../../crates/rome_service" } +xtask = { path = '../', version = "0.0" }