Skip to content

Commit 8cb4728

Browse files
committed
Add deprecation notices
1 parent 949880c commit 8cb4728

8 files changed

+63
-29
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
---
22
source: compiler-core/src/javascript/tests/custom_types.rs
3+
assertion_line: 138
34
expression: "\npub type Mine {\n Mine(a: Int, b: Int)\n}\n\npub const labels = Mine(b: 2, a: 1)\npub const no_labels = Mine(3, 4)\n"
5+
snapshot_kind: text
46
---
57
----- SOURCE CODE
68

@@ -16,9 +18,11 @@ pub const no_labels = Mine(3, 4)
1618
import type * as _ from "../gleam.d.mts";
1719

1820
export class Mine extends _.CustomType {
21+
/** @deprecated */
1922
constructor(a: number, b: number);
20-
23+
/** @deprecated */
2124
a: number;
25+
/** @deprecated */
2226
b: number;
2327
}
2428
export function Mine$Mine(a: number, b: number): Mine$;

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
---
22
source: compiler-core/src/javascript/tests/custom_types.rs
3+
assertion_line: 209
34
expression: "\npub type TypeWithALongNameAndSeveralArguments{\n TypeWithALongNameAndSeveralArguments(String, String, String, a: String, b: String)\n}\n\npub const local = TypeWithALongNameAndSeveralArguments(\"one\", \"two\", \"three\", \"four\", \"five\")\n"
5+
snapshot_kind: text
46
---
57
----- SOURCE CODE
68

