Skip to content

Commit 8f49543

Browse files
GearsDatapackslpil
authored andcommitted
Use fat-arrow functions for custom type API
1 parent a5603a2 commit 8f49543

File tree

51 files changed

+278
-763
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+278
-763
lines changed

compiler-core/src/javascript.rs

Lines changed: 22 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -374,16 +374,7 @@ impl<'a> Generator<'a> {
374374
// Only generate accessors for the API if the constructors are public
375375
&& constructor_publicity.is_public()
376376
{
377-
let function_head = if opaque || publicity.is_private() {
378-
"function "
379-
} else {
380-
"export function "
381-
};
382-
definitions.push(self.shared_custom_type_fields(
383-
name,
384-
&accessors_map.shared_accessors,
385-
function_head,
386-
));
377+
definitions.push(self.shared_custom_type_fields(name, &accessors_map.shared_accessors));
387378
}
388379

389380
definitions
@@ -433,25 +424,24 @@ impl<'a> Generator<'a> {
433424
}
434425

435426
let construction = docvec![
436-
line(),
437-
"return new ",
427+
break_("", " "),
428+
"new ",
438429
constructor.name.as_str(),
439430
"(",
440-
join(arguments.clone(), break_(",", ", ")),
431+
join(arguments.clone(), break_(",", ", ")).group(),
441432
");"
442-
];
433+
]
434+
.group();
443435

444436
docvec![
445-
"export function ",
437+
"export const ",
446438
type_name,
447439
"$",
448440
constructor.name.as_str(),
449-
"(",
441+
" = (",
450442
join(arguments, break_(",", ", ")),
451-
") {",
443+
") =>",
452444
construction.nest(INDENT),
453-
line(),
454-
"}"
455445
]
456446
}
457447

@@ -461,21 +451,20 @@ impl<'a> Generator<'a> {
461451
type_name: &'a str,
462452
) -> Document<'a> {
463453
let construction = docvec![
464-
line(),
465-
"return value instanceof ",
454+
break_("", " "),
455+
"value instanceof ",
466456
constructor.name.as_str(),
467457
";"
468-
];
458+
]
459+
.group();
469460

470461
docvec![
471-
"export function ",
462+
"export const ",
472463
type_name,
473464
"$is",
474465
constructor.name.as_str(),
475-
"(value) {",
466+
" = (value) =>",
476467
construction.nest(INDENT),
477-
line(),
478-
"}"
479468
]
480469
}
481470

@@ -504,16 +493,14 @@ impl<'a> Generator<'a> {
504493
field = eco_format!("[{index}]");
505494
};
506495

507-
let contents = docvec![line(), "return value", field, ";"];
496+
let contents = docvec![break_("", " "), "value", field, ";"].group();
508497

509498
functions.push(docvec![
510499
line(),
511-
"export function ",
500+
"export const ",
512501
function_name,
513-
"(value) {",
502+
" = (value) =>",
514503
contents.nest(INDENT),
515-
line(),
516-
"}"
517504
]);
518505
}
519506

@@ -524,23 +511,21 @@ impl<'a> Generator<'a> {
524511
&self,
525512
type_name: &'a str,
526513
shared_accessors: &HashMap<EcoString, RecordAccessor>,
527-
function_head: &'a str,
528514
) -> Document<'a> {
529515
let mut functions = Vec::new();
530516

531517
for field in shared_accessors.keys().sorted() {
532518
let function_name = eco_format!("{type_name}${field}");
533519

534-
let contents = docvec![line(), "return value.", maybe_escape_property(field), ";"];
520+
let contents =
521+
docvec![break_("", " "), "value.", maybe_escape_property(field), ";"].group();
535522

536523
functions.push(docvec![
537524
line(),
538-
function_head,
525+
"export const ",
539526
function_name,
540-
"(value) {",
527+
" = (value) =>",
541528
contents.nest(INDENT),
542-
line(),
543-
"}"
544529
]);
545530
}
546531

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

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,28 +18,16 @@ import { CustomType as $CustomType, makeError } from "../gleam.mjs";
1818
const FILEPATH = "src/module.gleam";
1919

