Skip to content

Commit 364c5bc

Browse files
authored
[JS/TS] Generate comments for members in class decorated with [<AttachMembers>] (#4403)
1 parent 3b478ef commit 364c5bc

8 files changed

Lines changed: 137 additions & 10 deletions

File tree

src/Fable.Cli/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1616
* [Beam] Fix "no effect" warning for pure BIF calls (`self/0`, `node/0`) in non-final block positions (by @dbrattli)
1717
* [Beam] Fix `reraise()` generating unbound `MatchValue` variable — use raw Erlang reason variable for re-throw (by @dbrattli)
1818
* [Beam] Fix `Erlang.receive<'T>()` resolving to timeout overload due to F# unit argument (by @dbrattli)
19+
* [JS/TS] Generate comments for members in class decorated with `[<AttachMembers>]` (by @MangelMaxime)
1920
* [Beam] Fix `[<ImportAll>]` generating invalid `module:*()` Erlang code when binding is used as a value (by @dbrattli)
2021
* [Beam] Fix string slicing and `Substring` with compound expressions producing wrong `binary:part` length due to missing parentheses in emitted Erlang (by @dbrattli)
2122
* [Beam] Fix non-ASCII characters in string literals being truncated to single bytes — emit `<<"..."/utf8>>` instead of `<<"...">>` (by @dbrattli)

src/Fable.Compiler/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1616
* [Beam] Fix "no effect" warning for pure BIF calls (`self/0`, `node/0`) in non-final block positions (by @dbrattli)
1717
* [Beam] Fix `reraise()` generating unbound `MatchValue` variable — use raw Erlang reason variable for re-throw (by @dbrattli)
1818
* [Beam] Fix `Erlang.receive<'T>()` resolving to timeout overload due to F# unit argument (by @dbrattli)
19+
* [JS/TS] Generate comments for members in class decorated with `[<AttachMembers>]` (by @MangelMaxime)
1920
* [Beam] Fix `[<ImportAll>]` generating invalid `module:*()` Erlang code when binding is used as a value (by @dbrattli)
2021
* [Beam] Fix string slicing and `Substring` with compound expressions producing wrong `binary:part` length due to missing parentheses in emitted Erlang (by @dbrattli)
2122
* [Beam] Fix non-ASCII characters in string literals being truncated to single bytes — emit `<<"..."/utf8>>` instead of `<<"...">>` (by @dbrattli)

src/Fable.Transforms/Fable2Babel.fs

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3379,7 +3379,8 @@ but thanks to the optimisation done below we get
33793379
?isAbstract = isAbstract,
33803380
?superClass = superClass,
33813381
typeParameters = typeParameters,
3382-
implements = implements
3382+
implements = implements,
3383+
?doc = info.JsDoc
33833384
)
33843385
| FunctionExpression(_, parameters, body, returnType, typeParameters, _) ->
33853386
Declaration.functionDeclaration (
@@ -3427,6 +3428,7 @@ but thanks to the optimisation done below we get
34273428
ctx
34283429
(ent: Fable.Entity)
34293430
entName
3431+
(doc: string option)
34303432
(consArgs: Parameter[])
34313433
(consArgsModifiers: AccessModifier[])
34323434
(consBody: BlockStatement)
@@ -3511,15 +3513,25 @@ but thanks to the optimisation done below we get
35113513
?implements = implements
35123514
)
35133515

3514-
ModuleDecl(entName, isPublic = ent.IsPublic)
3516+
ModuleDecl(entName, isPublic = ent.IsPublic, ?doc = doc)
35153517
|> declareModuleMember com ctx classExpr
35163518

