Skip to content

Commit da3d740

Browse files
authored
Gentype: support @tag (#6437)
Fixes #6436
1 parent b9df8ac commit da3d740

13 files changed

+62
-15
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#### :bug: Bug Fix
1616

1717
- Fix issue with Dynamic import of module in nested expressions https://github.com/rescript-lang/rescript-compiler/pull/6431
18+
- Fix issue where GenType was not supporting `@tag` on ordinary variatns https://github.com/rescript-lang/rescript-compiler/pull/6437
1819

1920
# 11.0.0-rc.4
2021

jscomp/gentype/Annotation.ml

+8
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ 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+
26+
let tagIsTag s = s = "tag"
27+
2528
let tagIsUnboxed s = s = "unboxed" || s = "ocaml.unboxed"
2629
let tagIsGenTypeImport s = s = "genType.import" || s = "gentype.import"
2730
let tagIsGenTypeOpaque s = s = "genType.opaque" || s = "gentype.opaque"
@@ -146,6 +149,11 @@ let getAttributeImportRenaming attributes =
146149
(Some importString, Some renameString)
147150
| _ -> (None, genTypeAsRenaming)
148151

152+
let getTag attributes =
153+
match attributes |> getAttributePayload tagIsTag with
154+
| Some (_, StringPayload s) -> Some s
155+
| _ -> None
156+
149157
let getDocPayload attributes =
150158
let docPayload = attributes |> getAttributePayload tagIsDoc in
151159
match docPayload with

jscomp/gentype/EmitType.ml

+4-3
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ let rec renderType ~(config : Config.t) ?(indent = None) ~typeNameIsInterface
156156
|> String.concat ", ")
157157
^ "]"
158158
| TypeVar s -> s
159-
| Variant {inherits; noPayloads; payloads; polymorphic; unboxed} ->
159+
| Variant {inherits; noPayloads; payloads; polymorphic; tag; unboxed} ->
160160
let inheritsRendered =
161161
inherits
162162
|> List.map (fun type_ ->
@@ -183,7 +183,8 @@ let rec renderType ~(config : Config.t) ?(indent = None) ~typeNameIsInterface
183183
in
184184
let tagField =
185185
case |> labelJSToString
186-
|> field ~name:(Runtime.jsVariantTag ~polymorphic:false)
186+
|> field
187+
~name:(Runtime.jsVariantTag ~polymorphic:false ~tag)
187188
in
188189
match (unboxed, type_) with
189190
| true, type_ ->
@@ -198,7 +199,7 @@ let rec renderType ~(config : Config.t) ?(indent = None) ~typeNameIsInterface
198199
(* poly variant *)
199200
[
200201
case |> labelJSToString
201-
|> field ~name:(Runtime.jsVariantTag ~polymorphic);
202+
|> field ~name:(Runtime.jsVariantTag ~polymorphic ~tag);
202203
type_ |> render
203204
|> field ~name:(Runtime.jsVariantValue ~polymorphic);
204205
]

jscomp/gentype/GenTypeCommon.ml

+3-2
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ and variant = {
9999
noPayloads: case list;
100100
payloads: payload list;
101101
polymorphic: bool; (* If true, this is a polymorphic variant *)
102+
tag: string option; (* The name of the tag field at runtime *)
102103
unboxed: bool;
103104
}
104105

@@ -168,8 +169,8 @@ let rec depToResolvedName (dep : dep) =
168169
| Internal resolvedName -> resolvedName
169170
| Dot (p, s) -> ResolvedName.dot s (p |> depToResolvedName)
170171

171-
let createVariant ~inherits ~noPayloads ~payloads ~polymorphic ~unboxed =
172-
Variant {inherits; noPayloads; payloads; polymorphic; unboxed}
172+
let createVariant ~inherits ~noPayloads ~payloads ~polymorphic ~tag ~unboxed =
173+
Variant {inherits; noPayloads; payloads; polymorphic; tag; unboxed}
173174

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

jscomp/gentype/Runtime.ml

+5-2
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,13 @@ let rec emitModuleAccessPath ~config moduleAccessPath =
2424
| Dot (p, moduleItem) ->
2525
p |> emitModuleAccessPath ~config |> EmitText.fieldAccess ~label:moduleItem
2626

27-
let jsVariantTag ~polymorphic =
27+
let jsVariantTag ~polymorphic ~tag =
2828
match polymorphic with
2929
| true -> "NAME"
30-
| false -> "TAG"
30+
| false -> (
31+
match tag with
32+
| Some tag -> tag
33+
| None -> "TAG")
3134

3235
let jsVariantPayloadTag ~n = "_" ^ string_of_int n
3336

jscomp/gentype/Runtime.mli

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@ val newModuleItem : name:string -> moduleItem
1414
val newRecordValue : unboxed:bool -> recordGen -> recordValue
1515
val recordGen : unit -> recordGen
1616
val recordValueToString : recordValue -> string
17-
val jsVariantTag : polymorphic:bool -> string
17+
val jsVariantTag : polymorphic:bool -> tag:string option -> string
1818
val jsVariantPayloadTag : n:int -> string
1919
val jsVariantValue : polymorphic:bool -> string

jscomp/gentype/TranslateCoreType.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ and translateCoreType_ ~config ~typeVarsGen
212212
let inherits = inheritsTranslations |> List.map (fun {type_} -> type_) in
213213
let type_ =
214214
createVariant ~noPayloads ~payloads ~inherits ~polymorphic:true
215-
~unboxed:false
215+
~tag:None ~unboxed:false
216216
in
217217
let dependencies =
218218
(inheritsTranslations

jscomp/gentype/TranslateTypeDeclarations.ml

+3-2
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ let traslateDeclarationKind ~config ~loc ~outputFileRelative ~resolver
6666
let unboxedAnnotation =
6767
typeAttributes |> Annotation.hasAttribute Annotation.tagIsUnboxed
6868
in
69+
let tagAnnotation = typeAttributes |> Annotation.getTag in
6970
let returnTypeDeclaration (typeDeclaration : CodeItem.typeDeclaration) =
7071
match opaque = Some true with
7172
| true -> [{typeDeclaration with importTypes = []}]
@@ -203,7 +204,7 @@ let traslateDeclarationKind ~config ~loc ~outputFileRelative ~resolver
203204
else variant.payloads
204205
in
205206
createVariant ~inherits:variant.inherits ~noPayloads ~payloads
206-
~polymorphic:true ~unboxed:false
207+
~polymorphic:true ~tag:None ~unboxed:false
207208
| _ -> translation.type_
208209
in
209210
{translation with type_} |> handleGeneralDeclaration
@@ -295,7 +296,7 @@ let traslateDeclarationKind ~config ~loc ~outputFileRelative ~resolver
295296
in
296297
let variantTyp =
297298
createVariant ~inherits:[] ~noPayloads ~payloads ~polymorphic:false
298-
~unboxed:unboxedAnnotation
299+
~tag:tagAnnotation ~unboxed:unboxedAnnotation
299300
in
300301
let resolvedTypeName = typeName |> TypeEnv.addModulePath ~typeEnv in
301302
let exportFromTypeDeclaration =

jscomp/gentype/TranslateTypeExprFromTypes.ml

+3-3
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ let translateConstr ~config ~paramsTranslation ~(path : Path.t) ~typeEnv =
147147
case 0 "Ok" paramTranslation1.type_;
148148
case 1 "Error" paramTranslation2.type_;
149149
]
150-
~polymorphic:false ~unboxed:false
150+
~polymorphic:false ~tag:None ~unboxed:false
151151
in
152152
{
153153
dependencies =
@@ -386,7 +386,7 @@ and translateTypeExprFromTypes_ ~config ~typeVarsGen ~typeEnv
386386
in
387387
let type_ =
388388
createVariant ~inherits:[] ~noPayloads ~payloads:[] ~polymorphic:true
389-
~unboxed:false
389+
~tag:None ~unboxed:false
390390
in
391391
{dependencies = []; type_}
392392
| {noPayloads = []; payloads = [(_label, t)]; unknowns = []} ->
@@ -415,7 +415,7 @@ and translateTypeExprFromTypes_ ~config ~typeVarsGen ~typeEnv
415415
in
416416
let type_ =
417417
createVariant ~inherits:[] ~noPayloads ~payloads ~polymorphic:true
418-
~unboxed:false
418+
~tag:None ~unboxed:false
419419
in
420420
let dependencies =
421421
payloadTranslations

jscomp/gentype_tests/typescript-react-example/package-lock.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

jscomp/gentype_tests/typescript-react-example/src/Lib.bs.js

+18
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/* TypeScript file generated from Lib.res by genType. */
2+
/* eslint-disable import/first */
3+
4+
5+
// tslint:disable-next-line:interface-over-type-literal
6+
export type action =
7+
{ action: "A"; _0: string }
8+
| { action: "B"; _0: string };
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
@gentype
2+
@tag("action")
3+
type action = | A(string) | B(string)
4+
5+
let a = A("a")
6+
let b = B("b")

0 commit comments

Comments
 (0)