fix(javascript): preserve primitive addition coercion (#666)
Signed-off-by: RudyCelekli <47457359+rudycelekli@users.noreply.github.com>
This commit is contained in:
@@ -273,3 +273,20 @@ describe("JavaScript semantic analysis: dataflow 2", () => {
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("primitive addition values", () => {
|
||||
it.each([
|
||||
["true + 1", 2],
|
||||
["null + 2", 2],
|
||||
["false + true", 1],
|
||||
['"x" + false', "xfalse"],
|
||||
['null + "x"', "nullx"],
|
||||
["3 + 4", 7],
|
||||
])("recovers %s using primitive coercion", (expression, expected) => {
|
||||
const ir = analyzeJavaScriptSemantics(`const answer = ${expression};`);
|
||||
expect(topLevelBinding(ir, "answer").value).toEqual({
|
||||
status: "literal",
|
||||
value: expected,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -247,9 +247,9 @@ const evaluateAddition = (
|
||||
return { status: "unknown", reason: "Non-primitive addition." };
|
||||
const values = left.flatMap((leftValue) =>
|
||||
right.map((rightValue) =>
|
||||
typeof leftValue === "number" && typeof rightValue === "number"
|
||||
? leftValue + rightValue
|
||||
: `${String(leftValue)}${String(rightValue)}`,
|
||||
typeof leftValue === "string" || typeof rightValue === "string"
|
||||
? `${String(leftValue)}${String(rightValue)}`
|
||||
: Number(leftValue) + Number(rightValue),
|
||||
),
|
||||
);
|
||||
return primitiveSet(values);
|
||||
|
||||
Reference in New Issue
Block a user