Skip to content

Commit 480b235

Browse files
cristianoccometkim
authored andcommitted
Add support for @tag in gentype.
1 parent 7811493 commit 480b235

File tree

10 files changed

+31
-14
lines changed

10 files changed

+31
-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.5 (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.4
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"
@@ -123,6 +125,11 @@ let getAsString attributes =
123125
| Some (_, StringPayload s) -> Some s
124126
| _ -> None
125127

128+
let getTag attributes =
129+
match attributes |> getAttributePayload tagIsTag with
130+
| Some (_, StringPayload s) -> Some s
131+
| _ -> None
132+
126133
let getAsInt attributes =
127134
match attributes |> getAttributePayload tagIsAs with
128135
| 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
@@ -90,6 +90,7 @@ and variant = {
9090
payloads: payload list;
9191
polymorphic: bool; (* If true, this is a polymorphic variant *)
9292
unboxed: bool;
93+
customTag: string option;
9394
}
9495

9596
and payload = {case: case; t: type_}
@@ -158,8 +159,9 @@ let rec depToResolvedName (dep : dep) =
158159
| Internal resolvedName -> resolvedName
159160
| Dot (p, s) -> ResolvedName.dot s (p |> depToResolvedName)
160161

161-
let createVariant ~inherits ~noPayloads ~payloads ~polymorphic ~unboxed =
162-
Variant {inherits; noPayloads; payloads; polymorphic; unboxed}
162+
let createVariant ~inherits ~noPayloads ~payloads ~polymorphic ~unboxed
163+
~customTag =
164+
Variant {inherits; noPayloads; payloads; polymorphic; unboxed; customTag}
163165

164166
let ident ?(builtin = true) ?(typeArgs = []) name =
165167
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
@@ -213,7 +213,7 @@ and translateCoreType_ ~config ~typeVarsGen
213213
let inherits = inheritsTranslations |> List.map (fun {type_} -> type_) in
214214
let type_ =
215215
createVariant ~noPayloads ~payloads ~inherits ~polymorphic:true
216-
~unboxed:false
216+
~unboxed:false ~customTag:None
217217
in
218218
let dependencies =
219219
(inheritsTranslations

jscomp/gentype/TranslateTypeDeclarations.ml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ let traslateDeclarationKind ~config ~loc ~outputFileRelative ~resolver
6363
let unboxedAnnotation =
6464
typeAttributes |> Annotation.hasAttribute Annotation.tagIsUnboxed
6565
in
66+
let customTag = typeAttributes |> Annotation.getTag in
6667
let returnTypeDeclaration (typeDeclaration : CodeItem.typeDeclaration) =
6768
match opaque = Some true with
6869
| true -> [{typeDeclaration with importTypes = []}]
@@ -197,7 +198,7 @@ let traslateDeclarationKind ~config ~loc ~outputFileRelative ~resolver
197198
else variant.payloads
198199
in
199200
createVariant ~inherits:variant.inherits ~noPayloads ~payloads
200-
~polymorphic:true ~unboxed:false
201+
~polymorphic:true ~unboxed:false ~customTag:None
201202
| _ -> translation.type_
202203
in
203204
{translation with type_} |> handleGeneralDeclaration
@@ -289,7 +290,7 @@ let traslateDeclarationKind ~config ~loc ~outputFileRelative ~resolver
289290
in
290291
let variantTyp =
291292
createVariant ~inherits:[] ~noPayloads ~payloads ~polymorphic:false
292-
~unboxed:unboxedAnnotation
293+
~unboxed:unboxedAnnotation ~customTag
293294
in
294295
let resolvedTypeName = typeName |> TypeEnv.addModulePath ~typeEnv in
295296
let exportFromTypeDeclaration =

jscomp/gentype/TranslateTypeExprFromTypes.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ let translateConstr ~config ~paramsTranslation ~(path : Path.t) ~typeEnv =
141141
case 0 "Ok" paramTranslation1.type_;
142142
case 1 "Error" paramTranslation2.type_;
143143
]
144-
~polymorphic:false ~unboxed:false
144+
~polymorphic:false ~unboxed:false ~customTag:None
145145
in
146146
{
147147
dependencies =
@@ -382,7 +382,7 @@ and translateTypeExprFromTypes_ ~config ~typeVarsGen ~typeEnv
382382
in
383383
let type_ =
384384
createVariant ~inherits:[] ~noPayloads ~payloads:[] ~polymorphic:true
385-
~unboxed:false
385+
~unboxed:false ~customTag:None
386386
in
387387
{dependencies = []; type_}
388388
| {noPayloads = []; payloads = [(_label, t)]; unknowns = []} ->
@@ -411,7 +411,7 @@ and translateTypeExprFromTypes_ ~config ~typeVarsGen ~typeEnv
411411
in
412412
let type_ =
413413
createVariant ~inherits:[] ~noPayloads ~payloads ~polymorphic:true
414-
~unboxed:false
414+
~unboxed:false ~customTag:None
415415
in
416416
let dependencies =
417417
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)