Skip to content

Commit f9346e2

Browse files
GearsDatapackslpil
authored andcommitted
Do not generate API functions for private or opaque custom types
1 parent 2654a2e commit f9346e2

11 files changed

+26
-201
lines changed

compiler-core/src/javascript.rs

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -354,9 +354,15 @@ impl<'a> Generator<'a> {
354354

355355
self.tracker.custom_type_used = true;
356356

357+
let constructor_publicity = if opaque || publicity.is_private() {
358+
Publicity::Private
359+
} else {
360+
Publicity::Public
361+
};
362+
357363
let mut definitions = constructors
358364
.iter()
359-
.map(|constructor| self.record_definition(constructor, name, publicity, opaque))
365+
.map(|constructor| self.record_definition(constructor, name, constructor_publicity))
360366
.collect_vec();
361367

362368
// Generate getters for fields shared between variants
@@ -365,6 +371,8 @@ impl<'a> Generator<'a> {
365371
// Don't bother generating shared getters when there's only one variant,
366372
// since the specific accessors can always be uses instead.
367373
&& constructors.len() != 1
374+
// Only generate accessors for the API if the constructors are public
375+
&& constructor_publicity.is_public()
368376
{
369377
let function_head = if opaque || publicity.is_private() {
370378
"function "
@@ -386,20 +394,18 @@ impl<'a> Generator<'a> {
386394
constructor: &'a TypedRecordConstructor,
387395
type_name: &'a str,
388396
publicity: Publicity,
389-
opaque: bool,
390397
) -> Document<'a> {
391-
let class_definition = self.record_class_definition(constructor, publicity, opaque);
392-
let function_head = if opaque || publicity.is_private() {
393-
"function "
394-
} else {
395-
"export function "
396-
};
397-
let constructor_definition =
398-
self.record_constructor_definition(constructor, type_name, function_head);
399-
let variant_check_definition =
400-
self.record_check_definition(constructor, type_name, function_head);
401-
let fields_definition =
402-
self.record_fields_definition(constructor, type_name, function_head);
398+
let class_definition = self.record_class_definition(constructor, publicity);
399+
400+
// If the custom type is private or opaque, we don't need to generate API
401+
// functions for it.
402+
if publicity.is_private() {
403+
return class_definition;
404+
}
405+
406+
let constructor_definition = self.record_constructor_definition(constructor, type_name);
407+
let variant_check_definition = self.record_check_definition(constructor, type_name);
408+
let fields_definition = self.record_fields_definition(constructor, type_name);
403409

404410
docvec![
405411
class_definition,
@@ -415,7 +421,6 @@ impl<'a> Generator<'a> {
415421
&self,
416422
constructor: &'a TypedRecordConstructor,
417423
type_name: &'a str,
418-
function_head: &'a str,
419424
) -> Document<'a> {
420425
let mut arguments = Vec::new();
421426

@@ -437,7 +442,7 @@ impl<'a> Generator<'a> {
437442
];
438443

439444
docvec![
440-
function_head,
445+
"export function ",
441446
type_name,
442447
"$",
443448
constructor.name.as_str(),
@@ -454,7 +459,6 @@ impl<'a> Generator<'a> {
454459
&self,
455460
constructor: &'a TypedRecordConstructor,
456461
type_name: &'a str,
457-
function_head: &'a str,
458462
) -> Document<'a> {
459463
let construction = docvec![
460464
line(),
@@ -464,7 +468,7 @@ impl<'a> Generator<'a> {
464468
];
465469

466470
docvec![
467-
function_head,
471+
"export function ",
468472
type_name,
469473
"$is",
470474
constructor.name.as_str(),
@@ -479,7 +483,6 @@ impl<'a> Generator<'a> {
479483
&self,
480484
constructor: &'a TypedRecordConstructor,
481485
type_name: &'a str,
482-
function_head: &'a str,
483486
) -> Document<'a> {
484487
let mut functions = Vec::new();
485488

@@ -505,7 +508,7 @@ impl<'a> Generator<'a> {
505508

506509
functions.push(docvec![
507510
line(),
508-
function_head,
511+
"export function ",
509512
function_name,
510513
"(value) {",
511514
contents.nest(INDENT),
@@ -548,7 +551,6 @@ impl<'a> Generator<'a> {
548551
&self,
549552
constructor: &'a TypedRecordConstructor,
550553
publicity: Publicity,
551-
opaque: bool,
552554
) -> Document<'a> {
553555
fn parameter((i, arg): (usize, &TypedRecordConstructorArg)) -> Document<'_> {
554556
arg.label
@@ -564,10 +566,10 @@ impl<'a> Generator<'a> {
564566
nil()
565567
};
566568

567-
let head = if publicity.is_private() || opaque {
568-
"class "
569-
} else {
569+
let head = if publicity.is_public() {
570570
"export class "
571+
} else {
572+
"class "
571573
};
572574
let head = docvec![head, &constructor.name, " extends $CustomType {"];
573575

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

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,31 +26,13 @@ class Wibble extends $CustomType {
2626
this[0] = $0;
2727
}
2828
}
29-
function Wibble$Wibble($0) {
30-
return new Wibble($0);
31-
}
32-
function Wibble$isWibble(value) {
33-
return value instanceof Wibble;
34-
}
35-
function Wibble$Wibble$0(value) {
36-
return value[0];
37-
}
3829

3930
class Wobble extends $CustomType {
4031
constructor($0) {
4132
super();
4233
this[0] = $0;
4334
}
4435
}
45-
function Wibble$Wobble($0) {
46-
return new Wobble($0);
47-
}
48-
function Wibble$isWobble(value) {
49-
return value instanceof Wobble;
50-
}
51-
function Wibble$Wobble$0(value) {
52-
return value[0];
53-
}
5436

5537
export function go() {
5638
let $ = new Wibble(1);

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

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,6 @@ class Wibble extends $CustomType {
2323
this[0] = $0;
2424
}
2525
}
26-
function Wibble$Wibble($0) {
27-
return new Wibble($0);
28-
}
29-
function Wibble$isWibble(value) {
30-
return value instanceof Wibble;
31-
}
32-
function Wibble$Wibble$0(value) {
33-
return value[0];
34-
}
3526

3627
export function go() {
3728
let $ = new Wibble(1);

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

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -27,47 +27,20 @@ class Wibble extends $CustomType {
2727
this[0] = $0;
2828
}
2929
}
30-
function Wibble$Wibble($0) {
31-
return new Wibble($0);
32-
}
33-
function Wibble$isWibble(value) {
34-
return value instanceof Wibble;
35-
}
36-
function Wibble$Wibble$0(value) {
37-
return value[0];
38-
}
3930

4031
class Wobble extends $CustomType {
4132
constructor($0) {
4233
super();
4334
this[0] = $0;
4435
}
4536
}
46-
function Wibble$Wobble($0) {
47-
return new Wobble($0);
48-
}
49-
function Wibble$isWobble(value) {
50-
return value instanceof Wobble;
51-
}
52-
function Wibble$Wobble$0(value) {
53-
return value[0];
54-
}
5537

5638
class Woo extends $CustomType {
5739
constructor($0) {
5840
super();
5941
this[0] = $0;
6042
}
6143
}
62-
function Wibble$Woo($0) {
63-
return new Woo($0);
64-
}
65-
function Wibble$isWoo(value) {
66-
return value instanceof Woo;
67-
}
68-
function Wibble$Woo$0(value) {
69-
return value[0];
70-
}
7144

7245
export function go() {
7346
let _block;

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

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,31 +24,13 @@ class Wibble extends $CustomType {
2424
this[0] = $0;
2525
}
2626
}
27-
function Wibble$Wibble($0) {
28-
return new Wibble($0);
29-
}
30-
function Wibble$isWibble(value) {
31-
return value instanceof Wibble;
32-
}
33-
function Wibble$Wibble$0(value) {
34-
return value[0];
35-
}
3627

3728
class Wobble extends $CustomType {
3829
constructor($0) {
3930
super();
4031
this[0] = $0;
4132
}
4233
}
43-
function Wibble$Wobble($0) {
44-
return new Wobble($0);
45-
}
46-
function Wibble$isWobble(value) {
47-
return value instanceof Wobble;
48-
}
49-
function Wibble$Wobble$0(value) {
50-
return value[0];
51-
}
5234

5335
export function go() {
5436
let $ = new Wibble(1);

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

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,6 @@ class Wibble extends $CustomType {
2525
this.wibble = wibble;
2626
}
2727
}
28-
function Wibble$Wibble(wibble) {
29-
return new Wibble(wibble);
30-
}
31-
function Wibble$isWibble(value) {
32-
return value instanceof Wibble;
33-
}
34-
function Wibble$Wibble$wibble(value) {
35-
return value.wibble;
36-
}
3728

3829
export function main() {
3930
let $ = new Wibble("wibble");

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

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -27,47 +27,20 @@ class Wibble extends $CustomType {
2727
this[0] = $0;
2828
}
2929
}
30-
function Wibble$Wibble($0) {
31-
return new Wibble($0);
32-
}
33-
function Wibble$isWibble(value) {
34-
return value instanceof Wibble;
35-
}
36-
function Wibble$Wibble$0(value) {
37-
return value[0];
38-
}
3930

4031
class Wobble extends $CustomType {
4132
constructor($0) {
4233
super();
4334
this[0] = $0;
4435
}
4536
}
46-
function Wibble$Wobble($0) {
47-
return new Wobble($0);
48-
}
49-
function Wibble$isWobble(value) {
50-
return value instanceof Wobble;
51-
}
52-
function Wibble$Wobble$0(value) {
53-
return value[0];
54-
}
5537

5638
class Woo extends $CustomType {
5739
constructor($0) {
5840
super();
5941
this[0] = $0;
6042
}
6143
}
62-
function Wibble$Woo($0) {
63-
return new Woo($0);
64-
}
65-
function Wibble$isWoo(value) {
66-
return value instanceof Woo;
67-
}
68-
function Wibble$Woo$0(value) {
69-
return value[0];
70-
}
7144

7245
function fun(f) {
7346
return f(new Wibble(1));

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

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -34,47 +34,20 @@ class Wibble extends $CustomType {
3434
this[0] = $0;
3535
}
3636
}
37-
function Wibble$Wibble($0) {
38-
return new Wibble($0);
39-
}
40-
function Wibble$isWibble(value) {
41-
return value instanceof Wibble;
42-
}
43-
function Wibble$Wibble$0(value) {
44-
return value[0];
45-
}
4637

4738
class Wobble extends $CustomType {
4839
constructor(wabble) {
4940
super();
5041
this.wabble = wabble;
5142
}
5243
}
53-
function Wobble$Wobble(wabble) {
54-
return new Wobble(wabble);
55-
}
56-
function Wobble$isWobble(value) {
57-
return value instanceof Wobble;
58-
}
59-
function Wobble$Wobble$wabble(value) {
60-
return value.wabble;
61-
}
6244

6345
class Wabble extends $CustomType {
6446
constructor(tuple) {
6547
super();
6648
this.tuple = tuple;
6749
}
6850
}
69-
function Wabble$Wabble(tuple) {
70-
return new Wabble(tuple);
71-
}
72-
function Wabble$isWabble(value) {
73-
return value instanceof Wabble;
74-
}
75-
function Wabble$Wabble$tuple(value) {
76-
return value.tuple;
77-
}
7851

7952
export function main() {
8053
let tmp = new Wibble(new Wobble(new Wabble([42, "wibble"])));

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

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,6 @@ class Wibble extends $CustomType {
2828
this.wobble = wobble;
2929
}
3030
}
31-
function Wibble$Wibble(wobble) {
32-
return new Wibble(wobble);
33-
}
34-
function Wibble$isWibble(value) {
35-
return value instanceof Wibble;
36-
}
37-
function Wibble$Wibble$wobble(value) {
38-
return value.wobble;
39-
}
4031

4132
export function main() {
4233
let tmp = new Wibble("wibble");

0 commit comments

Comments
 (0)