Skip to content

Commit 1293ec1

Browse files
authored
Gentype: propagate more comments (#6334)
* propagate docstrings for types emitted * emit comments in emitted module type definitions * to inline records * cleanup * updated files * simplify docstring abstraction
1 parent 882d67b commit 1293ec1

16 files changed

+112
-75
lines changed

jscomp/gentype/Annotation.ml

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -149,15 +149,10 @@ let getAttributeImportRenaming attributes =
149149
let getDocPayload attributes =
150150
let docPayload = attributes |> getAttributePayload tagIsDoc in
151151
match docPayload with
152-
| Some (_, StringPayload docString) -> Some docString
152+
| Some (_, StringPayload docString) when docString <> "" -> Some docString
153153
| _ -> None
154154

155-
let mkDocString maybeDoc =
156-
match maybeDoc with
157-
| Some docString -> "/** " ^ docString ^ " */\n"
158-
| _ -> ""
159-
160-
let getDocString attributes = getDocPayload attributes |> mkDocString
155+
let docStringFromAttrs attributes = attributes |> getDocPayload
161156

162157
let hasAttribute checkText (attributes : Typedtree.attributes) =
163158
getAttributePayload checkText attributes <> None

jscomp/gentype/CodeItem.ml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ type exportType = {
77
type_: type_;
88
typeVars: string list;
99
resolvedTypeName: ResolvedName.t;
10+
docString: DocString.t;
1011
}
1112

1213
type importValue = {
@@ -17,7 +18,7 @@ type importValue = {
1718
}
1819

1920
type exportValue = {
20-
docString: string;
21+
docString: DocString.t;
2122
moduleAccessPath: Runtime.moduleAccessPath;
2223
originalName: string;
2324
resolvedName: ResolvedName.t;

jscomp/gentype/EmitJs.ml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ let codeItemToString ~config ~typeNameIsInterface (codeItem : CodeItem.t) =
6969
"ImportValue " ^ (importAnnotation.importPath |> ImportPath.dump)
7070

7171
let emitExportType ~emitters ~config ~typeNameIsInterface
72-
{CodeItem.loc; nameAs; opaque; type_; typeVars; resolvedTypeName} =
72+
{CodeItem.loc; nameAs; opaque; type_; typeVars; resolvedTypeName; docString}
73+
=
7374
let freeTypeVars = TypeVars.free type_ in
7475
let isGADT =
7576
freeTypeVars |> List.exists (fun s -> not (List.mem s typeVars))
@@ -93,7 +94,7 @@ let emitExportType ~emitters ~config ~typeNameIsInterface
9394
in
9495
resolvedTypeName |> ResolvedName.toString
9596
|> EmitType.emitExportType ~config ~emitters ~nameAs ~opaque ~type_
96-
~typeNameIsInterface ~typeVars
97+
~typeNameIsInterface ~typeVars ~docString
9798

9899
let typeNameIsInterface ~(exportTypeMap : CodeItem.exportTypeMap)
99100
~(exportTypeMapFromOtherFiles : CodeItem.exportTypeMap) typeName =
@@ -335,7 +336,8 @@ let emitCodeItem ~config ~emitters ~moduleItemsEmitter ~env ~fileName
335336
| _ -> (type_, None)
336337
in
337338

338-
resolvedName |> ExportModule.extendExportModules ~moduleItemsEmitter ~type_;
339+
resolvedName
340+
|> ExportModule.extendExportModules ~docString ~moduleItemsEmitter ~type_;
339341
let emitters =
340342
match hookType with
341343
| Some {propsType; resolvedTypeName; typeVars} ->
@@ -347,6 +349,7 @@ let emitCodeItem ~config ~emitters ~moduleItemsEmitter ~env ~fileName
347349
type_ = propsType;
348350
typeVars;
349351
resolvedTypeName;
352+
docString;
350353
}
351354
: CodeItem.exportType)
352355
in

jscomp/gentype/EmitType.ml

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ let typeReactRef ~type_ =
4747
nameJS = reactRefCurrent;
4848
optional = Mandatory;
4949
type_ = Null type_;
50-
docString = None;
50+
docString = DocString.empty;
5151
};
5252
] )
5353