3517-
let declareClass (com: IBabelCompiler) ctx ent entName consArgs consBody superClass classMembers =
3519+
let declareClass
3520+
(com: IBabelCompiler)
3521+
ctx
3522+
(ent: Fable.Entity)
3523+
entName
3524+
doc
3525+
consArgs
3526+
consBody
3527+
superClass
3528+
classMembers
3529+
=
35183530
if com.IsTypeScript then
35193531
FSharp2Fable.Util.getEntityGenArgs ent |> makeTypeParamDecl com ctx |> Some
35203532
else
35213533
None
3522-
|> declareClassWithParams com ctx ent entName consArgs [||] consBody superClass classMembers
3534+
|> declareClassWithParams com ctx ent entName doc consArgs [||] consBody superClass classMembers
35233535

35243536
let declareTypeReflection (com: IBabelCompiler) ctx (ent: Fable.Entity) entName : ModuleDeclaration =
35253537
let ta =
@@ -3549,14 +3561,15 @@ but thanks to the optimisation done below we get
35493561
ctx
35503562
(ent: Fable.Entity)
35513563
entName
3564+
doc
35523565
(consArgs: Parameter[])
35533566
(consBody: BlockStatement)
35543567
baseExpr
35553568
classMembers
35563569
: ModuleDeclaration list
35573570
=
35583571
let typeDeclaration =
3559-
declareClass com ctx ent entName consArgs consBody baseExpr classMembers
3572+
declareClass com ctx ent entName doc consArgs consBody baseExpr classMembers
35603573

35613574
if com.Options.NoReflection then
35623575
[ typeDeclaration ]
@@ -3710,7 +3723,14 @@ but thanks to the optimisation done below we get
37103723
yield makeMethod "Symbol.iterator" [||] (enumerableThisToIterator com ctx) returnType None
37113724
|]
37123725

3713-
let transformUnion (com: IBabelCompiler) ctx (ent: Fable.Entity) (entName: string) classMembers =
3726+
let transformUnion
3727+
(com: IBabelCompiler)
3728+
ctx
3729+
(ent: Fable.Entity)
3730+
(entName: string)
3731+
(doc: string option)
3732+
classMembers
3733+
=
37143734
let isPublic = ent.IsPublic
37153735
let tagArgName = "Tag"
37163736
let tagArgTa = makeAliasTypeAnnotation com ctx tagArgName
@@ -3798,6 +3818,7 @@ but thanks to the optimisation done below we get
37983818
ctx
37993819
ent
38003820
entName
3821+
doc
38013822
args
38023823
consBody
38033824
baseExpr
@@ -3948,6 +3969,7 @@ but thanks to the optimisation done below we get
39483969
ctx
39493970
ent
39503971
union_cons.Name
3972+
doc
39513973
consArgs
39523974
consArgsModifiers
39533975
consBody
@@ -3976,7 +3998,7 @@ but thanks to the optimisation done below we get
39763998
|]
39773999

39784000
let classMembers = Array.append [| cases |] classMembers
3979-
declareType com ctx ent entName args body baseExpr classMembers
4001+
declareType com ctx ent entName doc args body baseExpr classMembers
39804002

39814003
let transformClassWithCompilerGeneratedConstructor
39824004
(com: IBabelCompiler)
@@ -4021,7 +4043,7 @@ but thanks to the optimisation done below we get
40214043
Parameter.parameter (fi.Name, ?typeAnnotation = makeFieldAnnotationIfTypeScript com ctx fi.Type)
40224044
)
40234045

4024-
declareType com ctx ent decl.Name args body baseExpr classMembers
4046+
declareType com ctx ent decl.Name decl.XmlDoc args body baseExpr classMembers
40254047

40264048
let transformPojoDefinedByConsArgsToInterface
40274049
(com: IBabelCompiler)
@@ -4319,7 +4341,7 @@ but thanks to the optimisation done below we get
43194341
|> Option.defaultValue (None, consBody)
43204342

