Skip to content

Commit 781ee14

Browse files
committed
Add support for @tag in gentype.
1 parent 1165df5 commit 781ee14

File tree

10 files changed

+32
-14
lines changed

10 files changed

+32
-14
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
1313
# 11.0.0-alpha.3 (Unreleased)
1414

15+
#### :bug: Bug Fix
16+
- GenType: add support for custom `@tag` in variant type declaration. https://github.com/rescript-lang/rescript-compiler/pull/6137/files
17+
1518
# 11.0.0-alpha.2
1619

1720
#### :rocket: Main New Feature

jscomp/gentype/Annotation.ml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ let tagIsGenTypeAs s = s = "genType.as" || s = "gentype.as"
2222
let tagIsAs s = s = "bs.as" || s = "as"
2323
let tagIsInt s = s = "bs.int" || s = "int"
2424
let tagIsString s = s = "bs.string" || s = "string"
25+
let tagIsTag s = s = "tag"
26+
2527
let tagIsUnboxed s = s = "unboxed" || s = "ocaml.unboxed"
2628
let tagIsGenTypeImport s = s = "genType.import" || s = "gentype.import"
2729
let tagIsGenTypeOpaque s = s = "genType.opaque" || s = "gentype.opaque"
@@ -125,6 +127,11 @@ let getAsString attributes =
125127
| Some (_, StringPayload s) -> Some s
126128
| _ -> None
127129

