From 0e036c0217647286d8e0f452cdb1c7d9c4649d34 Mon Sep 17 00:00:00 2001 From: cannorin Date: Tue, 27 Feb 2024 20:24:34 +0900 Subject: [PATCH] rescript: use Core instead of Js --- dist/res/package.json | 2 + dist/res/rescript.json | 1 + dist/res/src/ts2ocaml.res | 318 ++++++++++++++-------- dist/res/yarn.lock | 5 + docs/rescript.md | 18 +- src/Targets/ReScript/ReScriptHelper.fs | 22 +- src/Targets/ReScript/Writer.fs | 2 +- test/res/package.json | 1 + test/res/rescript.json | 3 + test/res/src/main.res | 4 +- test/res/src/placeholders/Iterable.res | 1 - test/res/src/placeholders/Map.res | 1 - test/res/src/placeholders/ReadonlyMap.res | 1 - test/res/src/placeholders/Set.res | 1 - test/res/src/placeholders/WeakMap.res | 1 - test/res/src/placeholders/WeakSet.res | 1 - test/res/yarn.lock | 5 + 17 files changed, 256 insertions(+), 131 deletions(-) delete mode 100644 test/res/src/placeholders/Iterable.res delete mode 100644 test/res/src/placeholders/Map.res delete mode 100644 test/res/src/placeholders/ReadonlyMap.res delete mode 100644 test/res/src/placeholders/Set.res delete mode 100644 test/res/src/placeholders/WeakMap.res delete mode 100644 test/res/src/placeholders/WeakSet.res diff --git a/dist/res/package.json b/dist/res/package.json index 99faf0a8..f914d73b 100644 --- a/dist/res/package.json +++ b/dist/res/package.json @@ -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" } } diff --git a/dist/res/rescript.json b/dist/res/rescript.json index 50c249ca..3b7609bc 100644 --- a/dist/res/rescript.json +++ b/dist/res/rescript.json @@ -11,6 +11,7 @@ }, "suffix": ".bs.js", "bs-dependencies": [ + "@rescript/core" ], "warnings": { "error" : "+101" diff --git a/dist/res/src/ts2ocaml.res b/dist/res/src/ts2ocaml.res index ce3c7ae6..9721b3c8 100644 --- a/dist/res/src/ts2ocaml.res +++ b/dist/res/src/ts2ocaml.res @@ -1,87 +1,112 @@ @@warning("-27") +open RescriptCore -@unboxed type never = { absurd : 'a. 'a } +@unboxed type never = {absurd: 'a. 'a} module Never = { type t = never - external absurd : t => 'a = "%identity" + external absurd: t => 'a = "%identity" } @unboxed type rec any = Any('a): any module Any = { type t = any - external upcast : 'a => t = "%identity" - external unsafeCast : t => 'a = "%identity" + external upcast: 'a => t = "%identity" + external unsafeCast: t => 'a = "%identity" } @unboxed type rec unknown = Unknown('a): unknown module Unknown = { type t = unknown - external upcast : 'a => t = "%identity" - external unsafeCast : t => 'a = "%identity" + external upcast: 'a => t = "%identity" + external unsafeCast: t => 'a = "%identity" } type true_ = bool type false_ = bool -type symbol = Js.Types.symbol type intrinsic = private string -type untypedObject = any -type untypedFunction = any +type object = Type.Classify.object +type function = Type.Classify.function module Union = { type container<+'cases> - external return1 : 't1 => container<[> #U1('t1)]> = "%identity" - external return2 : 't2 => container<[> #U2('t2)]> = "%identity" - external return3 : 't3 => container<[> #U3('t3)]> = "%identity" - external return4 : 't4 => container<[> #U4('t4)]> = "%identity" - external return5 : 't5 => container<[> #U5('t5)]> = "%identity" - external return6 : 't6 => container<[> #U6('t6)]> = "%identity" - external return7 : 't7 => container<[> #U7('t7)]> = "%identity" - external return8 : 't8 => container<[> #U8('t8)]> = "%identity" - - external getUnsafe1 : container<[> #U1('t1)]> => 't1 = "%identity" - external getUnsafe2 : container<[> #U2('t2)]> => 't2 = "%identity" - external getUnsafe3 : container<[> #U3('t3)]> => 't3 = "%identity" - external getUnsafe4 : container<[> #U4('t4)]> => 't4 = "%identity" - external getUnsafe5 : container<[> #U5('t5)]> => 't5 = "%identity" - external getUnsafe6 : container<[> #U6('t6)]> => 't6 = "%identity" - external getUnsafe7 : container<[> #U7('t7)]> => 't7 = "%identity" - external getUnsafe8 : container<[> #U8('t8)]> => 't8 = "%identity" - - type t2<'t1, 't2> = container<[ #U1('t1) | #U2('t2) ]> - type t3<'t1, 't2, 't3> = container<[ #U1('t1) | #U2('t2) | #U3('t3) ]> - type t4<'t1, 't2, 't3, 't4> = container<[ #U1('t1) | #U2('t2) | #U3('t3) | #U4('t4) ]> - type t5<'t1, 't2, 't3, 't4, 't5> = container<[ #U1('t1) | #U2('t2) | #U3('t3) | #U4('t4) | #U5('t5) ]> - type t6<'t1, 't2, 't3, 't4, 't5, 't6> = container<[ #U1('t1) | #U2('t2) | #U3('t3) | #U4('t4) | #U5('t5) | #U6('t6) ]> - type t7<'t1, 't2, 't3, 't4, 't5, 't6, 't7> = container<[ #U1('t1) | #U2('t2) | #U3('t3) | #U4('t4) | #U5('t5) | #U6('t6) | #U7('t7) ]> - type t8<'t1, 't2, 't3, 't4, 't5, 't6, 't7, 't8> = container<[ #U1('t1) | #U2('t2) | #U3('t3) | #U4('t4) | #U5('t5) | #U6('t6) | #U7('t7) | #U8('t8) ]> + external return1: 't1 => container<[> #U1('t1)]> = "%identity" + external return2: 't2 => container<[> #U2('t2)]> = "%identity" + external return3: 't3 => container<[> #U3('t3)]> = "%identity" + external return4: 't4 => container<[> #U4('t4)]> = "%identity" + external return5: 't5 => container<[> #U5('t5)]> = "%identity" + external return6: 't6 => container<[> #U6('t6)]> = "%identity" + external return7: 't7 => container<[> #U7('t7)]> = "%identity" + external return8: 't8 => container<[> #U8('t8)]> = "%identity" + + external getUnsafe1: container<[> #U1('t1)]> => 't1 = "%identity" + external getUnsafe2: container<[> #U2('t2)]> => 't2 = "%identity" + external getUnsafe3: container<[> #U3('t3)]> => 't3 = "%identity" + external getUnsafe4: container<[> #U4('t4)]> => 't4 = "%identity" + external getUnsafe5: container<[> #U5('t5)]> => 't5 = "%identity" + external getUnsafe6: container<[> #U6('t6)]> => 't6 = "%identity" + external getUnsafe7: container<[> #U7('t7)]> => 't7 = "%identity" + external getUnsafe8: container<[> #U8('t8)]> => 't8 = "%identity" + + type t2<'t1, 't2> = container<[#U1('t1) | #U2('t2)]> + type t3<'t1, 't2, 't3> = container<[#U1('t1) | #U2('t2) | #U3('t3)]> + type t4<'t1, 't2, 't3, 't4> = container<[#U1('t1) | #U2('t2) | #U3('t3) | #U4('t4)]> + type t5<'t1, 't2, 't3, 't4, 't5> = container< + [#U1('t1) | #U2('t2) | #U3('t3) | #U4('t4) | #U5('t5)], + > + type t6<'t1, 't2, 't3, 't4, 't5, 't6> = container< + [#U1('t1) | #U2('t2) | #U3('t3) | #U4('t4) | #U5('t5) | #U6('t6)], + > + type t7<'t1, 't2, 't3, 't4, 't5, 't6, 't7> = container< + [#U1('t1) | #U2('t2) | #U3('t3) | #U4('t4) | #U5('t5) | #U6('t6) | #U7('t7)], + > + type t8<'t1, 't2, 't3, 't4, 't5, 't6, 't7, 't8> = container< + [#U1('t1) | #U2('t2) | #U3('t3) | #U4('t4) | #U5('t5) | #U6('t6) | #U7('t7) | #U8('t8)], + > } module Intersection = { type container<-'cases> - external get1 : container<[> #I1('t1)]> => 't1 = "%identity" - external get2 : container<[> #I2('t2)]> => 't2 = "%identity" - external get3 : container<[> #I3('t3)]> => 't3 = "%identity" - external get4 : container<[> #I4('t4)]> => 't4 = "%identity" - external get5 : container<[> #I5('t5)]> => 't5 = "%identity" - external get6 : container<[> #I6('t6)]> => 't6 = "%identity" - external get7 : container<[> #I7('t7)]> => 't7 = "%identity" - external get8 : container<[> #I8('t8)]> => 't8 = "%identity" - - type t2<'t1, 't2> = container<[ #I1('t1) | #I2('t2) ]> - type t3<'t1, 't2, 't3> = container<[ #I1('t1) | #I2('t2) | #I3('t3) ]> - type t4<'t1, 't2, 't3, 't4> = container<[ #I1('t1) | #I2('t2) | #I3('t3) | #I4('t4) ]> - type t5<'t1, 't2, 't3, 't4, 't5> = container<[ #I1('t1) | #I2('t2) | #I3('t3) | #I4('t4) | #I5('t5) ]> - type t6<'t1, 't2, 't3, 't4, 't5, 't6> = container<[ #I1('t1) | #I2('t2) | #I3('t3) | #I4('t4) | #I5('t5) | #I6('t6) ]> - type t7<'t1, 't2, 't3, 't4, 't5, 't6, 't7> = container<[ #I1('t1) | #I2('t2) | #I3('t3) | #I4('t4) | #I5('t5) | #I6('t6) | #I7('t7) ]> - type t8<'t1, 't2, 't3, 't4, 't5, 't6, 't7, 't8> = container<[ #I1('t1) | #I2('t2) | #I3('t3) | #I4('t4) | #I5('t5) | #I6('t6) | #I7('t7) | #I8('t8) ]> + external get1: container<[> #I1('t1)]> => 't1 = "%identity" + external get2: container<[> #I2('t2)]> => 't2 = "%identity" + external get3: container<[> #I3('t3)]> => 't3 = "%identity" + external get4: container<[> #I4('t4)]> => 't4 = "%identity" + external get5: container<[> #I5('t5)]> => 't5 = "%identity" + external get6: container<[> #I6('t6)]> => 't6 = "%identity" + external get7: container<[> #I7('t7)]> => 't7 = "%identity" + external get8: container<[> #I8('t8)]> => 't8 = "%identity" + + type t2<'t1, 't2> = container<[#I1('t1) | #I2('t2)]> + type t3<'t1, 't2, 't3> = container<[#I1('t1) | #I2('t2) | #I3('t3)]> + type t4<'t1, 't2, 't3, 't4> = container<[#I1('t1) | #I2('t2) | #I3('t3) | #I4('t4)]> + type t5<'t1, 't2, 't3, 't4, 't5> = container< + [#I1('t1) | #I2('t2) | #I3('t3) | #I4('t4) | #I5('t5)], + > + type t6<'t1, 't2, 't3, 't4, 't5, 't6> = container< + [#I1('t1) | #I2('t2) | #I3('t3) | #I4('t4) | #I5('t5) | #I6('t6)], + > + type t7<'t1, 't2, 't3, 't4, 't5, 't6, 't7> = container< + [#I1('t1) | #I2('t2) | #I3('t3) | #I4('t4) | #I5('t5) | #I6('t6) | #I7('t7)], + > + type t8<'t1, 't2, 't3, 't4, 't5, 't6, 't7, 't8> = container< + [#I1('t1) | #I2('t2) | #I3('t3) | #I4('t4) | #I5('t5) | #I6('t6) | #I7('t7) | #I8('t8)], + > } type intf<-'tags> module Primitive = { - type cases<'other> = [ #Null | #Undefined | #String(string) | #Number(float) | #Boolean(bool) | #Symbol(symbol) | #Bigint(Js.Bigint.t) | #Other('other) ] + type cases<'other> = [ + | #Null + | #Undefined + | #String(string) + | #Number(float) + | #Boolean(bool) + | #Symbol(Symbol.t) + | #BigInt(BigInt.t) + | #Other('other) + ] type t<+'cases> let return: ([< cases<'other>] as 'cases) => t<'cases> = x => @@ -98,30 +123,29 @@ module Primitive = { external string: string => t<[> #String(string)]> = "%identity" external number: float => t<[> #Number(float)]> = "%identity" external boolean: bool => t<[> #Boolean(bool)]> = "%identity" - external symbol: symbol => t<[> #Symbol(symbol)]> = "%identity" - external bigint: Js.Bigint.t => t<[> #Bigint(Js.Bigint.t)]> = "%identity" + external symbol: Symbol.t => t<[> #Symbol(Symbol.t)]> = "%identity" + external bigint: BigInt.t => t<[> #BigInt(BigInt.t)]> = "%identity" external other: 'a => t<[> #Other('a)]> = "%identity" - external fromNull: Js.null<'a> => t<[> #Null | #Other('a) ]> = "%identity" - external toNull: t<[< #Null | #Other('a) ]> => Js.null<'a> = "%identity" + external fromNull: null<'a> => t<[> #Null | #Other('a)]> = "%identity" + external toNull: t<[< #Null | #Other('a)]> => null<'a> = "%identity" - external fromUndefined: Js.undefined<'a> => t<[> #Undefined | #Other('a) ]> = "%identity" - external toUndefined: t<[< #Undefined | #Other('a) ]> => Js.undefined<'a> = "%identity" + external fromUndefined: undefined<'a> => t<[> #Undefined | #Other('a)]> = "%identity" + external toUndefined: t<[< #Undefined | #Other('a)]> => undefined<'a> = "%identity" - external fromNullable: Js.nullable<'a> => t<[> #Null | #Undefined | #Other('a) ]> = "%identity" - external toNullable: t<[< #Null | #Undefined | #Other('a) ]> => Js.nullable<'a> = "%identity" + external fromNullable: nullable<'a> => t<[> #Null | #Undefined | #Other('a)]> = "%identity" + external toNullable: t<[< #Null | #Undefined | #Other('a)]> => nullable<'a> = "%identity" let classify: t<[< cases<'other>] as 'cases> => 'cases = x => - switch (Js.typeof(x)) { - | "string" => Obj.magic(#String(Obj.magic(x))) - | "number" => Obj.magic(#Number(Obj.magic(x))) - | "boolean" => Obj.magic(#Boolean(Obj.magic(x))) - | "symbol" => Obj.magic(#Symbol(Obj.magic(x))) - | "bigint" => Obj.magic(#Bigint(Obj.magic(x))) - | "undefined" => Obj.magic(#Undefined) - | _ => - if (Js.testAny(x)) { Obj.magic(#Null) } - else { Obj.magic(#Other(x)) } + switch Type.Classify.classify(x) { + | Null => Obj.magic(#Null) + | Undefined => Obj.magic(#Undefined) + | Bool(b) => Obj.magic(#Boolean(b)) + | String(s) => Obj.magic(#String(s)) + | Number(n) => Obj.magic(#Number(n)) + | Symbol(s) => Obj.magic(#Symbol(s)) + | BigInt(i) => Obj.magic(#BigInt(i)) + | Object(_) | Function(_) => Obj.magic(#Other(Obj.magic(x))) } } @@ -132,9 +156,9 @@ module Newable = { @ocaml.doc(`\`'args\` must be a tuple type.`) type tn<'args, 't> - let apply0 = (f0: t0<'t>) : 't => %raw(`new f0()`) - let apply1 = (f1: t1<'arg1, 't>, arg1: 'arg1) : 't => %raw(`new f1(arg1)`) - let applyN = (fn: tn<'args, 't>, args: 'args) : 't => %raw(`new fn(...args)`) + let apply0 = (f0: t0<'t>): 't => %raw(`new f0()`) + let apply1 = (f1: t1<'arg1, 't>, arg1: 'arg1): 't => %raw(`new f1(arg1)`) + let applyN = (fn: tn<'args, 't>, args: 'args): 't => %raw(`new fn(...args)`) } module Variadic = { @@ -148,35 +172,65 @@ module Variadic = { type tn<'args, 'variadic, 't> @ocaml.doc(`\`'variadic\` is expected to be array or some other iterable type.`) - let make0 : ('variadic => 't) => t0<'variadic, 't> = f => %raw(`(function(...args) { return f(args); })`) + let make0: ('variadic => 't) => t0<'variadic, 't> = f => + %raw(`(function(...args) { return f(args); })`) @ocaml.doc(`\`'variadic\` is expected to be array or some other iterable type.`) - let make1 : (('arg1, 'variadic) => 't) => t1<'arg1, 'variadic, 't> = f => %raw(`(function(arg1, ...args) { return f(arg1, args); })`) + let make1: (('arg1, 'variadic) => 't) => t1<'arg1, 'variadic, 't> = f => + %raw(`(function(arg1, ...args) { return f(arg1, args); })`) @ocaml.doc(`\`'variadic\` is expected to be array or some other iterable type.`) - let make2 : (('arg1, 'arg2, 'variadic) => 't) => tn<('arg1, 'arg2), 'variadic, 't> = f => %raw(`(function(arg1, arg2, ...args) { return f(arg1, arg2, args); })`) + let make2: (('arg1, 'arg2, 'variadic) => 't) => tn<('arg1, 'arg2), 'variadic, 't> = f => + %raw(`(function(arg1, arg2, ...args) { return f(arg1, arg2, args); })`) @ocaml.doc(`\`'variadic\` is expected to be array or some other iterable type.`) - let make3 : (('arg1, 'arg2, 'arg3, 'variadic) => 't) => tn<('arg1, 'arg2, 'arg3), 'variadic, 't> = f => %raw(`(function(arg1, arg2, arg3, ...args) { return f(arg1, arg2, arg3, args); })`) + let make3: (('arg1, 'arg2, 'arg3, 'variadic) => 't) => tn< + ('arg1, 'arg2, 'arg3), + 'variadic, + 't, + > = f => %raw(`(function(arg1, arg2, arg3, ...args) { return f(arg1, arg2, arg3, args); })`) @ocaml.doc(`\`'variadic\` is expected to be array or some other iterable type.`) - let make4 : (('arg1, 'arg2, 'arg3, 'arg4, 'variadic) => 't) => tn<('arg1, 'arg2, 'arg3, 'arg4), 'variadic, 't> = f => %raw(`(function(arg1, arg2, arg3, arg4, ...args) { return f(arg1, arg2, arg3, arg4, args); })`) + let make4: (('arg1, 'arg2, 'arg3, 'arg4, 'variadic) => 't) => tn< + ('arg1, 'arg2, 'arg3, 'arg4), + 'variadic, + 't, + > = f => + %raw(`(function(arg1, arg2, arg3, arg4, ...args) { return f(arg1, arg2, arg3, arg4, args); })`) @ocaml.doc(`\`'variadic\` is expected to be array or some other iterable type.`) - let make5 : (('arg1, 'arg2, 'arg3, 'arg4, 'arg5, 'variadic) => 't) => tn<('arg1, 'arg2, 'arg3, 'arg4, 'arg5), 'variadic, 't> = f => %raw(`(function(arg1, arg2, arg3, arg4, arg5, ...args) { return f(arg1, arg2, arg3, arg4, arg5, args); })`) + let make5: (('arg1, 'arg2, 'arg3, 'arg4, 'arg5, 'variadic) => 't) => tn< + ('arg1, 'arg2, 'arg3, 'arg4, 'arg5), + 'variadic, + 't, + > = f => + %raw(`(function(arg1, arg2, arg3, arg4, arg5, ...args) { return f(arg1, arg2, arg3, arg4, arg5, args); })`) @ocaml.doc(`\`'variadic\` is expected to be array or some other iterable type.`) - let make6 : (('arg1, 'arg2, 'arg3, 'arg4, 'arg5, 'arg6, 'variadic) => 't) => tn<('arg1, 'arg2, 'arg3, 'arg4, 'arg5, 'arg6), 'variadic, 't> = f => %raw(`(function(arg1, arg2, arg3, arg4, arg5, arg6, ...args) { return f(arg1, arg2, arg3, arg4, arg5, arg6, args); })`) + let make6: (('arg1, 'arg2, 'arg3, 'arg4, 'arg5, 'arg6, 'variadic) => 't) => tn< + ('arg1, 'arg2, 'arg3, 'arg4, 'arg5, 'arg6), + 'variadic, + 't, + > = f => + %raw(`(function(arg1, arg2, arg3, arg4, arg5, arg6, ...args) { return f(arg1, arg2, arg3, arg4, arg5, arg6, args); })`) @ocaml.doc(`\`'variadic\` is expected to be array or some other iterable type.`) - let make7 : (('arg1, 'arg2, 'arg3, 'arg4, 'arg5, 'arg6, 'arg7, 'variadic) => 't) => tn<('arg1, 'arg2, 'arg3, 'arg4, 'arg5, 'arg6, 'arg7), 'variadic, 't> = f => %raw(`(function(arg1, arg2, arg3, arg4, arg5, arg6, arg7, ...args) { return f(arg1, arg2, arg3, arg4, arg5, arg6, arg7, args); })`) + let make7: (('arg1, 'arg2, 'arg3, 'arg4, 'arg5, 'arg6, 'arg7, 'variadic) => 't) => tn< + ('arg1, 'arg2, 'arg3, 'arg4, 'arg5, 'arg6, 'arg7), + 'variadic, + 't, + > = f => + %raw(`(function(arg1, arg2, arg3, arg4, arg5, arg6, arg7, ...args) { return f(arg1, arg2, arg3, arg4, arg5, arg6, arg7, args); })`) @ocaml.doc(`\`'args\` must be a tuple type. \`'variadic\` is expected to be array or some other iterable type.`) - let makeN : (('args, 'variadic) => 't, int) => tn<'args, 'variadic, 't> = (f, n) => %raw(`(function(...args) { return f(args.slice(0, n), args.slice(n)); })`) - - let apply0 = (f0: t0<'variadic, 't>, variadic: 'variadic) : 't => %raw(`f0(...variadic)`) - let apply1 = (f1: t1<'arg1, 'variadic, 't>, arg1: 'arg1, variadic: 'variadic) : 't => %raw(`f1(arg1, ...variadic)`) - let applyN = (fn: tn<'args, 'variadic, 't>, args: 'args, variadic: 'variadic) : 't => %raw(`fn(...args, ...variadic)`) + let makeN: (('args, 'variadic) => 't, int) => tn<'args, 'variadic, 't> = (f, n) => + %raw(`(function(...args) { return f(args.slice(0, n), args.slice(n)); })`) + + let apply0 = (f0: t0<'variadic, 't>, variadic: 'variadic): 't => %raw(`f0(...variadic)`) + let apply1 = (f1: t1<'arg1, 'variadic, 't>, arg1: 'arg1, variadic: 'variadic): 't => + %raw(`f1(arg1, ...variadic)`) + let applyN = (fn: tn<'args, 'variadic, 't>, args: 'args, variadic: 'variadic): 't => + %raw(`fn(...args, ...variadic)`) } module NewableVariadic = { @@ -189,37 +243,79 @@ module NewableVariadic = { @ocaml.doc(`\`'args\` must be a tuple type. \`'variadic\` is expected to be array or some other iterable type.`) type tn<'args, 'variadic, 't> - let apply0 = (f0: t0<'variadic, 't>, variadic: 'variadic) : 't => %raw(`new f0(...variadic)`) - let apply1 = (f1: t1<'arg1, 'variadic, 't>, arg1: 'arg1, variadic: 'variadic) : 't => %raw(`new f1(arg1, ...variadic)`) - let applyN = (fn: tn<'args, 'variadic, 't>, args: 'args, variadic: 'variadic) : 't => %raw(`new fn(...args, ...variadic)`) + let apply0 = (f0: t0<'variadic, 't>, variadic: 'variadic): 't => %raw(`new f0(...variadic)`) + let apply1 = (f1: t1<'arg1, 'variadic, 't>, arg1: 'arg1, variadic: 'variadic): 't => + %raw(`new f1(arg1, ...variadic)`) + let applyN = (fn: tn<'args, 'variadic, 't>, args: 'args, variadic: 'variadic): 't => + %raw(`new fn(...args, ...variadic)`) } // utility type fallbacks -module Partial = { type t<'a> = 'a } -module Required = { type t<'a> = 'a } -module Readonly = { type t<'a> = 'a } -module Pick = { type t<'a, 'keys> = 'a } -module Record = { type t<'keys, 'a> = Js.Dict.t<'a> } -module Exclude = { type t<'a, 'b> = 'a } -module Extract = { type t<'a, 'b> = 'a } -module Omit = { type t<'a, 'keys> = 'a } -module NonNullable = { type t<'a> = 'a } -module Parameters = { type t<'a> } -module ConstructorParameters = { type t<'a> } -module ReturnType = { type t<'a> } -module InstanceType = { type t<'a> } -module ThisParameterType = { type t<'a> } -module OmitThisParameter = { type t<'a> } -module ThisType = { type t<'a> } -module Uppercase = { type t<'s> = private intrinsic } -module Lowercase = { type t<'s> = private intrinsic } -module Capitalize = { type t<'s> = private intrinsic } -module Uncapitalize = { type t<'s> = private intrinsic } +module Partial = { + type t<'a> = 'a +} +module Required = { + type t<'a> = 'a +} +module Readonly = { + type t<'a> = 'a +} +module Pick = { + type t<'a, 'keys> = 'a +} +module Record = { + type t<'keys, 'a> = Dict.t<'a> +} +module Exclude = { + type t<'a, 'b> = 'a +} +module Extract = { + type t<'a, 'b> = 'a +} +module Omit = { + type t<'a, 'keys> = 'a +} +module NonNullable = { + type t<'a> = 'a +} +module Parameters = { + type t<'a> +} +module ConstructorParameters = { + type t<'a> +} +module ReturnType = { + type t<'a> +} +module InstanceType = { + type t<'a> +} +module ThisParameterType = { + type t<'a> +} +module OmitThisParameter = { + type t<'a> +} +module ThisType = { + type t<'a> +} +module Uppercase = { + type t<'s> = private intrinsic +} +module Lowercase = { + type t<'s> = private intrinsic +} +module Capitalize = { + type t<'s> = private intrinsic +} +module Uncapitalize = { + type t<'s> = private intrinsic +} // utilities for experimental features module Experimental = { module Variant = { - let box = (it: 't, tag: string) : 'Variant => %raw(`{ [tag]: it[tag], _0: it }`) - let unbox = (it: 'Variant) : 't => %raw(`it._0`) + let box = (it: 't, tag: string): 'Variant => %raw(`{ [tag]: it[tag], _0: it }`) + let unbox = (it: 'Variant): 't => %raw(`it._0`) } -} \ No newline at end of file +} diff --git a/dist/res/yarn.lock b/dist/res/yarn.lock index 405d1f49..b7caec7a 100644 --- a/dist/res/yarn.lock +++ b/dist/res/yarn.lock @@ -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== + rescript@11.0.1: version "11.0.1" resolved "https://registry.yarnpkg.com/rescript/-/rescript-11.0.1.tgz#c74af134dc8a16d152169b2456d0720324835f54" diff --git a/docs/rescript.md b/docs/rescript.md index ec1ad6ec..63334f96 100644 --- a/docs/rescript.md +++ b/docs/rescript.md @@ -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 @@ -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: @@ -82,7 +96,7 @@ let result = Ts.transpileModule( ), ) -Js.log(result->Ts.TranspileOutput.get_outputText) +Console.log(result->Ts.TranspileOutput.get_outputText) ``` # Conventions diff --git a/src/Targets/ReScript/ReScriptHelper.fs b/src/Targets/ReScript/ReScriptHelper.fs index fe2ed436..6e94f489 100644 --- a/src/Targets/ReScript/ReScriptHelper.fs +++ b/src/Targets/ReScript/ReScriptHelper.fs @@ -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" @@ -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" diff --git a/src/Targets/ReScript/Writer.fs b/src/Targets/ReScript/Writer.fs index 8ecfcb64..ce3e7dff 100644 --- a/src/Targets/ReScript/Writer.fs +++ b/src/Targets/ReScript/Writer.fs @@ -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 diff --git a/test/res/package.json b/test/res/package.json index d0843ca5..d508aa9a 100644 --- a/test/res/package.json +++ b/test/res/package.json @@ -15,6 +15,7 @@ "@ocsigen/ts2ocaml": "link:../../" }, "dependencies": { + "@rescript/core": "^1.1.0", "rescript": "11.0.1" } } diff --git a/test/res/rescript.json b/test/res/rescript.json index 0dde58eb..62b8f631 100644 --- a/test/res/rescript.json +++ b/test/res/rescript.json @@ -10,5 +10,8 @@ "in-source": true }, "suffix": ".bs.js", + "bs-dependencies": [ + "@rescript/core" + ], "uncurried": true } diff --git a/test/res/src/main.res b/test/res/src/main.res index 11aa340f..588cca05 100644 --- a/test/res/src/main.res +++ b/test/res/src/main.res @@ -1,3 +1,5 @@ +open RescriptCore + module Ts = Typescript.Export let source = "let x: string = 'hello, world!'" @@ -9,4 +11,4 @@ let result = Ts.transpileModule( ), ) -Js.log(result->Ts.TranspileOutput.get_outputText) +Console.log(result->Ts.TranspileOutput.get_outputText) diff --git a/test/res/src/placeholders/Iterable.res b/test/res/src/placeholders/Iterable.res deleted file mode 100644 index 9bbaf1d1..00000000 --- a/test/res/src/placeholders/Iterable.res +++ /dev/null @@ -1 +0,0 @@ -type t<'a> \ No newline at end of file diff --git a/test/res/src/placeholders/Map.res b/test/res/src/placeholders/Map.res deleted file mode 100644 index 77030897..00000000 --- a/test/res/src/placeholders/Map.res +++ /dev/null @@ -1 +0,0 @@ -type t<'k, 'v> \ No newline at end of file diff --git a/test/res/src/placeholders/ReadonlyMap.res b/test/res/src/placeholders/ReadonlyMap.res deleted file mode 100644 index 77030897..00000000 --- a/test/res/src/placeholders/ReadonlyMap.res +++ /dev/null @@ -1 +0,0 @@ -type t<'k, 'v> \ No newline at end of file diff --git a/test/res/src/placeholders/Set.res b/test/res/src/placeholders/Set.res deleted file mode 100644 index 9bbaf1d1..00000000 --- a/test/res/src/placeholders/Set.res +++ /dev/null @@ -1 +0,0 @@ -type t<'a> \ No newline at end of file diff --git a/test/res/src/placeholders/WeakMap.res b/test/res/src/placeholders/WeakMap.res deleted file mode 100644 index 77030897..00000000 --- a/test/res/src/placeholders/WeakMap.res +++ /dev/null @@ -1 +0,0 @@ -type t<'k, 'v> \ No newline at end of file diff --git a/test/res/src/placeholders/WeakSet.res b/test/res/src/placeholders/WeakSet.res deleted file mode 100644 index 9bbaf1d1..00000000 --- a/test/res/src/placeholders/WeakSet.res +++ /dev/null @@ -1 +0,0 @@ -type t<'a> \ No newline at end of file diff --git a/test/res/yarn.lock b/test/res/yarn.lock index dcddde29..6e255a40 100644 --- a/test/res/yarn.lock +++ b/test/res/yarn.lock @@ -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"