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

Casing namespace #145

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
33 changes: 27 additions & 6 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,11 +287,32 @@ fn check_if_rescript11_or_higher(version: &str) -> Result<bool, String> {
}

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 {
Expand All @@ -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)),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is another use of to_case(Case::Pascal) on line 348, but actually I think you can simplify this match quite a lot and always process namespace whatever the initial case with this new function.

},
(Some(self::NamespaceConfig::String(str)), Some(entry)) => match str.as_str() {
"true" => packages::Namespace::NamespaceWithEntry {
Expand Down
3 changes: 2 additions & 1 deletion testrepo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"packages/main",
"packages/dep01",
"packages/dep02",
"packages/new-namespace"
"packages/new-namespace",
"packages/namespace-casing"
]
},
"scripts": {
Expand Down
22 changes: 22 additions & 0 deletions testrepo/packages/namespace-casing/bsconfig.json
Original file line number Diff line number Diff line change
@@ -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
}
}
12 changes: 12 additions & 0 deletions testrepo/packages/namespace-casing/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "@testrepo/namespace-casing",
"version": "0.0.1",
"keywords": [
"rescript"
],
"author": "",
"license": "MIT",
"dependencies": {
"rescript": "*"
}
}
10 changes: 10 additions & 0 deletions testrepo/packages/namespace-casing/src/Consume.mjs
Original file line number Diff line number Diff line change
@@ -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 */
1 change: 1 addition & 0 deletions testrepo/packages/namespace-casing/src/Consume.res
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
let x = Produce.meh(1)
11 changes: 11 additions & 0 deletions testrepo/packages/namespace-casing/src/Produce.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Generated by ReScript, PLEASE EDIT WITH CARE


function meh(a) {
return true;
}

export {
meh ,
}
/* No side effect */
3 changes: 3 additions & 0 deletions testrepo/packages/namespace-casing/src/Produce.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
let meh = (a: int) => {
true
}
2 changes: 1 addition & 1 deletion tests/snapshots/dependency-cycle.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion tests/snapshots/remove-file.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand Down
2 changes: 1 addition & 1 deletion tests/snapshots/rename-file-with-interface.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/snapshots/rename-interface-file.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading