changeset 3aa7475631ac in sao:default
details: https://hg.tryton.org/sao?cmd=changeset&node=3aa7475631ac
description:
        Remove same types restriction on PYSON If

        issue11644
        review419661003
diffstat:

 src/pyson.js |  22 ++++++++++++++++------
 tests/sao.js |   7 +++----
 2 files changed, 19 insertions(+), 10 deletions(-)

diffs (65 lines):

diff -r 5ea9ffa9c692 -r 3aa7475631ac src/pyson.js
--- a/src/pyson.js      Thu Sep 08 13:08:39 2022 +0200
+++ b/src/pyson.js      Thu Sep 08 13:17:54 2022 +0200
@@ -532,10 +532,6 @@
             } else {
                 else_types = [typeof else_statement];
             }
-            if (jQuery(then_types).not(else_types).length ||
-                jQuery(else_types).not(then_types).length) {
-                throw 'then and else statements must be the same type';
-            }
             this._condition = condition;
             this._then_statement = then_statement;
             this._else_statement = else_statement;
@@ -549,11 +545,25 @@
             };
         },
         types: function() {
+            var types;
             if (this._then_statement instanceof Sao.PYSON.PYSON) {
-                return this._then_statement.types();
+                types = this._then_statement.types();
             } else {
-                return [typeof this._then_statement];
+                types = [typeof this._then_statement];
             }
+            if (this._else_statement instanceof Sao.PYSON.PYSON) {
+                for (const type of this._else_statement.types()) {
+                    if (!~types.indexOf(type)) {
+                        types.push(type);
+                    }
+                }
+            } else {
+                const type = typeof this._else_statement;
+                if (!~types.indexOf(type)) {
+                    types.push(type);
+                }
+            }
+            return types;
         },
         __string_params__: function() {
             return [this._condition, this._then_statement,
diff -r 5ea9ffa9c692 -r 3aa7475631ac tests/sao.js
--- a/tests/sao.js      Thu Sep 08 13:08:39 2022 +0200
+++ b/tests/sao.js      Thu Sep 08 13:17:54 2022 +0200
@@ -644,16 +644,15 @@
         QUnit.strictEqual(value.t, 'foo', "If(true, 'foo', 'bar')");
         QUnit.strictEqual(value.e, 'bar', "If(true, 'foo', 'bar')");
 
-        QUnit.throws(function() {
-            new Sao.PYSON.If(true, 'foo', false);
-        }, 'then and else statements must be the same type');
-
         QUnit.ok(Sao.common.compare(
                 new Sao.PYSON.If(true, 'foo', 'bar').types(),
                 [typeof 'foo']), "If(true, 'foo', 'bar').types()");
         QUnit.ok(Sao.common.compare(
                 new Sao.PYSON.If(true, false, true).types(),
                 [typeof true]), 'If(true, false, true).types()');
+        QUnit.ok(Sao.common.compare(
+                new Sao.PYSON.If(true, 'foo', false).types(),
+                [typeof 'foo', typeof true]), "If(true, 'foo', 
false).types()");
 
         var eval_;
         eval_ = new Sao.PYSON.Encoder().encode(

Reply via email to