From a34a59d35d33770eacd55b715e2ddc8cd65020ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Thu, 8 Sep 2022 13:17:54 +0200 Subject: [PATCH] Remove same types restriction on PYSON If issue11644 review419661003 --- src/pyson.js | 22 ++++++++++++++++------ tests/sao.js | 7 +++---- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/src/pyson.js b/src/pyson.js index 16522803eb..05a085b5c0 100644 --- a/src/pyson.js +++ b/src/pyson.js @@ -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 --git a/tests/sao.js b/tests/sao.js index a78ffcf91c..d86022c52b 100644 --- a/tests/sao.js +++ b/tests/sao.js @@ -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(