Skip to content

Commit 6c0a164

Browse files
committed
Emit comments except for recursive modules
1 parent 05c11a1 commit 6c0a164

File tree

3 files changed

+58
-18
lines changed

3 files changed

+58
-18
lines changed

lib/Syntax.fs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,21 @@ and [<CustomEquality; CustomComparison>] Comment =
9191
| See of name:string option * text:string list
9292
| ESVersion of Ts.ScriptTarget
9393
| Other of tag:string * text:string list * orig:Ts.JSDocTag
94+
member x.ToJsDoc() =
95+
let concat (lines: string list) = String.concat "\n" lines
96+
match x with
97+
| Description lines -> "@description " + concat lines
98+
| Summary lines -> "@summary " + concat lines
99+
| Param (name, lines) -> sprintf "@param %s " name + concat lines
100+
| Return lines -> "@returns " + concat lines
101+
| Deprecated lines -> "@deprecated " + concat lines
102+
| Example lines -> "@example" + "\n" + concat lines
103+
| See (Some name, []) -> sprintf "@see %s" name
104+
| See (Some name, lines) -> sprintf "@see {@link %s} " name + concat lines
105+
| See (None, lines) -> "@see " + concat lines
106+
| ESVersion target -> sprintf "@since %s" (Enum.pp target)
107+
| Other (_, _, orig) -> orig.getText()
108+
94109
override x.Equals(yo) =
95110
match yo with
96111
| :? Comment as y -> true

src/Targets/ReScript/ReScriptHelper.fs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ let comment text =
2020
between "/*" "*/" inner
2121
let commentStr text = tprintf "/* %s */" text
2222

23+
let docComment text =
24+
if text = empty then empty
25+
else
26+
let inner =
27+
if isMultiLine text then newline + indent text + newline
28+
else between " " " " text
29+
between "/**" "*/" inner
30+
2331
module Attr =
2432
let as_ value = between "@as(" ")" value
2533

