Skip to content

Commit 1e147d1

Browse files
GearsDatapackslpil
authored andcommitted
Fix codegen bug with iifes
1 parent 90e8164 commit 1e147d1

File tree

4 files changed

+68
-4
lines changed

4 files changed

+68
-4
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,3 +395,7 @@
395395
- Fixed a bug where the compiler would not correctly check the size of bit
396396
arrays when doing bit array pattern matching on the JavaScript target.
397397
([Giacomo Cavalieri](https://github.com/giacomocavalieri))
398+
399+
- Fixed a bug where using the pipe operator inside a record update would cause
400+
the compiler to generate invalid code on the JavaScript target.
401+
([Surya Rose](https://github.com/GearsDatapacks))

compiler-core/src/javascript/expression.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -225,9 +225,7 @@ impl<'module, 'a> Generator<'module, 'a> {
225225
} else {
226226
let mut statements = std::mem::take(&mut self.statement_level);
227227
statements.push(expression);
228-
Itertools::intersperse(statements.into_iter(), line())
229-
.collect_vec()
230-
.to_doc()
228+
join(statements.into_iter(), line())
231229
}
232230
}
233231

@@ -582,7 +580,8 @@ impl<'module, 'a> Generator<'module, 'a> {
582580
self.scope_position = scope_position;
583581

584582
// Wrap in iife document
585-
let doc = immediately_invoked_function_expression_document(result?);
583+
let doc =
584+
immediately_invoked_function_expression_document(self.add_statement_level(result?));
586585
Ok(self.wrap_return(doc))
587586
}
588587

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,3 +541,22 @@ pub fn main() {
541541
"
542542
);
543543
}
544+
545+
// https://github.com/gleam-lang/gleam/issues/4533
546+
#[test]
547+
fn immediately_invoked_function_expressions_include_statement_level() {
548+
assert_js!(
549+
"
550+
fn identity(x) { x }
551+
552+
pub type Wibble {
553+
Wibble(a: Int, b: Int)
554+
}
555+
556+
pub fn main() {
557+
let w = Wibble(1, 2)
558+
identity(Wibble(..w |> identity, b: 4)) |> identity
559+
}
560+
"
561+
);
562+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
---
2+
source: compiler-core/src/javascript/tests/functions.rs
3+
expression: "\nfn identity(x) { x }\n\npub type Wibble {\n Wibble(a: Int, b: Int)\n}\n\npub fn main() {\n let w = Wibble(1, 2)\n identity(Wibble(..w |> identity, b: 4)) |> identity\n}\n"
4+
---
5+
----- SOURCE CODE
6+
7+
fn identity(x) { x }
8+
9+
pub type Wibble {
10+
Wibble(a: Int, b: Int)
11+
}
12+
13+
pub fn main() {
14+
let w = Wibble(1, 2)
15+
identity(Wibble(..w |> identity, b: 4)) |> identity
16+
}
17+
18+
19+
----- COMPILED JAVASCRIPT
20+
import { CustomType as $CustomType } from "../gleam.mjs";
21+
22+
export class Wibble extends $CustomType {
23+
constructor(a, b) {
24+
super();
25+
this.a = a;
26+
this.b = b;
27+
}
28+
}
29+
30+
function identity(x) {
31+
return x;
32+
}
33+
34+
export function main() {
35+
let w = new Wibble(1, 2);
36+
let _pipe = identity((() => { let _block;
37+
let _pipe = w;
38+
_block = identity(_pipe);
39+
let _record = _block;
40+
return new Wibble(_record.a, 4); })());
41+
return identity(_pipe);
42+
}

0 commit comments

Comments
 (0)