From f47192d3999ee2696f0333f48e301848b1b65211 Mon Sep 17 00:00:00 2001 From: Hyeseong Kim Date: Sat, 10 Dec 2022 22:54:30 +0900 Subject: [PATCH] upgrade toolchain and export gentype binding --- bsconfig.json | 4 + examples/FetchExample.js | 11 ++- package-lock.json | 153 +++++++++++++++++++++++++++++++++++++-- package.json | 8 +- src/Promise.gen.tsx | 6 ++ src/Promise.js | 8 +- src/Promise.resi | 43 +++++------ tests/PromiseTest.js | 103 +++++++++++--------------- tests/Test.js | 3 +- 9 files changed, 234 insertions(+), 105 deletions(-) create mode 100644 src/Promise.gen.tsx diff --git a/bsconfig.json b/bsconfig.json index e7191e8..009baf0 100644 --- a/bsconfig.json +++ b/bsconfig.json @@ -21,5 +21,9 @@ "suffix": ".js", "warnings": { "error" : "+101" + }, + "gentypeconfig": { + "language": "typescript", + "module": "commonjs" } } diff --git a/examples/FetchExample.js b/examples/FetchExample.js index 5f3a084..0f13889 100644 --- a/examples/FetchExample.js +++ b/examples/FetchExample.js @@ -2,9 +2,9 @@ 'use strict'; var $$Promise = require("../src/Promise.js"); -var Belt_Array = require("bs-platform/lib/js/belt_Array.js"); +var Belt_Array = require("rescript/lib/js/belt_Array.js"); var NodeFetch = require("node-fetch"); -var Caml_exceptions = require("bs-platform/lib/js/caml_exceptions.js"); +var Caml_exceptions = require("rescript/lib/js/caml_exceptions.js"); globalThis.fetch = NodeFetch; @@ -66,7 +66,7 @@ var Login = { function getProducts(token, param) { var params = { - Authorization: "Bearer " + token + Authorization: "Bearer " + token + "" }; return $$Promise.$$catch(globalThis.fetch("https://reqres.in/api/products", params).then(function (res) { return res.json(); @@ -96,7 +96,7 @@ var Product = { getProducts: getProducts }; -var FailedRequest = Caml_exceptions.create("FetchExample.FailedRequest"); +var FailedRequest = /* @__PURE__ */Caml_exceptions.create("FetchExample.FailedRequest"); $$Promise.$$catch(login("emma.wong@reqres.in", "pw").then(function (ret) { if (ret.TAG !== /* Ok */0) { @@ -112,8 +112,7 @@ $$Promise.$$catch(login("emma.wong@reqres.in", "pw").then(function (ret) { if (result.TAG === /* Ok */0) { console.log("\nAvailable Products:\n---"); tmp = Belt_Array.forEach(result._0, (function (p) { - console.log(String(p.id) + " - " + p.name); - + console.log("" + String(p.id) + " - " + p.name + ""); })); } else { console.log("Could not query products: " + result._0); diff --git a/package-lock.json b/package-lock.json index e83e658..d3e1f38 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,8 +1,145 @@ { - "name": "rescript-promise", + "name": "@ryyppy/rescript-promise", "version": "2.1.0", - "lockfileVersion": 1, + "lockfileVersion": 2, "requires": true, + "packages": { + "": { + "name": "@ryyppy/rescript-promise", + "version": "2.1.0", + "license": "MIT", + "devDependencies": { + "@babel/code-frame": "^7.12.11", + "node-fetch": "^2.6.1", + "rescript": "^10.0.1" + } + }, + "node_modules/@babel/code-frame": { + "version": "7.12.11", + "resolved": "https://registry.npmjs.org/@babel/code-frame/-/code-frame-7.12.11.tgz", + "integrity": "sha512-Zt1yodBx1UcyiePMSkWnU4hPqhwq7hGi2nFL1LeA3EUl+q2LQx16MISgJ0+z7dnmgvP9QtIleuETGOiOH1RcIw==", + "dev": true, + "dependencies": { + "@babel/highlight": "^7.10.4" + } + }, + "node_modules/@babel/helper-validator-identifier": { + "version": "7.12.11", + "resolved": "https://registry.npmjs.org/@babel/helper-validator-identifier/-/helper-validator-identifier-7.12.11.tgz", + "integrity": "sha512-np/lG3uARFybkoHokJUmf1QfEvRVCPbmQeUQpKow5cQ3xWrV9i3rUHodKDJPQfTVX61qKi+UdYk8kik84n7XOw==", + "dev": true + }, + "node_modules/@babel/highlight": { + "version": "7.10.4", + "resolved": "https://registry.npmjs.org/@babel/highlight/-/highlight-7.10.4.tgz", + "integrity": "sha512-i6rgnR/YgPEQzZZnbTHHuZdlE8qyoBNalD6F+q4vAFlcMEcqmkoG+mPqJYJCo63qPf74+Y1UZsl3l6f7/RIkmA==", + "dev": true, + "dependencies": { + "@babel/helper-validator-identifier": "^7.10.4", + "chalk": "^2.0.0", + "js-tokens": "^4.0.0" + } + }, + "node_modules/ansi-styles": { + "version": "3.2.1", + "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-3.2.1.tgz", + "integrity": "sha512-VT0ZI6kZRdTh8YyJw3SMbYm/u+NqfsAxEpWO0Pf9sq8/e94WxxOpPKx9FR1FlyCtOVDNOQ+8ntlqFxiRc+r5qA==", + "dev": true, + "dependencies": { + "color-convert": "^1.9.0" + }, + "engines": { + "node": ">=4" + } + }, + "node_modules/chalk": { + "version": "2.4.2", + "resolved": "https://registry.npmjs.org/chalk/-/chalk-2.4.2.tgz", + "integrity": "sha512-Mti+f9lpJNcwF4tWV8/OrTTtF1gZi+f8FqlyAdouralcFWFQWF2+NgCHShjkCb+IFBLq9buZwE1xckQU4peSuQ==", + "dev": true, + "dependencies": { + "ansi-styles": "^3.2.1", + "escape-string-regexp": "^1.0.5", + "supports-color": "^5.3.0" + }, + "engines": { + "node": ">=4" + } + }, + "node_modules/color-convert": { + "version": "1.9.3", + "resolved": "https://registry.npmjs.org/color-convert/-/color-convert-1.9.3.tgz", + "integrity": "sha512-QfAUtd+vFdAtFQcC8CCyYt1fYWxSqAiK2cSD6zDB8N3cpsEBAvRxp9zOGg6G/SHHJYAT88/az/IuDGALsNVbGg==", + "dev": true, + "dependencies": { + "color-name": "1.1.3" + } + }, + "node_modules/color-name": { + "version": "1.1.3", + "resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.3.tgz", + "integrity": "sha1-p9BVi9icQveV3UIyj3QIMcpTvCU=", + "dev": true + }, + "node_modules/escape-string-regexp": { + "version": "1.0.5", + "resolved": "https://registry.npmjs.org/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz", + "integrity": "sha1-G2HAViGQqN/2rjuyzwIAyhMLhtQ=", + "dev": true, + "engines": { + "node": ">=0.8.0" + } + }, + "node_modules/has-flag": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-3.0.0.tgz", + "integrity": "sha1-tdRU3CGZriJWmfNGfloH87lVuv0=", + "dev": true, + "engines": { + "node": ">=4" + } + }, + "node_modules/js-tokens": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/js-tokens/-/js-tokens-4.0.0.tgz", + "integrity": "sha512-RdJUflcE3cUzKiMqQgsCu06FPu9UdIJO0beYbPhHN4k6apgJtifcoCtT9bcxOpYBtpD2kCM6Sbzg4CausW/PKQ==", + "dev": true + }, + "node_modules/node-fetch": { + "version": "2.6.1", + "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.1.tgz", + "integrity": "sha512-V4aYg89jEoVRxRb2fJdAg8FHvI7cEyYdVAh94HH0UIK8oJxUfkjlDQN9RbMx+bEjP7+ggMiFRprSti032Oipxw==", + "dev": true, + "engines": { + "node": "4.x || >=6.0.0" + } + }, + "node_modules/rescript": { + "version": "10.0.1", + "resolved": "https://registry.npmjs.org/rescript/-/rescript-10.0.1.tgz", + "integrity": "sha512-XwO1GPDtoEU4H03xQE5bp0/qtSVR6YLaJRPxWKrfFgKc+LI36ODOCie7o9UJfgzQdoMYkkZyiTGZ4N9OQEaiUw==", + "dev": true, + "hasInstallScript": true, + "bin": { + "bsc": "bsc", + "bsrefmt": "bsrefmt", + "bstracing": "lib/bstracing", + "rescript": "rescript" + } + }, + "node_modules/supports-color": { + "version": "5.5.0", + "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-5.5.0.tgz", + "integrity": "sha512-QjVjwdXIt408MIiAqCX4oUKsgU2EqAGzs2Ppkm4aQYbjm+ZEWEcW4SfFNTr4uMNZma0ey4f5lgLrkB0aX0QMow==", + "dev": true, + "dependencies": { + "has-flag": "^3.0.0" + }, + "engines": { + "node": ">=4" + } + } + }, "dependencies": { "@babel/code-frame": { "version": "7.12.11", @@ -39,12 +176,6 @@ "color-convert": "^1.9.0" } }, - "bs-platform": { - "version": "8.4.2", - "resolved": "https://registry.npmjs.org/bs-platform/-/bs-platform-8.4.2.tgz", - "integrity": "sha512-9q7S4/LLV/a68CweN382NJdCCr/lOSsJR3oQYnmPK98ChfO/AdiA3lYQkQTp6T+U0I5Z5RypUAUprNstwDtMDQ==", - "dev": true - }, "chalk": { "version": "2.4.2", "resolved": "https://registry.npmjs.org/chalk/-/chalk-2.4.2.tgz", @@ -95,6 +226,12 @@ "integrity": "sha512-V4aYg89jEoVRxRb2fJdAg8FHvI7cEyYdVAh94HH0UIK8oJxUfkjlDQN9RbMx+bEjP7+ggMiFRprSti032Oipxw==", "dev": true }, + "rescript": { + "version": "10.0.1", + "resolved": "https://registry.npmjs.org/rescript/-/rescript-10.0.1.tgz", + "integrity": "sha512-XwO1GPDtoEU4H03xQE5bp0/qtSVR6YLaJRPxWKrfFgKc+LI36ODOCie7o9UJfgzQdoMYkkZyiTGZ4N9OQEaiUw==", + "dev": true + }, "supports-color": { "version": "5.5.0", "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-5.5.0.tgz", diff --git a/package.json b/package.json index cae76f3..b52ec4c 100644 --- a/package.json +++ b/package.json @@ -6,8 +6,8 @@ "main": "index.js", "scripts": { "test": "node tests/PromiseTest.js", - "build": "bsb -make-world", - "watch": "bsb -make-world -w" + "build": "rescript build", + "watch": "rescript build -w" }, "files": [ "src/Promise.res", @@ -23,7 +23,7 @@ "license": "MIT", "devDependencies": { "@babel/code-frame": "^7.12.11", - "bs-platform": "8.4.2", - "node-fetch": "^2.6.1" + "node-fetch": "^2.6.1", + "rescript": "^10.0.1" } } diff --git a/src/Promise.gen.tsx b/src/Promise.gen.tsx new file mode 100644 index 0000000..5cf4fc4 --- /dev/null +++ b/src/Promise.gen.tsx @@ -0,0 +1,6 @@ +/* TypeScript file generated from Promise.resi by genType. */ +/* eslint-disable import/first */ + + +// tslint:disable-next-line:interface-over-type-literal +export type t = Promise; diff --git a/src/Promise.js b/src/Promise.js index 61631ce..ad9ba63 100644 --- a/src/Promise.js +++ b/src/Promise.js @@ -1,14 +1,14 @@ // Generated by ReScript, PLEASE EDIT WITH CARE 'use strict'; -var Curry = require("bs-platform/lib/js/curry.js"); -var Caml_exceptions = require("bs-platform/lib/js/caml_exceptions.js"); +var Curry = require("rescript/lib/js/curry.js"); +var Caml_exceptions = require("rescript/lib/js/caml_exceptions.js"); -var JsError = Caml_exceptions.create("Promise.JsError"); +var JsError = /* @__PURE__ */Caml_exceptions.create("Promise.JsError"); function $$catch(promise, callback) { return promise.catch(function (err) { - return Curry._1(callback, Caml_exceptions.caml_is_extension(err) ? err : ({ + return Curry._1(callback, Caml_exceptions.is_extension(err) ? err : ({ RE_EXN_ID: JsError, _1: err })); diff --git a/src/Promise.resi b/src/Promise.resi index 2a6dbdc..25138a1 100644 --- a/src/Promise.resi +++ b/src/Promise.resi @@ -6,6 +6,7 @@ // // More details about polymorphism / invariance / covariance,... can be found here: // https://caml.inria.fr/pub/docs/manual-ocaml/polymorphism.html#ss:variance:abstract-data-types +@genType type t<+'a> = Js.Promise.t<'a> // JsError is currently a shim exception and will be deprecated for @@ -22,11 +23,11 @@ exception JsError(Js.Exn.t) let p = Promise.resolve(5) // Promise.t ``` ") -@bs.val -@bs.scope("Promise") +@val +@scope("Promise") external resolve: 'a => t<'a> = "resolve" -@bs.scope("Promise") @bs.val +@scope("Promise") @val external reject: exn => t<_> = "reject" @ocaml.doc(" @@ -55,8 +56,8 @@ Promise.make((resolve, reject) => { ->ignore ``` ") -@bs.new -external make: ((@bs.uncurry (. 'a) => unit, (. 'e) => unit) => unit) => t<'a> = "Promise" +@new +external make: ((@uncurry (. 'a) => unit, (. 'e) => unit) => unit) => t<'a> = "Promise" @ocaml.doc(" `catch(promise, errorCallback)` registers an exception handler in a promise chain. @@ -116,7 +117,7 @@ Promise.resolve(5) ->ignore ``` ") -@bs.send +@send external then: (t<'a>, @uncurry ('a => t<'b>)) => t<'b> = "then" @ocaml.doc(" @@ -137,7 +138,7 @@ resolve(\"Anna\") In case you want to return another promise in your `callback`, consider using \`then\` instead. ") -@bs.send +@send external thenResolve: (t<'a>, @uncurry ('a => 'b)) => t<'b> = "then" @ocaml.doc(" @@ -171,11 +172,11 @@ resolve(5) ->ignore ``` ") -@bs.send +@send external finally: (t<'a>, unit => unit) => t<'a> = "finally" /* Combining promises. */ -@bs.scope("Promise") @bs.val +@scope("Promise") @val external race: array> => t<'a> = "race" @ocaml.doc(" @@ -196,41 +197,41 @@ all(promises) ->ignore ``` ") -@bs.scope("Promise") -@bs.val +@scope("Promise") +@val external all: array> => t> = "all" @ocaml.doc(" [all2((p1, p2))]. Like `all()`, but with a fixed size tuple of 2 ") -@bs.scope("Promise") -@bs.val +@scope("Promise") +@val external all2: ((t<'a>, t<'b>)) => t<('a, 'b)> = "all" @ocaml.doc(" [all3((p1, p2, p3))]. Like `all()`, but with a fixed size tuple of 3 ") -@bs.scope("Promise") -@bs.val +@scope("Promise") +@val external all3: ((t<'a>, t<'b>, t<'c>)) => t<('a, 'b, 'c)> = "all" @ocaml.doc(" [all4((p1, p2, p3, p4))]. Like `all()`, but with a fixed size tuple of 4 ") -@bs.scope("Promise") -@bs.val +@scope("Promise") +@val external all4: ((t<'a>, t<'b>, t<'c>, t<'d>)) => t<('a, 'b, 'c, 'd)> = "all" @ocaml.doc(" [all5((p1, p2, p3, p4, p5))]. Like `all()`, but with a fixed size tuple of 5 ") -@bs.scope("Promise") -@bs.val +@scope("Promise") +@val external all5: ((t<'a>, t<'b>, t<'c>, t<'d>, t<'e>)) => t<('a, 'b, 'c, 'd, 'e)> = "all" @ocaml.doc(" [all6((p1, p2, p4, p5, p6))]. Like `all()`, but with a fixed size tuple of 6 ") -@bs.scope("Promise") -@bs.val +@scope("Promise") +@val external all6: ((t<'a>, t<'b>, t<'c>, t<'d>, t<'e>, t<'f>)) => t<('a, 'b, 'c, 'd, 'e, 'f)> = "all" diff --git a/tests/PromiseTest.js b/tests/PromiseTest.js index e68e37c..3172deb 100644 --- a/tests/PromiseTest.js +++ b/tests/PromiseTest.js @@ -2,17 +2,17 @@ 'use strict'; var Test = require("./Test.js"); -var Curry = require("bs-platform/lib/js/curry.js"); -var Js_exn = require("bs-platform/lib/js/js_exn.js"); +var Curry = require("rescript/lib/js/curry.js"); +var Js_exn = require("rescript/lib/js/js_exn.js"); var $$Promise = require("../src/Promise.js"); -var Caml_obj = require("bs-platform/lib/js/caml_obj.js"); -var Caml_exceptions = require("bs-platform/lib/js/caml_exceptions.js"); +var Caml_obj = require("rescript/lib/js/caml_obj.js"); +var Caml_exceptions = require("rescript/lib/js/caml_exceptions.js"); -var TestError = Caml_exceptions.create("PromiseTest.TestError"); +var TestError = /* @__PURE__ */Caml_exceptions.create("PromiseTest.TestError"); var fail = Js_exn.raiseError; -var equal = Caml_obj.caml_equal; +var equal = Caml_obj.equal; function resolveTest(param) { Promise.resolve("test").then(function (str) { @@ -27,11 +27,10 @@ function resolveTest(param) { ], str, equal, "test"); return Promise.resolve(undefined); }); - } function runTests(param) { - return resolveTest(undefined); + resolveTest(undefined); } var Creation = { @@ -92,15 +91,15 @@ function testThenResolve(param) { return Promise.resolve(1).then(function (num) { return num + 1 | 0; }).then(function (ret) { - return Test.run([ - [ - "PromiseTest.res", - 79, - 26, - 39 - ], - "Should be 2" - ], ret, equal, 2); + Test.run([ + [ + "PromiseTest.res", + 79, + 26, + 39 + ], + "Should be 2" + ], ret, equal, 2); }); } @@ -132,7 +131,6 @@ function runTests$1(param) { testInvalidThen(undefined); testThenResolve(undefined); testInvalidThenResolve(undefined); - } var ThenChaining = { @@ -162,12 +160,10 @@ function testExnRejection(param) { }); return Promise.resolve(undefined); })); - } function runTests$2(param) { testExnRejection(undefined); - } var Rejection = { @@ -186,7 +182,7 @@ function testExternalPromiseThrow(param) { return $$Promise.$$catch(Curry._1(asyncParseFail, undefined).then(function (param) { return Promise.resolve(undefined); }), (function (e) { - var success = e.RE_EXN_ID === $$Promise.JsError ? Caml_obj.caml_equal(e._1.message, "Unexpected token . in JSON at position 1") : false; + var success = e.RE_EXN_ID === $$Promise.JsError ? Caml_obj.equal(e._1.message, "Unexpected token . in JSON at position 1") : false; Test.run([ [ "PromiseTest.res", @@ -226,7 +222,7 @@ function testRaiseErrorThrow(param) { return $$Promise.$$catch(Promise.resolve(undefined).then(function (param) { return Js_exn.raiseError("Some JS error"); }), (function (e) { - var isTestErr = e.RE_EXN_ID === $$Promise.JsError ? Caml_obj.caml_equal(e._1.message, "Some JS error") : false; + var isTestErr = e.RE_EXN_ID === $$Promise.JsError ? Caml_obj.equal(e._1.message, "Some JS error") : false; Test.run([ [ "PromiseTest.res", @@ -279,7 +275,6 @@ function testCatchFinally(param) { return Promise.resolve(undefined); })).finally(function (param) { wasCalled.contents = true; - }).then(function (v) { Test.run([ [ @@ -301,7 +296,6 @@ function testCatchFinally(param) { ], wasCalled.contents, equal, true); return Promise.resolve(undefined); }); - } function testResolveFinally(param) { @@ -312,7 +306,6 @@ function testResolveFinally(param) { return Promise.resolve(v + 5 | 0); }).finally(function (param) { wasCalled.contents = true; - }).then(function (v) { Test.run([ [ @@ -334,7 +327,6 @@ function testResolveFinally(param) { ], wasCalled.contents, equal, true); return Promise.resolve(undefined); }); - } function runTests$3(param) { @@ -344,7 +336,6 @@ function runTests$3(param) { thenAfterCatch(undefined); testCatchFinally(undefined); testResolveFinally(undefined); - } var Catching = { @@ -366,12 +357,11 @@ function testParallel(param) { return new Promise((function (resolve, param) { setTimeout((function (param) { place.contents = place.contents + 1 | 0; - return resolve([ - place.contents, - msg - ]); + resolve([ + place.contents, + msg + ]); }), ms); - })); }; var p1 = delayedMsg(1000, "is Anna"); @@ -413,9 +403,8 @@ function testRace(param) { var racer = function (ms, name) { return new Promise((function (resolve, param) { setTimeout((function (param) { - return resolve(name); + resolve(name); }), ms); - })); }; var promises = [ @@ -445,12 +434,11 @@ function testParallel2(param) { return new Promise((function (resolve, param) { setTimeout((function (param) { place.contents = place.contents + 1 | 0; - return resolve([ - place.contents, - msg - ]); + resolve([ + place.contents, + msg + ]); }), ms); - })); }; var p1 = delayedMsg(1000, "is Anna"); @@ -489,12 +477,11 @@ function testParallel3(param) { return new Promise((function (resolve, param) { setTimeout((function (param) { place.contents = place.contents + 1 | 0; - return resolve([ - place.contents, - msg - ]); + resolve([ + place.contents, + msg + ]); }), ms); - })); }; var p1 = delayedMsg(1000, "is Anna"); @@ -539,12 +526,11 @@ function testParallel4(param) { return new Promise((function (resolve, param) { setTimeout((function (param) { place.contents = place.contents + 1 | 0; - return resolve([ - place.contents, - msg - ]); + resolve([ + place.contents, + msg + ]); }), ms); - })); }; var p1 = delayedMsg(1500, "Anna"); @@ -595,12 +581,11 @@ function testParallel5(param) { return new Promise((function (resolve, param) { setTimeout((function (param) { place.contents = place.contents + 1 | 0; - return resolve([ - place.contents, - msg - ]); + resolve([ + place.contents, + msg + ]); }), ms); - })); }; var p1 = delayedMsg(1500, "Anna"); @@ -657,12 +642,11 @@ function testParallel6(param) { return new Promise((function (resolve, param) { setTimeout((function (param) { place.contents = place.contents + 1 | 0; - return resolve([ - place.contents, - msg - ]); + resolve([ + place.contents, + msg + ]); }), ms); - })); }; var p1 = delayedMsg(1500, "Anna"); @@ -725,7 +709,6 @@ function runTests$4(param) { testParallel4(undefined); testParallel5(undefined); testParallel6(undefined); - } var Concurrently = { diff --git a/tests/Test.js b/tests/Test.js index d6e4fb0..74418da 100644 --- a/tests/Test.js +++ b/tests/Test.js @@ -3,7 +3,7 @@ var Fs = require("fs"); var Path = require("path"); -var Curry = require("bs-platform/lib/js/curry.js"); +var Curry = require("rescript/lib/js/curry.js"); var CodeFrame = require("@babel/code-frame"); var dirname = typeof __dirname === "undefined" ? undefined : __dirname; @@ -53,7 +53,6 @@ function run(loc, left, comparator, right) { var obj = {}; Error.captureStackTrace(obj); console.log(cleanUpStackTrace(obj.stack)); - } exports.dirname = dirname$1;