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 in namespace is not respected #144

Open
nojaf opened this issue Nov 20, 2024 · 2 comments · May be fixed by #145
Open

Casing in namespace is not respected #144

nojaf opened this issue Nov 20, 2024 · 2 comments · May be fixed by #145
Assignees

Comments

@nojaf
Copy link

nojaf commented Nov 20, 2024

In https://github.com/rescript-lang/experimental-rescript-webapi I noticed that bunx rewatch has slightly different output than bunx rescript.

Steps to reproduce:

  • Create new project with [email protected]
  • Add "namespace": "PkmnAPI", to rescript.json
  • Create a function in one file and use it in another

ProduceAPI.res

let meh = (a: int) => {
  true
}

Consume.res

let b  = ProduceAPI.meh(1);

After bunx rescript I get:

// Generated by ReScript, PLEASE EDIT WITH CARE

import * as ProduceAPI$PkmnAPI from "./ProduceAPI.re.js";

let b = ProduceAPI$PkmnAPI.meh(1);

export {
  b,
}
/* b Not a pure module */

with bunx rewatch:

// Generated by ReScript, PLEASE EDIT WITH CARE

import * as ProduceAPI$PkmnApi from "./ProduceAPI.re.js";

let b = ProduceAPI$PkmnApi.meh(1);

export {
  b,
}
/* b Not a pure module */

ProduceAPI$PkmnAPI (rescript) versus ProduceAPI$PkmnApi (rewatch)

@rolandpeelen
Copy link
Member

@jfrolich -- I think you did some work on namespacing. Care to have a look?

@tsnobip
Copy link
Contributor

tsnobip commented Nov 29, 2024

here is the culprit:

namespace => packages::Namespace::Namespace(namespace.to_string().to_case(Case::Pascal)),

while bsb doesn't actually convert to Pascal case but use a custom algorithm:

let namespace_of_package_name (s : string) : string =
  let len = String.length s in
  let buf = Ext_buffer.create len in
  let add capital ch =
    Ext_buffer.add_char buf (if capital then Char.uppercase_ascii ch else ch)
  in
  let rec aux capital off len =
    if off >= len then ()
    else
      let ch = String.unsafe_get s off in
      match ch with
      | 'a' .. 'z' | 'A' .. 'Z' | '0' .. '9' | '_' ->
        add capital ch;
        aux false (off + 1) len
      | '/' | '-' -> aux true (off + 1) len
      | _ -> aux capital (off + 1) len
  in
  aux true 0 len;
  Ext_buffer.contents buf

IMO it's a bit unfortunate that all this mangling happens for namespace, but if we don't want to introduce a breaking change we should likely make rewatch implement the same algorithm.

@nojaf nojaf linked a pull request Nov 29, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants