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

rescript: use Core instead of Js #388

Merged
merged 1 commit into from
Feb 27, 2024
Merged
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
2 changes: 2 additions & 0 deletions dist/res/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@
"author": "",
"license": "Apache-2.0",
"devDependencies": {
"@rescript/core": "^1.1.0",
"rescript": "11.0.1"
},
"peerDependencies": {
"@rescript/core": "1.1.0",
"rescript": "^11.0.1"
}
}
1 change: 1 addition & 0 deletions dist/res/rescript.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
},
"suffix": ".bs.js",
"bs-dependencies": [
"@rescript/core"
],
"warnings": {
"error" : "+101"
Expand Down
318 changes: 207 additions & 111 deletions dist/res/src/ts2ocaml.res

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions dist/res/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
# yarn lockfile v1


"@rescript/core@^1.1.0":
version "1.1.0"
resolved "https://registry.yarnpkg.com/@rescript/core/-/core-1.1.0.tgz#e228a2635665b6b6a3288f7fd58552d5c361e253"
integrity sha512-pz/CL8+9hBUTeMpUouvZohNsa5rqIwurlXoa1CZWN0ZKuWjMVjaoQ3V+0NB72J/QBbs6/8W82VABKBaDn3fGCA==

[email protected]:
version "11.0.1"
resolved "https://registry.yarnpkg.com/rescript/-/rescript-11.0.1.tgz#c74af134dc8a16d152169b2456d0720324835f54"
Expand Down
18 changes: 16 additions & 2 deletions docs/rescript.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
> **Note:** ReScript support is currently available in `2.0.0-beta.0` or later.
>
>
> Please install it with `npm install -g @ocsigen/ts2ocaml@beta`.

# ts2ocaml for ReScript
Expand All @@ -18,6 +18,20 @@ The documentation for the `ts2ocaml` command and its options comes after the wal

`ts2ocaml` targets ReScript v11 or later.

Also, `ts2ocaml` depends on ReScript's new standard library `Core`.
You may need to add `@rescript/core` to your project dependencies (`dependencies` in `package.json` and `bs-dependencies` in `rescript.json`).
Please see [ReScript's official documentation](https://rescript-lang.org/docs/manual/latest/api) for details.

> Every code generated by `ts2ocaml` explicitly opens `RescriptCore`, so you don't have to set
>
> ```json
> "bsc-flags": [
> "-open RescriptCore"
> ]
> ```
>
> in your `rescript.json`.

## Adding `ts2ocaml.res`

ReScript has a rich standard library to use JS and DOM APIs and `ts2ocaml` makes use of it as much as possible. In addition to that, `ts2ocaml` uses a small standard library to handle some TypeScript-specific concepts:
Expand Down Expand Up @@ -82,7 +96,7 @@ let result = Ts.transpileModule(
),
)

Js.log(result->Ts.TranspileOutput.get_outputText)
Console.log(result->Ts.TranspileOutput.get_outputText)
```

# Conventions
Expand Down
22 changes: 12 additions & 10 deletions src/Targets/ReScript/ReScriptHelper.fs
Original file line number Diff line number Diff line change
Expand Up @@ -220,16 +220,18 @@ module Type =
/// non-primitive types defined in the standard library
let predefinedTypes =
let builtins = [
"RegExp", ("Re.t", 0)
"RegExp", ("RegExp.t", 0)
"PromiseLike", ("Promise.t", 1)
"Array", ("array", 1)
"ArrayLike", ("Array2.array_like", 1)
"ArrayLike", ("Js.Array2.array_like", 1)
"Iterable", ("Js.Array2.array_like", 1)
"ReadonlyArray", ("array", 1)
"ArrayBuffer", ("TypedArray2.ArrayBuffer.t", 0)
"Error", ("Exn.t", 0)
"ArrayBuffer", ("ArrayBuffer.t", 0)
"Error", ("Error.t", 0)
"ReadonlyMap", ("Map.t", 2)
]
let typedArrays =
let typedArray name = name, (sprintf "TypedArray2.%s.t" name, 0)
let typedArray name = name, (sprintf "%s.t" name, 0)
[
typedArray "DataView"
typedArray "Int8Array"
Expand Down Expand Up @@ -330,12 +332,12 @@ module Type =

// JS types
// ES5
let object = str "untypedObject"
let function_ = str "untypedFunction"
let symbol = str "symbol"
let regexp = str "Re.t"
let object = str "object"
let function_ = str "function"
let symbol = str "Symbol.t"
let regexp = str "RegExp.t"
// ES2020
let bigint = str "Bigint.t"
let bigint = str "BigInt.t"

// TS types
let never = str "never"
Expand Down
2 changes: 1 addition & 1 deletion src/Targets/ReScript/Writer.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2102,7 +2102,7 @@ let private emitImpl (sources: SourceFile list) (info: PackageInfo option) (ctx:
let m = emitModule dt flags ctx st

let opens = [
yield Statement.open_ "Js"
yield Statement.open_ "RescriptCore"
yield Statement.open_ "Ts2ocaml"
for src in sources do
yield! emitReferenceTypeDirectives ctx src
Expand Down
1 change: 1 addition & 0 deletions test/res/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"@ocsigen/ts2ocaml": "link:../../"
},
"dependencies": {
"@rescript/core": "^1.1.0",
"rescript": "11.0.1"
}
}
3 changes: 3 additions & 0 deletions test/res/rescript.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,8 @@
"in-source": true
},
"suffix": ".bs.js",
"bs-dependencies": [
"@rescript/core"
],
"uncurried": true
}
4 changes: 3 additions & 1 deletion test/res/src/main.res
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
open RescriptCore

module Ts = Typescript.Export

let source = "let x: string = 'hello, world!'"
Expand All @@ -9,4 +11,4 @@ let result = Ts.transpileModule(
),
)

Js.log(result->Ts.TranspileOutput.get_outputText)
Console.log(result->Ts.TranspileOutput.get_outputText)
1 change: 0 additions & 1 deletion test/res/src/placeholders/Iterable.res

This file was deleted.

1 change: 0 additions & 1 deletion test/res/src/placeholders/Map.res

This file was deleted.

1 change: 0 additions & 1 deletion test/res/src/placeholders/ReadonlyMap.res

This file was deleted.

1 change: 0 additions & 1 deletion test/res/src/placeholders/Set.res

This file was deleted.

1 change: 0 additions & 1 deletion test/res/src/placeholders/WeakMap.res

This file was deleted.

1 change: 0 additions & 1 deletion test/res/src/placeholders/WeakSet.res

This file was deleted.

5 changes: 5 additions & 0 deletions test/res/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@
version "0.0.0"
uid ""

"@rescript/core@^1.1.0":
version "1.1.0"
resolved "https://registry.yarnpkg.com/@rescript/core/-/core-1.1.0.tgz#e228a2635665b6b6a3288f7fd58552d5c361e253"
integrity sha512-pz/CL8+9hBUTeMpUouvZohNsa5rqIwurlXoa1CZWN0ZKuWjMVjaoQ3V+0NB72J/QBbs6/8W82VABKBaDn3fGCA==

ansi-regex@^5.0.1:
version "5.0.1"
resolved "https://registry.yarnpkg.com/ansi-regex/-/ansi-regex-5.0.1.tgz#082cb2c89c9fe8659a311a53bd6a4dc5301db304"
Expand Down
Loading