File tree 13 files changed +62
-15
lines changed
gentype_tests/typescript-react-example
13 files changed +62
-15
lines changed Original file line number Diff line number Diff line change 15
15
#### :bug : Bug Fix
16
16
17
17
- 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
18
19
19
20
# 11.0.0-rc.4
20
21
Original file line number Diff line number Diff line change @@ -22,6 +22,9 @@ let tagIsGenTypeAs s = s = "genType.as" || s = "gentype.as"
22
22
let tagIsAs s = s = " bs.as" || s = " as"
23
23
let tagIsInt s = s = " bs.int" || s = " int"
24
24
let tagIsString s = s = " bs.string" || s = " string"
25
+
26
+ let tagIsTag s = s = " tag"
27
+
25
28
let tagIsUnboxed s = s = " unboxed" || s = " ocaml.unboxed"
26
29
let tagIsGenTypeImport s = s = " genType.import" || s = " gentype.import"
27
30
let tagIsGenTypeOpaque s = s = " genType.opaque" || s = " gentype.opaque"
@@ -146,6 +149,11 @@ let getAttributeImportRenaming attributes =
146
149
(Some importString, Some renameString)
147
150
| _ -> (None , genTypeAsRenaming)
148
151
152
+ let getTag attributes =
153
+ match attributes |> getAttributePayload tagIsTag with
154
+ | Some (_ , StringPayload s ) -> Some s
155
+ | _ -> None
156
+
149
157
let getDocPayload attributes =
150
158
let docPayload = attributes |> getAttributePayload tagIsDoc in
151
159
match docPayload with
Original file line number Diff line number Diff line change @@ -156,7 +156,7 @@ let rec renderType ~(config : Config.t) ?(indent = None) ~typeNameIsInterface
156
156
|> String. concat " , " )
157
157
^ " ]"
158
158
| TypeVar s -> s
159
- | Variant {inherits; noPayloads; payloads; polymorphic; unboxed} ->
159
+ | Variant {inherits; noPayloads; payloads; polymorphic; tag; unboxed} ->
160
160
let inheritsRendered =
161
161
inherits
162
162
|> List. map (fun type_ ->
@@ -183,7 +183,8 @@ let rec renderType ~(config : Config.t) ?(indent = None) ~typeNameIsInterface
183
183
in
184
184
let tagField =
185
185
case |> labelJSToString
186
- |> field ~name: (Runtime. jsVariantTag ~polymorphic: false )
186
+ |> field
187
+ ~name: (Runtime. jsVariantTag ~polymorphic: false ~tag )
187
188
in
188
189
match (unboxed, type_) with
189
190
| true , type_ ->
@@ -198,7 +199,7 @@ let rec renderType ~(config : Config.t) ?(indent = None) ~typeNameIsInterface
198
199
(* poly variant *)
199
200
[
200
201
case |> labelJSToString
201
- |> field ~name: (Runtime. jsVariantTag ~polymorphic );
202
+ |> field ~name: (Runtime. jsVariantTag ~polymorphic ~tag );
202
203
type_ |> render
203
204
|> field ~name: (Runtime. jsVariantValue ~polymorphic );
204
205
]
Original file line number Diff line number Diff line change @@ -99,6 +99,7 @@ and variant = {
99
99
noPayloads : case list ;
100
100
payloads : payload list ;
101
101
polymorphic : bool ; (* If true, this is a polymorphic variant *)
102
+ tag : string option ; (* The name of the tag field at runtime *)
102
103
unboxed : bool ;
103
104
}
104
105
@@ -168,8 +169,8 @@ let rec depToResolvedName (dep : dep) =
168
169
| Internal resolvedName -> resolvedName
169
170
| Dot (p , s ) -> ResolvedName. dot s (p |> depToResolvedName)
170
171
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}
173
174
174
175
let ident ?(builtin = true ) ?(typeArgs = [] ) name =
175
176
Ident {builtin; name; typeArgs}
Original file line number Diff line number Diff line change @@ -24,10 +24,13 @@ let rec emitModuleAccessPath ~config moduleAccessPath =
24
24
| Dot (p , moduleItem ) ->
25
25
p |> emitModuleAccessPath ~config |> EmitText. fieldAccess ~label: moduleItem
26
26
27
- let jsVariantTag ~polymorphic =
27
+ let jsVariantTag ~polymorphic ~ tag =
28
28
match polymorphic with
29
29
| true -> " NAME"
30
- | false -> " TAG"
30
+ | false -> (
31
+ match tag with
32
+ | Some tag -> tag
33
+ | None -> " TAG" )
31
34
32
35
let jsVariantPayloadTag ~n = " _" ^ string_of_int n
33
36
Original file line number Diff line number Diff line change @@ -14,6 +14,6 @@ val newModuleItem : name:string -> moduleItem
14
14
val newRecordValue : unboxed :bool -> recordGen -> recordValue
15
15
val recordGen : unit -> recordGen
16
16
val recordValueToString : recordValue -> string
17
- val jsVariantTag : polymorphic :bool -> string
17
+ val jsVariantTag : polymorphic :bool -> tag : string option -> string
18
18
val jsVariantPayloadTag : n :int -> string
19
19
val jsVariantValue : polymorphic :bool -> string
Original file line number Diff line number Diff line change @@ -212,7 +212,7 @@ and translateCoreType_ ~config ~typeVarsGen
212
212
let inherits = inheritsTranslations |> List. map (fun {type_} -> type_) in
213
213
let type_ =
214
214
createVariant ~no Payloads ~payloads ~inherits ~polymorphic: true
215
- ~unboxed: false
215
+ ~tag: None ~ unboxed:false
216
216
in
217
217
let dependencies =
218
218
(inheritsTranslations
Original file line number Diff line number Diff line change @@ -66,6 +66,7 @@ let traslateDeclarationKind ~config ~loc ~outputFileRelative ~resolver
66
66
let unboxedAnnotation =
67
67
typeAttributes |> Annotation. hasAttribute Annotation. tagIsUnboxed
68
68
in
69
+ let tagAnnotation = typeAttributes |> Annotation. getTag in
69
70
let returnTypeDeclaration (typeDeclaration : CodeItem.typeDeclaration ) =
70
71
match opaque = Some true with
71
72
| true -> [{typeDeclaration with importTypes = [] }]
@@ -203,7 +204,7 @@ let traslateDeclarationKind ~config ~loc ~outputFileRelative ~resolver
203
204
else variant.payloads
204
205
in
205
206
createVariant ~inherits: variant.inherits ~no Payloads ~payloads
206
- ~polymorphic: true ~unboxed: false
207
+ ~polymorphic: true ~tag: None ~ unboxed:false
207
208
| _ -> translation.type_
208
209
in
209
210
{translation with type_} |> handleGeneralDeclaration
@@ -295,7 +296,7 @@ let traslateDeclarationKind ~config ~loc ~outputFileRelative ~resolver
295
296
in
296
297
let variantTyp =
297
298
createVariant ~inherits: [] ~no Payloads ~payloads ~polymorphic: false
298
- ~unboxed: unboxedAnnotation
299
+ ~tag: tagAnnotation ~ unboxed: unboxedAnnotation
299
300
in
300
301
let resolvedTypeName = typeName |> TypeEnv. addModulePath ~type Env in
301
302
let exportFromTypeDeclaration =
Original file line number Diff line number Diff line change @@ -147,7 +147,7 @@ let translateConstr ~config ~paramsTranslation ~(path : Path.t) ~typeEnv =
147
147
case 0 " Ok" paramTranslation1.type_;
148
148
case 1 " Error" paramTranslation2.type_;
149
149
]
150
- ~polymorphic: false ~unboxed: false
150
+ ~polymorphic: false ~tag: None ~ unboxed:false
151
151
in
152
152
{
153
153
dependencies =
@@ -386,7 +386,7 @@ and translateTypeExprFromTypes_ ~config ~typeVarsGen ~typeEnv
386
386
in
387
387
let type_ =
388
388
createVariant ~inherits: [] ~no Payloads ~payloads: [] ~polymorphic: true
389
- ~unboxed: false
389
+ ~tag: None ~ unboxed:false
390
390
in
391
391
{dependencies = [] ; type_}
392
392
| {noPayloads = [] ; payloads = [(_label, t)]; unknowns = [] } ->
@@ -415,7 +415,7 @@ and translateTypeExprFromTypes_ ~config ~typeVarsGen ~typeEnv
415
415
in
416
416
let type_ =
417
417
createVariant ~inherits: [] ~no Payloads ~payloads ~polymorphic: true
418
- ~unboxed: false
418
+ ~tag: None ~ unboxed:false
419
419
in
420
420
let dependencies =
421
421
payloadTranslations
Original file line number Diff line number Diff line change
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 number Diff line number Diff line change
1
+ @gentype
2
+ @tag ("action" )
3
+ type action = | A (string ) | B (string )
4
+
5
+ let a = A ("a" )
6
+ let b = B ("b" )
You can’t perform that action at this time.
0 commit comments