Skip to content

Commit 43e1175

Browse files
authored
Propagate comments from record fields to gentype output (#6333)
* propagate comments from record fields to gentype output * changelog
1 parent 062452f commit 43e1175

12 files changed

+131
-17
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
1313
# 11.0.0-beta.5 (Unreleased)
1414

15+
#### :rocket: New Feature
16+
17+
- GenType: Propagate comments from record fields to emitted TypeScript types. https://github.com/rescript-lang/rescript-compiler/pull/6333
18+
1519
# 11.0.0-beta.4
1620

1721
#### :rocket: New Feature

jscomp/gentype/Annotation.ml

+14-4
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,10 @@ let tagIsOneOfTheGenTypeAnnotations s =
3333
let tagIsGenTypeIgnoreInterface s =
3434
s = "genType.ignoreInterface" || s = "gentype.ignoreInterface"
3535

36-
let tagIsOcamlDoc s = s = "ocaml.doc"
36+
let tagIsDoc s =
37+
match s with
38+
| "ocaml.doc" | "res.doc" -> true
39+
| _ -> false
3740
let tagIsInternLocal s = s = "internal.local"
3841

3942
let rec getAttributePayload checkText (attributes : Typedtree.attributes) =
@@ -143,12 +146,19 @@ let getAttributeImportRenaming attributes =
143146
(Some importString, Some renameString)
144147
| _ -> (None, genTypeAsRenaming)
145148

146-
let getDocString attributes =
147-
let docPayload = attributes |> getAttributePayload tagIsOcamlDoc in
149+
let getDocPayload attributes =
150+
let docPayload = attributes |> getAttributePayload tagIsDoc in
148151
match docPayload with
149-
| Some (_, StringPayload docString) -> "/** " ^ docString ^ " */\n"
152+
| Some (_, StringPayload docString) -> Some docString
153+
| _ -> None
154+
155+
let mkDocString maybeDoc =
156+
match maybeDoc with
157+
| Some docString -> "/** " ^ docString ^ " */\n"
150158
| _ -> ""
151159

160+
let getDocString attributes = getDocPayload attributes |> mkDocString
161+
152162
let hasAttribute checkText (attributes : Typedtree.attributes) =
153163
getAttributePayload checkText attributes <> None
154164

jscomp/gentype/EmitType.ml

+14-4
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ let typeReactRef ~type_ =
4747
nameJS = reactRefCurrent;
4848
optional = Mandatory;
4949
type_ = Null type_;
50+
docString = None;
5051
};
5152
] )
5253

@@ -162,12 +163,13 @@ let rec renderType ~(config : Config.t) ?(indent = None) ~typeNameIsInterface
162163
type_ |> renderType ~config ~indent ~typeNameIsInterface ~inFunType)
163164
in
164165
let noPayloadsRendered = noPayloads |> List.map labelJSToString in
165-
let field ~name value =
166+
let field ~name ?docString value =
166167
{
167168
mutable_ = Mutable;
168169
nameJS = name;
169170
optional = Mandatory;
170171
type_ = TypeVar value;
172+
docString;
171173
}
172174
in
173175
let fields fields =
@@ -232,7 +234,7 @@ let rec renderType ~(config : Config.t) ?(indent = None) ~typeNameIsInterface
232234
^ "| "))
233235

234236
and renderField ~config ~indent ~typeNameIsInterface ~inFunType
235-
{mutable_; nameJS = lbl; optional; type_} =
237+
{mutable_; nameJS = lbl; optional; type_; docString} =
236238
let optMarker =
237239
match optional == Optional with
238240
| true -> "?"
@@ -249,8 +251,16 @@ and renderField ~config ~indent ~typeNameIsInterface ~inFunType
249251
| false -> EmitText.quotes lbl
250252
in
251253

