Skip to content

Commit 1a9e894

Browse files
authored
Casing namespace (#145)
1 parent 464dff4 commit 1a9e894

19 files changed

+105
-20
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,6 @@
1616
"/rewatch-linux-arm64",
1717
"/rewatch-macos",
1818
"/rewatch-windows.exe"
19-
]
19+
],
20+
"packageManager": "[email protected]+sha512.a6b2f7906b721bba3d67d4aff083df04dad64c399707841b7acf00f6b133b7ac24255f2652fa22ae3534329dc6180534e98d17432037ff6fd140556e2bb3137e"
2021
}

src/config.rs

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -287,11 +287,32 @@ fn check_if_rescript11_or_higher(version: &str) -> Result<bool, String> {
287287
}
288288

289289
fn namespace_from_package_name(package_name: &str) -> String {
290-
package_name
291-
.to_owned()
292-
.replace('@', "")
293-
.replace('/', "_")
294-
.to_case(Case::Pascal)
290+
let len = package_name.len();
291+
let mut buf = String::with_capacity(len);
292+
293+
fn aux(s: &str, capital: bool, buf: &mut String, off: usize) {
294+
if off >= s.len() {
295+
return;
296+
}
297+
298+
let ch = s.as_bytes()[off] as char;
299+
match ch {
300+
'a'..='z' | 'A'..='Z' | '0'..='9' | '_' => {
301+
let new_capital = false;
302+
buf.push(if capital { ch.to_ascii_uppercase() } else { ch });
303+
aux(s, new_capital, buf, off + 1);
304+
}
305+
'/' | '-' => {
306+
aux(s, true, buf, off + 1);
307+
}
308+
_ => {
309+
aux(s, capital, buf, off + 1);
310+
}
311+
}
312+
}
313+
314+
aux(package_name, true, &mut buf, 0);
315+
buf
295316
}
296317

297318
impl Config {
@@ -312,7 +333,7 @@ impl Config {
312333
namespace if namespace.is_case(Case::UpperFlat) => {
313334
packages::Namespace::Namespace(namespace.to_string())
314335
}
315-
namespace => packages::Namespace::Namespace(namespace.to_string().to_case(Case::Pascal)),
336+
namespace => packages::Namespace::Namespace(namespace_from_package_name(namespace)),
316337
},
317338
(Some(self::NamespaceConfig::String(str)), Some(entry)) => match str.as_str() {
318339
"true" => packages::Namespace::NamespaceWithEntry {

testrepo/bsconfig.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,16 @@
1818
"@testrepo/main",
1919
"@testrepo/dep01",
2020
"@testrepo/dep02",
21-
"@testrepo/new-namespace"
21+
"@testrepo/new-namespace",
22+
"@testrepo/namespace-casing"
23+
2224
],
2325
"bs-dependencies": [
2426
"@testrepo/main",
2527
"@testrepo/dep01",
2628
"@testrepo/dep02",
27-
"@testrepo/new-namespace"
29+
"@testrepo/new-namespace",
30+
"@testrepo/namespace-casing"
2831
],
2932
"reason": {
3033
"react-jsx": 3

testrepo/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
"packages/main",
77
"packages/dep01",
88
"packages/dep02",
9-
"packages/new-namespace"
9+
"packages/new-namespace",
10+
"packages/namespace-casing"
1011
]
1112
},
1213
"scripts": {
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "@testrepo/namespace-casing",
3+
"namespace": "NamespaceCasingAPI",
4+
"sources": [
5+
{
6+
"dir": "src",
7+
"subdirs": true
8+
}
9+
],
10+
"package-specs": [
11+
{
12+
"module": "es6",
13+
"in-source": true
14+
}
15+
],
16+
"suffix": ".mjs",
17+
"bs-dependencies": [],
18+
"bsc-flags": [],
19+
"jsx": {
20+
"version": 4
21+
}
22+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"name": "@testrepo/namespace-casing",
3+
"version": "0.0.1",
4+
"keywords": [
5+
"rescript"
6+
],
7+
"author": "",
8+
"license": "MIT",
9+
"dependencies": {
10+
"rescript": "*"
11+
}
12+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Generated by ReScript, PLEASE EDIT WITH CARE
2+
3+
import * as Produce$NamespaceCasingAPI from "./Produce.mjs";
4+
5+
var x = Produce$NamespaceCasingAPI.meh(1);
6+
7+
export {
8+
x ,
9+
}
10+
/* x Not a pure module */
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
let x = Produce.meh(1)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Generated by ReScript, PLEASE EDIT WITH CARE
2+
3+
4+
function meh(param) {
5+
return true;
6+
}
7+
8+
export {
9+
meh ,
10+
}
11+
/* No side effect */
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
let meh = (_: int) => {
2+
true
3+
}

0 commit comments

Comments
 (0)