Skip to content

Commit 0452169

Browse files
Merge pull request #614 from PassiveLogic/krodak/ts-enum-fix
BridgeJS: Fix incorrect TypeScript type names for `tsEnum`-style enums and struct associated values
2 parents c8565c9 + c6f2642 commit 0452169

File tree

4 files changed

+22
-13
lines changed

4 files changed

+22
-13
lines changed

Plugins/BridgeJS/Sources/BridgeJSLink/BridgeJSLink.swift

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1359,6 +1359,9 @@ public struct BridgeJSLink {
13591359
case .namespaceEnum:
13601360
return enumDef.tsFullPath
13611361
default:
1362+
if enumDef.emitStyle == .tsEnum {
1363+
return enumDef.tsFullPath
1364+
}
13621365
return "\(enumDef.tsFullPath)Tag"
13631366
}
13641367
}
@@ -1679,7 +1682,7 @@ public struct BridgeJSLink {
16791682
]
16801683
for (associatedValueIndex, associatedValue) in enumCase.associatedValues.enumerated() {
16811684
let prop = associatedValue.label ?? "param\(associatedValueIndex)"
1682-
fields.append("\(prop): \(associatedValue.type.tsType)")
1685+
fields.append("\(prop): \(resolveTypeScriptType(associatedValue.type))")
16831686
}
16841687
unionParts.append("{ \(fields.joined(separator: "; ")) }")
16851688
}
@@ -2819,6 +2822,7 @@ extension BridgeJSLink {
28192822
depth: 1,
28202823
printer: printer,
28212824
exposeToGlobal: true,
2825+
exportedSkeletons: exportedSkeletons,
28222826
renderTSSignatureCallback: renderTSSignatureCallback
28232827
)
28242828
printer.unindent()
@@ -2837,6 +2841,7 @@ extension BridgeJSLink {
28372841
depth: 1,
28382842
printer: printer,
28392843
exposeToGlobal: false,
2844+
exportedSkeletons: exportedSkeletons,
28402845
renderTSSignatureCallback: renderTSSignatureCallback
28412846
)
28422847
}
@@ -2850,6 +2855,7 @@ extension BridgeJSLink {
28502855
depth: Int,
28512856
printer: CodeFragmentPrinter,
28522857
exposeToGlobal: Bool,
2858+
exportedSkeletons: [ExportedSkeleton],
28532859
renderTSSignatureCallback: @escaping ([Parameter], BridgeType, Effects) -> String
28542860
) {
28552861
func hasContent(node: NamespaceNode) -> Bool {
@@ -3004,7 +3010,9 @@ extension BridgeJSLink {
30043010
.enumerated()
30053011
{
30063012
let prop = associatedValue.label ?? "param\(associatedValueIndex)"
3007-
fields.append("\(prop): \(associatedValue.type.tsType)")
3013+
fields.append(
3014+
"\(prop): \(BridgeJSLink.resolveTypeScriptType(associatedValue.type, exportedSkeletons: exportedSkeletons))"
3015+
)
30083016
}
30093017
unionParts.append("{ \(fields.joined(separator: "; ")) }")
30103018
}
@@ -3038,6 +3046,7 @@ extension BridgeJSLink {
30383046
depth: depth + 1,
30393047
printer: printer,
30403048
exposeToGlobal: exposeToGlobal,
3049+
exportedSkeletons: exportedSkeletons,
30413050
renderTSSignatureCallback: renderTSSignatureCallback
30423051
)
30433052

Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/EnumAssociatedValue.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ export const AllTypesResultValues: {
8282
};
8383

8484
export type AllTypesResultTag =
85-
{ tag: typeof AllTypesResultValues.Tag.StructPayload; param0: PointTag } | { tag: typeof AllTypesResultValues.Tag.ClassPayload; param0: User } | { tag: typeof AllTypesResultValues.Tag.JsObjectPayload; param0: any } | { tag: typeof AllTypesResultValues.Tag.NestedEnum; param0: APIResultTag } | { tag: typeof AllTypesResultValues.Tag.ArrayPayload; param0: number[] } | { tag: typeof AllTypesResultValues.Tag.Empty }
85+
{ tag: typeof AllTypesResultValues.Tag.StructPayload; param0: Point } | { tag: typeof AllTypesResultValues.Tag.ClassPayload; param0: User } | { tag: typeof AllTypesResultValues.Tag.JsObjectPayload; param0: any } | { tag: typeof AllTypesResultValues.Tag.NestedEnum; param0: APIResultTag } | { tag: typeof AllTypesResultValues.Tag.ArrayPayload; param0: number[] } | { tag: typeof AllTypesResultValues.Tag.Empty }
8686

8787
export const OptionalAllTypesResultValues: {
8888
readonly Tag: {
@@ -96,7 +96,7 @@ export const OptionalAllTypesResultValues: {
9696
};
9797

9898
export type OptionalAllTypesResultTag =
99-
{ tag: typeof OptionalAllTypesResultValues.Tag.OptStruct; param0: PointTag | null } | { tag: typeof OptionalAllTypesResultValues.Tag.OptClass; param0: User | null } | { tag: typeof OptionalAllTypesResultValues.Tag.OptJSObject; param0: any | null } | { tag: typeof OptionalAllTypesResultValues.Tag.OptNestedEnum; param0: APIResultTag | null } | { tag: typeof OptionalAllTypesResultValues.Tag.OptArray; param0: number[] | null } | { tag: typeof OptionalAllTypesResultValues.Tag.Empty }
99+
{ tag: typeof OptionalAllTypesResultValues.Tag.OptStruct; param0: Point | null } | { tag: typeof OptionalAllTypesResultValues.Tag.OptClass; param0: User | null } | { tag: typeof OptionalAllTypesResultValues.Tag.OptJSObject; param0: any | null } | { tag: typeof OptionalAllTypesResultValues.Tag.OptNestedEnum; param0: APIResultTag | null } | { tag: typeof OptionalAllTypesResultValues.Tag.OptArray; param0: number[] | null } | { tag: typeof OptionalAllTypesResultValues.Tag.Empty }
100100

101101
export interface Point {
102102
x: number;

Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/EnumCase.d.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ export type Exports = {
4242
getDirection(): DirectionTag;
4343
processDirection(input: DirectionTag): StatusTag;
4444
roundTripOptionalDirection(input: DirectionTag | null): DirectionTag | null;
45-
setTSDirection(direction: TSDirectionTag): void;
46-
getTSDirection(): TSDirectionTag;
47-
roundTripOptionalTSDirection(input: TSDirectionTag | null): TSDirectionTag | null;
45+
setTSDirection(direction: TSDirection): void;
46+
getTSDirection(): TSDirection;
47+
roundTripOptionalTSDirection(input: TSDirection | null): TSDirection | null;
4848
Direction: DirectionObject
4949
Status: StatusObject
5050
PublicStatus: PublicStatusObject

Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/EnumRawType.d.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -113,18 +113,18 @@ export type Exports = {
113113
setTheme(theme: ThemeTag): void;
114114
getTheme(): ThemeTag;
115115
roundTripOptionalTheme(input: ThemeTag | null): ThemeTag | null;
116-
setTSTheme(theme: TSThemeTag): void;
117-
getTSTheme(): TSThemeTag;
118-
roundTripOptionalTSTheme(input: TSThemeTag | null): TSThemeTag | null;
116+
setTSTheme(theme: TSTheme): void;
117+
getTSTheme(): TSTheme;
118+
roundTripOptionalTSTheme(input: TSTheme | null): TSTheme | null;
119119
setFeatureFlag(flag: FeatureFlagTag): void;
120120
getFeatureFlag(): FeatureFlagTag;
121121
roundTripOptionalFeatureFlag(input: FeatureFlagTag | null): FeatureFlagTag | null;
122122
setHttpStatus(status: HttpStatusTag): void;
123123
getHttpStatus(): HttpStatusTag;
124124
roundTripOptionalHttpStatus(input: HttpStatusTag | null): HttpStatusTag | null;
125-
setTSHttpStatus(status: TSHttpStatusTag): void;
126-
getTSHttpStatus(): TSHttpStatusTag;
127-
roundTripOptionalHttpStatus(input: TSHttpStatusTag | null): TSHttpStatusTag | null;
125+
setTSHttpStatus(status: TSHttpStatus): void;
126+
getTSHttpStatus(): TSHttpStatus;
127+
roundTripOptionalHttpStatus(input: TSHttpStatus | null): TSHttpStatus | null;
128128
setPriority(priority: PriorityTag): void;
129129
getPriority(): PriorityTag;
130130
roundTripOptionalPriority(input: PriorityTag | null): PriorityTag | null;

0 commit comments

Comments
 (0)