2020
export class True extends $CustomType {}
21-
export function True$True() {
22-
return new True();
23-
}
24-
export function True$isTrue(value) {
25-
return value instanceof True;
26-
}
21+
export const True$True = () => new True();
22+
export const True$isTrue = (value) => value instanceof True;
2723

2824
export class False extends $CustomType {}
29-
export function True$False() {
30-
return new False();
31-
}
32-
export function True$isFalse(value) {
33-
return value instanceof False;
34-
}
25+
export const True$False = () => new False();
26+
export const True$isFalse = (value) => value instanceof False;
3527

3628
export class Nil extends $CustomType {}
37-
export function True$Nil() {
38-
return new Nil();
39-
}
40-
export function True$isNil(value) {
41-
return value instanceof Nil;
42-
}
29+
export const True$Nil = () => new Nil();
30+
export const True$isNil = (value) => value instanceof Nil;
4331

4432
export function go(x, y) {
4533
if (!(x instanceof True)) {

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

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -26,34 +26,20 @@ export class Wibble extends $CustomType {
2626
this.string = string;
2727
}
2828
}
29-
export function Wibble$Wibble(int, string) {
30-
return new Wibble(int, string);
31-
}
32-
export function Wibble$isWibble(value) {
33-
return value instanceof Wibble;
34-
}
35-
export function Wibble$Wibble$int(value) {
36-
return value.int;
37-
}
38-
export function Wibble$Wibble$string(value) {
39-
return value.string;
40-
}
29+
export const Wibble$Wibble = (int, string) => new Wibble(int, string);
30+
export const Wibble$isWibble = (value) => value instanceof Wibble;
31+
export const Wibble$Wibble$int = (value) => value.int;
32+
export const Wibble$Wibble$string = (value) => value.string;
4133

4234
export class Wobble extends $CustomType {
4335
constructor($0) {
4436
super();
4537
this[0] = $0;
4638
}
4739
}
48-
export function Wibble$Wobble($0) {
49-
return new Wobble($0);
50-
}
51-
export function Wibble$isWobble(value) {
52-
return value instanceof Wobble;
53-
}
54-
export function Wibble$Wobble$0(value) {
55-
return value[0];
56-
}
40+
export const Wibble$Wobble = ($0) => new Wobble($0);
41+
export const Wibble$isWobble = (value) => value instanceof Wobble;
42+
export const Wibble$Wobble$0 = (value) => value[0];
5743

5844
export function go(x) {
5945
if (x instanceof Wibble) {

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

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -26,34 +26,20 @@ export class Wibble extends $CustomType {
2626
this.string = string;
2727
}
2828
}
29-
export function Wibble$Wibble(int, string) {
30-
return new Wibble(int, string);
31-
}
32-
export function Wibble$isWibble(value) {
33-
return value instanceof Wibble;
34-
}
35-
export function Wibble$Wibble$int(value) {
36-
return value.int;
37-
}
38-
export function Wibble$Wibble$string(value) {
39-
return value.string;
40-
}
29+
export const Wibble$Wibble = (int, string) => new Wibble(int, string);
30+
export const Wibble$isWibble = (value) => value instanceof Wibble;
31+
export const Wibble$Wibble$int = (value) => value.int;
32+
export const Wibble$Wibble$string = (value) => value.string;
4133

4234
export class Wobble extends $CustomType {
4335
constructor($0) {
4436
super();
4537
this[0] = $0;
4638
}
4739
}
48-
export function Wibble$Wobble($0) {
49-
return new Wobble($0);
50-
}
51-
export function Wibble$isWobble(value) {
52-
return value instanceof Wobble;
53-
}
54-
export function Wibble$Wobble$0(value) {
55-
return value[0];
56-
}
40+
export const Wibble$Wobble = ($0) => new Wobble($0);
41+
export const Wibble$isWobble = (value) => value instanceof Wobble;
42+
export const Wibble$Wobble$0 = (value) => value[0];
5743

5844
export function go(x) {
5945
if (x instanceof Wibble) {

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

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -27,34 +27,20 @@ export class Wibble extends $CustomType {
2727
this.string = string;
2828
}
2929
}
30-
export function Wibble$Wibble(int, string) {
31-
return new Wibble(int, string);
32-
}
33-
export function Wibble$isWibble(value) {
34-
return value instanceof Wibble;
35-
}
36-
export function Wibble$Wibble$int(value) {
37-
return value.int;
38-
}
39-
export function Wibble$Wibble$string(value) {
40-
return value.string;
41-
}
30+
export const Wibble$Wibble = (int, string) => new Wibble(int, string);
31+
export const Wibble$isWibble = (value) => value instanceof Wibble;
32+
export const Wibble$Wibble$int = (value) => value.int;
33+
export const Wibble$Wibble$string = (value) => value.string;
4234

4335
export class Wobble extends $CustomType {
4436
constructor($0) {
4537
super();
4638
this[0] = $0;
4739
}
4840
}
49-
export function Wibble$Wobble($0) {
50-
return new Wobble($0);
51-
}
52-
export function Wibble$isWobble(value) {
53-
return value instanceof Wobble;
54-
}
55-
export function Wibble$Wobble$0(value) {
56-
return value[0];
57-
}
41+
export const Wibble$Wobble = ($0) => new Wobble($0);
42+
export const Wibble$isWobble = (value) => value instanceof Wobble;
43+
export const Wibble$Wobble$0 = (value) => value[0];
5844

5945
export function go(x) {
6046
if (x instanceof Wibble) {

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

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -26,34 +26,20 @@ export class Wibble extends $CustomType {
2626
this.string = string;
2727
}
2828
}
29-
export function Wibble$Wibble(int, string) {
30-
return new Wibble(int, string);
31-
}
32-
export function Wibble$isWibble(value) {
33-
return value instanceof Wibble;
34-
}
35-
export function Wibble$Wibble$int(value) {
36-
return value.int;
37-
}
38-
export function Wibble$Wibble$string(value) {
39-
return value.string;
40-
}
29+
export const Wibble$Wibble = (int, string) => new Wibble(int, string);
30+
export const Wibble$isWibble = (value) => value instanceof Wibble;
31+
export const Wibble$Wibble$int = (value) => value.int;
32+
export const Wibble$Wibble$string = (value) => value.string;
4133

4234
export class Wobble extends $CustomType {
4335
constructor($0) {
4436
super();
4537
this[0] = $0;
4638
}
4739
}
48-
export function Wibble$Wobble($0) {
49-
return new Wobble($0);
50-
}
51-
export function Wibble$isWobble(value) {
52-
return value instanceof Wobble;
53-
}
54-
export function Wibble$Wobble$0(value) {
55-
return value[0];
56-
}
40+
export const Wibble$Wobble = ($0) => new Wobble($0);
41+
export const Wibble$isWobble = (value) => value instanceof Wobble;
42+
export const Wibble$Wobble$0 = (value) => value[0];
5743

5844
export function go(x) {
5945
if (x instanceof Wibble) {

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

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -26,34 +26,20 @@ export class Wibble extends $CustomType {
2626
this.string = string;
2727
}
2828
}
29-
export function Wibble$Wibble(int, string) {
30-
return new Wibble(int, string);
31-
}
32-
export function Wibble$isWibble(value) {
33-
return value instanceof Wibble;
34-
}
35-
export function Wibble$Wibble$int(value) {
36-
return value.int;
37-
}
38-
export function Wibble$Wibble$string(value) {
39-
return value.string;
40-
}
29+
export const Wibble$Wibble = (int, string) => new Wibble(int, string);
30+
export const Wibble$isWibble = (value) => value instanceof Wibble;
31+
export const Wibble$Wibble$int = (value) => value.int;
32+
export const Wibble$Wibble$string = (value) => value.string;
4133

4234
export class Wobble extends $CustomType {
4335
constructor($0) {
4436
super();
4537
this[0] = $0;
4638
}
4739
}
48-
export function Wibble$Wobble($0) {
49-
return new Wobble($0);
50-
}
51-
export function Wibble$isWobble(value) {
52-
return value instanceof Wobble;
53-
}
54-
export function Wibble$Wobble$0(value) {
55-
return value[0];
56-
}
40+
export const Wibble$Wobble = ($0) => new Wobble($0);
41+
export const Wibble$isWobble = (value) => value instanceof Wobble;
42+
export const Wibble$Wobble$0 = (value) => value[0];
5743

5844
export function go(x) {
5945
if (x instanceof Wibble) {

0 commit comments

Comments
 (0)