Skip to content

Commit 5a74b5b

Browse files
committed
Fix invalid TS definition for long variant constructors with no args
1 parent 45c770c commit 5a74b5b

File tree

4 files changed

+49
-4
lines changed

4 files changed

+49
-4
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,3 +153,7 @@
153153
- Fixed a bug where useless comparison warnings for floats compared literal
154154
strings, claiming for example that `1.0 == 1.` was always false.
155155
([fruno](https://github.com/fruno-bulax/))
156+
157+
- Fix invalid TypeScript definition being generated for variant constructors
158+
with long names that take no arguments.
159+
([Richard Viney](https://github.com/richard-viney))

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,17 @@ pub const local = TypeWithALongNameAndSeveralArguments("one", "two", "three", "f
217217
);
218218
}
219219

220+
#[test]
221+
fn long_name_variant_without_arguments() {
222+
assert_ts_def!(
223+
r#"
224+
pub type TypeWithALongNameAndNoArguments {
225+
TypeWithALongNameAndNoArguments
226+
}
227+
"#
228+
);
229+
}
230+
220231
#[test]
221232
fn custom_type_with_named_fields() {
222233
assert_js!(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
---
2+
source: compiler-core/src/javascript/tests/custom_types.rs
3+
expression: "\npub type TypeWithALongNameAndNoArguments {\n TypeWithALongNameAndNoArguments\n}\n"
4+
snapshot_kind: text
5+
---
6+
----- SOURCE CODE
7+
8+
pub type TypeWithALongNameAndNoArguments {
9+
TypeWithALongNameAndNoArguments
10+
}
11+
12+
13+
----- TYPESCRIPT DEFINITIONS
14+
import type * as _ from "../gleam.d.mts";
15+
16+
export class TypeWithALongNameAndNoArguments extends _.CustomType {}
17+
export function TypeWithALongNameAndNoArguments$TypeWithALongNameAndNoArguments(): TypeWithALongNameAndNoArguments$;
18+
export function TypeWithALongNameAndNoArguments$isTypeWithALongNameAndNoArguments(
19+
value: TypeWithALongNameAndNoArguments$,
20+
): boolean;
21+
22+
export type TypeWithALongNameAndNoArguments$ = TypeWithALongNameAndNoArguments;

compiler-core/src/javascript/typescript.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -670,13 +670,21 @@ impl<'a> TypeScriptGenerator<'a> {
670670
)
671671
.to_doc();
672672

673+
let arguments = if arguments.is_empty() {
674+
docvec!["(): "]
675+
} else {
676+
docvec![
677+
"(",
678+
docvec![break_("", ""), join(arguments, break_(",", ", ")),].nest(INDENT),
679+
break_(",", ""),
680+
"): ",
681+
]
682+
};
683+
673684
docvec![
674685
"export function ",
675686
name_with_generics(function_name, type_parameters),
676-
"(",
677-
docvec![break_("", ""), join(arguments, break_(",", ", ")),].nest(INDENT),
678-
break_(",", ""),
679-
"): ",
687+
arguments,
680688
type_name_with_generics.clone(),
681689
";"
682690
]

0 commit comments

Comments
 (0)