@@ -15,18 +17,23 @@ pub const local = TypeWithALongNameAndSeveralArguments("one", "two", "three", "f
1517
import type * as _ from "../gleam.d.mts";
1618

1719
export class TypeWithALongNameAndSeveralArguments extends _.CustomType {
20+
/** @deprecated */
1821
constructor(
1922
argument$0: string,
2023
argument$1: string,
2124
argument$2: string,
2225
a: string,
2326
b: string
2427
);
25-
28+
/** @deprecated */
2629
0: string;
30+
/** @deprecated */
2731
1: string;
32+
/** @deprecated */
2833
2: string;
34+
/** @deprecated */
2935
a: string;
36+
/** @deprecated */
3037
b: string;
3138
}
3239
export function TypeWithALongNameAndSeveralArguments$TypeWithALongNameAndSeveralArguments(

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
---
22
source: compiler-core/src/javascript/tests/custom_types.rs
3+
assertion_line: 567
34
expression: "pub opaque type Animal {\n Cat(goes_outside: Bool)\n Dog(plays_fetch: Bool)\n}\n"
5+
snapshot_kind: text
46
---
57
----- SOURCE CODE
68
pub opaque type Animal {
@@ -13,14 +15,16 @@ pub opaque type Animal {
1315
import type * as _ from "../gleam.d.mts";
1416

1517
declare class Cat extends _.CustomType {
18+
/** @deprecated */
1619
constructor(goes_outside: boolean);
17-
20+
/** @deprecated */
1821
goes_outside: boolean;
1922
}
2023

2124
declare class Dog extends _.CustomType {
25+
/** @deprecated */
2226
constructor(plays_fetch: boolean);
23-
27+
/** @deprecated */
2428
plays_fetch: boolean;
2529
}
2630

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
---
22
source: compiler-core/src/javascript/tests/custom_types.rs
3+
assertion_line: 555
34
expression: "pub type Cat { Cat(name: String) }\n\npub fn return_unapplied_cat() {\n Cat\n}\n"
5+
snapshot_kind: text
46
---
57
----- SOURCE CODE
68
pub type Cat { Cat(name: String) }
@@ -14,8 +16,9 @@ pub fn return_unapplied_cat() {
1416
import type * as _ from "../gleam.d.mts";
1517

1618
export class Cat extends _.CustomType {
19+
/** @deprecated */
1720
constructor(name: string);
18-
21+
/** @deprecated */
1922
name: string;
2023
}
2124
export function Cat$Cat(name: string): Cat$;

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
---
22
source: compiler-core/src/javascript/tests/custom_types.rs
3+
assertion_line: 179
34
expression: "\npub type Ip{\n Ip(String)\n}\n\npub const local = Ip(\"0.0.0.0\")\n\n"
5+
snapshot_kind: text
46
---
57
----- SOURCE CODE
68

@@ -16,8 +18,9 @@ pub const local = Ip("0.0.0.0")
1618
import type * as _ from "../gleam.d.mts";
1719

1820
export class Ip extends _.CustomType {
21+
/** @deprecated */
1922
constructor(argument$0: string);
20-
23+
/** @deprecated */
2124
0: string;
2225
}
2326
export function Ip$Ip($0: string): Ip$;

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
---
22
source: compiler-core/src/javascript/tests/generics.rs
3+
assertion_line: 15
34
expression: "pub type Animal(t) {\n Cat(type_: t)\n Dog(type_: t)\n}\n\npub fn main() {\n Cat(type_: 6)\n}\n"
5+
snapshot_kind: text
46
---
57
----- SOURCE CODE
68
pub type Animal(t) {
@@ -17,8 +19,9 @@ pub fn main() {
1719
import type * as _ from "../gleam.d.mts";
1820

1921
export class Cat<I> extends _.CustomType {
22+
/** @deprecated */
2023
constructor(type_: I);
21-
24+
/** @deprecated */
2225
type_: I;
2326
}
2427
export function Animal$Cat<I>(type_: I): Animal$<I>;
@@ -27,8 +30,9 @@ export function Animal$Cat$0<I>(value: Animal$<I>): I;
2730
export function Animal$Cat$type_<I>(value: Animal$<I>): I;
2831

2932
export class Dog<I> extends _.CustomType {
33+
/** @deprecated */
3034
constructor(type_: I);
31-
35+
/** @deprecated */
3236
type_: I;
3337
}
3438
export function Animal$Dog<I>(type_: I): Animal$<I>;

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
---
22
source: compiler-core/src/javascript/tests/type_alias.rs
3+
assertion_line: 14
34
expression: "\ntype PrivateType {\n PrivateType\n}\n\npub opaque type OpaqueType {\n OpaqueType(PrivateType)\n}\n"
5+
snapshot_kind: text
46
---
57
----- SOURCE CODE
68

@@ -21,8 +23,9 @@ declare class PrivateType extends _.CustomType {}
2123
declare type PrivateType$ = PrivateType;
2224

2325
declare class OpaqueType extends _.CustomType {
26+
/** @deprecated */
2427
constructor(argument$0: PrivateType$);
25-
28+
/** @deprecated */
2629
0: PrivateType$;
2730
}
2831

compiler-core/src/javascript/typescript.rs

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,7 @@ impl<'a> TypeScriptGenerator<'a> {
449449
let mut definitions = constructors
450450
.iter()
451451
.map(|constructor| {
452-
self.record_definition(
452+
self.variant_definition(
453453
constructor,
454454
constructor_publicity,
455455
name,
@@ -504,7 +504,7 @@ impl<'a> TypeScriptGenerator<'a> {
504504
definitions
505505
}
506506

507-
fn record_definition(
507+
fn variant_definition(
508508
&mut self,
509509
constructor: &'a TypedRecordConstructor,
510510
publicity: Publicity,
@@ -513,27 +513,27 @@ impl<'a> TypeScriptGenerator<'a> {
513513
type_parameters: &'a [Arc<Type>],
514514
) -> Document<'a> {
515515
self.set_prelude_used();
516-
let class_definition = self.record_class_definition(constructor, publicity);
516+
let class_definition = self.variant_class_definition(constructor, publicity);
517517

518518
// If the custom type is private or opaque, we don't need to generate API
519519
// functions for it.
520520
if publicity.is_private() {
521521
return class_definition;
522522
}
523523

524-
let constructor_definition = self.record_constructor_definition(
524+
let constructor_definition = self.variant_constructor_definition(
525525
constructor,
526526
type_name,
527527
type_name_with_generics,
528528
type_parameters,
529529
);
530-
let record_check_definition = self.record_check_definition(
530+
let variant_check_definition = self.variant_check_definition(
531531
constructor,
532532
type_name,
533533
type_name_with_generics,
534534
type_parameters,
535535
);
536-
let fields_definition = self.record_fields_definition(
536+
let fields_definition = self.variant_fields_definition(
537537
constructor,
538538
type_name,
539539
type_name_with_generics,
@@ -545,12 +545,12 @@ impl<'a> TypeScriptGenerator<'a> {
545545
line(),
546546
constructor_definition,
547547
line(),
548-
record_check_definition,
548+
variant_check_definition,
549549
fields_definition,
550550
]
551551
}
552552

553-
fn record_class_definition(
553+
fn variant_class_definition(
554554
&mut self,
555555
constructor: &'a TypedRecordConstructor,
556556
publicity: Publicity,
@@ -574,6 +574,8 @@ impl<'a> TypeScriptGenerator<'a> {
574574
};
575575

576576
let class_body = docvec![
577+
line(),
578+
"/** @deprecated */",
577579
line(),
578580
// First add the constructor
579581
"constructor",
@@ -598,7 +600,6 @@ impl<'a> TypeScriptGenerator<'a> {
598600
),
599601
";",
600602
line(),
601-
line(),
602603
// Then add each field to the class
603604
join(
604605
constructor.arguments.iter().enumerate().map(|(i, arg)| {
@@ -609,6 +610,8 @@ impl<'a> TypeScriptGenerator<'a> {
609610
.unwrap_or_else(|| eco_format!("{i}"))
610611
.to_doc();
611612
docvec![
613+
"/** @deprecated */",
614+
line(),
612615
name,
613616
": ",
614617
self.do_print_force_generic_param(&arg.type_),
@@ -623,7 +626,7 @@ impl<'a> TypeScriptGenerator<'a> {
623626
docvec![head, class_body, line(), "}"]
624627
}
625628

626-
fn record_constructor_definition(
629+
fn variant_constructor_definition(
627630
&mut self,
628631
constructor: &'a TypedRecordConstructor,
629632
type_name: &'a str,
@@ -646,8 +649,11 @@ impl<'a> TypeScriptGenerator<'a> {
646649
])
647650
}
648651

649-
let function_name =
650-
eco_format!("{type_name}${record_name}", record_name = constructor.name).to_doc();
652+
let function_name = eco_format!(
653+
"{type_name}${variant_name}",
654+
variant_name = constructor.name
655+
)
656+
.to_doc();
651657

652658
docvec![
653659
"export function ",
@@ -662,16 +668,16 @@ impl<'a> TypeScriptGenerator<'a> {
662668
.group()
663669
}
664670

665-
fn record_check_definition(
671+
fn variant_check_definition(
666672
&self,
667673
constructor: &'a TypedRecordConstructor,
668674
type_name: &'a str,
669675
type_name_with_generics: &Document<'a>,
670676
type_parameters: &'a [Arc<Type>],
671677
) -> Document<'a> {
672678
let function_name = eco_format!(
673-
"{type_name}$is{record_name}",
674-
record_name = constructor.name
679+
"{type_name}$is{variant_name}",
680+
variant_name = constructor.name
675681
)
676682
.to_doc();
677683

@@ -686,7 +692,7 @@ impl<'a> TypeScriptGenerator<'a> {
686692
.group()
687693
}
688694

689-
fn record_fields_definition(
695+
fn variant_fields_definition(
690696
&mut self,
691697
constructor: &'a TypedRecordConstructor,
692698
type_name: &'a str,
@@ -703,8 +709,8 @@ impl<'a> TypeScriptGenerator<'a> {
703709
// present to ensure consistent behaviour between labelled and unlabelled
704710
// field access.
705711
let function_name = eco_format!(
706-
"{type_name}${record_name}${index}",
707-
record_name = constructor.name
712+
"{type_name}${variant_name}${index}",
713+
variant_name = constructor.name
708714
)
709715
.to_doc();
710716

@@ -728,8 +734,8 @@ impl<'a> TypeScriptGenerator<'a> {
728734
// argument.
729735
if let Some((_, label)) = &argument.label {
730736
let function_name = eco_format!(
731-
"{type_name}${record_name}${label}",
732-
record_name = constructor.name
737+
"{type_name}${variant_name}${label}",
738+
variant_name = constructor.name
733739
)
734740
.to_doc();
735741

0 commit comments

Comments
 (0)