File tree Expand file tree Collapse file tree 10 files changed +31
-14
lines changed
gentype_tests/typescript-react-example/src Expand file tree Collapse file tree 10 files changed +31
-14
lines changed Original file line number Diff line number Diff line change 12
12
13
13
# 11.0.0-alpha.5 (Unreleased)
14
14
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
+
15
18
# 11.0.0-alpha.4
16
19
17
20
#### :rocket : Main New Feature
Original file line number Diff line number Diff line change @@ -22,6 +22,8 @@ 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
+ let tagIsTag s = s = " tag"
26
+
25
27
let tagIsUnboxed s = s = " unboxed" || s = " ocaml.unboxed"
26
28
let tagIsGenTypeImport s = s = " genType.import" || s = " gentype.import"
27
29
let tagIsGenTypeOpaque s = s = " genType.opaque" || s = " gentype.opaque"
@@ -123,6 +125,11 @@ let getAsString attributes =
123
125
| Some (_ , StringPayload s ) -> Some s
124
126
| _ -> None
125
127
128
+ let getTag attributes =
129
+ match attributes |> getAttributePayload tagIsTag with
130
+ | Some (_ , StringPayload s ) -> Some s
131
+ | _ -> None
132
+
126
133
let getAsInt attributes =
127
134
match attributes |> getAttributePayload tagIsAs with
128
135
| Some (_ , IntPayload s ) -> (
Original file line number Diff line number Diff line change @@ -170,7 +170,7 @@ let rec renderType ~(config : Config.t) ?(indent = None) ~typeNameIsInterface
170
170
|> String. concat " , " )
171
171
^ " ]"
172
172
| TypeVar s -> s
173
- | Variant {inherits; noPayloads; payloads; polymorphic; unboxed} ->
173
+ | Variant {inherits; noPayloads; payloads; polymorphic; unboxed; customTag } ->
174
174
let inheritsRendered =
175
175
inherits
176
176
|> List. map (fun type_ ->
@@ -195,7 +195,8 @@ let rec renderType ~(config : Config.t) ?(indent = None) ~typeNameIsInterface
195
195
t |> renderType ~config ~indent ~type NameIsInterface ~in FunType
196
196
in
197
197
let tagField =
198
- case |> labelJSToString |> field ~name: Runtime. jsVariantTag
198
+ case |> labelJSToString
199
+ |> field ~name: (Runtime. jsVariantTag ~custom Tag)
199
200
in
200
201
match (unboxed, type_) with
201
202
| true , type_ -> type_ |> render
Original file line number Diff line number Diff line change @@ -90,6 +90,7 @@ and variant = {
90
90
payloads : payload list ;
91
91
polymorphic : bool ; (* If true, this is a polymorphic variant *)
92
92
unboxed : bool ;
93
+ customTag : string option ;
93
94
}
94
95
95
96
and payload = {case : case ; t : type_ }
@@ -158,8 +159,9 @@ let rec depToResolvedName (dep : dep) =
158
159
| Internal resolvedName -> resolvedName
159
160
| Dot (p , s ) -> ResolvedName. dot s (p |> depToResolvedName)
160
161
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}
163
165
164
166
let ident ?(builtin = true ) ?(typeArgs = [] ) name =
165
167
Ident {builtin; name; typeArgs}
Original file line number Diff line number Diff line change @@ -24,7 +24,10 @@ let rec emitModuleAccessPath ~config moduleAccessPath =
24
24
| Dot (p , moduleItem ) ->
25
25
p |> emitModuleAccessPath ~config |> EmitText. fieldAccess ~label: moduleItem
26
26
27
- let jsVariantTag = " TAG"
27
+ let jsVariantTag ~customTag =
28
+ match customTag with
29
+ | None -> " TAG"
30
+ | Some tag -> tag
28
31
let jsPolymorphicVariantTag = " NAME"
29
32
30
33
let jsVariantPayloadTag ~n = " _" ^ string_of_int n
Original file line number Diff line number Diff line change @@ -15,7 +15,7 @@ val newModuleItem : name:string -> moduleItem
15
15
val newRecordValue : unboxed :bool -> recordGen -> recordValue
16
16
val recordGen : unit -> recordGen
17
17
val recordValueToString : recordValue -> string
18
- val jsVariantTag : string
18
+ val jsVariantTag : customTag : string option -> string
19
19
val jsPolymorphicVariantTag : string
20
20
val jsVariantPayloadTag : n :int -> string
21
21
val jsVariantValue : polymorphic :bool -> string
Original file line number Diff line number Diff line change @@ -213,7 +213,7 @@ and translateCoreType_ ~config ~typeVarsGen
213
213
let inherits = inheritsTranslations |> List. map (fun {type_} -> type_) in
214
214
let type_ =
215
215
createVariant ~no Payloads ~payloads ~inherits ~polymorphic: true
216
- ~unboxed: false
216
+ ~unboxed: false ~custom Tag: None
217
217
in
218
218
let dependencies =
219
219
(inheritsTranslations
Original file line number Diff line number Diff line change @@ -63,6 +63,7 @@ let traslateDeclarationKind ~config ~loc ~outputFileRelative ~resolver
63
63
let unboxedAnnotation =
64
64
typeAttributes |> Annotation. hasAttribute Annotation. tagIsUnboxed
65
65
in
66
+ let customTag = typeAttributes |> Annotation. getTag in
66
67
let returnTypeDeclaration (typeDeclaration : CodeItem.typeDeclaration ) =
67
68
match opaque = Some true with
68
69
| true -> [{typeDeclaration with importTypes = [] }]
@@ -197,7 +198,7 @@ let traslateDeclarationKind ~config ~loc ~outputFileRelative ~resolver
197
198
else variant.payloads
198
199
in
199
200
createVariant ~inherits: variant.inherits ~no Payloads ~payloads
200
- ~polymorphic: true ~unboxed: false
201
+ ~polymorphic: true ~unboxed: false ~custom Tag: None
201
202
| _ -> translation.type_
202
203
in
203
204
{translation with type_} |> handleGeneralDeclaration
@@ -289,7 +290,7 @@ let traslateDeclarationKind ~config ~loc ~outputFileRelative ~resolver
289
290
in
290
291
let variantTyp =
291
292
createVariant ~inherits: [] ~no Payloads ~payloads ~polymorphic: false
292
- ~unboxed: unboxedAnnotation
293
+ ~unboxed: unboxedAnnotation ~custom Tag
293
294
in
294
295
let resolvedTypeName = typeName |> TypeEnv. addModulePath ~type Env in
295
296
let exportFromTypeDeclaration =
Original file line number Diff line number Diff line change @@ -141,7 +141,7 @@ let translateConstr ~config ~paramsTranslation ~(path : Path.t) ~typeEnv =
141
141
case 0 " Ok" paramTranslation1.type_;
142
142
case 1 " Error" paramTranslation2.type_;
143
143
]
144
- ~polymorphic: false ~unboxed: false
144
+ ~polymorphic: false ~unboxed: false ~custom Tag: None
145
145
in
146
146
{
147
147
dependencies =
@@ -382,7 +382,7 @@ and translateTypeExprFromTypes_ ~config ~typeVarsGen ~typeEnv
382
382
in
383
383
let type_ =
384
384
createVariant ~inherits: [] ~no Payloads ~payloads: [] ~polymorphic: true
385
- ~unboxed: false
385
+ ~unboxed: false ~custom Tag: None
386
386
in
387
387
{dependencies = [] ; type_}
388
388
| {noPayloads = [] ; payloads = [(_label, t)]; unknowns = [] } ->
@@ -411,7 +411,7 @@ and translateTypeExprFromTypes_ ~config ~typeVarsGen ~typeEnv
411
411
in
412
412
let type_ =
413
413
createVariant ~inherits: [] ~no Payloads ~payloads ~polymorphic: true
414
- ~unboxed: false
414
+ ~unboxed: false ~custom Tag: None
415
415
in
416
416
let dependencies =
417
417
payloadTranslations
Original file line number Diff line number Diff line change @@ -17,8 +17,8 @@ export type toPayload = { readonly result: string };
17
17
18
18
// tslint:disable-next-line:interface-over-type-literal
19
19
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 } ;
22
22
23
23
// tslint:disable-next-line:interface-over-type-literal
24
24
export type settled = settledResult < string > ;
You can’t perform that action at this time.
0 commit comments