@@ -163,7 +163,7 @@ let rec renderType ~(config : Config.t) ?(indent = None) ~typeNameIsInterface
163163
type_ |> renderType ~config ~indent ~typeNameIsInterface ~inFunType)
164164
in
165165
let noPayloadsRendered = noPayloads |> List.map labelJSToString in
166-
let field ~name ?docString value =
166+
let field ~name ?(docString = DocString.empty) value =
167167
{
168168
mutable_ = Mutable;
169169
nameJS = name;
@@ -255,12 +255,11 @@ and renderField ~config ~indent ~typeNameIsInterface ~inFunType
255255
mutMarker ^ lbl ^ optMarker ^ ": "
256256
^ (type_ |> renderType ~config ~indent ~typeNameIsInterface ~inFunType)
257257
in
258-
match docString with
259-
| None -> Indent.break ~indent ^ defStr
260-
| Some docString ->
258+
if DocString.hasContent docString then
261259
(* Always print comments on newline before definition. *)
262260
let indentStr = indent |> Option.value ~default:"" in
263-
"\n" ^ indentStr ^ "/**" ^ docString ^ "*/\n" ^ indentStr ^ defStr
261+
"\n" ^ indentStr ^ DocString.render docString ^ indentStr ^ defStr
262+
else Indent.break ~indent ^ defStr
264263

265264
and renderFields ~config ~indent ~inFunType ~typeNameIsInterface fields =
266265
let indent1 = indent |> Indent.more in
@@ -312,12 +311,13 @@ let typeToString ~config ~typeNameIsInterface type_ =
312311
let ofType ~config ~typeNameIsInterface ~type_ s =
313312
s ^ ": " ^ (type_ |> typeToString ~config ~typeNameIsInterface)
314313

315-
let emitExportConst ~early ?(comment = "") ~config ?(docString = "") ~emitters
316-
~name ~type_ ~typeNameIsInterface line =
314+
let emitExportConst ~early ?(comment = "") ~config
315+
?(docString = DocString.empty) ~emitters ~name ~type_ ~typeNameIsInterface
316+
line =
317317
(match comment = "" with
318318
| true -> comment
319319
| false -> "// " ^ comment ^ "\n")
320-
^ docString ^ "export const "
320+
^ DocString.render docString ^ "export const "
321321
^ (name |> ofType ~config ~typeNameIsInterface ~type_)
322322
^ " = " ^ line
323323
|> (match early with
@@ -329,7 +329,8 @@ let emitExportDefault ~emitters name =
329329
"export default " ^ name ^ ";" |> Emitters.export ~emitters
330330

331331
let emitExportType ~(config : Config.t) ~emitters ~nameAs ~opaque ~type_
332-
~typeNameIsInterface ~typeVars resolvedTypeName =
332+
~typeNameIsInterface ~typeVars ~docString resolvedTypeName =
333+
let docString = DocString.render docString in
333334
let typeParamsString = EmitText.genericsString ~typeVars in
334335
let isInterface = resolvedTypeName |> typeNameIsInterface in
335336
let resolvedTypeName =
@@ -357,15 +358,15 @@ let emitExportType ~(config : Config.t) ~emitters ~nameAs ~opaque ~type_
357358
^ (match String.capitalize_ascii resolvedTypeName <> resolvedTypeName with
358359
| true -> "// tslint:disable-next-line:class-name\n"
359360
| false -> "")
360-
^ "export abstract class " ^ resolvedTypeName ^ typeParamsString
361+
^ docString ^ "export abstract class " ^ resolvedTypeName ^ typeParamsString
361362
^ " { protected opaque!: " ^ typeOfOpaqueField
362363
^ " }; /* simulate opaque types */" ^ exportNameAs
363364
|> Emitters.export ~emitters
364365
else
365366
(if isInterface && config.exportInterfaces then
366-
"export interface " ^ resolvedTypeName ^ typeParamsString ^ " "
367+
docString ^ "export interface " ^ resolvedTypeName ^ typeParamsString ^ " "
367368
else
368-
"// tslint:disable-next-line:interface-over-type-literal\n"
369+
"// tslint:disable-next-line:interface-over-type-literal\n" ^ docString
369370
^ "export type " ^ resolvedTypeName ^ typeParamsString ^ " = ")
370371
^ (match type_ with
371372
| _ -> type_ |> typeToString ~config ~typeNameIsInterface)

jscomp/gentype/ExportModule.ml

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,21 @@ open GenTypeCommon
22

33
type exportModuleItem = (string, exportModuleValue) Hashtbl.t
44

5-
and exportModuleValue = S of string * type_ | M of exportModuleItem
5+
and exportModuleValue =
6+
| S of {name: string; type_: type_; docString: DocString.t}
7+
| M of {exportModuleItem: exportModuleItem}
68

79
type exportModuleItems = (string, exportModuleItem) Hashtbl.t
810

9-
type types = {typeForValue: type_; typeForType: type_}
11+
type types = {typeForValue: type_; typeForType: type_; docString: DocString.t}
1012

1113
type fieldInfo = {fieldForValue: field; fieldForType: field}
1214

1315
let rec exportModuleValueToType ~config exportModuleValue =
1416
match exportModuleValue with
15-
| S (s, type_) -> {typeForValue = ident s; typeForType = type_}
16-
| M exportModuleItem ->
17+
| S {name; type_; docString} ->
18+
{typeForValue = ident name; typeForType = type_; docString}
19+
| M {exportModuleItem} ->
1720
let fieldsInfo = exportModuleItem |> exportModuleItemToFields ~config in
1821
let fieldsForValue =
1922
fieldsInfo |> List.map (fun {fieldForValue} -> fieldForValue)
@@ -24,13 +27,14 @@ let rec exportModuleValueToType ~config exportModuleValue =
2427
{
2528
typeForValue = Object (Open, fieldsForValue);
2629
typeForType = Object (Open, fieldsForType);
30+
docString = DocString.empty;
2731
}
2832

2933
and exportModuleItemToFields =
3034
(fun ~config exportModuleItem ->
3135
Hashtbl.fold
3236
(fun fieldName exportModuleValue fields ->
33-
let {typeForValue; typeForType} =
37+
let {typeForValue; typeForType; docString} =
3438
exportModuleValue |> exportModuleValueToType ~config
3539
in
3640
let fieldForType =
@@ -39,36 +43,38 @@ and exportModuleItemToFields =
3943
nameJS = fieldName;
4044
optional = Mandatory;
4145
type_ = typeForType;
42-
docString = None;
46+
docString;
4347
}
4448
in
4549
let fieldForValue = {fieldForType with type_ = typeForValue} in
4650
{fieldForValue; fieldForType} :: fields)
4751
exportModuleItem []
4852
: config:Config.t -> exportModuleItem -> fieldInfo list)
4953

50-
let rec extendExportModuleItem x ~(exportModuleItem : exportModuleItem) ~type_
51-
~valueName =
54+
let rec extendExportModuleItem ~docString x
55+
~(exportModuleItem : exportModuleItem) ~type_ ~valueName =
5256
match x with
5357
| [] -> ()
5458
| [fieldName] ->
55-
Hashtbl.replace exportModuleItem fieldName (S (valueName, type_))
59+
Hashtbl.replace exportModuleItem fieldName
60+
(S {name = valueName; type_; docString})
5661
| fieldName :: rest ->
5762
let innerExportModuleItem =
5863
match Hashtbl.find exportModuleItem fieldName with
59-
| M innerExportModuleItem -> innerExportModuleItem
64+
| M {exportModuleItem = innerExportModuleItem} -> innerExportModuleItem
6065
| S _ -> assert false
6166
| exception Not_found ->
6267
let innerExportModuleItem = Hashtbl.create 1 in
63-
Hashtbl.replace exportModuleItem fieldName (M innerExportModuleItem);
68+
Hashtbl.replace exportModuleItem fieldName
69+
(M {exportModuleItem = innerExportModuleItem});
6470
innerExportModuleItem
6571
in
6672
rest
67-
|> extendExportModuleItem ~exportModuleItem:innerExportModuleItem ~valueName
68-
~type_
73+
|> extendExportModuleItem ~docString ~exportModuleItem:innerExportModuleItem
74+
~valueName ~type_
6975

70-
let extendExportModuleItems x ~(exportModuleItems : exportModuleItems) ~type_
71-
~valueName =
76+
let extendExportModuleItems x ~docString
77+
~(exportModuleItems : exportModuleItems) ~type_ ~valueName =
7278
match x with
7379
| [] -> assert false
7480
| [_valueName] -> ()
@@ -81,7 +87,8 @@ let extendExportModuleItems x ~(exportModuleItems : exportModuleItems) ~type_
8187
Hashtbl.replace exportModuleItems moduleName exportModuleItem;
8288
exportModuleItem
8389
in
84-
rest |> extendExportModuleItem ~exportModuleItem ~type_ ~valueName
90+
rest
91+
|> extendExportModuleItem ~docString ~exportModuleItem ~type_ ~valueName
8592

8693
let createModuleItemsEmitter =
8794
(fun () -> Hashtbl.create 1 : unit -> exportModuleItems)
@@ -95,22 +102,23 @@ let emitAllModuleItems ~config ~emitters ~fileName
95102
emitters
96103
|> rev_fold
97104
(fun moduleName exportModuleItem emitters ->
98-
let {typeForType} =
99-
M exportModuleItem |> exportModuleValueToType ~config
105+
let {typeForType; docString} =
106+
M {exportModuleItem} |> exportModuleValueToType ~config
100107
in
101108
if !Debug.codeItems then Log_.item "EmitModule %s @." moduleName;
102109
let emittedModuleItem =
103110
ModuleName.forInnerModule ~fileName ~innerModuleName:moduleName
104111
|> ModuleName.toString
105112
in
106113
emittedModuleItem
107-
|> EmitType.emitExportConst ~early:false ~config ~emitters
114+
|> EmitType.emitExportConst ~docString ~early:false ~config ~emitters
108115
~name:moduleName ~type_:typeForType ~typeNameIsInterface:(fun _ ->
109116
false))
110117
exportModuleItems
111118

112-
let extendExportModules ~(moduleItemsEmitter : exportModuleItems) ~type_
113-
resolvedName =
119+
let extendExportModules ~(moduleItemsEmitter : exportModuleItems) ~docString
120+
~type_ resolvedName =
114121
resolvedName |> ResolvedName.toList
115122
|> extendExportModuleItems ~exportModuleItems:moduleItemsEmitter ~type_
123+
~docString
116124
~valueName:(resolvedName |> ResolvedName.toString)

jscomp/gentype/GenTypeCommon.ml

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,16 @@ module StringMap = Map.Make (String)
22
module StringSet = Set.Make (String)
33
module Config = GenTypeConfig
44

5+
module DocString = struct
6+
type t = string option
7+
let render t =
8+
match t with
9+
| None | Some "" -> ""
10+
| Some docString -> "/** " ^ String.trim docString ^ " */\n"
11+
let empty = None
12+
let hasContent docString = Option.is_some docString
13+
end
14+
515
let logNotImplemented x =
616
if !Debug.notImplemented then Log_.item "Not Implemented: %s\n" x
717

@@ -78,7 +88,7 @@ and field = {
7888
nameJS: string;
7989
optional: optional;
8090
type_: type_;
81-
docString: string option;
91+
docString: DocString.t;
8292
}
8393

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

jscomp/gentype/NamedArgs.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ let rec groupReversed ~revCurGroup ~revResult labeledTypes =
1919
nameJS = name;
2020
optional = Optional;
2121
type_;
22-
docString = None;
22+
docString = DocString.empty;
2323
}
2424
:: revCurGroup)
2525
~revResult tl
@@ -31,7 +31,7 @@ let rec groupReversed ~revCurGroup ~revResult labeledTypes =
3131
nameJS = name;
3232
optional = Mandatory;
3333
type_;
34-
docString = None;
34+
docString = DocString.empty;
3535
}
3636
:: revCurGroup)
3737
~revResult tl

