Skip to content

Commit 80d8de5

Browse files
giacomocavalierilpil
authored andcommitted
fix assert code generation bug on the JavaScript target
the compiler would generate invalid JavaScript code for `assert` statements that: - compared two values using `==`, and - the second value was a call - whose argument was a pipeline expression
1 parent 048d7c1 commit 80d8de5

File tree

4 files changed

+76
-6
lines changed

4 files changed

+76
-6
lines changed

CHANGELOG.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@
8787
or `let assert` would not be formatted properly when preceded by a comment.
8888
([Giacomo Cavalieri](https://github.com/giacomocavalieri))
8989

90+
- Fixed a bug where the compiler would generate invalid code for an `assert`
91+
using pipes on the JavaScript target.
92+
([Giacomo Cavalieri](https://github.com/giacomocavalieri))
93+
9094
## v1.12.0-rc1 - 2025-07-18
9195

9296
### Compiler
@@ -360,7 +364,8 @@
360364
### Build tool
361365

362366
- `gleam update`, `gleam deps update`, and `gleam deps download` will now print
363-
a message when there are new major versions of packages available. For example:
367+
a message when there are new major versions of packages available. For
368+
example:
364369

365370
```text
366371
$ gleam update
@@ -668,8 +673,8 @@
668673
properly.
669674
([Giacomo Cavalieri](https://github.com/giacomocavalieri))
670675

671-
- Fixed a bug where fields of custom types named `prototype` would not be properly
672-
escaped on the JavaScript target.
676+
- Fixed a bug where fields of custom types named `prototype` would not be
677+
properly escaped on the JavaScript target.
673678
([Surya Rose](https://github.com/GearsDatapacks))
674679

675680
## v1.11.1 - 2025-06-05

compiler-core/src/javascript/expression.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -613,20 +613,21 @@ impl<'module, 'a> Generator<'module, 'a> {
613613
{
614614
// Save initial state
615615
let scope_position = std::mem::replace(&mut self.scope_position, Position::Tail);
616+
let statement_level = std::mem::take(&mut self.statement_level);
616617

617618
// Set state for in this iife
618619
let current_scope_vars = self.current_scope_vars.clone();
619620

620621
// Generate the expression
621622
let result = to_doc(self, statements);
623+
let doc = self.add_statement_level(result);
624+
let doc = immediately_invoked_function_expression_document(doc);
622625

623626
// Reset
624627
self.current_scope_vars = current_scope_vars;
625628
self.scope_position = scope_position;
629+
self.statement_level = statement_level;
626630

627-
// Wrap in iife document
628-
let doc =
629-
immediately_invoked_function_expression_document(self.add_statement_level(result));
630631
self.wrap_return(doc)
631632
}
632633

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,3 +187,16 @@ pub fn main() {
187187
"
188188
);
189189
}
190+
191+
#[test]
192+
fn prova() {
193+
assert_js!(
194+
"
195+
pub fn main() {
196+
assert Ok([]) == Ok([] |> id)
197+
}
198+
199+
fn id(x) { x }
200+
"
201+
)
202+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
---
2+
source: compiler-core/src/javascript/tests/assert.rs
3+
expression: "\npub fn main() {\n assert Ok([]) == Ok([] |> id)\n}\n\nfn id(x) { x }\n"
4+
---
5+
----- SOURCE CODE
6+
7+
pub fn main() {
8+
assert Ok([]) == Ok([] |> id)
9+
}
10+
11+
fn id(x) { x }
12+
13+
14+
----- COMPILED JAVASCRIPT
15+
import { Ok, toList, makeError, isEqual } from "../gleam.mjs";
16+
17+
const FILEPATH = "src/module.gleam";
18+
19+
function id(x) {
20+
return x;
21+
}
22+
23+
export function main() {
24+
let $ = new Ok(toList([]));
25+
let $1 = new Ok(
26+
(() => {
27+
let _pipe = toList([]);
28+
return id(_pipe);
29+
})(),
30+
);
31+
if (!(isEqual($, $1))) {
32+
throw makeError(
33+
"assert",
34+
FILEPATH,
35+
"my/mod",
36+
3,
37+
"main",
38+
"Assertion failed.",
39+
{
40+
kind: "binary_operator",
41+
operator: "==",
42+
left: { kind: "literal", value: $, start: 26, end: 32 },
43+
right: { kind: "expression", value: $1, start: 36, end: 48 },
44+
start: 19,
45+
end: 48,
46+
expression_start: 26
47+
}
48+
)
49+
}
50+
return undefined;
51+
}

0 commit comments

Comments
 (0)