From d5094cb6aa5df578d6c8c4ea11f885e8d63cc513 Mon Sep 17 00:00:00 2001 From: Alfonso Garcia-Caro Date: Tue, 11 Feb 2025 20:57:45 +0900 Subject: [PATCH] [JS/TS/Python] Fix #4041: Replace idents with unit type --- src/Fable.Transforms/Fable2Babel.fs | 16 ++++++++++------ src/Fable.Transforms/Python/Fable2Python.fs | 10 ++++++++-- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/src/Fable.Transforms/Fable2Babel.fs b/src/Fable.Transforms/Fable2Babel.fs index fc1bba82c..12d25ef7a 100644 --- a/src/Fable.Transforms/Fable2Babel.fs +++ b/src/Fable.Transforms/Fable2Babel.fs @@ -2827,13 +2827,17 @@ module Util = | _ -> transformDecisionTreeWithExtraSwitch com ctx returnStrategy targets treeExpr - let transformIdent (com: IBabelCompiler) ctx id = - let e = identAsExpr id + let transformIdent (com: IBabelCompiler) ctx (id: Fable.Ident) = + match id.Type with + // Remove unit idents, because the declaration may have been erased #4041 + | Fable.Unit -> undefined id.Range None + | _ -> + let e = identAsExpr id - if com.IsTypeScript && ctx.ForcedIdents.Contains id.Name then - Expression.unaryExpression (UnaryNot, e, isSuffix = true) - else - e + if com.IsTypeScript && ctx.ForcedIdents.Contains id.Name then + Expression.unaryExpression (UnaryNot, e, isSuffix = true) + else + e let rec transformAsExpr (com: IBabelCompiler) ctx (expr: Fable.Expr) : Expression = match expr with diff --git a/src/Fable.Transforms/Python/Fable2Python.fs b/src/Fable.Transforms/Python/Fable2Python.fs index a21d570e8..b55ab36cd 100644 --- a/src/Fable.Transforms/Python/Fable2Python.fs +++ b/src/Fable.Transforms/Python/Fable2Python.fs @@ -2950,6 +2950,12 @@ module Util = let name = Expression.name name Expression.call name, [ func ] + let transformIdent (com: IPythonCompiler) ctx (id: Fable.Ident) = + match id.Type with + // Remove unit idents, because the declaration may have been erased #4041 + | Fable.Unit -> undefined id.Range + | _ -> identAsExpr com ctx id + let rec transformAsExpr (com: IPythonCompiler) ctx (expr: Fable.Expr) : Expression * Statement list = // printfn "transformAsExpr: %A" expr match expr with @@ -2959,7 +2965,7 @@ module Util = | Fable.Value(kind, r) -> transformValue com ctx r kind - | Fable.IdentExpr id -> identAsExpr com ctx id, [] + | Fable.IdentExpr id -> transformIdent com ctx id, [] | Fable.Import({ Selector = selector @@ -3154,7 +3160,7 @@ module Util = stmts @ (expr |> resolveExpr ctx kind.Type returnStrategy) - | Fable.IdentExpr id -> identAsExpr com ctx id |> resolveExpr ctx id.Type returnStrategy + | Fable.IdentExpr id -> transformIdent com ctx id |> resolveExpr ctx id.Type returnStrategy | Fable.Import({ Selector = selector