jscomp/gentype/TranslateSignature.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ let translateSignatureValue ~config ~outputFileRelative ~resolver ~typeEnv
1515
| id, GenType ->
1616
id |> Ident.name
1717
|> Translation.translateValue ~attributes:val_attributes ~config
18-
~docString:(Annotation.getDocString val_attributes)
18+
~docString:(Annotation.docStringFromAttrs val_attributes)
1919
~outputFileRelative ~resolver ~typeEnv ~typeExpr
2020
~addAnnotationsToFunction
2121
| _ -> Translation.empty

jscomp/gentype/TranslateSignatureFromTypes.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ and translateSignatureItemFromTypes ~config ~outputFileRelative ~resolver
8282
then
8383
name
8484
|> Translation.translateValue ~attributes:val_attributes ~config
85-
~docString:(Annotation.getDocString val_attributes)
85+
~docString:(Annotation.docStringFromAttrs val_attributes)
8686
~outputFileRelative ~resolver ~typeEnv ~typeExpr:val_type
8787
~addAnnotationsToFunction:(fun t -> t)
8888
else Translation.empty

jscomp/gentype/TranslateStructure.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ let translateValueBinding ~config ~outputFileRelative ~resolver ~typeEnv
101101
then
102102
id |> Ident.name
103103
|> Translation.translateValue ~attributes:vb_attributes ~config
104-
~docString:(Annotation.getDocString vb_attributes)
104+
~docString:(Annotation.docStringFromAttrs vb_attributes)
105105
~outputFileRelative ~resolver ~typeEnv ~typeExpr:vb_pat.pat_type
106106
~addAnnotationsToFunction:
107107
(addAnnotationsToFunctionType ~config vb_expr)

0 commit comments

Comments
 (0)