Remove same types restriction on PYSON If

issue11644
review419661003
This commit is contained in:
Cédric Krier
2022-09-08 13:17:54 +02:00
parent d146793216
commit a34a59d35d
2 changed files with 19 additions and 10 deletions
+16 -6
View File
@@ -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,
+3 -4
View File
@@ -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(