252-
Indent.break ~indent ^ mutMarker ^ lbl ^ optMarker ^ ": "
253-
^ (type_ |> renderType ~config ~indent ~typeNameIsInterface ~inFunType)
254+
let defStr =
255+
mutMarker ^ lbl ^ optMarker ^ ": "
256+
^ (type_ |> renderType ~config ~indent ~typeNameIsInterface ~inFunType)
257+
in
258+
match docString with
259+
| None -> Indent.break ~indent ^ defStr
260+
| Some docString ->
261+
(* Always print comments on newline before definition. *)
262+
let indentStr = indent |> Option.value ~default:"" in
263+
"\n" ^ indentStr ^ "/**" ^ docString ^ "*/\n" ^ indentStr ^ defStr
254264

255265
and renderFields ~config ~indent ~inFunType ~typeNameIsInterface fields =
256266
let indent1 = indent |> Indent.more in

jscomp/gentype/ExportModule.ml

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ and exportModuleItemToFields =
3939
nameJS = fieldName;
4040
optional = Mandatory;
4141
type_ = typeForType;
42+
docString = None;
4243
}
4344
in
4445
let fieldForValue = {fieldForType with type_ = typeForValue} in

jscomp/gentype/GenTypeCommon.ml

+1
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ and field = {
7878
nameJS: string;
7979
optional: optional;
8080
type_: type_;
81+
docString: string option;
8182
}
8283

8384
and function_ = {argTypes: argType list; retType: type_; typeVars: string list}

jscomp/gentype/NamedArgs.ml

+14-2
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,25 @@ let rec groupReversed ~revCurGroup ~revResult labeledTypes =
1414
(* Add it to the current group, not result. *)
1515
groupReversed
1616
~revCurGroup:
17-
({mutable_ = Immutable; nameJS = name; optional = Optional; type_}
17+
({
18+
mutable_ = Immutable;
19+
nameJS = name;
20+
optional = Optional;
21+
type_;
22+
docString = None;
23+
}
1824
:: revCurGroup)
1925
~revResult tl
2026
| _, (Label name, type_) :: tl ->
2127
groupReversed
2228
~revCurGroup:
23-
({mutable_ = Immutable; nameJS = name; optional = Mandatory; type_}
29+
({
30+
mutable_ = Immutable;
31+
nameJS = name;
32+
optional = Mandatory;
33+
type_;
34+
docString = None;
35+
}
2436
:: revCurGroup)
2537
~revResult tl
2638
| [], [] -> revResult

jscomp/gentype/TranslateTypeDeclarations.ml

+7-4
Original file line numberDiff line numberDiff line change
@@ -103,23 +103,26 @@ let traslateDeclarationKind ~config ~loc ~outputFileRelative ~resolver
103103
mutability,
104104
ld_type
105105
|> TranslateTypeExprFromTypes.translateTypeExprFromTypes ~config
106-
~typeEnv ))
106+
~typeEnv,
107+
Annotation.getDocPayload ld_attributes ))
107108
in
108109
let dependencies =
109110
fieldTranslations
110-
|> List.map (fun (_, _, {TranslateTypeExprFromTypes.dependencies}) ->
111+
|> List.map (fun (_, _, {TranslateTypeExprFromTypes.dependencies}, _) ->
111112
dependencies)
112113
|> List.concat
113114
in
114115
let fields =
115116
fieldTranslations
116-
|> List.map (fun (name, mutable_, {TranslateTypeExprFromTypes.type_}) ->
117+
|> List.map
118+
(fun (name, mutable_, {TranslateTypeExprFromTypes.type_}, docString)
119+
->
117120
let optional, type1 =
118121
match type_ with
119122
| Option type1 when isOptional name -> (Optional, type1)
120123
| _ -> (Mandatory, type_)
121124
in
122-
{mutable_; nameJS = name; optional; type_ = type1})
125+
{mutable_; nameJS = name; optional; type_ = type1; docString})
123126
in
124127
let type_ =
125128
match fields with

jscomp/gentype/TranslateTypeExprFromTypes.ml

+6-2
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ let translateObjType closedFlag fieldsTranslations =
4545
| _ -> (Mandatory, t)
4646
in
4747
let name = name |> Runtime.mangleObjectField in
48-
{mutable_; nameJS = name; optional; type_})
48+
{mutable_; nameJS = name; optional; type_; docString = None})
4949
in
5050
let type_ = Object (closedFlag, fields) in
5151
{dependencies; type_}
@@ -126,6 +126,7 @@ let translateConstr ~config ~paramsTranslation ~(path : Path.t) ~typeEnv =
126126
nameJS = "contents";
127127
optional = Mandatory;
128128
type_ = paramTranslation.type_;
129+
docString = None;
129130
};
130131
] );
131132
}
@@ -465,7 +466,7 @@ and signatureToModuleRuntimeRepresentation ~config ~typeVarsGen ~typeEnv
465466
|> List.map (fun signatureItem ->
466467
match signatureItem with
467468
| Types.Sig_value (_id, {val_kind = Val_prim _}) -> ([], [])
468-
| Types.Sig_value (id, {val_type = typeExpr}) ->
469+
| Types.Sig_value (id, {val_type = typeExpr; val_attributes}) ->
469470
let {dependencies; type_} =
470471
typeExpr
471472
|> translateTypeExprFromTypes_ ~config ~typeVarsGen ~typeEnv
@@ -476,6 +477,7 @@ and signatureToModuleRuntimeRepresentation ~config ~typeVarsGen ~typeEnv
476477
nameJS = id |> Ident.name;
477478
optional = Mandatory;
478479
type_;
480+
docString = Annotation.getDocPayload val_attributes;
479481
}
480482
in
481483
(dependencies, [field])
@@ -499,6 +501,8 @@ and signatureToModuleRuntimeRepresentation ~config ~typeVarsGen ~typeEnv
499501
nameJS = id |> Ident.name;
500502
optional = Mandatory;
501503
type_;
504+
docString =
505+
Annotation.getDocPayload moduleDeclaration.md_attributes;
502506
}
503507
in
504508
(dependencies, [field])

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/Comments.bs.js

