Skip to content

Commit

Permalink
[Python] - Print root module and module function comments (fable-comp…
Browse files Browse the repository at this point in the history
…iler#4027)

* Print Xml module and function docs in Python

* Update Changelog

* Run fantomas

* Fix `[Py]` to `[Python]`

* Fix the changelog entry

---------

Co-authored-by: Maxime Mangel <[email protected]>
  • Loading branch information
alfonsogarciacaro and MangelMaxime authored Jan 28, 2025
1 parent 81292bc commit 9949737
Show file tree
Hide file tree
Showing 13 changed files with 118 additions and 35 deletions.
6 changes: 5 additions & 1 deletion src/Fable.Cli/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

### Added

* [Python] - Print root module and module function comments (by @alfonsogarciacaro)

## 5.0.0-alpha.9 - 2025-01-28

### Fixed
Expand Down Expand Up @@ -40,7 +44,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Fixed

* [Py] Add missing unicode categories in python library (by @joprice)
* [Python] Add missing unicode categories in python library (by @joprice)
* [All] Log JSON output if we fail to parse MSBuild result (by @MangelMaxime)

## 5.0.0-alpha.5 - 2025-01-09
Expand Down
6 changes: 5 additions & 1 deletion src/Fable.Compiler/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

### Added

* [Python] - Print root module and module function comments (by @alfonsogarciacaro)

## 5.0.0-alpha.9 - 2025-01-28

### Fixed
Expand Down Expand Up @@ -40,7 +44,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Fixed

* [Py] Add missing unicode categories in python library (by @joprice)
* [Python] Add missing unicode categories in python library (by @joprice)
* [All] Log JSON output if we fail to parse MSBuild result (by @MangelMaxime)

## 5.0.0-alpha.5 - 2025-01-09
Expand Down
11 changes: 5 additions & 6 deletions src/Fable.Transforms/BabelPrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -635,13 +635,11 @@ module PrinterExtensions =
match doc with
| None -> ()
| Some doc ->
// TODO: Check docs with params, etc
let regex = Regex(@"<summary>([\s\S]*?)</summary>", RegexOptions.Compiled)
let doc = ParsedXmlDoc.Parse doc

let m = regex.Match(doc)

if m.Success then
let lines = m.Groups[1].Value.Trim().Split('\n')
match doc.Summary with
| Some summary ->
let lines = summary.Split('\n')
printer.Print("/**")
printer.PrintNewLine()

Expand All @@ -654,6 +652,7 @@ module PrinterExtensions =

printer.Print(" */")
printer.PrintNewLine()
| None -> ()

member printer.PrintDeclaration(decl: Declaration) =
match decl with
Expand Down
2 changes: 1 addition & 1 deletion src/Fable.Transforms/FSharp2Fable.Util.fs
Original file line number Diff line number Diff line change
Expand Up @@ -613,7 +613,7 @@ module Helpers =

match trimRootModule, ent.Path with
| TrimRootModule com, (Fable.SourcePath sourcePath | Fable.PrecompiledLib(sourcePath, _)) ->
let rootMod = com.GetRootModule(sourcePath)
let rootMod, _ = com.GetRootModule(sourcePath)

if fullName.StartsWith(rootMod, StringComparison.Ordinal) then
fullName.Substring(rootMod.Length).TrimStart('.')
Expand Down
6 changes: 3 additions & 3 deletions src/Fable.Transforms/FSharp2Fable.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2113,14 +2113,14 @@ let rec getRootFSharpEntities (declarations: FSharpImplementationFileDeclaration

Seq.collect getRootFSharpEntitiesInner declarations

let getRootModule (declarations: FSharpImplementationFileDeclaration list) =
let getRootModule (declarations: FSharpImplementationFileDeclaration list) : string * (FSharpXmlDoc option) =
let rec getRootModuleInner outerEnt decls =
match decls, outerEnt with
| [ FSharpImplementationFileDeclaration.Entity(ent, decls) ], _ when ent.IsFSharpModule || ent.IsNamespace ->
getRootModuleInner (Some ent) decls
| CommonNamespace(ent, decls), _ -> getRootModuleInner (Some ent) decls
| _, Some e -> FsEnt.FullName e
| _, None -> ""
| _, Some e -> FsEnt.FullName e, Some e.XmlDoc
| _, None -> "", None

getRootModuleInner None declarations

Expand Down
2 changes: 1 addition & 1 deletion src/Fable.Transforms/FSharp2Fable.fsi
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ open Fable.AST

val getRootFSharpEntities: declarations: FSharpImplementationFileDeclaration list -> FSharpEntity seq

val getRootModule: declarations: FSharpImplementationFileDeclaration list -> string
val getRootModule: declarations: FSharpImplementationFileDeclaration list -> string * FSharpXmlDoc option

val getInlineExprs:
fileName: string -> declarations: FSharpImplementationFileDeclaration list -> (string * InlineExprLazy) list
Expand Down
4 changes: 2 additions & 2 deletions src/Fable.Transforms/Global/Compiler.fs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ type Compiler =

abstract GetImplementationFile: fileName: string -> FSharpImplementationFileDeclaration list

abstract GetRootModule: fileName: string -> string
abstract GetRootModule: fileName: string -> string * FSharpXmlDoc option
abstract TryGetEntity: Fable.EntityRef -> Fable.Entity option
abstract GetInlineExpr: string -> InlineExpr
abstract AddWatchDependency: file: string -> unit
Expand Down Expand Up @@ -167,7 +167,7 @@ module CompilerExt =
member _.ProjectFile = com.ProjectFile
member _.SourceFiles = com.SourceFiles
member _.Options = com.Options
member _.GetRootModule(fileName) = com.GetRootModule(fileName)
member _.GetRootModule(fileName) = com.GetRootModule(fileName) |> fst
member _.GetEntity(ref) = com.GetEntity(ref)
member _.GetMember(ref) = com.GetMember(ref)

Expand Down
8 changes: 4 additions & 4 deletions src/Fable.Transforms/Php/Fable2Php.fs
Original file line number Diff line number Diff line change
Expand Up @@ -828,7 +828,7 @@ and getPhpTypeForEntity (com: IPhpCompiler) (entity: Fable.Entity) =
| Some path ->
match entity with
| :? Fable.Transforms.FSharp2Fable.FsEnt as fs ->
let ns = com.GetRootModule(path) |> nsreplacement |> Some
let ns = com.GetRootModule(path) |> fst |> nsreplacement |> Some

{
Name = fixName fs.FSharpEntity.CompiledName
Expand All @@ -837,7 +837,7 @@ and getPhpTypeForEntity (com: IPhpCompiler) (entity: Fable.Entity) =
}

| _ ->
let rootModule = com.GetRootModule(path)
let rootModule = com.GetRootModule(path) |> fst

{
Name = fixName entity.DisplayName
Expand Down Expand Up @@ -1408,7 +1408,7 @@ and convertValue (com: IPhpCompiler) (value: Fable.ValueKind) range =
match ent.Ref.SourcePath with
| Some p ->
com.AddRequire(p)
Some(com.GetRootModule(p))
Some(com.GetRootModule(p) |> fst)
| None -> None

let t = withNamespace rootModule name
Expand Down Expand Up @@ -2169,7 +2169,7 @@ module Compiler =
let phpComp = PhpCompiler(com) :> IPhpCompiler
phpComp.ClearRequire(__SOURCE_DIRECTORY__ + @"/src/")

let rootModule = com.GetRootModule(phpComp.CurrentFile) |> nsreplacement
let rootModule = com.GetRootModule(phpComp.CurrentFile) |> fst |> nsreplacement
phpComp.SetPhpNamespace(rootModule)

let decls =
Expand Down
18 changes: 18 additions & 0 deletions src/Fable.Transforms/Printer.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,27 @@
module Fable.Transforms.Printer

open System
open System.Text.RegularExpressions
open Fable
open Fable.AST

type ParsedXmlDoc =
{
Summary: string option
}
// TODO: Parse the whole XML
static member Parse(xml: string) =
let regex = Regex(@"<summary>([\s\S]*?)</summary>", RegexOptions.Compiled)
let m = regex.Match(xml)

{
Summary =
if m.Success then
Some(m.Groups.[1].Value.Trim())
else
None
}

type Writer =
inherit IDisposable

Expand Down
21 changes: 14 additions & 7 deletions src/Fable.Transforms/Python/Fable2Python.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1487,10 +1487,10 @@ module Util =
|> Option.map Identifier
|> Option.defaultWith (fun _ -> Helpers.getUniqueIdentifier "_arrow")

let func = createFunction ident args body [] returnType
let func = createFunction ident args body [] returnType None
Expression.name ident, [ func ]

let createFunction name args body decoratorList returnType =
let createFunction name args body decoratorList returnType (comment: string option) =
let (|Awaitable|_|) expr =
match expr with
| Expression.Call {
Expand Down Expand Up @@ -1557,21 +1557,23 @@ module Util =
args = args,
body = body',
decoratorList = decoratorList,
returns = returnType
returns = returnType,
?comment = comment
)
| _ ->
Statement.functionDef (
name = name,
args = args,
body = body,
decoratorList = decoratorList,
returns = returnType
returns = returnType,
?comment = comment
)

let makeFunction name (args: Arguments, body: Expression, decoratorList, returnType) : Statement =
// printfn "makeFunction: %A" name
let body = wrapExprInBlockWithReturn (body, [])
createFunction name args body decoratorList returnType
createFunction name args body decoratorList returnType None

let makeFunctionExpression
(com: IPythonCompiler)
Expand Down Expand Up @@ -3769,7 +3771,7 @@ module Util =
getMemberArgsAndBody com ctx (NonAttached membName) info.HasSpread args body

let name = com.GetIdentifier(ctx, membName)
let stmt = createFunction name args body' [] returnType
let stmt = createFunction name args body' [] returnType info.XmlDoc
let expr = Expression.name name

info.Attributes
Expand Down Expand Up @@ -4461,8 +4463,13 @@ module Compiler =
//printfn "file: %A" file.Declarations
let rootDecls = List.collect (transformDeclaration com ctx) file.Declarations

let rootComment =
com.GetRootModule(com.CurrentFile)
|> snd
|> Option.bind FSharp2Fable.TypeHelpers.tryGetXmlDoc

let typeVars = com.GetAllTypeVars() |> transformTypeVars com ctx
let importDecls = com.GetAllImports() |> transformImports com
let exports = com.GetAllExports() |> transformExports com ctx
let body = importDecls @ typeVars @ rootDecls @ exports
Module.module' body
Module.module' (body, ?comment = rootComment)
23 changes: 18 additions & 5 deletions src/Fable.Transforms/Python/Python.fs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,11 @@ type Statement =
| FunctionDef of FunctionDef
| AsyncFunctionDef of AsyncFunctionDef

type Module = { Body: Statement list }
type Module =
{
Body: Statement list
Comment: string option
}

/// Both parameters are raw strings of the names. asname can be None if the regular name is to be used.
///
Expand Down Expand Up @@ -425,6 +429,7 @@ type FunctionDef =
DecoratorList: Expression list
Returns: Expression option
TypeComment: string option
Comment: string option
}

/// global and nonlocal statements. names is a list of raw strings.
Expand Down Expand Up @@ -485,16 +490,18 @@ type AsyncFunctionDef =
DecoratorList: Expression list
Returns: Expression option
TypeComment: string option
Comment: string option
}

static member Create(name, args, body, decoratorList, ?returns, ?typeComment) =
static member Create(name, args, body, decoratorList, ?returns, ?typeComment, ?comment) =
{
Name = name
Args = args
Body = body
DecoratorList = decoratorList
Returns = returns
TypeComment = typeComment
Comment = comment
}

/// An import statement. names is a list of alias nodes.
Expand Down Expand Up @@ -902,25 +909,27 @@ module PythonExtensions =
}
|> ClassDef

static member functionDef(name, args, body, ?decoratorList, ?returns, ?typeComment) : Statement =
static member functionDef(name, args, body, ?decoratorList, ?returns, ?typeComment, ?comment) : Statement =
{
FunctionDef.Name = name
Args = args
Body = body
DecoratorList = defaultArg decoratorList []
Returns = returns
TypeComment = typeComment
Comment = comment
}
|> FunctionDef

static member asyncFunctionDef(name, args, body, ?decoratorList, ?returns, ?typeComment) : Statement =
static member asyncFunctionDef(name, args, body, ?decoratorList, ?returns, ?typeComment, ?comment) : Statement =
{
AsyncFunctionDef.Name = name
Args = args
Body = body
DecoratorList = defaultArg decoratorList []
Returns = returns
TypeComment = typeComment
Comment = comment
}
|> AsyncFunctionDef

Expand Down Expand Up @@ -1215,7 +1224,11 @@ module PythonExtensions =

type Module with

static member module'(body) = { Body = body }
static member module'(body, ?comment) =
{
Body = body
Comment = comment
}

type Arg with

Expand Down
Loading

0 comments on commit 9949737

Please sign in to comment.