From c0b7eba2544356b74e9c539650649930ace8b7aa Mon Sep 17 00:00:00 2001 From: Dmitry Zakharov Date: Sat, 27 Apr 2024 14:43:09 +0400 Subject: [PATCH] Use built-in dict type --- packages/prepack/src/Prepack.res | 6 ++--- .../tests/src/ppx/Ppx_Primitive_test.bs.mjs | 7 +++++ packages/tests/src/ppx/Ppx_Primitive_test.res | 8 +++++- src/S.resi | 6 ++--- src/S_Core.res | 26 +++++++++---------- src/S_Core.resi | 8 +++--- 6 files changed, 37 insertions(+), 24 deletions(-) diff --git a/packages/prepack/src/Prepack.res b/packages/prepack/src/Prepack.res index db8bf22e..afe50a37 100644 --- a/packages/prepack/src/Prepack.res +++ b/packages/prepack/src/Prepack.res @@ -13,7 +13,7 @@ let jsInputPath = NodeJs.Path.join2(artifactsPath, "src/S.js") module Stdlib = { module Dict = { @val - external copy: (@as(json`{}`) _, Dict.t<'a>) => Dict.t<'a> = "Object.assign" + external copy: (@as(json`{}`) _, dict<'a>) => dict<'a> = "Object.assign" } module Json = { @@ -45,7 +45,7 @@ module Stdlib = { module Execa = { type returnValue = {stdout: string} - type options = {env?: Dict.t, cwd?: string} + type options = {env?: dict, cwd?: string} @module("execa") external sync: (string, array, ~options: options=?, unit) => returnValue = "execaSync" @@ -68,7 +68,7 @@ module Rollup = { } module ReplacePlugin = { - type options = {values: Dict.t} + type options = {values: dict} @module("@rollup/plugin-replace") external make: options => Plugin.t = "default" } diff --git a/packages/tests/src/ppx/Ppx_Primitive_test.bs.mjs b/packages/tests/src/ppx/Ppx_Primitive_test.bs.mjs index 87ebaeac..142709ec 100644 --- a/packages/tests/src/ppx/Ppx_Primitive_test.bs.mjs +++ b/packages/tests/src/ppx/Ppx_Primitive_test.bs.mjs @@ -56,6 +56,12 @@ Ava("Dict of string schema", (function (t) { U.assertEqualSchemas(t, myDictOfStringSchema, S$RescriptSchema.dict(S$RescriptSchema.string), undefined); })); +var myDictOfStringFromJsSchema = S$RescriptSchema.dict(S$RescriptSchema.string); + +Ava("Dict of string schema from Js", (function (t) { + U.assertEqualSchemas(t, myDictOfStringSchema, S$RescriptSchema.dict(S$RescriptSchema.string), undefined); + })); + var myDictOfStringFromCoreSchema = S$RescriptSchema.dict(S$RescriptSchema.string); Ava("Dict of string schema from Core", (function (t) { @@ -171,6 +177,7 @@ export { myArrayOfStringSchema , myListOfStringSchema , myDictOfStringSchema , + myDictOfStringFromJsSchema , myDictOfStringFromCoreSchema , myJsonSchema , myJsonFromCoreSchema , diff --git a/packages/tests/src/ppx/Ppx_Primitive_test.res b/packages/tests/src/ppx/Ppx_Primitive_test.res index 20489a74..f5c0064a 100644 --- a/packages/tests/src/ppx/Ppx_Primitive_test.res +++ b/packages/tests/src/ppx/Ppx_Primitive_test.res @@ -63,11 +63,17 @@ test("List of string schema", t => { }) @schema -type myDictOfString = Js.Dict.t +type myDictOfString = dict test("Dict of string schema", t => { t->assertEqualSchemas(myDictOfStringSchema, S.dict(S.string)) }) +@schema +type myDictOfStringFromJs = Js.Dict.t +test("Dict of string schema from Js", t => { + t->assertEqualSchemas(myDictOfStringSchema, S.dict(S.string)) +}) + @schema type myDictOfStringFromCore = Dict.t test("Dict of string schema from Core", t => { diff --git a/src/S.resi b/src/S.resi index 17c984fb..4a2e1093 100644 --- a/src/S.resi +++ b/src/S.resi @@ -10,7 +10,7 @@ module Literal: { | BigInt(Js.Types.bigint_val) | Symbol(Js.Types.symbol) | Array(array) - | Dict(Js.Dict.t) + | Dict(dict) | Function(Js.Types.function_val) | Object(Js.Types.obj_val) | Null @@ -55,7 +55,7 @@ and tagged = | Option(t) | Null(t) | Array(t) - | Object({fields: Js.Dict.t>, fieldNames: array, unknownKeys: unknownKeys}) + | Object({fields: dict>, fieldNames: array, unknownKeys: unknownKeys}) | Tuple(array>) | Union(array>) | Dict(t) @@ -110,7 +110,7 @@ let json: t let literal: 'value => t<'value> let array: t<'value> => t> let list: t<'value> => t> -let dict: t<'value> => t> +let dict: t<'value> => t> let option: t<'value> => t> let null: t<'value> => t> let nullable: t<'value> => t> diff --git a/src/S_Core.res b/src/S_Core.res index 614e5102..0928131d 100644 --- a/src/S_Core.res +++ b/src/S_Core.res @@ -97,7 +97,7 @@ module Stdlib = { module Dict = { @val - external copy: (@as(json`{}`) _, Js.Dict.t<'a>) => Js.Dict.t<'a> = "Object.assign" + external copy: (@as(json`{}`) _, dict<'a>) => dict<'a> = "Object.assign" @inline let has = (dict, key) => { @@ -106,10 +106,10 @@ module Stdlib = { @inline let deleteInPlace = (dict, key) => { - Js.Dict.unsafeDeleteKey(dict->(Obj.magic: Js.Dict.t<'a> => Js.Dict.t), key) + Js.Dict.unsafeDeleteKey(dict->(Obj.magic: dict<'a> => dict), key) } - let mapValues: (Js.Dict.t<'a>, 'a => 'b) => Js.Dict.t<'b> = %raw(`(dict, fn)=>{ + let mapValues: (dict<'a>, 'a => 'b) => dict<'b> = %raw(`(dict, fn)=>{ var key,newDict = {}; for (key in dict) { newDict[key] = fn(dict[key]) @@ -117,7 +117,7 @@ module Stdlib = { return newDict }`) - let every: (Js.Dict.t<'a>, 'a => bool) => bool = %raw(`(dict, fn)=>{ + let every: (dict<'a>, 'a => bool) => bool = %raw(`(dict, fn)=>{ for (var key in dict) { if (!fn(dict[key])) { return false @@ -193,7 +193,7 @@ module Literal = { | BigInt(Js.Types.bigint_val) | Symbol(Js.Types.symbol) | Array(array) - | Dict(Js.Dict.t) + | Dict(dict) | Function(Js.Types.function_val) | Object(Js.Types.obj_val) | Null @@ -209,7 +209,7 @@ module Literal = { Array(value->(Obj.magic: 'a => array<'b>)->Js.Array2.map(i => i->classify)) | #object if (value->(Obj.magic: 'a => {"constructor": unknown}))["constructor"] === %raw("Object") => - Dict(value->(Obj.magic: 'a => Js.Dict.t<'b>)->Dict.mapValues(classify)) + Dict(value->(Obj.magic: 'a => dict<'b>)->Dict.mapValues(classify)) | #object => Object(value->(Obj.magic: 'a => Js.Types.obj_val)) | #function => Function(value->(Obj.magic: 'a => Js.Types.function_val)) | #string => String(value->(Obj.magic: 'a => string)) @@ -340,7 +340,7 @@ type rec t<'value> = { @as("i") mutable isAsyncParse: isAsyncParse, @as("m") - metadataMap: Js.Dict.t, + metadataMap: dict, } and tagged = | Never @@ -353,7 +353,7 @@ and tagged = | Option(t) | Null(t) | Array(t) - | Object({fields: Js.Dict.t>, fieldNames: array, unknownKeys: unknownKeys}) + | Object({fields: dict>, fieldNames: array, unknownKeys: unknownKeys}) | Tuple(array>) | Union(array>) | Dict(t) @@ -1443,7 +1443,7 @@ let rec literalCheckBuilder = (b, ~value, ~inputVar) => { ->Js.Array2.joinWith("&&") : "") ++ ")" } else if %raw(`value&&value.constructor===Object`) { - let value = value->(Obj.magic: unknown => Js.Dict.t) + let value = value->(Obj.magic: unknown => dict) let keys = value->Js.Dict.keys let numberOfKeys = keys->Js.Array2.length `(${check}||${inputVar}&&${inputVar}.constructor===Object&&Object.keys(${inputVar}).length===${numberOfKeys->Stdlib.Int.unsafeToString}` ++ @@ -1494,7 +1494,7 @@ let unit = literal(%raw("void 0")) module Definition = { type t<'embeded> - type node<'embeded> = Js.Dict.t> + type node<'embeded> = dict> type kind = | @as(0) Node | @as(1) Constant | @as(2) Embeded let toKindWithSet = (definition: t<'embeded>, ~embededSet: Stdlib.Set.t<'embeded>) => { @@ -1879,7 +1879,7 @@ module Object = { @as("n") fieldNames: array, @as("h") - fields: Js.Dict.t>, + fields: dict>, @as("d") itemDefinitionsSet: Stdlib.Set.t, // Public API for JS/TS users. @@ -3163,7 +3163,7 @@ let json = makeWithNoopSerializer( } output->Js.Json.array } else { - let input = input->(Obj.magic: unknown => Js.Dict.t) + let input = input->(Obj.magic: unknown => dict) let keys = input->Js.Dict.keys let output = Js.Dict.empty() for idx in 0 to keys->Js.Array2.length - 1 { @@ -3764,7 +3764,7 @@ let js_object = definer => { let definer = definer->(Obj.magic: unknown => Object.ctx => 'a) object(definer) } else { - let definer = definer->(Obj.magic: unknown => Js.Dict.t>) + let definer = definer->(Obj.magic: unknown => dict>) object(s => { let definition = Js.Dict.empty() let fieldNames = definer->Js.Dict.keys diff --git a/src/S_Core.resi b/src/S_Core.resi index 49838d40..4a6f11fc 100644 --- a/src/S_Core.resi +++ b/src/S_Core.resi @@ -10,7 +10,7 @@ module Literal: { | BigInt(Js.Types.bigint_val) | Symbol(Js.Types.symbol) | Array(array) - | Dict(Js.Dict.t) + | Dict(dict) | Function(Js.Types.function_val) | Object(Js.Types.obj_val) | Null @@ -55,7 +55,7 @@ and tagged = | Option(t) | Null(t) | Array(t) - | Object({fields: Js.Dict.t>, fieldNames: array, unknownKeys: unknownKeys}) + | Object({fields: dict>, fieldNames: array, unknownKeys: unknownKeys}) | Tuple(array>) | Union(array>) | Dict(t) @@ -110,7 +110,7 @@ let json: t let literal: 'value => t<'value> let array: t<'value> => t> let list: t<'value> => t> -let dict: t<'value> => t> +let dict: t<'value> => t> let option: t<'value> => t> let null: t<'value> => t> let nullable: t<'value> => t> @@ -382,7 +382,7 @@ let js_transform: ( ~serializer: ('transformed, effectCtx<'transformed>) => 'output=?, ) => t<'transformed> -let js_object: unknown => t> +let js_object: unknown => t> let js_merge: (t, t) => t