Skip to content

Commit 77e85bc

Browse files
fable-repo-assist[bot]github-actions[bot]Copilotdbrattliclaude
authored
refactor(js/ts/python): use struct partial active patterns and postfix array syntax (#4569)
Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Co-authored-by: Dag Brattli <dag@brattli.net> Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: fable-repo-assist[bot] <279220273+fable-repo-assist[bot]@users.noreply.github.com> Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
1 parent 7289c6a commit 77e85bc

4 files changed

Lines changed: 25 additions & 19 deletions

File tree

src/Fable.Transforms/BabelPrinter.fs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,25 @@ module PrinterExtensions =
3131
| ExpressionStatement(expr) -> hasSideEffects (expr)
3232
| _ -> true
3333

34+
[<return: Struct>]
3435
let (|UndefinedOrVoid|_|) =
3536
function
36-
| Undefined _ -> Some()
37-
| UnaryExpression(argument, "void", false, _loc) when not (hasSideEffects (argument)) -> Some()
38-
| _ -> None
37+
| Undefined _ -> ValueSome()
38+
| UnaryExpression(argument, "void", false, _loc) when not (hasSideEffects (argument)) -> ValueSome()
39+
| _ -> ValueNone
3940

41+
[<return: Struct>]
4042
let (|NullOrUndefinedOrVoid|_|) =
4143
function
4244
| Literal(NullLiteral _)
43-
| UndefinedOrVoid _ -> Some()
44-
| _ -> None
45+
| UndefinedOrVoid _ -> ValueSome()
46+
| _ -> ValueNone
4547

48+
[<return: Struct>]
4649
let (|StringConstant|_|) =
4750
function
48-
| Literal(Literal.StringLiteral(StringLiteral(value = value))) -> Some value
49-
| _ -> None
51+
| Literal(Literal.StringLiteral(StringLiteral(value = value))) -> ValueSome value
52+
| _ -> ValueNone
5053

5154
type Printer with
5255

@@ -233,7 +236,7 @@ module PrinterExtensions =
233236
id: Identifier option,
234237
isAbstract: bool option,
235238
superClass: SuperClass option,
236-
typeParameters: TypeParameter[],
239+
typeParameters: TypeParameter array,
237240
implements: TypeAnnotation array,
238241
members: ClassMember array,
239242
loc
@@ -278,7 +281,7 @@ module PrinterExtensions =
278281
id: Identifier option,
279282
parameters: Parameter array,
280283
body: BlockStatement,
281-
typeParameters: TypeParameter[],
284+
typeParameters: TypeParameter array,
282285
returnType: TypeAnnotation option,
283286
loc,
284287
?isDeclaration,
@@ -1521,7 +1524,7 @@ module PrinterExtensions =
15211524
printer.PrintOptional(bound, " extends ")
15221525
// printer.PrintOptional(``default``)
15231526

1524-
member printer.Print(parameters: TypeParameter[]) =
1527+
member printer.Print(parameters: TypeParameter array) =
15251528
if parameters.Length > 0 then
15261529
printer.Print("<")
15271530
printer.PrintCommaSeparatedArray(parameters)

src/Fable.Transforms/Python/Fable2Python.Transforms.fs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2693,6 +2693,7 @@ let transformAsArray (com: IPythonCompiler) ctx expr (info: Fable.CallInfo) : Ex
26932693

26942694
/// Active pattern to detect F# `use` statements compiled as TryCatch with Dispose
26952695
/// This helps us to transform F# `use` (i.e. TryCatch) as Python `with` statements
2696+
[<return: Struct>]
26962697
let (|UsePattern|_|) (body: Fable.Expr) =
26972698
match body with
26982699
| Fable.TryCatch(tryBody,
@@ -2707,8 +2708,8 @@ let (|UsePattern|_|) (body: Fable.Expr) =
27072708
_),
27082709
_,
27092710
_)),
2710-
_) -> Some(disposeName, tryBody)
2711-
| _ -> None
2711+
_) -> ValueSome(disposeName, tryBody)
2712+
| _ -> ValueNone
27122713

27132714
let rec transformAsStatements (com: IPythonCompiler) ctx returnStrategy (expr: Fable.Expr) : Statement list =
27142715
// printfn "transformAsStatements: %A" expr

src/Fable.Transforms/Python/Fable2Python.Util.fs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -432,11 +432,12 @@ module Util =
432432

433433
let (|TransformExpr|) (com: IPythonCompiler) ctx e : Expression * Statement list = com.TransformAsExpr(ctx, e)
434434

435+
[<return: Struct>]
435436
let (|Function|_|) =
436437
function
437-
| Fable.Lambda(arg, body, _) -> Some([ arg ], body)
438-
| Fable.Delegate(args, body, _, []) -> Some(args, body)
439-
| _ -> None
438+
| Fable.Lambda(arg, body, _) -> ValueSome([ arg ], body)
439+
| Fable.Delegate(args, body, _, []) -> ValueSome(args, body)
440+
| _ -> ValueNone
440441

441442
let getUniqueNameInRootScope (ctx: Context) name =
442443
let name =
@@ -910,15 +911,16 @@ module Util =
910911

911912

912913
// Active patterns for type matching
914+
[<return: Struct>]
913915
let (|IEnumerableOfKeyValuePair|_|) (targetType: Fable.Type, sourceExpr: Fable.Expr) =
914916
match targetType, sourceExpr.Type with
915917
| Fable.DeclaredType(ent, [ Fable.DeclaredType(kvpEnt, [ _; _ ]) ]), Fable.DeclaredType(sourceEnt, _) when
916918
ent.FullName = Types.ienumerableGeneric
917919
&& kvpEnt.FullName = Types.keyValuePair
918920
&& (sourceEnt.FullName = Types.dictionary || sourceEnt.FullName = Types.idictionary)
919921
->
920-
Some(kvpEnt)
921-
| _ -> None
922+
ValueSome(kvpEnt)
923+
| _ -> ValueNone
922924

923925
let makeFieldGet (expr: Expression) (field: string) =
924926
Expression.attribute (expr, Identifier field, ctx = Load), []
@@ -1104,7 +1106,7 @@ module Util =
11041106
/// Common utilities for Python transformations
11051107
module Helpers =
11061108
/// Returns true if the first field type can be None in Python
1107-
let isOptional (fields: Fable.Ident[]) =
1109+
let isOptional (fields: Fable.Ident array) =
11081110
if fields.Length < 1 then
11091111
false
11101112
else

src/Fable.Transforms/Python/Python.AST.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ type Literal =
127127
| FloatLiteral of float
128128
| IntLiteral of obj
129129
| BoolLiteral of bool
130-
| BytesLiteral of byte[]
130+
| BytesLiteral of byte array
131131
| StringLiteral of string
132132
| NoneLiteral
133133
| TupleLiteral of Literal list

0 commit comments

Comments
 (0)