Skip to content

Commit 377f9fa

Browse files
GearsDatapackslpil
authored andcommitted
Generate index getters for labelled fields
1 parent dd5eb87 commit 377f9fa

File tree

32 files changed

+98
-18
lines changed

32 files changed

+98
-18
lines changed

compiler-core/src/javascript.rs

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -476,24 +476,18 @@ impl<'a> Generator<'a> {
476476
let mut functions = Vec::new();
477477

478478
for (index, argument) in constructor.arguments.iter().enumerate() {
479-
let function_name;
480-
let field;
481-
482-
if let Some((_, label)) = &argument.label {
483-
function_name = eco_format!(
484-
"{type_name}${record_name}${label}",
485-
record_name = constructor.name,
486-
);
487-
field = eco_format!(".{field_name}", field_name = maybe_escape_property(label));
488-
} else {
489-
function_name = eco_format!(
490-
"{type_name}${record_name}${index}",
491-
record_name = constructor.name,
492-
);
493-
field = eco_format!("[{index}]");
494-
};
495-
496-
let contents = docvec![break_("", " "), "value", field, ";"].group();
479+
// Always generate the accessor for the value at this index. Although
480+
// this is not necessary when a label is present, we want to make sure
481+
// that adding a label to a record isn't a breaking change. For this
482+
// reason, we need to generate an index getter even when a label is
483+
// present to ensure consistent behaviour between labelled and unlabelled
484+
// field access.
485+
let function_name = eco_format!(
486+
"{type_name}${record_name}${index}",
487+
record_name = constructor.name,
488+
);
489+
490+
let contents = docvec![break_("", " "), "value[", index, "];"].group();
497491

498492
functions.push(docvec![
499493
line(),
@@ -502,6 +496,26 @@ impl<'a> Generator<'a> {
502496
" = (value) =>",
503497
contents.nest(INDENT),
504498
]);
499+
500+
// If the argument is labelled, also generate a getter for the labelled
501+
// argument.
502+
if let Some((_, label)) = &argument.label {
503+
let function_name = eco_format!(
504+
"{type_name}${record_name}${label}",
505+
record_name = constructor.name,
506+
);
507+
508+
let contents =
509+
docvec![break_("", " "), "value.", maybe_escape_property(label), ";"].group();
510+
511+
functions.push(docvec![
512+
line(),
513+
"export const ",
514+
function_name,
515+
" = (value) =>",
516+
contents.nest(INDENT),
517+
]);
518+
}
505519
}
506520

507521
concat(functions)

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ export class Wibble extends $CustomType {
2828
}
2929
export const Wibble$Wibble = (int, string) => new Wibble(int, string);
3030
export const Wibble$isWibble = (value) => value instanceof Wibble;
31+
export const Wibble$Wibble$0 = (value) => value[0];
3132
export const Wibble$Wibble$int = (value) => value.int;
33+
export const Wibble$Wibble$1 = (value) => value[1];
3234
export const Wibble$Wibble$string = (value) => value.string;
3335

3436
export class Wobble extends $CustomType {

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ export class Wibble extends $CustomType {
2828
}
2929
export const Wibble$Wibble = (int, string) => new Wibble(int, string);
3030
export const Wibble$isWibble = (value) => value instanceof Wibble;
31+
export const Wibble$Wibble$0 = (value) => value[0];
3132
export const Wibble$Wibble$int = (value) => value.int;
33+
export const Wibble$Wibble$1 = (value) => value[1];
3234
export const Wibble$Wibble$string = (value) => value.string;
3335

3436
export class Wobble extends $CustomType {

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ export class Wibble extends $CustomType {
2929
}
3030
export const Wibble$Wibble = (int, string) => new Wibble(int, string);
3131
export const Wibble$isWibble = (value) => value instanceof Wibble;
32+
export const Wibble$Wibble$0 = (value) => value[0];
3233
export const Wibble$Wibble$int = (value) => value.int;
34+
export const Wibble$Wibble$1 = (value) => value[1];
3335
export const Wibble$Wibble$string = (value) => value.string;
3436

3537
export class Wobble extends $CustomType {

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ export class Wibble extends $CustomType {
2828
}
2929
export const Wibble$Wibble = (int, string) => new Wibble(int, string);
3030
export const Wibble$isWibble = (value) => value instanceof Wibble;
31+
export const Wibble$Wibble$0 = (value) => value[0];
3132
export const Wibble$Wibble$int = (value) => value.int;
33+
export const Wibble$Wibble$1 = (value) => value[1];
3234
export const Wibble$Wibble$string = (value) => value.string;
3335

3436
export class Wobble extends $CustomType {

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ export class Wibble extends $CustomType {
2828
}
2929
export const Wibble$Wibble = (int, string) => new Wibble(int, string);
3030
export const Wibble$isWibble = (value) => value instanceof Wibble;
31+
export const Wibble$Wibble$0 = (value) => value[0];
3132
export const Wibble$Wibble$int = (value) => value.int;
33+
export const Wibble$Wibble$1 = (value) => value[1];
3234
export const Wibble$Wibble$string = (value) => value.string;
3335

3436
export class Wobble extends $CustomType {

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ export class Wibble extends $CustomType {
2828
}
2929
export const Wibble$Wibble = (int, string) => new Wibble(int, string);
3030
export const Wibble$isWibble = (value) => value instanceof Wibble;
31+
export const Wibble$Wibble$0 = (value) => value[0];
3132
export const Wibble$Wibble$int = (value) => value.int;
33+
export const Wibble$Wibble$1 = (value) => value[1];
3234
export const Wibble$Wibble$string = (value) => value.string;
3335

3436
export class Wobble extends $CustomType {

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ export class Wibble extends $CustomType {
3535
}
3636
export const Wibble$Wibble = (wibble, wobble) => new Wibble(wibble, wobble);
3737
export const Wibble$isWibble = (value) => value instanceof Wibble;
38+
export const Wibble$Wibble$0 = (value) => value[0];
3839
export const Wibble$Wibble$wibble = (value) => value.wibble;
40+
export const Wibble$Wibble$1 = (value) => value[1];
3941
export const Wibble$Wibble$wobble = (value) => value.wobble;
4042

4143
function identity(x) {

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,11 @@ export class Person extends $CustomType {
3131
export const Person$Person = (username, name, age) =>
3232
new Person(username, name, age);
3333
export const Person$isPerson = (value) => value instanceof Person;
34+
export const Person$Person$0 = (value) => value[0];
3435
export const Person$Person$username = (value) => value.username;
36+
export const Person$Person$1 = (value) => value[1];
3537
export const Person$Person$name = (value) => value.name;
38+
export const Person$Person$2 = (value) => value[2];
3639
export const Person$Person$age = (value) => value.age;
3740

3841
export function main() {

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export class A extends $CustomType {
3535
}
3636
export const A$A = (b) => new A(b);
3737
export const A$isA = (value) => value instanceof A;
38+
export const A$A$0 = (value) => value[0];
3839
export const A$A$b = (value) => value.b;
3940

4041
export class B extends $CustomType {
@@ -45,6 +46,7 @@ export class B extends $CustomType {
4546
}
4647
export const B$B = (c) => new B(c);
4748
export const B$isB = (value) => value instanceof B;
49+
export const B$B$0 = (value) => value[0];
4850
export const B$B$c = (value) => value.c;
4951

5052
export class C extends $CustomType {
@@ -55,6 +57,7 @@ export class C extends $CustomType {
5557
}
5658
export const C$C = (d) => new C(d);
5759
export const C$isC = (value) => value instanceof C;
60+
export const C$C$0 = (value) => value[0];
5861
export const C$C$d = (value) => value.d;
5962

6063
export function a(a) {

0 commit comments

Comments
 (0)