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 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
Original file line number Diff line number Diff line change @@ -22,6 +22,8 @@ let tagIsGenTypeAs s = s = "genType.as" || s = "gentype.as"
2222let tagIsAs s = s = " bs.as" || s = " as"
2323let tagIsInt s = s = " bs.int" || s = " int"
2424let tagIsString s = s = " bs.string" || s = " string"
25+ let tagIsTag s = s = " tag"
26+
2527let tagIsUnboxed s = s = " unboxed" || s = " ocaml.unboxed"
2628let tagIsGenTypeImport s = s = " genType.import" || s = " gentype.import"
2729let 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+
126133let getAsInt attributes =
127134 match attributes |> getAttributePayload tagIsAs with
128135 | 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
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 ~type NameIsInterface ~in FunType
196196 in
197197 let tagField =
198- case |> labelJSToString |> field ~name: Runtime. jsVariantTag
198+ case |> labelJSToString
199+ |> field ~name: (Runtime. jsVariantTag ~custom Tag)
199200 in
200201 match (unboxed, type_) with
201202 | true , type_ -> type_ |> render
Original file line number Diff line number Diff 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
9596and 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
164166let ident ?(builtin = true ) ?(typeArgs = [] ) name =
165167 Ident {builtin; name; typeArgs}
Original file line number Diff line number Diff 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
2831let jsPolymorphicVariantTag = " NAME"
2932
3033let 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
1515val newRecordValue : unboxed :bool -> recordGen -> recordValue
1616val recordGen : unit -> recordGen
1717val recordValueToString : recordValue -> string
18- val jsVariantTag : string
18+ val jsVariantTag : customTag : string option -> string
1919val jsPolymorphicVariantTag : string
2020val jsVariantPayloadTag : n :int -> string
2121val jsVariantValue : polymorphic :bool -> string
Original file line number Diff line number Diff 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 ~no Payloads ~payloads ~inherits ~polymorphic: true
216- ~unboxed: false
216+ ~unboxed: false ~custom Tag: None
217217 in
218218 let dependencies =
219219 (inheritsTranslations
Original file line number Diff line number Diff 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 ~no Payloads ~payloads
200- ~polymorphic: true ~unboxed: false
201+ ~polymorphic: true ~unboxed: false ~custom Tag: 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: [] ~no Payloads ~payloads ~polymorphic: false
292- ~unboxed: unboxedAnnotation
293+ ~unboxed: unboxedAnnotation ~custom Tag
293294 in
294295 let resolvedTypeName = typeName |> TypeEnv. addModulePath ~type Env in
295296 let exportFromTypeDeclaration =
Original file line number Diff line number Diff 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 ~custom Tag: None
145145 in
146146 {
147147 dependencies =
@@ -382,7 +382,7 @@ and translateTypeExprFromTypes_ ~config ~typeVarsGen ~typeEnv
382382 in
383383 let type_ =
384384 createVariant ~inherits: [] ~no Payloads ~payloads: [] ~polymorphic: true
385- ~unboxed: false
385+ ~unboxed: false ~custom Tag: 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: [] ~no Payloads ~payloads ~polymorphic: true
414- ~unboxed: false
414+ ~unboxed: false ~custom Tag: None
415415 in
416416 let dependencies =
417417 payloadTranslations
Original file line number Diff line number Diff line change @@ -17,8 +17,8 @@ export type toPayload = { readonly result: string };
1717
1818// tslint:disable-next-line:interface-over-type-literal
1919export 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
2424export type settled = settledResult < string > ;
You can’t perform that action at this time.
0 commit comments