43214343
[
4322-
yield! declareType com ctx ent classDecl.Name consArgs consBody baseExpr classMembers
4344+
yield! declareType com ctx ent classDecl.Name classDecl.XmlDoc consArgs consBody baseExpr classMembers
43234345

43244346
if not ent.IsAbstractClass then
43254347
yield
@@ -4519,7 +4541,7 @@ but thanks to the optimisation done below we get
45194541
<| fun ctx -> transformClassWithPrimaryConstructor com ctx ent decl classMembers cons
45204542
| None ->
45214543
if ent.IsFSharpUnion then
4522-
transformUnion com ctx ent decl.Name classMembers
4544+
transformUnion com ctx ent decl.Name decl.XmlDoc classMembers
45234545
else
45244546
transformClassWithCompilerGeneratedConstructor com ctx ent decl classMembers
45254547

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module ClassMember
2+
3+
/// My superb class with attachments and doc comments
4+
type ClassWithAttachmentsAndDoc(v: int) =
5+
/// Gets the value
6+
member _.Value = v
7+
/// Returns a greeting
8+
member _.Greet(name: string) = $"Hello, {name}! Value is {v}."
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
2+
import { class_type } from "./fable_modules/fable-library-js.5.0.0-rc.3/Reflection.js";
3+
4+
/**
5+
* My superb class with attachments and doc comments
6+
*/
7+
export class ClassWithAttachmentsAndDoc {
8+
constructor(v) {
9+
this.v = (v | 0);
10+
}
11+
}
12+
13+
export function ClassWithAttachmentsAndDoc_$reflection() {
14+
return class_type("ClassMember.ClassWithAttachmentsAndDoc", undefined, ClassWithAttachmentsAndDoc);
15+
}
16+
17+
export function ClassWithAttachmentsAndDoc_$ctor_Z524259A4(v) {
18+
return new ClassWithAttachmentsAndDoc(v);
19+
}
20+
21+
/**
22+
* Gets the value
23+
*/
24+
export function ClassWithAttachmentsAndDoc__get_Value(_) {
25+
return _.v | 0;
26+
}
27+
28+
/**
29+
* Returns a greeting
30+
*/
31+
export function ClassWithAttachmentsAndDoc__Greet_Z721C83C5(_, name) {
32+
return `Hello, ${name}! Value is ${_.v}.`;
33+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module ClassMemberClassMemberWithAttachMembers
2+
3+
open Fable.Core
4+
5+
/// My superb class with attachments and doc comments
6+
[<Fable.Core.AttachMembers>]
7+
type ClassWithAttachmentsAndDoc(v: int) =
8+
/// Gets the value
9+
member _.Value = v
10+
/// Returns a greeting
11+
member _.Greet(name: string) = $"Hello, {name}! Value is {v}."
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
2+
import { class_type } from "./fable_modules/fable-library-js.5.0.0-rc.3/Reflection.js";
3+
4+
/**
5+
* My superb class with attachments and doc comments
6+
*/
7+
export class ClassWithAttachmentsAndDoc {
8+
constructor(v) {
9+
this.v = (v | 0);
10+
}
11+
/**
12+
* Gets the value
13+
*/
14+
get Value() {
15+
const _ = this;
16+
return _.v | 0;
17+
}
18+
/**
19+
* Returns a greeting
20+
*/
21+
Greet(name) {
22+
const _ = this;
23+
return `Hello, ${name}! Value is ${_.v}.`;
24+
}
25+
}
26+
27+
export function ClassWithAttachmentsAndDoc_$reflection() {
28+
return class_type("ClassMemberClassMemberWithAttachMembers.ClassWithAttachmentsAndDoc", undefined, ClassWithAttachmentsAndDoc);
29+
}
30+
31+
export function ClassWithAttachmentsAndDoc_$ctor_Z524259A4(v) {
32+
return new ClassWithAttachmentsAndDoc(v);
33+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net10.0</TargetFramework>
5+
<RollForward>Major</RollForward>
6+
<GenerateDocumentationFile>true</GenerateDocumentationFile>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<Compile Include="ClassMember.fs" />
11+
<Compile Include="ClassMemberWithAttachMembers.fs" />
12+
</ItemGroup>
13+
14+
<ItemGroup>
15+
<PackageReference Include="Fable.Core" Version="4.5.0" />
16+
</ItemGroup>
17+
18+
</Project>

0 commit comments

Comments
 (0)