+15
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,30 @@
1+
/* TypeScript file generated from Comments.res by genType. */
2+
/* eslint-disable import/first */
3+
4+
5+
// @ts-ignore: Implicit any on import
6+
import * as CommentsBS__Es6Import from './Comments.bs';
7+
const CommentsBS: any = CommentsBS__Es6Import;
8+
9+
// tslint:disable-next-line:interface-over-type-literal
10+
export type DecideSubject_payload = {
11+
/** A hint to use as a guide when thinking of your poem.*/
12+
readonly hint: string };
13+
14+
// tslint:disable-next-line:max-classes-per-file
15+
export abstract class DecideSubject_input { protected opaque!: any }; /* simulate opaque types */
16+
17+
// tslint:disable-next-line:interface-over-type-literal
18+
export type DecideSubject_output = {
19+
/** The text of the poem.*/
20+
readonly text: string;
21+
/** The prompt used to generate the poem.*/
22+
readonly prompt: string;
23+
/** The system prompt used to generate the poem.*/
24+
readonly systemPrompt: string
25+
};
26+
27+
/** Decide on a subject matter for a poem. */
28+
export const DecideSubject__placeholder: (run:string, times:number) => void = CommentsBS.DecideSubject._placeholder;
29+
30+
export const DecideSubject: { _placeholder: (run:string, times:number) => void } = CommentsBS.DecideSubject
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
@genType /** A module for deciding on a subject matter for a poem.*/
2+
module DecideSubject = {
3+
type payload = {
4+
/** A hint to use as a guide when thinking of your poem.*/
5+
hint: string,
6+
}
7+
/** The input used to generate the prompt and system prompt.*/
8+
type input
9+
/** The output from evaluating the llm prompt*/
10+
type output = {
11+
/** The text of the poem.*/
12+
text: string,
13+
/** The prompt used to generate the poem.*/
14+
prompt: string,
15+
/** The system prompt used to generate the poem.*/
16+
systemPrompt: string,
17+
}
18+
19+
@genType /** Decide on a subject matter for a poem.*/
20+
let _placeholder = (
21+
@ocaml.doc("The runner specification") run: string,
22+
@ocaml.doc("The number of times to cycle through the runner") times: int,
23+
) => (run, times)->ignore
24+
}

0 commit comments

Comments
 (0)