Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,6 @@
- The compiler now correctly tracks the minimum required version for expressions
in `BitArray`s' `size` option to be `>= 1.12.0`.
([Giacomo Cavalieri](https://github.com/giacomocavalieri))

- Clause guard with logical expression now compiles correctly in JS.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Be more precise with the description please, this sounds like it never worked 🙏

([vyacheslavhere](https://github.com/vyacheslavhere))
4 changes: 2 additions & 2 deletions compiler-core/src/javascript/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2259,13 +2259,13 @@ impl<'module, 'a> Generator<'module, 'a> {
ClauseGuard::Or { left, right, .. } => {
let left = self.wrapped_guard(left);
let right = self.wrapped_guard(right);
docvec![left, " || ", right]
docvec!["(", left, " || ", right, ")"]
}

ClauseGuard::And { left, right, .. } => {
let left = self.wrapped_guard(left);
let right = self.wrapped_guard(right);
docvec![left, " && ", right]
docvec!["(", left, " && ", right, ")"]
}

ClauseGuard::Var { name, .. } => self.local_var(name).to_doc(),
Expand Down
20 changes: 20 additions & 0 deletions compiler-core/src/javascript/tests/case_clause_guards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -622,3 +622,23 @@ pub fn main() {
"
);
}

// https://github.com/gleam-lang/gleam/issues/5254
#[test]
fn logical_or() {
assert_js!(
"
pub fn main() {
let a = False
let b = []
let c = True

let r = case c == True {
False if a || b == [] -> \"1\"
_else -> \"0\"
}
assert r == \"0\"
}
"
)
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test is quite complex, so it's hard to understand. Could you simplify it please 🙏

Is this testing what the issue reported? The title of the issue says it's related to instanceof, but this one doesn't use it? I'm finding it hard to understand this due to the complex test.

Copy link
Contributor Author

@vyacheslavhere vyacheslavhere Jan 4, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the feedback, I'll work on it when I have time.

Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export function anything() {
let $ = toList([]);
if (!($ instanceof $Empty)) {
let $1 = $.tail;
if ($1 instanceof $Empty && false || true) {
if ($1 instanceof $Empty && (false || true)) {
let a = $.head;
return a;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
---
source: compiler-core/src/javascript/tests/case_clause_guards.rs
expression: "\n pub fn main() {\n let a = False\n let b = []\n let c = True\n\n let r = case c == True {\n False if a || b == [] -> \"1\"\n _else -> \"0\"\n }\n assert r == \"0\"\n }\n "
---
----- SOURCE CODE

pub fn main() {
let a = False
let b = []
let c = True

let r = case c == True {
False if a || b == [] -> "1"
_else -> "0"
}
assert r == "0"
}


----- COMPILED JAVASCRIPT
import { toList, makeError, isEqual } from "../gleam.mjs";

const FILEPATH = "src/module.gleam";

export function main() {
let a = false;
let b = toList([]);
let c = true;
let _block;
let $ = c === true;
if (!$ && (a || (isEqual(b, toList([]))))) {
_block = "1";
} else {
_block = "0";
}
let r = _block;
let $1 = "0";
if (!(r === $1)) {
throw makeError(
"assert",
FILEPATH,
"my/mod",
11,
"main",
"Assertion failed.",
{
kind: "binary_operator",
operator: "==",
left: { kind: "expression", value: r, start: 224, end: 225 },
right: { kind: "literal", value: $1, start: 229, end: 232 },
start: 217,
end: 232,
expression_start: 224
}
)
}
return undefined;
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub fn main(x, y) {

----- COMPILED JAVASCRIPT
export function main(x, y) {
if (!y && !x) {
if ((!y && !x)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be good to not have these parens here and in other places they are not needed.

return 0;
} else {
return 1;
Expand Down
Loading