Skip to content

Commit

Permalink
fix(compiler): returning negative bigints from asBigInit (#5295)
Browse files Browse the repository at this point in the history
Co-authored-by: Will Temple <[email protected]>
  • Loading branch information
TomerAberbach and witemple-msft authored Dec 22, 2024
1 parent ac41d8a commit 477560b
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
7 changes: 7 additions & 0 deletions .chronus/changes/fix-negative-big-int-2024-12-06-23-51-23.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: fix
packages:
- "@typespec/compiler"
---

Fix incorrectly returning a positive `BigInt` for a negative `Numeric`.
7 changes: 6 additions & 1 deletion packages/compiler/src/core/numeric.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,12 @@ const NumericPrototype = {
return equals(this[InternalDataSym], Numeric(num.toString())[InternalDataSym]) ? num : null;
},
asBigInt: function (this: Numeric) {
return this.isInteger ? this[InternalDataSym].n : null;
if (!this.isInteger) {
return null;
}

const { s, n } = this[InternalDataSym];
return BigInt(s) * n;
},
equals: function (this: Numeric, other: Numeric) {
return equals(this[InternalDataSym], other[InternalDataSym]);
Expand Down
2 changes: 2 additions & 0 deletions packages/compiler/test/core/numeric.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ describe("asNumber", () => {
["123.456", 123.456],
["123.00", 123],
["123456789123456789123456789123456789", null],
["-123456789123456789123456789123456789", null],
["123456789123456789.123456789123456789", null],
])("%s => %d", (a, b) => {
const numeric = Numeric(a);
Expand All @@ -229,6 +230,7 @@ describe("asBigInt", () => {
["123.456", null],
["123.00", 123n],
["123456789123456789123456789123456789", 123456789123456789123456789123456789n],
["-123456789123456789123456789123456789", -123456789123456789123456789123456789n],
["123456789123456789.123456789123456789", null],
])("%s => %d", (a, b) => {
const numeric = Numeric(a);
Expand Down

0 comments on commit 477560b

Please sign in to comment.