diff --git a/src/config.rs b/src/config.rs index c575b90..8bb2cb6 100644 --- a/src/config.rs +++ b/src/config.rs @@ -287,11 +287,32 @@ fn check_if_rescript11_or_higher(version: &str) -> Result { } fn namespace_from_package_name(package_name: &str) -> String { - package_name - .to_owned() - .replace('@', "") - .replace('/', "_") - .to_case(Case::Pascal) + let len = package_name.len(); + let mut buf = String::with_capacity(len); + + fn aux(s: &str, capital: bool, buf: &mut String, off: usize) { + if off >= s.len() { + return; + } + + let ch = s.as_bytes()[off] as char; + match ch { + 'a'..='z' | 'A'..='Z' | '0'..='9' | '_' => { + let new_capital = false; + buf.push(if capital { ch.to_ascii_uppercase() } else { ch }); + aux(s, new_capital, buf, off + 1); + } + '/' | '-' => { + aux(s, true, buf, off + 1); + } + _ => { + aux(s, capital, buf, off + 1); + } + } + } + + aux(package_name, true, &mut buf, 0); + buf } impl Config { @@ -312,7 +333,7 @@ impl Config { namespace if namespace.is_case(Case::UpperFlat) => { packages::Namespace::Namespace(namespace.to_string()) } - namespace => packages::Namespace::Namespace(namespace.to_string().to_case(Case::Pascal)), + namespace => packages::Namespace::Namespace(namespace_from_package_name(namespace)), }, (Some(self::NamespaceConfig::String(str)), Some(entry)) => match str.as_str() { "true" => packages::Namespace::NamespaceWithEntry { diff --git a/testrepo/package.json b/testrepo/package.json index ae81646..e9802c0 100644 --- a/testrepo/package.json +++ b/testrepo/package.json @@ -6,7 +6,8 @@ "packages/main", "packages/dep01", "packages/dep02", - "packages/new-namespace" + "packages/new-namespace", + "packages/namespace-casing" ] }, "scripts": { diff --git a/testrepo/packages/namespace-casing/bsconfig.json b/testrepo/packages/namespace-casing/bsconfig.json new file mode 100644 index 0000000..bc74c18 --- /dev/null +++ b/testrepo/packages/namespace-casing/bsconfig.json @@ -0,0 +1,22 @@ +{ + "name": "namespace-casing", + "namespace": "NamespaceCasingAPI", + "sources": [ + { + "dir": "src", + "subdirs": true + } + ], + "package-specs": [ + { + "module": "es6", + "in-source": true + } + ], + "suffix": ".mjs", + "bs-dependencies": [], + "bsc-flags": [], + "jsx": { + "version": 4 + } +} \ No newline at end of file diff --git a/testrepo/packages/namespace-casing/package.json b/testrepo/packages/namespace-casing/package.json new file mode 100644 index 0000000..65b2411 --- /dev/null +++ b/testrepo/packages/namespace-casing/package.json @@ -0,0 +1,12 @@ +{ + "name": "@testrepo/namespace-casing", + "version": "0.0.1", + "keywords": [ + "rescript" + ], + "author": "", + "license": "MIT", + "dependencies": { + "rescript": "*" + } +} diff --git a/testrepo/packages/namespace-casing/src/Consume.mjs b/testrepo/packages/namespace-casing/src/Consume.mjs new file mode 100644 index 0000000..c89a31a --- /dev/null +++ b/testrepo/packages/namespace-casing/src/Consume.mjs @@ -0,0 +1,10 @@ +// Generated by ReScript, PLEASE EDIT WITH CARE + +import * as Produce$NamespaceCasingAPI from "./Produce.mjs"; + +var x = Produce$NamespaceCasingAPI.meh(1); + +export { + x , +} +/* x Not a pure module */ diff --git a/testrepo/packages/namespace-casing/src/Consume.res b/testrepo/packages/namespace-casing/src/Consume.res new file mode 100644 index 0000000..e2d9a26 --- /dev/null +++ b/testrepo/packages/namespace-casing/src/Consume.res @@ -0,0 +1 @@ +let x = Produce.meh(1) \ No newline at end of file diff --git a/testrepo/packages/namespace-casing/src/Produce.mjs b/testrepo/packages/namespace-casing/src/Produce.mjs new file mode 100644 index 0000000..78a43f1 --- /dev/null +++ b/testrepo/packages/namespace-casing/src/Produce.mjs @@ -0,0 +1,11 @@ +// Generated by ReScript, PLEASE EDIT WITH CARE + + +function meh(a) { + return true; +} + +export { + meh , +} +/* No side effect */ diff --git a/testrepo/packages/namespace-casing/src/Produce.res b/testrepo/packages/namespace-casing/src/Produce.res new file mode 100644 index 0000000..cba1a66 --- /dev/null +++ b/testrepo/packages/namespace-casing/src/Produce.res @@ -0,0 +1,3 @@ +let meh = (a: int) => { + true +} \ No newline at end of file diff --git a/tests/snapshots/dependency-cycle.txt b/tests/snapshots/dependency-cycle.txt index b30824a..a6a30ad 100644 --- a/tests/snapshots/dependency-cycle.txt +++ b/tests/snapshots/dependency-cycle.txt @@ -8,7 +8,7 @@  [4/7] 🧹 Cleaned 0/11 0.00s  [5/7] 🧱 Parsed 1 source files in 0.00s  [6/7] ️🌴 Collected deps in 0.00s - [7/7] ️🛑 Compiled 0 modules in 0.00s + [7/7] ️🛑 Compiled 1 modules in 0.00s ERROR: Can't continue... Found a circular dependency in your code: diff --git a/tests/snapshots/remove-file.txt b/tests/snapshots/remove-file.txt index 344d8d3..b363781 100644 --- a/tests/snapshots/remove-file.txt +++ b/tests/snapshots/remove-file.txt @@ -8,7 +8,7 @@  [4/7] 🧹 Cleaned 1/11 0.00s  [5/7] 🧱 Parsed 0 source files in 0.00s  [6/7] ️🌴 Collected deps in 0.00s - [7/7] ️🛑 Compiled 1 modules in 0.00s + [7/7] ️🛑 Compiled 2 modules in 0.00s ERROR: We've found a bug for you! diff --git a/tests/snapshots/rename-file-with-interface.txt b/tests/snapshots/rename-file-with-interface.txt index 9927321..6688b6e 100644 --- a/tests/snapshots/rename-file-with-interface.txt +++ b/tests/snapshots/rename-file-with-interface.txt @@ -10,6 +10,6 @@ WARN:  [4/7] 🧹 Cleaned 2/11 0.00s  [5/7] 🧱 Parsed 1 source files in 0.00s  [6/7] ️🌴 Collected deps in 0.00s - [7/7] ⚔️ Compiled 1 modules in 0.00s + [7/7] ⚔️ Compiled 2 modules in 0.00s  ✨ Finished Compilation in 0.00s diff --git a/tests/snapshots/rename-interface-file.txt b/tests/snapshots/rename-interface-file.txt index 50dcd91..a292ef6 100644 --- a/tests/snapshots/rename-interface-file.txt +++ b/tests/snapshots/rename-interface-file.txt @@ -10,6 +10,6 @@ WARN:  [4/7] 🧹 Cleaned 1/11 0.00s  [5/7] 🧱 Parsed 1 source files in 0.00s  [6/7] ️🌴 Collected deps in 0.00s - [7/7] ⚔️ Compiled 1 modules in 0.00s + [7/7] ⚔️ Compiled 2 modules in 0.00s  ✨ Finished Compilation in 0.00s