Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Numeric not handling trailing zeros #3371

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
# Change versionKind to one of: internal, fix, dependencies, feature, deprecation, breaking
changeKind: fix
packages:
- "@typespec/compiler"
---

Numeric not handling trailing zeros and causing freeze(e.g. `const a = 100.0`)
7 changes: 5 additions & 2 deletions packages/compiler/src/core/numeric.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,14 +118,17 @@ function parse(original: string): InternalData {
}

let end = stringValue.length;
while (stringValue[end - 1] === "0") {
if (end < adjustedPointIndex) {
let index = end;
timotheeguerin marked this conversation as resolved.
Show resolved Hide resolved
while (stringValue[index - 1] === "0") {
timotheeguerin marked this conversation as resolved.
Show resolved Hide resolved
if (index < adjustedPointIndex - 1) {
// if we are looking at a zero before the decimal point, we need to decrease the exponent
exp++;
} else {
end--;
}
index--;
}

try {
stringValue = stringValue.slice(0, end);
stringValue = stringValue + "0".repeat(Math.max(exp - stringValue.length, 0)); // add remaining zeros for cases like 3e30
Expand Down
5 changes: 5 additions & 0 deletions packages/compiler/test/core/numeric.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ describe("parsing", () => {
it("simple decimal", () => {
expectNumericData("123.456", 123456n, 3);
});

it("decimal with trailing zeros", () => {
timotheeguerin marked this conversation as resolved.
Show resolved Hide resolved
expectNumericData("100.0", 100n, 3);
});

it("negative decimal", () => {
expectNumericData("-123.456", 123456n, 3, -1);
});
Expand Down
Loading