Skip to content

Commit a9e8207

Browse files
committed
Fix invalid TS definition for long variant constructors with no args
1 parent fd4b266 commit a9e8207

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
@@ -70,3 +70,7 @@
7070
- Fixed a bug where the "pattern match on variable" code action would generate
7171
invalid patterns by repeating a variable name already used in the same pattern.
7272
([Giacomo Cavalieri](https://github.com/giacomocavalieri))
73+
74+
- Fix invalid TypeScript definition being generated for variant constructors
75+
with long names that take no arguments.
76+
([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
@@ -655,13 +655,21 @@ impl<'a> TypeScriptGenerator<'a> {
655655
)
656656
.to_doc();
657657

658+
let arguments = if arguments.is_empty() {
659+
docvec!["(): "]
660+
} else {
661+
docvec![
662+
"(",
663+
docvec![break_("", ""), join(arguments, break_(",", ", ")),].nest(INDENT),
664+
break_(",", ""),
665+
"): ",
666+
]
667+
};
668+
658669
docvec![
659670
"export function ",
660671
name_with_generics(function_name, type_parameters),
661-
"(",
662-
docvec![break_("", ""), join(arguments, break_(",", ", ")),].nest(INDENT),
663-
break_(",", ""),
664-
"): ",
672+
arguments,
665673
type_name_with_generics.clone(),
666674
";"
667675
]

0 commit comments

Comments
 (0)