@@ -438,7 +446,11 @@ let private moduleSigImplLines (prefix: string) (isRec: bool) (m: TextModule) =
438446
prefix
439447
(if isRec then "rec " else "")
440448
m.name
441-
[ yield! m.comments
449+
[
450+
// FIXME: https://github.com/rescript-lang/rescript-compiler/issues/6598
451+
if prefix <> "and" then
452+
yield! m.comments
453+
442454
yield! moduleSigImplBody head oneliner m ]
443455

444456
let private moduleSigImpl (prefix: string) (isRec: bool) (m: TextModule) =
@@ -562,6 +574,7 @@ module Binding =
562574
Binding.Ext {| name = name; ty = ty; target = ""; attrs = [Attr.External.obj]; comments = []|}
563575

564576
let emitForImplementation (b: Binding) = [
577+
yield! b.comments
565578
match b with
566579
| Binding.Let x -> yield Statement.let_ x.attrs x.name x.ty x.body
567580
| Binding.Ext x -> yield Statement.external x.attrs x.name x.ty x.target

src/Targets/ReScript/Writer.fs

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -630,9 +630,19 @@ module StructuredText =
630630
let removeLabels (xs: Choice<FieldLike, Type> list) =
631631
xs |> List.map (function Choice2Of2 t -> Choice2Of2 t | Choice1Of2 fl -> Choice2Of2 fl.value)
632632

633-
let emitComments (comments: Comment list) : text list =
634-
// TODO
635-
[]
633+
let emitComments (floating: bool) (comments: Comment list) : text list =
634+
if List.isEmpty comments then []
635+
else
636+
let escape =
637+
String.replace "/*" "/ *"
638+
>> String.replace "*/" "* /"
639+
let emit (c: Comment) =
640+
match c with
641+
| Description lines
642+
| Summary lines -> lines |> List.map escape |> strLines
643+
| c -> c.ToJsDoc() |> escape |> str
644+
let body = comments |> List.map emit |> concat newline
645+
if floating then [comment body] else [docComment body]
636646

637647
let inline binding (f: (string -> string) -> CurrentScope -> Binding) : StructuredTextItem list =
638648
[Binding (fun renamer scope -> f (renamer.Rename "value") scope)]
@@ -688,7 +698,7 @@ let extValue flags overrideFunc ctx (t: Type) =
688698
let rec emitMembers flags overrideFunc ctx (selfTy: Type) (isExportDefaultClass: bool) (ma: MemberAttribute) m =
689699
let emitType_ = emitTypeImpl flags overrideFunc
690700

691-
let comments = emitComments ma.comments
701+
let comments = emitComments false ma.comments
692702

693703
let inline extFunc ft = extFunc flags overrideFunc ctx ft
694704
let inline extValue t = extValue flags overrideFunc ctx t
@@ -845,11 +855,13 @@ let rec emitMembers flags overrideFunc ctx (selfTy: Type) (isExportDefaultClass:
845855
emitMembers flags overrideFunc ctx selfTy isExportDefaultClass ma (Indexer (ft, WriteOnly))
846856
]
847857
| SymbolIndexer (symbol, ft, _) ->
858+
let comments = emitComments true ma.comments
848859
let c =
849860
let ft = func ft
850861
tprintf "external [Symbol.%s]: " symbol + ft + tprintf " = \"[Symbol.%s]\"" symbol
851862
binding (fun _ _ -> Binding.unknown comments (Some c))
852863
| UnknownMember msgo ->
864+
let comments = emitComments true ma.comments
853865
binding (fun _ _ -> Binding.unknown comments (msgo |> Option.map str))
854866

855867
let emitTypeAliasesImpl
@@ -1053,7 +1065,7 @@ let rec emitClass flags overrideFunc (ctx: Context) (current: StructuredText) (c
10531065
| ClassKind.NormalClass _ -> forceScope |> Option.defaultValue Scope.Default
10541066
| _ -> Scope.Ignore
10551067

1056-
let comments = c.comments |> emitComments
1068+
let comments = c.comments |> emitComments false
10571069

10581070
let tagsDefinition =
10591071
if useTags && innerCtx.options.inheritWithTags.HasProvide then
@@ -1105,7 +1117,7 @@ let rec emitClass flags overrideFunc (ctx: Context) (current: StructuredText) (c
11051117

11061118
emitTypeAliasesImpl "t" flags overrideFunc innerCtx c.loc c.typeParams selfTyText.ty (fun x ->
11071119
if not x.isOverload then
1108-
[TypeDefText.Create(x.name, x.tyargs, x.target, isRec=selfTyText.isRec, comments=emitComments c.comments)]
1120+
[TypeDefText.Create(x.name, x.tyargs, x.target, isRec=selfTyText.isRec, comments=emitComments false c.comments)]
11091121
else
11101122
[TypeAliasText (Statement.typeAlias false x.name (x.tyargs |> List.map snd) x.target)]
11111123
)
@@ -1246,9 +1258,9 @@ let emitEnum (ctx: Context) (current: StructuredText) (e: Enum) =
12461258
for key, value in distinctCases do
12471259
yield emitConstructor key [Attr.as_ (Term.literal value)] [] |> indent
12481260
]
1249-
let item = TypeDefText.Create("t", [], Some casesText, shouldAssert=true, comments=emitComments e.comments)
1261+
let item = TypeDefText.Create("t", [], Some casesText, shouldAssert=true, comments=emitComments false e.comments)
12501262
let items = item :: List.map childNode e.cases
1251-
let comments = e.comments |> emitComments
1263+
let comments = e.comments |> emitComments false
12521264
{| StructuredTextNode.empty with items = items; comments = comments |}
12531265

12541266
let exports = getExportFromStatement ctx e.name Kind.OfEnum "enum" (Enum e)
@@ -1260,7 +1272,7 @@ let emitEnum (ctx: Context) (current: StructuredText) (e: Enum) =
12601272
let emitTypeAlias flags overrideFunc (ctx: Context) (current: StructuredText) (ta: TypeAlias) : StructuredText =
12611273
let emitType = emitTypeImpl flags overrideFunc
12621274

1263-
let comments = (ta :> ICommented<_>).getComments() |> emitComments
1275+
let comments = (ta :> ICommented<_>).getComments() |> emitComments false
12641276
let knownTypes = Statement.getKnownTypes ctx [TypeAlias ta]
12651277

12661278
let items =
@@ -1272,7 +1284,7 @@ let emitTypeAlias flags overrideFunc (ctx: Context) (current: StructuredText) (t
12721284
[TypeDefText.Create (
12731285
x.name, x.tyargs, x.target,
12741286
isRec=isRec, attrs=attrs, shouldAssert=shouldAssert,
1275-
comments=emitComments ta.comments
1287+
comments=emitComments false ta.comments
12761288
)]
12771289
else
12781290
[TypeAliasText (Statement.typeAlias false x.name (x.tyargs |> List.map snd) x.target)]
@@ -1380,7 +1392,7 @@ let rec emitFunction flags overrideFunc ctx (f: Function) =
13801392
let inline extFunc ft = extFunc flags overrideFunc ctx ft
13811393
let ty, attr = extFunc f.typ
13821394
let attr = attr |> impossibleNone (fun () -> "emitFunction")
1383-
let comments = emitComments f.comments
1395+
let comments = emitComments false f.comments
13841396
binding (fun rename s -> createExternalForValue ctx rename s (Attr.External.val_ :: attr) comments f.name ty)
13851397

13861398
and emitVariable flags overrideFunc ctx (v: Variable) =
@@ -1393,7 +1405,7 @@ and emitVariable flags overrideFunc ctx (v: Variable) =
13931405
let emitType = emitTypeImpl flags
13941406
let emitType_ = emitType overrideFunc
13951407
let ty, attr = emitType_ ctx v.typ, [Attr.External.val_]
1396-
let comments = emitComments v.comments
1408+
let comments = emitComments false v.comments
13971409
binding (fun rename s -> createExternalForValue ctx rename s attr comments v.name ty)
13981410

13991411
let emitImport (ctx: Context) (i: Import) : StructuredTextItem list =
@@ -1452,7 +1464,7 @@ let emitImport (ctx: Context) (i: Import) : StructuredTextItem list =
14521464
|> Option.defaultValue []
14531465
| NamespaceImport _ | ES6DefaultImport _ | ES6Import _ -> []
14541466

1455-
[ yield! emitComments i.comments |> List.map ImportText
1467+
[ yield! emitComments true i.comments |> List.map ImportText
14561468
yield commentStr i.origText |> ImportText
14571469
for c in i.clauses do
14581470
yield! emitImportClause c]
@@ -1481,7 +1493,7 @@ let createStructuredText (rootCtx: Context) (stmts: Statement list) : Structured
14811493
comments = memberAttr.comments; loc = memberAttr.loc }
14821494
emitFunction flags overrideFunc ctx f
14831495
[ for ma, m in moduleIntf.members do
1484-
let comments = emitComments ma.comments
1496+
let comments = emitComments false ma.comments
14851497
match m with
14861498
| Field (fl, mt) ->
14871499
yield! emitAsVariable fl.name fl.value (mt = ReadOnly) ma
@@ -1527,7 +1539,7 @@ let createStructuredText (rootCtx: Context) (stmts: Statement list) : Structured
15271539
| SymbolIndexer _ | UnknownMember None -> () ]
15281540

15291541
let rec folder ctx (current: StructuredText) (s: Statement) : StructuredText =
1530-
let comments = (s :> ICommented<_>).getComments() |> emitComments
1542+
let comments = (s :> ICommented<_>).getComments() |> emitComments false
15311543

15321544
let knownTypes () = Statement.getKnownTypes ctx [s]
15331545
let addExport name kind kindString current =
@@ -1667,7 +1679,7 @@ let createStructuredText (rootCtx: Context) (stmts: Statement list) : Structured
16671679
| Some s -> commentStr s | None -> commentStr "unknown statement"
16681680
current |> set {| StructuredTextNode.empty with items = [Comment cmt] |}
16691681
| FloatingComment c ->
1670-
let cmt = c.comments |> emitComments |> List.map Comment
1682+
let cmt = c.comments |> emitComments true |> List.map Comment
16711683
current |> set {| StructuredTextNode.empty with items = Comment empty :: cmt |}
16721684

16731685
stmts |> List.fold (folder rootCtx) Trie.empty
@@ -1751,7 +1763,7 @@ let rec emitModule (dt: DependencyTrie<string>) flags (ctx: Context) st =
17511763
yield Statement.open_ moduleName
17521764
yield str "type nonrec t = t"
17531765
]
1754-
let m content = {| name = moduleName; origName = e.name; content = content; comments = emitComments e.comments |}
1766+
let m content = {| name = moduleName; origName = e.name; content = content; comments = emitComments false e.comments |}
17551767
{| types = types
17561768
intf = Statement.moduleSig (m intf)
17571769
impl = Statement.moduleVal (m (if isLinear then intf else impl))

0 commit comments

Comments
 (0)