From 7f1bc64ab2a6f6948a22abf9e58adbd7150d3279 Mon Sep 17 00:00:00 2001 From: Dmitry Zakharov Date: Sat, 13 Jul 2024 13:18:23 +0400 Subject: [PATCH] Invert literals check --- packages/tests/src/core/Example_test.res | 4 +- .../tests/src/core/S_literal_Array_test.res | 4 +- .../tests/src/core/S_literal_Boolean_test.res | 4 +- .../tests/src/core/S_literal_NaN_test.res | 4 +- .../tests/src/core/S_literal_Null_test.res | 4 +- .../tests/src/core/S_literal_Number_test.res | 4 +- .../tests/src/core/S_literal_Object_test.res | 4 +- .../tests/src/core/S_literal_String_test.res | 8 ++- .../src/core/S_literal_Undefined_test.res | 4 +- packages/tests/src/core/S_object_test.res | 2 +- packages/tests/src/core/S_tuple_test.res | 2 +- packages/tests/src/core/S_union_test.res | 14 ++-- packages/tests/src/core/S_variant_test.res | 2 +- src/S_Core.bs.mjs | 62 ++++++++--------- src/S_Core.res | 68 ++++++++++--------- 15 files changed, 98 insertions(+), 92 deletions(-) diff --git a/packages/tests/src/core/Example_test.res b/packages/tests/src/core/Example_test.res index 8ca8d7fb..c19fba78 100644 --- a/packages/tests/src/core/Example_test.res +++ b/packages/tests/src/core/Example_test.res @@ -69,7 +69,7 @@ test("Compiled parse code snapshot", t => { t->U.assertCompiledCode( ~schema=filmSchema, ~op=#Parse, - `i=>{if(!i||i.constructor!==Object){e[11](i)}let v0=i["Id"],v1=i["Title"],v2=i["Tags"],v6=i["Rating"],v7=i["Age"];if(typeof v0!=="number"||Number.isNaN(v0)){e[0](v0)}if(typeof v1!=="string"){e[1](v1)}if(v2!==void 0&&(!Array.isArray(v2))){e[2](v2)}if(v2!==void 0){for(let v3=0;v32147483647||v7<-2147483648||v7%1!==0)){e[10](v7)}return {"id":v0,"title":v1,"tags":v2===void 0?e[4]:v2,"rating":v6,"deprecatedAgeRestriction":v7,}}`, + `i=>{if(!i||i.constructor!==Object){e[11](i)}let v0=i["Id"],v1=i["Title"],v2=i["Tags"],v6=i["Rating"],v7=i["Age"];if(typeof v0!=="number"||Number.isNaN(v0)){e[0](v0)}if(typeof v1!=="string"){e[1](v1)}if(v2!==void 0&&(!Array.isArray(v2))){e[2](v2)}if(v2!==void 0){for(let v3=0;v32147483647||v7<-2147483648||v7%1!==0)){e[10](v7)}return {"id":v0,"title":v1,"tags":v2===void 0?e[4]:v2,"rating":v6,"deprecatedAgeRestriction":v7,}}`, ) }) @@ -77,6 +77,6 @@ test("Compiled serialize code snapshot", t => { t->U.assertCompiledCode( ~schema=filmSchema, ~op=#Serialize, - `i=>{let v0=i["tags"],v1,v2=i["rating"],v3,v4=i["deprecatedAgeRestriction"],v5;if(v0!==void 0){v1=e[0](v0)}try{v2==="G"||e[1](v2);v3=v2}catch(e0){try{v2==="PG"||e[2](v2);v3=v2}catch(e1){try{v2==="PG13"||e[3](v2);v3=v2}catch(e2){try{v2==="R"||e[4](v2);v3=v2}catch(e3){e[5]([e0,e1,e2,e3,])}}}}if(v4!==void 0){v5=e[6](v4)}return {"Id":i["id"],"Title":i["title"],"Tags":v1,"Rating":v3,"Age":v5,}}`, + `i=>{let v0=i["tags"],v1,v2=i["rating"],v3,v4=i["deprecatedAgeRestriction"],v5;if(v0!==void 0){v1=e[0](v0)}try{if(v2!=="G"){e[1](v2)}v3=v2}catch(e0){try{if(v2!=="PG"){e[2](v2)}v3=v2}catch(e1){try{if(v2!=="PG13"){e[3](v2)}v3=v2}catch(e2){try{if(v2!=="R"){e[4](v2)}v3=v2}catch(e3){e[5]([e0,e1,e2,e3,])}}}}if(v4!==void 0){v5=e[6](v4)}return {"Id":i["id"],"Title":i["title"],"Tags":v1,"Rating":v3,"Age":v5,}}`, ) }) diff --git a/packages/tests/src/core/S_literal_Array_test.res b/packages/tests/src/core/S_literal_Array_test.res index 39ce705d..2bcb655b 100644 --- a/packages/tests/src/core/S_literal_Array_test.res +++ b/packages/tests/src/core/S_literal_Array_test.res @@ -87,7 +87,7 @@ module Common = { t->U.assertCompiledCode( ~schema, ~op=#Parse, - `i=>{(i===e[0]||Array.isArray(i)&&i.length===2&&i[0]==="bar"&&i[1]===true)||e[1](i);return i}`, + `i=>{if(i!==e[0]&&(!Array.isArray(i)||i.length!==2||i[0]!=="bar"||i[1]!==true)){e[1](i)}return i}`, ) }) @@ -97,7 +97,7 @@ module Common = { t->U.assertCompiledCode( ~schema, ~op=#Serialize, - `i=>{(i===e[0]||Array.isArray(i)&&i.length===2&&i[0]==="bar"&&i[1]===true)||e[1](i);return i}`, + `i=>{if(i!==e[0]&&(!Array.isArray(i)||i.length!==2||i[0]!=="bar"||i[1]!==true)){e[1](i)}return i}`, ) }) } diff --git a/packages/tests/src/core/S_literal_Boolean_test.res b/packages/tests/src/core/S_literal_Boolean_test.res index 8bcd21a2..fa387d0a 100644 --- a/packages/tests/src/core/S_literal_Boolean_test.res +++ b/packages/tests/src/core/S_literal_Boolean_test.res @@ -62,12 +62,12 @@ module Common = { test("Compiled parse code snapshot", t => { let schema = factory() - t->U.assertCompiledCode(~schema, ~op=#Parse, `i=>{i===false||e[0](i);return i}`) + t->U.assertCompiledCode(~schema, ~op=#Parse, `i=>{if(i!==false){e[0](i)}return i}`) }) test("Compiled serialize code snapshot", t => { let schema = factory() - t->U.assertCompiledCode(~schema, ~op=#Serialize, `i=>{i===false||e[0](i);return i}`) + t->U.assertCompiledCode(~schema, ~op=#Serialize, `i=>{if(i!==false){e[0](i)}return i}`) }) } diff --git a/packages/tests/src/core/S_literal_NaN_test.res b/packages/tests/src/core/S_literal_NaN_test.res index 09e7dd80..ae546681 100644 --- a/packages/tests/src/core/S_literal_NaN_test.res +++ b/packages/tests/src/core/S_literal_NaN_test.res @@ -48,12 +48,12 @@ module Common = { test("Compiled parse code snapshot", t => { let schema = factory() - t->U.assertCompiledCode(~schema, ~op=#Parse, `i=>{Number.isNaN(i)||e[0](i);return i}`) + t->U.assertCompiledCode(~schema, ~op=#Parse, `i=>{if(!Number.isNaN(i)){e[0](i)}return i}`) }) test("Compiled serialize code snapshot", t => { let schema = factory() - t->U.assertCompiledCode(~schema, ~op=#Serialize, `i=>{Number.isNaN(i)||e[0](i);return i}`) + t->U.assertCompiledCode(~schema, ~op=#Serialize, `i=>{if(!Number.isNaN(i)){e[0](i)}return i}`) }) } diff --git a/packages/tests/src/core/S_literal_Null_test.res b/packages/tests/src/core/S_literal_Null_test.res index 2234dc5d..865649a9 100644 --- a/packages/tests/src/core/S_literal_Null_test.res +++ b/packages/tests/src/core/S_literal_Null_test.res @@ -49,12 +49,12 @@ module Common = { test("Compiled parse code snapshot", t => { let schema = factory() - t->U.assertCompiledCode(~schema, ~op=#Parse, `i=>{i===null||e[0](i);return i}`) + t->U.assertCompiledCode(~schema, ~op=#Parse, `i=>{if(i!==null){e[0](i)}return i}`) }) test("Compiled serialize code snapshot", t => { let schema = factory() - t->U.assertCompiledCode(~schema, ~op=#Serialize, `i=>{i===null||e[0](i);return i}`) + t->U.assertCompiledCode(~schema, ~op=#Serialize, `i=>{if(i!==null){e[0](i)}return i}`) }) } diff --git a/packages/tests/src/core/S_literal_Number_test.res b/packages/tests/src/core/S_literal_Number_test.res index d578c401..5ed4a4de 100644 --- a/packages/tests/src/core/S_literal_Number_test.res +++ b/packages/tests/src/core/S_literal_Number_test.res @@ -62,13 +62,13 @@ module Common = { test("Compiled parse code snapshot", t => { let schema = factory() - t->U.assertCompiledCode(~schema, ~op=#Parse, `i=>{i===123||e[0](i);return i}`) + t->U.assertCompiledCode(~schema, ~op=#Parse, `i=>{if(i!==123){e[0](i)}return i}`) }) test("Compiled serialize code snapshot", t => { let schema = factory() - t->U.assertCompiledCode(~schema, ~op=#Serialize, `i=>{i===123||e[0](i);return i}`) + t->U.assertCompiledCode(~schema, ~op=#Serialize, `i=>{if(i!==123){e[0](i)}return i}`) }) } diff --git a/packages/tests/src/core/S_literal_Object_test.res b/packages/tests/src/core/S_literal_Object_test.res index 11c741cd..66c5819c 100644 --- a/packages/tests/src/core/S_literal_Object_test.res +++ b/packages/tests/src/core/S_literal_Object_test.res @@ -116,7 +116,7 @@ module Common = { t->U.assertCompiledCode( ~schema, ~op=#Parse, - `i=>{(i===e[0]||i&&i.constructor===Object&&Object.keys(i).length===1&&i["foo"]==="bar")||e[1](i);return i}`, + `i=>{if(i!==e[0]&&(!i||i.constructor!==Object||Object.keys(i).length!==1||i["foo"]!=="bar")){e[1](i)}return i}`, ) }) @@ -126,7 +126,7 @@ module Common = { t->U.assertCompiledCode( ~schema, ~op=#Serialize, - `i=>{(i===e[0]||i&&i.constructor===Object&&Object.keys(i).length===1&&i["foo"]==="bar")||e[1](i);return i}`, + `i=>{if(i!==e[0]&&(!i||i.constructor!==Object||Object.keys(i).length!==1||i["foo"]!=="bar")){e[1](i)}return i}`, ) }) } diff --git a/packages/tests/src/core/S_literal_String_test.res b/packages/tests/src/core/S_literal_String_test.res index 453965e9..0ae345ac 100644 --- a/packages/tests/src/core/S_literal_String_test.res +++ b/packages/tests/src/core/S_literal_String_test.res @@ -71,7 +71,11 @@ module Common = { test("Compiled parse code snapshot", t => { let schema = factory() - t->U.assertCompiledCode(~schema, ~op=#Parse, `i=>{i==="ReScript is Great!"||e[0](i);return i}`) + t->U.assertCompiledCode( + ~schema, + ~op=#Parse, + `i=>{if(i!=="ReScript is Great!"){e[0](i)}return i}`, + ) }) test("Compiled serialize code snapshot", t => { @@ -80,7 +84,7 @@ module Common = { t->U.assertCompiledCode( ~schema, ~op=#Serialize, - `i=>{i==="ReScript is Great!"||e[0](i);return i}`, + `i=>{if(i!=="ReScript is Great!"){e[0](i)}return i}`, ) }) } diff --git a/packages/tests/src/core/S_literal_Undefined_test.res b/packages/tests/src/core/S_literal_Undefined_test.res index 77a12851..dc62bd08 100644 --- a/packages/tests/src/core/S_literal_Undefined_test.res +++ b/packages/tests/src/core/S_literal_Undefined_test.res @@ -48,12 +48,12 @@ module Common = { test("Compiled parse code snapshot", t => { let schema = factory() - t->U.assertCompiledCode(~schema, ~op=#Parse, `i=>{i===undefined||e[0](i);return i}`) + t->U.assertCompiledCode(~schema, ~op=#Parse, `i=>{if(i!==undefined){e[0](i)}return i}`) }) test("Compiled serialize code snapshot", t => { let schema = factory() - t->U.assertCompiledCode(~schema, ~op=#Serialize, `i=>{i===undefined||e[0](i);return i}`) + t->U.assertCompiledCode(~schema, ~op=#Serialize, `i=>{if(i!==undefined){e[0](i)}return i}`) }) } diff --git a/packages/tests/src/core/S_object_test.res b/packages/tests/src/core/S_object_test.res index 4357d52c..068c9c94 100644 --- a/packages/tests/src/core/S_object_test.res +++ b/packages/tests/src/core/S_object_test.res @@ -1143,7 +1143,7 @@ module Compiled = { t->U.assertCompiledCode( ~schema, ~op=#Parse, - `i=>{if(!i||i.constructor!==Object){e[5](i)}let v0=i["tag"],v1=i["FOO"],v2=i["BAR"],v3;v0===0||e[0](v0);if(typeof v1!=="string"){e[1](v1)}if(typeof v2!=="boolean"){e[2](v2)}for(v3 in i){if(v3!=="tag"&&v3!=="FOO"&&v3!=="BAR"){e[3](v3)}}return {"foo":v1,"bar":v2,"zoo":e[4],}}`, + `i=>{if(!i||i.constructor!==Object){e[5](i)}let v0=i["tag"],v1=i["FOO"],v2=i["BAR"],v3;if(v0!==0){e[0](v0)}if(typeof v1!=="string"){e[1](v1)}if(typeof v2!=="boolean"){e[2](v2)}for(v3 in i){if(v3!=="tag"&&v3!=="FOO"&&v3!=="BAR"){e[3](v3)}}return {"foo":v1,"bar":v2,"zoo":e[4],}}`, ) }, ) diff --git a/packages/tests/src/core/S_tuple_test.res b/packages/tests/src/core/S_tuple_test.res index 5de5a2dd..354d9d6e 100644 --- a/packages/tests/src/core/S_tuple_test.res +++ b/packages/tests/src/core/S_tuple_test.res @@ -285,7 +285,7 @@ module Compiled = { t->U.assertCompiledCode( ~schema, ~op=#Parse, - `i=>{if(!Array.isArray(i)||i.length!==3){e[4](i)}let v0=i["0"],v1=i["1"],v2=i["2"];v0===0||e[0](v0);if(typeof v1!=="string"){e[1](v1)}if(typeof v2!=="boolean"){e[2](v2)}return {"foo":v1,"bar":v2,"zoo":e[3],}}`, + `i=>{if(!Array.isArray(i)||i.length!==3){e[4](i)}let v0=i["0"],v1=i["1"],v2=i["2"];if(v0!==0){e[0](v0)}if(typeof v1!=="string"){e[1](v1)}if(typeof v2!=="boolean"){e[2](v2)}return {"foo":v1,"bar":v2,"zoo":e[3],}}`, ) }, ) diff --git a/packages/tests/src/core/S_union_test.res b/packages/tests/src/core/S_union_test.res index bc7f9f43..41eeba3e 100644 --- a/packages/tests/src/core/S_union_test.res +++ b/packages/tests/src/core/S_union_test.res @@ -100,7 +100,7 @@ test("Parses when both schemas misses parser and have different types", t => { t->U.assertCompiledCode( ~schema, ~op=#Parse, - `i=>{if(!(i==="apple")){if(typeof i!=="string"){e[0](i)}else{throw e[1]}}else{throw e[3]}return i}`, + `i=>{if(i!=="apple"){if(typeof i!=="string"){e[0](i)}else{throw e[1]}}else{throw e[3]}return i}`, ) }) @@ -141,7 +141,7 @@ test("Parses when second struct misses parser", t => { t->U.assertCompiledCode( ~schema, ~op=#Parse, - `i=>{if(!(i==="apple")){if(typeof i!=="string"){e[0](i)}else{throw e[1]}}else{i==="apple"||e[2](i);}return i}`, + `i=>{if(i!=="apple"){if(typeof i!=="string"){e[0](i)}else{throw e[1]}}else{if(i!=="apple"){e[2](i)}}return i}`, ) }) @@ -153,7 +153,7 @@ test("Serializes when second struct misses serializer", t => { t->U.assertCompiledCode( ~schema, ~op=#Serialize, - `i=>{let v0;try{i==="apple"||e[0](i);v0=i}catch(e0){e[2]([e0,e[1],])}return v0}`, + `i=>{let v0;try{if(i!=="apple"){e[0](i)}v0=i}catch(e0){e[2]([e0,e[1],])}return v0}`, ) }) @@ -405,7 +405,7 @@ module Advanced = { t->U.assertCompiledCode( ~schema=shapeSchema, ~op=#Parse, - `i=>{let v2=i;if(!i||i.constructor!==Object){e[0](i)}else{try{if(!i||i.constructor!==Object){e[1](i)}let v0=i["kind"],v1=i["radius"];v0==="circle"||e[2](v0);if(typeof v1!=="number"||Number.isNaN(v1)){e[3](v1)}v2={"TAG":e[4],"radius":v1,}}catch(e0){try{if(!i||i.constructor!==Object){e[5](i)}let v3=i["kind"],v4=i["x"];v3==="square"||e[6](v3);if(typeof v4!=="number"||Number.isNaN(v4)){e[7](v4)}v2={"TAG":e[8],"x":v4,}}catch(e1){try{if(!i||i.constructor!==Object){e[9](i)}let v5=i["kind"],v6=i["x"],v7=i["y"];v5==="triangle"||e[10](v5);if(typeof v6!=="number"||Number.isNaN(v6)){e[11](v6)}if(typeof v7!=="number"||Number.isNaN(v7)){e[12](v7)}v2={"TAG":e[13],"x":v6,"y":v7,}}catch(e2){e[14]([e0,e1,e2,])}}}}return v2}`, + `i=>{let v2=i;if(!i||i.constructor!==Object){e[0](i)}else{try{if(!i||i.constructor!==Object){e[1](i)}let v0=i["kind"],v1=i["radius"];if(v0!=="circle"){e[2](v0)}if(typeof v1!=="number"||Number.isNaN(v1)){e[3](v1)}v2={"TAG":e[4],"radius":v1,}}catch(e0){try{if(!i||i.constructor!==Object){e[5](i)}let v3=i["kind"],v4=i["x"];if(v3!=="square"){e[6](v3)}if(typeof v4!=="number"||Number.isNaN(v4)){e[7](v4)}v2={"TAG":e[8],"x":v4,}}catch(e1){try{if(!i||i.constructor!==Object){e[9](i)}let v5=i["kind"],v6=i["x"],v7=i["y"];if(v5!=="triangle"){e[10](v5)}if(typeof v6!=="number"||Number.isNaN(v6)){e[11](v6)}if(typeof v7!=="number"||Number.isNaN(v7)){e[12](v7)}v2={"TAG":e[13],"x":v6,"y":v7,}}catch(e2){e[14]([e0,e1,e2,])}}}}return v2}`, ) }) @@ -441,7 +441,7 @@ test("Compiled parse code snapshot", t => { t->U.assertCompiledCode( ~schema, ~op=#Parse, - `i=>{if(!(i===0)){if(!(i===1)){e[0](i)}else{i===1||e[1](i);}}else{i===0||e[2](i);}return i}`, + `i=>{if(i!==0){if(i!==1){e[0](i)}else{if(i!==1){e[1](i)}}}else{if(i!==0){e[2](i)}}return i}`, ) }) @@ -454,7 +454,7 @@ test("Compiled async parse code snapshot", t => { t->U.assertCompiledCode( ~schema, ~op=#Parse, - `i=>{let v0=i;if(!(i===0)){if(!(i===1)){e[0](i)}else{i===1||e[1](i);}}else{i===0||e[2](i);v0=e[3](i)}return Promise.resolve(v0)}`, + `i=>{let v0=i;if(i!==0){if(i!==1){e[0](i)}else{if(i!==1){e[1](i)}}}else{if(i!==0){e[2](i)}v0=e[3](i)}return Promise.resolve(v0)}`, ) }) @@ -465,7 +465,7 @@ test("Compiled serialize code snapshot", t => { t->U.assertCompiledCode( ~schema, ~op=#Serialize, - `i=>{let v0;try{i===0||e[0](i);v0=i}catch(e0){try{i===1||e[1](i);v0=i}catch(e1){e[2]([e0,e1,])}}return v0}`, + `i=>{let v0;try{if(i!==0){e[0](i)}v0=i}catch(e0){try{if(i!==1){e[1](i)}v0=i}catch(e1){e[2]([e0,e1,])}}return v0}`, ) }) diff --git a/packages/tests/src/core/S_variant_test.res b/packages/tests/src/core/S_variant_test.res index 100a4140..1b90320b 100644 --- a/packages/tests/src/core/S_variant_test.res +++ b/packages/tests/src/core/S_variant_test.res @@ -180,7 +180,7 @@ test( t->U.assertCompiledCode( ~schema, ~op=#Serialize, - `i=>{let v0=e[2];if(i!==e[0]){e[1](i)}(v0===e[3]||Array.isArray(v0)&&v0.length===2&&v0[0]===true&&v0[1]===12)||e[4](v0);return v0}`, + `i=>{let v0=e[2];if(i!==e[0]){e[1](i)}if(v0!==e[3]&&(!Array.isArray(v0)||v0.length!==2||v0[0]!==true||v0[1]!==12)){e[4](v0)}return v0}`, ) }, ) diff --git a/src/S_Core.bs.mjs b/src/S_Core.bs.mjs index 250f6723..e9a60960 100644 --- a/src/S_Core.bs.mjs +++ b/src/S_Core.bs.mjs @@ -425,33 +425,33 @@ function toString(literal) { return literal.s; } -function arrayCheckBuilder(b, inputVar, literal) { +function arrayFilterBuilder(b, inputVar, literal) { var items = literal.i; - return "(" + inputVar + "===" + ("e[" + (b.g.e.push(literal.value) - 1) + "]") + "||Array.isArray(" + inputVar + ")&&" + inputVar + ".length===" + items.length + ( - items.length > 0 ? "&&" + items.map(function (literal, idx) { - return literal.b(b, inputVar + "[" + idx + "]", literal); - }).join("&&") : "" + return inputVar + "!==" + ("e[" + (b.g.e.push(literal.value) - 1) + "]") + "&&(!Array.isArray(" + inputVar + ")||" + inputVar + ".length!==" + items.length + ( + items.length > 0 ? "||" + items.map(function (literal, idx) { + return literal.f(b, inputVar + "[" + idx + "]", literal); + }).join("||") : "" ) + ")"; } -function dictCheckBuilder(b, inputVar, literal) { +function dictFilterBuilder(b, inputVar, literal) { var items = literal.i; var fields = Object.keys(items); var numberOfFields = fields.length; - return "(" + inputVar + "===" + ("e[" + (b.g.e.push(value) - 1) + "]") + "||" + inputVar + "&&" + inputVar + ".constructor===Object&&Object.keys(" + inputVar + ").length===" + numberOfFields + ( - numberOfFields > 0 ? "&&" + fields.map(function (field) { + return inputVar + "!==" + ("e[" + (b.g.e.push(value) - 1) + "]") + "&&(!" + inputVar + "||" + inputVar + ".constructor!==Object||Object.keys(" + inputVar + ").length!==" + numberOfFields + ( + numberOfFields > 0 ? "||" + fields.map(function (field) { var literal = items[field]; - return literal.b(b, inputVar + "[" + JSON.stringify(field) + "]", literal); - }).join("&&") : "" + return literal.f(b, inputVar + "[" + JSON.stringify(field) + "]", literal); + }).join("||") : "" ) + ")"; } -function inlinedStrictEqualCheckBuilder(param, inputVar, literal) { - return inputVar + "===" + literal.s; +function inlinedStrictEqualFilterBuilder(param, inputVar, literal) { + return inputVar + "!==" + literal.s; } -function strictEqualCheckBuilder(b, inputVar, literal) { - return inputVar + "===" + ("e[" + (b.g.e.push(literal.value) - 1) + "]"); +function strictEqualFilterBuilder(b, inputVar, literal) { + return inputVar + "!==" + ("e[" + (b.g.e.push(literal.value) - 1) + "]"); } var undefined_value = undefined; @@ -460,7 +460,7 @@ var $$undefined = { kind: "Undefined", value: undefined_value, s: "undefined", - b: inlinedStrictEqualCheckBuilder, + f: inlinedStrictEqualFilterBuilder, j: false }; @@ -470,21 +470,21 @@ var $$null = { kind: "Null", value: null_value, s: "null", - b: inlinedStrictEqualCheckBuilder, + f: inlinedStrictEqualFilterBuilder, j: true }; var nan_value = NaN; -function nan_b(param, inputVar, param$1) { - return "Number.isNaN(" + inputVar + ")"; +function nan_f(param, inputVar, param$1) { + return "!Number.isNaN(" + inputVar + ")"; } var nan = { kind: "NaN", value: nan_value, s: "NaN", - b: nan_b, + f: nan_f, j: false }; @@ -495,7 +495,7 @@ function parseInternal(value) { kind: "Symbol", value: value, s: value.toString(), - b: strictEqualCheckBuilder, + f: strictEqualFilterBuilder, j: false }; } else if (typeOfValue === "boolean") { @@ -503,7 +503,7 @@ function parseInternal(value) { kind: "Boolean", value: value, s: value ? "true" : "false", - b: inlinedStrictEqualCheckBuilder, + f: inlinedStrictEqualFilterBuilder, j: true }; } else if (typeOfValue === "string") { @@ -511,7 +511,7 @@ function parseInternal(value) { kind: "String", value: value, s: JSON.stringify(value), - b: inlinedStrictEqualCheckBuilder, + f: inlinedStrictEqualFilterBuilder, j: true }; } else if (typeOfValue === "function") { @@ -519,7 +519,7 @@ function parseInternal(value) { kind: "Function", value: value, s: value.toString(), - b: strictEqualCheckBuilder, + f: strictEqualFilterBuilder, j: false }; } else if (typeOfValue === "object") { @@ -545,7 +545,7 @@ function parseInternal(value) { kind: "Array", value: value, s: string + "]", - b: arrayCheckBuilder, + f: arrayFilterBuilder, j: isJsonable, i: Caml_option.some(items) }; @@ -572,7 +572,7 @@ function parseInternal(value) { kind: "Dict", value: value, s: string$1 + "}", - b: dictCheckBuilder, + f: dictFilterBuilder, j: isJsonable$1, i: Caml_option.some(items$1) }; @@ -581,7 +581,7 @@ function parseInternal(value) { kind: "Object", value: value, s: Object.prototype.toString.call(value), - b: strictEqualCheckBuilder, + f: strictEqualFilterBuilder, j: false }; } @@ -595,7 +595,7 @@ function parseInternal(value) { kind: "Number", value: value, s: value.toString(), - b: inlinedStrictEqualCheckBuilder, + f: inlinedStrictEqualFilterBuilder, j: true }; } @@ -604,7 +604,7 @@ function parseInternal(value) { kind: "BigInt", value: value, s: value.toString() + "n", - b: inlinedStrictEqualCheckBuilder, + f: inlinedStrictEqualFilterBuilder, j: false }; } @@ -1168,13 +1168,13 @@ function literal(value) { registerInvalidJson(b, selfSchema, path); } var inputVar = $$var(b, input); - b.c = b.c + (literal$1.b(b, inputVar, literal$1) + "||" + failWithArg(b, path, (function (input) { + b.c = b.c + ("if(" + literal$1.f(b, inputVar, literal$1) + "){" + failWithArg(b, path, (function (input) { return { TAG: "InvalidLiteral", expected: literal$1, received: input }; - }), inputVar) + ";"); + }), inputVar) + "}"); return input; }; return make((function () { @@ -1992,7 +1992,7 @@ function factory$8(schemas) { typeFilterCode = "false"; } else { var literal$1 = literal._0; - typeFilterCode = "!(" + literal$1.b(b, inputVar, literal$1) + ")"; + typeFilterCode = literal$1.f(b, inputVar, literal$1); } } var schemas$1 = Js_dict.get(groupsByTypeFilter, typeFilterCode); diff --git a/src/S_Core.res b/src/S_Core.res index df1cc381..d272aa87 100644 --- a/src/S_Core.res +++ b/src/S_Core.res @@ -787,8 +787,8 @@ module Literal = { value: unknown, @as("s") string: string, - @as("b") - checkBuilder: (b, ~inputVar: string, ~literal: literal) => string, + @as("f") + filterBuilder: (b, ~inputVar: string, ~literal: literal) => string, @as("j") isJsonable: bool, @as("i") @@ -820,61 +820,63 @@ module Literal = { @inline let toString = literal => (literal->toInternal).string - let arrayCheckBuilder = (b, ~inputVar, ~literal) => { + // FIXME: + let arrayFilterBuilder = (b, ~inputVar, ~literal) => { let items = (literal->toInternal).items->(Obj.magic: option => array) - `(${inputVar}===${b->B.embed( + `${inputVar}!==${b->B.embed( literal->value, - )}||Array.isArray(${inputVar})&&${inputVar}.length===${items + )}&&(!Array.isArray(${inputVar})||${inputVar}.length!==${items ->Js.Array2.length ->Stdlib.Int.unsafeToString}` ++ (items->Js.Array2.length > 0 - ? "&&" ++ + ? "||" ++ items ->Js.Array2.mapi((literal, idx) => - b->literal.checkBuilder( + b->literal.filterBuilder( ~inputVar=`${inputVar}[${idx->Stdlib.Int.unsafeToString}]`, ~literal=literal->toPublic, ) ) - ->Js.Array2.joinWith("&&") + ->Js.Array2.joinWith("||") : "") ++ ")" } - let dictCheckBuilder = (b, ~inputVar, ~literal) => { + // FIXME: + let dictFilterBuilder = (b, ~inputVar, ~literal) => { let items = (literal->toInternal).items->(Obj.magic: option => dict) let fields = items->Js.Dict.keys let numberOfFields = fields->Js.Array2.length - `(${inputVar}===${b->B.embed( + `${inputVar}!==${b->B.embed( value, - )}||${inputVar}&&${inputVar}.constructor===Object&&Object.keys(${inputVar}).length===${numberOfFields->Stdlib.Int.unsafeToString}` ++ + )}&&(!${inputVar}||${inputVar}.constructor!==Object||Object.keys(${inputVar}).length!==${numberOfFields->Stdlib.Int.unsafeToString}` ++ (numberOfFields > 0 - ? "&&" ++ + ? "||" ++ fields ->Js.Array2.map(field => { let literal = items->Js.Dict.unsafeGet(field) - b->literal.checkBuilder( + b->literal.filterBuilder( ~inputVar=`${inputVar}[${field->Stdlib.Inlined.Value.fromString}]`, ~literal=literal->toPublic, ) }) - ->Js.Array2.joinWith("&&") + ->Js.Array2.joinWith("||") : "") ++ ")" } - let inlinedStrictEqualCheckBuilder = (_, ~inputVar, ~literal) => - `${inputVar}===${literal->toString}` + let inlinedStrictEqualFilterBuilder = (_, ~inputVar, ~literal) => + `${inputVar}!==${literal->toString}` - let strictEqualCheckBuilder = (b, ~inputVar, ~literal) => - `${inputVar}===${b->B.embed(literal->value)}` + let strictEqualFilterBuilder = (b, ~inputVar, ~literal) => + `${inputVar}!==${b->B.embed(literal->value)}` let undefined = { kind: Undefined, value: %raw(`undefined`), string: "undefined", isJsonable: false, - checkBuilder: inlinedStrictEqualCheckBuilder, + filterBuilder: inlinedStrictEqualFilterBuilder, } let null = { @@ -882,7 +884,7 @@ module Literal = { value: %raw(`null`), string: "null", isJsonable: true, - checkBuilder: inlinedStrictEqualCheckBuilder, + filterBuilder: inlinedStrictEqualFilterBuilder, } let nan = { @@ -890,7 +892,7 @@ module Literal = { value: %raw(`NaN`), string: "NaN", isJsonable: false, - checkBuilder: (_, ~inputVar, ~literal as _) => `Number.isNaN(${inputVar})`, + filterBuilder: (_, ~inputVar, ~literal as _) => `!Number.isNaN(${inputVar})`, } let string = value => { @@ -899,7 +901,7 @@ module Literal = { value: value->castAnyToUnknown, string: Stdlib.Inlined.Value.fromString(value), isJsonable: true, - checkBuilder: inlinedStrictEqualCheckBuilder, + filterBuilder: inlinedStrictEqualFilterBuilder, } } @@ -909,7 +911,7 @@ module Literal = { value: value->castAnyToUnknown, string: value ? "true" : "false", isJsonable: true, - checkBuilder: inlinedStrictEqualCheckBuilder, + filterBuilder: inlinedStrictEqualFilterBuilder, } } @@ -919,7 +921,7 @@ module Literal = { value: value->castAnyToUnknown, string: value->Js.Float.toString, isJsonable: true, - checkBuilder: inlinedStrictEqualCheckBuilder, + filterBuilder: inlinedStrictEqualFilterBuilder, } } @@ -929,7 +931,7 @@ module Literal = { value: value->castAnyToUnknown, string: value->Symbol.toString, isJsonable: false, - checkBuilder: strictEqualCheckBuilder, + filterBuilder: strictEqualFilterBuilder, } } @@ -939,7 +941,7 @@ module Literal = { value: value->castAnyToUnknown, string: value->BigInt.toString, isJsonable: false, - checkBuilder: inlinedStrictEqualCheckBuilder, + filterBuilder: inlinedStrictEqualFilterBuilder, } } @@ -949,7 +951,7 @@ module Literal = { value: value->castAnyToUnknown, string: value->Stdlib.Function.toString, isJsonable: false, - checkBuilder: strictEqualCheckBuilder, + filterBuilder: strictEqualFilterBuilder, } } @@ -959,7 +961,7 @@ module Literal = { value: value->castAnyToUnknown, string: value->Object.internalClass, isJsonable: false, - checkBuilder: strictEqualCheckBuilder, + filterBuilder: strictEqualFilterBuilder, } } @@ -1010,7 +1012,7 @@ module Literal = { items: items->castAnyToUnknown, string: string.contents ++ "}", isJsonable: isJsonable.contents, - checkBuilder: dictCheckBuilder, + filterBuilder: dictFilterBuilder, } } and array = value => { @@ -1036,7 +1038,7 @@ module Literal = { items: items->castAnyToUnknown, isJsonable: isJsonable.contents, string: string.contents ++ "]", - checkBuilder: arrayCheckBuilder, + filterBuilder: arrayFilterBuilder, } } @@ -1691,14 +1693,14 @@ let literal = value => { let inputVar = b->B.Val.var(input) b.code = b.code ++ - `${b->internalLiteral.checkBuilder(~inputVar, ~literal)}||${b->B.failWithArg( + `if(${b->internalLiteral.filterBuilder(~inputVar, ~literal)}){${b->B.failWithArg( ~path, input => InvalidLiteral({ expected: literal, received: input, }), inputVar, - )};` + )}}` input }) make( @@ -2906,7 +2908,7 @@ module Union = { | None => switch schema.rawTagged { | Literal(literal) => - `!(${b->(literal->Literal.toInternal).checkBuilder(~inputVar, ~literal)})` + b->(literal->Literal.toInternal).filterBuilder(~inputVar, ~literal) | _ => "false" } }