File tree Expand file tree Collapse file tree 10 files changed +32
-14
lines changed
gentype_tests/typescript-react-example/src Expand file tree Collapse file tree 10 files changed +32
-14
lines changed Original file line number Diff line number Diff line change 12
12
13
13
# 11.0.0-alpha.3 (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.2
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"
@@ -125,6 +127,11 @@ let getAsString attributes =
125
127
| Some (_ , StringPayload s ) -> Some s
126
128
| _ -> None
127
129
130
+ let getTag attributes =
131
+ match attributes |> getAttributePayload tagIsTag with
132
+ | Some (_ , StringPayload s ) -> Some s
133
+ | _ -> None
134
+
128
135
let getAsInt attributes =
129
136
match attributes |> getAttributePayload tagIsAs with
130
137
| 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 @@ -97,6 +97,7 @@ and variant = {
97
97
payloads : payload list ;
98
98
polymorphic : bool ; (* If true, this is a polymorphic variant *)
99
99
unboxed : bool ;
100
+ customTag : string option ;
100
101
}
101
102
102
103
and payload = {case : case ; inlineRecord : bool ; numArgs : int ; t : type_ }
@@ -166,8 +167,9 @@ let rec depToResolvedName (dep : dep) =
166
167
| Dot (p , s ) -> ResolvedName. dot s (p |> depToResolvedName)
167
168
168
169
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}
171
173
172
174
let ident ?(builtin = true ) ?(typeArgs = [] ) name =
173
175
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 @@ -224,7 +224,7 @@ and translateCoreType_ ~config ~typeVarsGen
224
224
let inherits = inheritsTranslations |> List. map (fun {type_} -> type_) in
225
225
let type_ =
226
226
createVariant ~bs StringOrInt:(asString || asInt) ~no Payloads ~payloads
227
- ~inherits ~polymorphic: true ~unboxed: false
227
+ ~inherits ~polymorphic: true ~unboxed: false ~custom Tag: None
228
228
in
229
229
let dependencies =
230
230
(inheritsTranslations
Original file line number Diff line number Diff line change @@ -63,6 +63,8 @@ let traslateDeclarationKind ~config ~loc ~outputFileRelative ~resolver
63
63
let unboxedAnnotation =
64
64
typeAttributes |> Annotation. hasAttribute Annotation. tagIsUnboxed
65
65
in
66
+ let customTag =
67
+ typeAttributes |> Annotation. getTag in
66
68
let returnTypeDeclaration (typeDeclaration : CodeItem.typeDeclaration ) =
67
69
match opaque = Some true with
68
70
| true -> [{typeDeclaration with importTypes = [] }]
@@ -197,7 +199,7 @@ let traslateDeclarationKind ~config ~loc ~outputFileRelative ~resolver
197
199
else variant.payloads
198
200
in
199
201
createVariant ~bs StringOrInt:false ~inherits: variant.inherits
200
- ~no Payloads ~payloads ~polymorphic: true ~unboxed: false
202
+ ~no Payloads ~payloads ~polymorphic: true ~unboxed: false ~custom Tag: None
201
203
| _ -> translation.type_
202
204
in
203
205
{translation with type_} |> handleGeneralDeclaration
@@ -312,7 +314,7 @@ let traslateDeclarationKind ~config ~loc ~outputFileRelative ~resolver
312
314
in
313
315
let variantTyp =
314
316
createVariant ~bs StringOrInt:false ~inherits: [] ~no Payloads ~payloads
315
- ~polymorphic: false ~unboxed: unboxedAnnotation
317
+ ~polymorphic: false ~unboxed: unboxedAnnotation ~custom Tag
316
318
in
317
319
let resolvedTypeName = typeName |> TypeEnv. addModulePath ~type Env in
318
320
let exportFromTypeDeclaration =
Original file line number Diff line number Diff line change @@ -146,7 +146,7 @@ let translateConstr ~config ~paramsTranslation ~(path : Path.t) ~typeEnv =
146
146
case 0 " Ok" paramTranslation1.type_;
147
147
case 1 " Error" paramTranslation2.type_;
148
148
]
149
- ~polymorphic: false ~unboxed: false
149
+ ~polymorphic: false ~unboxed: false ~custom Tag: None
150
150
in
151
151
{
152
152
dependencies =
@@ -408,7 +408,7 @@ and translateTypeExprFromTypes_ ~config ~typeVarsGen ~typeEnv
408
408
in
409
409
let type_ =
410
410
createVariant ~bs StringOrInt:false ~inherits: [] ~no Payloads ~payloads: []
411
- ~polymorphic: true ~unboxed: false
411
+ ~polymorphic: true ~unboxed: false ~custom Tag: None
412
412
in
413
413
{dependencies = [] ; type_}
414
414
| {noPayloads = [] ; payloads = [(_label, t)]; unknowns = [] } ->
@@ -439,7 +439,7 @@ and translateTypeExprFromTypes_ ~config ~typeVarsGen ~typeEnv
439
439
in
440
440
let type_ =
441
441
createVariant ~bs StringOrInt:false ~inherits: [] ~no Payloads ~payloads
442
- ~polymorphic: true ~unboxed: false
442
+ ~polymorphic: true ~unboxed: false ~custom Tag: None
443
443
in
444
444
let dependencies =
445
445
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