Skip to content

Commit 21b3de0

Browse files
yoshi-monsterlpil
authored andcommitted
escape properties in guards and patterns as well
1 parent 4c17959 commit 21b3de0

4 files changed

+127
-2
lines changed

compiler-core/src/javascript/pattern.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ impl<'module_ctx, 'expression_gen, 'a> Generator<'module_ctx, 'expression_gen, '
156156
concat(self.path.iter().map(|segment| match segment {
157157
Index::Int(i) => eco_format!("[{i}]").to_doc(),
158158
// TODO: escape string if needed
159-
Index::String(s) => docvec!(".", s),
159+
Index::String(s) => docvec!(".", maybe_escape_property_doc(s)),
160160
Index::ByteAt(i) => docvec!(".byteAt(", i, ")"),
161161
Index::IntFromSlice {
162162
start,
@@ -365,7 +365,11 @@ impl<'module_ctx, 'expression_gen, 'a> Generator<'module_ctx, 'expression_gen, '
365365
ClauseGuard::FieldAccess {
366366
label, container, ..
367367
} => {
368-
docvec!(self.guard(container)?, ".", label)
368+
docvec!(
369+
self.guard(container)?,
370+
".",
371+
maybe_escape_property_doc(label)
372+
)
369373
}
370374

371375
ClauseGuard::ModuleSelect {

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

+39
Original file line numberDiff line numberDiff line change
@@ -728,3 +728,42 @@ pub fn main() {
728728
"#
729729
);
730730
}
731+
#[test]
732+
fn record_access_in_guard_with_reserved_field_name() {
733+
assert_js!(
734+
r#"
735+
pub type Thing {
736+
Thing(constructor: Nil)
737+
}
738+
739+
pub fn main() {
740+
let a = Thing(constructor: Nil)
741+
case Nil {
742+
Nil if a.constructor == Nil -> a.constructor
743+
_ -> Nil
744+
}
745+
}
746+
"#
747+
);
748+
}
749+
750+
#[test]
751+
fn record_access_in_pattern_with_reserved_field_name() {
752+
assert_js!(
753+
r#"
754+
pub type Thing {
755+
Thing(constructor: Nil)
756+
}
757+
758+
pub fn main() {
759+
let a = Thing(constructor: Nil)
760+
let Thing(constructor: ctor) = a
761+
case a {
762+
a if a.constructor == ctor -> Nil
763+
Thing(constructor:) if ctor == constructor -> Nil
764+
_ -> Nil
765+
}
766+
}
767+
"#
768+
);
769+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
---
2+
source: compiler-core/src/javascript/tests/custom_types.rs
3+
expression: "\npub type Thing {\n Thing(constructor: Nil)\n}\n\npub fn main() {\n let a = Thing(constructor: Nil)\n case Nil {\n Nil if a.constructor == Nil -> a.constructor\n _ -> Nil\n }\n}\n"
4+
---
5+
----- SOURCE CODE
6+
7+
pub type Thing {
8+
Thing(constructor: Nil)
9+
}
10+
11+
pub fn main() {
12+
let a = Thing(constructor: Nil)
13+
case Nil {
14+
Nil if a.constructor == Nil -> a.constructor
15+
_ -> Nil
16+
}
17+
}
18+
19+
20+
----- COMPILED JAVASCRIPT
21+
import { CustomType as $CustomType } from "../gleam.mjs";
22+
23+
export class Thing extends $CustomType {
24+
constructor(constructor) {
25+
super();
26+
this.constructor$ = constructor;
27+
}
28+
}
29+
30+
export function main() {
31+
let a = new Thing(undefined);
32+
let $ = undefined;
33+
if (!$ && (a.constructor$ === undefined)) {
34+
return a.constructor$;
35+
} else {
36+
return undefined;
37+
}
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
---
2+
source: compiler-core/src/javascript/tests/custom_types.rs
3+
expression: "\npub type Thing {\n Thing(constructor: Nil)\n}\n\npub fn main() {\n let a = Thing(constructor: Nil)\n let Thing(constructor: ctor) = a\n case a {\n a if a.constructor == ctor -> Nil\n Thing(constructor:) if ctor == constructor -> Nil\n _ -> Nil\n }\n}\n"
4+
---
5+
----- SOURCE CODE
6+
7+
pub type Thing {
8+
Thing(constructor: Nil)
9+
}
10+
11+
pub fn main() {
12+
let a = Thing(constructor: Nil)
13+
let Thing(constructor: ctor) = a
14+
case a {
15+
a if a.constructor == ctor -> Nil
16+
Thing(constructor:) if ctor == constructor -> Nil
17+
_ -> Nil
18+
}
19+
}
20+
21+
22+
----- COMPILED JAVASCRIPT
23+
import { CustomType as $CustomType } from "../gleam.mjs";
24+
25+
export class Thing extends $CustomType {
26+
constructor(constructor) {
27+
super();
28+
this.constructor$ = constructor;
29+
}
30+
}
31+
32+
export function main() {
33+
let a = new Thing(undefined);
34+
let ctor = a.constructor$;
35+
if (a.constructor$ === ctor) {
36+
let a$1 = a;
37+
return undefined;
38+
} else if (a instanceof Thing && (ctor === a.constructor$)) {
39+
let constructor = a.constructor$;
40+
return undefined;
41+
} else {
42+
return undefined;
43+
}
44+
}

0 commit comments

Comments
 (0)