130+
let getTag attributes =
131+
match attributes |> getAttributePayload tagIsTag with
132+
| Some (_, StringPayload s) -> Some s
133+
| _ -> None
134+
128135
let getAsInt attributes =
129136
match attributes |> getAttributePayload tagIsAs with
130137
| Some (_, IntPayload s) -> (

jscomp/gentype/EmitType.ml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ let rec renderType ~(config : Config.t) ?(indent = None) ~typeNameIsInterface
170170
|> String.concat ", ")
171171
^ "]"
172172
| TypeVar s -> s
173-
| Variant {inherits; noPayloads; payloads; polymorphic; unboxed} ->
173+
| Variant {inherits; noPayloads; payloads; polymorphic; unboxed; customTag} ->
174174
let inheritsRendered =
175175
inherits
176176
|> List.map (fun type_ ->
@@ -195,7 +195,8 @@ let rec renderType ~(config : Config.t) ?(indent = None) ~typeNameIsInterface
195195
t |> renderType ~config ~indent ~typeNameIsInterface ~inFunType
196196
in
197197
let tagField =
198-
case |> labelJSToString |> field ~name:Runtime.jsVariantTag
198+
case |> labelJSToString
199+
|> field ~name:(Runtime.jsVariantTag ~customTag)
199200
in
200201
match (unboxed, type_) with
201202
| true, type_ -> type_ |> render

jscomp/gentype/GenTypeCommon.ml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ and variant = {
9797
payloads: payload list;
9898
polymorphic: bool; (* If true, this is a polymorphic variant *)
9999
unboxed: bool;
100+
customTag: string option;
100101
}
101102

102103
and payload = {case: case; inlineRecord: bool; numArgs: int; t: type_}
@@ -166,8 +167,9 @@ let rec depToResolvedName (dep : dep) =
166167
| Dot (p, s) -> ResolvedName.dot s (p |> depToResolvedName)
167168

168169
let createVariant ~bsStringOrInt ~inherits ~noPayloads ~payloads ~polymorphic
169-
~unboxed =
170-
Variant {bsStringOrInt; inherits; noPayloads; payloads; polymorphic; unboxed}
170+
~unboxed ~customTag =
171+
Variant
172+
{bsStringOrInt; inherits; noPayloads; payloads; polymorphic; unboxed; customTag}
171173

172174
let ident ?(builtin = true) ?(typeArgs = []) name =
173175
Ident {builtin; name; typeArgs}

jscomp/gentype/Runtime.ml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@ let rec emitModuleAccessPath ~config moduleAccessPath =
2424
| Dot (p, moduleItem) ->
2525
p |> emitModuleAccessPath ~config |> EmitText.fieldAccess ~label:moduleItem
2626

27-
let jsVariantTag = "TAG"
27+
let jsVariantTag ~customTag =
28+
match customTag with
29+
| None -> "TAG"
30+
| Some tag -> tag
2831
let jsPolymorphicVariantTag = "NAME"
2932

3033
let jsVariantPayloadTag ~n = "_" ^ string_of_int n

jscomp/gentype/Runtime.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ val newModuleItem : name:string -> moduleItem
1515
val newRecordValue : unboxed:bool -> recordGen -> recordValue
1616
val recordGen : unit -> recordGen
1717
val recordValueToString : recordValue -> string
18-
val jsVariantTag : string
18+
val jsVariantTag : customTag:string option -> string
1919
val jsPolymorphicVariantTag : string
2020
val jsVariantPayloadTag : n:int -> string
2121
val jsVariantValue : polymorphic:bool -> string

jscomp/gentype/TranslateCoreType.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ and translateCoreType_ ~config ~typeVarsGen
224224
let inherits = inheritsTranslations |> List.map (fun {type_} -> type_) in
225225
let type_ =
226226
createVariant ~bsStringOrInt:(asString || asInt) ~noPayloads ~payloads
227-
~inherits ~polymorphic:true ~unboxed:false
227+
~inherits ~polymorphic:true ~unboxed:false ~customTag:None
228228
in
229229
let dependencies =
230230
(inheritsTranslations

jscomp/gentype/TranslateTypeDeclarations.ml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ let traslateDeclarationKind ~config ~loc ~outputFileRelative ~resolver
6363
let unboxedAnnotation =
6464
typeAttributes |> Annotation.hasAttribute Annotation.tagIsUnboxed
6565
in
66+
let customTag =
67+
typeAttributes |> Annotation.getTag in
6668
let returnTypeDeclaration (typeDeclaration : CodeItem.typeDeclaration) =
6769
match opaque = Some true with
6870
| true -> [{typeDeclaration with importTypes = []}]
@@ -197,7 +199,7 @@ let traslateDeclarationKind ~config ~loc ~outputFileRelative ~resolver
197199
else variant.payloads
198200
in
199201
createVariant ~bsStringOrInt:false ~inherits:variant.inherits
200-
~noPayloads ~payloads ~polymorphic:true ~unboxed:false
202+
~noPayloads ~payloads ~polymorphic:true ~unboxed:false ~customTag:None
201203
| _ -> translation.type_
202204
in
203205
{translation with type_} |> handleGeneralDeclaration
@@ -312,7 +314,7 @@ let traslateDeclarationKind ~config ~loc ~outputFileRelative ~resolver
312314
in
313315
let variantTyp =
314316
createVariant ~bsStringOrInt:false ~inherits:[] ~noPayloads ~payloads
315-
~polymorphic:false ~unboxed:unboxedAnnotation
317+
~polymorphic:false ~unboxed:unboxedAnnotation ~customTag
316318
in
317319
let resolvedTypeName = typeName |> TypeEnv.addModulePath ~typeEnv in
318320
let exportFromTypeDeclaration =

jscomp/gentype/TranslateTypeExprFromTypes.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ let translateConstr ~config ~paramsTranslation ~(path : Path.t) ~typeEnv =
146146
case 0 "Ok" paramTranslation1.type_;
147147
case 1 "Error" paramTranslation2.type_;
148148
]
149-
~polymorphic:false ~unboxed:false
149+
~polymorphic:false ~unboxed:false ~customTag:None
150150
in
151151
{
152152
dependencies =
@@ -408,7 +408,7 @@ and translateTypeExprFromTypes_ ~config ~typeVarsGen ~typeEnv
408408
in
409409
let type_ =
410410
createVariant ~bsStringOrInt:false ~inherits:[] ~noPayloads ~payloads:[]
411-
~polymorphic:true ~unboxed:false
411+
~polymorphic:true ~unboxed:false ~customTag:None
412412
in
413413
{dependencies = []; type_}
414414
| {noPayloads = []; payloads = [(_label, t)]; unknowns = []} ->
@@ -439,7 +439,7 @@ and translateTypeExprFromTypes_ ~config ~typeVarsGen ~typeEnv
439439
in
440440
let type_ =
441441
createVariant ~bsStringOrInt:false ~inherits:[] ~noPayloads ~payloads
442-
~polymorphic:true ~unboxed:false
442+
~polymorphic:true ~unboxed:false ~customTag:None
443443
in
444444
let dependencies =
445445
payloadTranslations

jscomp/gentype_tests/typescript-react-example/src/TestPromise.gen.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ export type toPayload = { readonly result: string };
1717

1818
// tslint:disable-next-line:interface-over-type-literal
1919
export type settledResult<a> =
20-
{ TAG: "fulfilled"; readonly value: a }
21-
| { TAG: "rejected"; readonly reason: unknown };
20+
{ status: "fulfilled"; readonly value: a }
21+
| { status: "rejected"; readonly reason: unknown };
2222

2323
// tslint:disable-next-line:interface-over-type-literal
2424
export type settled = settledResult<string>;

0 commit comments

Comments
 (0)