Skip to content

Commit ed81131

Browse files
committed
fix js codegen allowing underscore after decimal
1 parent afcd7e9 commit ed81131

8 files changed

+74
-18
lines changed

compiler-core/src/javascript/expression.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ impl<'module, 'a> Generator<'module, 'a> {
288288
TypedExpr::String { value, .. } => string(value),
289289

290290
TypedExpr::Int { value, .. } => int(value),
291-
TypedExpr::Float { value, .. } => float(value),
291+
TypedExpr::Float { float_value, .. } => float_value.value().to_doc(),
292292

293293
TypedExpr::List { elements, tail, .. } => {
294294
self.not_in_tail_position(Some(Ordering::Strict), |this| match tail {

compiler-core/src/javascript/tests/numbers.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,31 @@ pub fn main() {
431431
);
432432
}
433433

434+
#[test]
435+
fn underscore_after_decimal_point() {
436+
assert_js!(
437+
"
438+
pub fn main() {
439+
0._1
440+
}
441+
"
442+
);
443+
}
444+
445+
#[test]
446+
fn underscore_after_decimal_point_case_statement() {
447+
assert_js!(
448+
"
449+
pub fn main(x) {
450+
case x {
451+
0._1 -> \"bar\"
452+
_ -> \"foo\"
453+
}
454+
}
455+
"
456+
);
457+
}
458+
434459
#[test]
435460
fn division_by_zero_float() {
436461
assert_js!(

compiler-core/src/javascript/tests/snapshots/gleam_core__javascript__tests__numbers__float_literals.snap

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
---
22
source: compiler-core/src/javascript/tests/numbers.rs
3-
assertion_line: 23
43
expression: "\npub fn go() {\n 1.5\n 2.0\n -0.1\n 1.\n}\n"
5-
snapshot_kind: text
64
---
75
----- SOURCE CODE
86

@@ -19,5 +17,5 @@ export function go() {
1917
1.5;
2018
2.0;
2119
-0.1;
22-
return 1.;
20+
return 1.0;
2321
}
Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
---
22
source: compiler-core/src/javascript/tests/numbers.rs
3-
assertion_line: 37
43
expression: "\npub fn go() {\n 0.01e-1\n 0.01e-0\n -10.01e-1\n -10.01e-0\n 100.001e523\n -100.001e-523\n 100.001e123_456_789\n -100.001e-123_456_789\n}\n"
5-
snapshot_kind: text
64
---
75
----- SOURCE CODE
86

@@ -20,12 +18,12 @@ pub fn go() {
2018

2119
----- COMPILED JAVASCRIPT
2220
export function go() {
23-
0.01e-1;
24-
0.01e-0;
25-
-10.01e-1;
26-
-10.01e-0;
27-
100.001e523;
28-
-100.001e-523;
29-
100.001e123_456_789;
30-
return -100.001e-123_456_789;
21+
0.001;
22+
0.01;
23+
-1.001;
24+
-10.01;
25+
inf;
26+
-0.0;
27+
inf;
28+
return -0.0;
3129
}
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
---
22
source: compiler-core/src/javascript/tests/numbers.rs
3-
assertion_line: 264
43
expression: "\npub fn main() {\n 09_179.1\n}\n"
5-
snapshot_kind: text
64
---
75
----- SOURCE CODE
86

@@ -13,5 +11,5 @@ pub fn main() {
1311

1412
----- COMPILED JAVASCRIPT
1513
export function main() {
16-
return 9_179.1;
14+
return 9179.1;
1715
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
source: compiler-core/src/javascript/tests/numbers.rs
3+
expression: "\npub fn main() {\n 0._1\n}\n"
4+
---
5+
----- SOURCE CODE
6+
7+
pub fn main() {
8+
0._1
9+
}
10+
11+
12+
----- COMPILED JAVASCRIPT
13+
export function main() {
14+
return 0.1;
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
---
2+
source: compiler-core/src/javascript/tests/numbers.rs
3+
expression: "\npub fn main(x) {\n case x {\n 0._1 -> \"bar\"\n _ -> \"foo\"\n }\n}\n"
4+
---
5+
----- SOURCE CODE
6+
7+
pub fn main(x) {
8+
case x {
9+
0._1 -> "bar"
10+
_ -> "foo"
11+
}
12+
}
13+
14+
15+
----- COMPILED JAVASCRIPT
16+
export function main(x) {
17+
if (x === 0.1) {
18+
return "bar";
19+
} else {
20+
return "foo";
21+
}
22+
}

compiler-core/src/javascript/tests/snapshots/gleam_core__javascript__tests__numbers__wide_float_div.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@ pub fn go() {
1111

1212
----- COMPILED JAVASCRIPT
1313
export function go() {
14-
return 111111111111111111111111111111. / 22222222222222222222222222222222222.;
14+
return 1.111111111111111e29 / 2.222222222222222e34;
1515
}

0 commit comments

Comments
 (0)