From 94cf161cb2a328b3c727c43e43542a6e33338596 Mon Sep 17 00:00:00 2001 From: Maxime Mangel Date: Fri, 25 Oct 2024 23:19:35 +0200 Subject: [PATCH] [JS/TS] Fix regression, generate `let` variable when using `import` on a private mutable variable (#3941) --- src/Fable.Cli/CHANGELOG.md | 1 + src/Fable.Transforms/FSharp2Fable.fs | 2 +- .../data/import/PrivateMutableImport.fs | 12 ++++++++++++ .../import/PrivateMutableImport.js.expected | 4 ++++ .../Integration/data/import/import.fsproj | 17 +++++++++++++++++ 5 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 tests/Integration/Integration/data/import/PrivateMutableImport.fs create mode 100644 tests/Integration/Integration/data/import/PrivateMutableImport.js.expected create mode 100644 tests/Integration/Integration/data/import/import.fsproj diff --git a/src/Fable.Cli/CHANGELOG.md b/src/Fable.Cli/CHANGELOG.md index 22e32d739..240427a31 100644 --- a/src/Fable.Cli/CHANGELOG.md +++ b/src/Fable.Cli/CHANGELOG.md @@ -23,6 +23,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * [Rust] Fixed try finally handler order of execution (by @ncave) * [JS/TS/Python/Rust] Fixed String.StartsWith/EndsWith (#3934) (by @ncave) * [All/Rust] Removed Regex.Replace from hot paths (by @ncave) +* [JS] Fix regression, generate `let` variable when using `import` on a private mutable variable (by @MangelMaxime) ## 4.22.0 - 2024-10-02 diff --git a/src/Fable.Transforms/FSharp2Fable.fs b/src/Fable.Transforms/FSharp2Fable.fs index b4e46594c..c3502d2f2 100644 --- a/src/Fable.Transforms/FSharp2Fable.fs +++ b/src/Fable.Transforms/FSharp2Fable.fs @@ -1579,7 +1579,7 @@ let private transformImportValue com r typ name (memb: FSharpMemberOrFunctionOrV "Imported members cannot be mutable and public, please make it private: " + name |> addError com [] None - let memberRef = Fable.GeneratedMember.Value(name, typ) + let memberRef = Fable.GeneratedMember.Value(name, typ, isMutable = memb.IsMutable) transformImport com r typ name [] memberRef selector path let private transformMemberValue diff --git a/tests/Integration/Integration/data/import/PrivateMutableImport.fs b/tests/Integration/Integration/data/import/PrivateMutableImport.fs new file mode 100644 index 000000000..d9b7964a0 --- /dev/null +++ b/tests/Integration/Integration/data/import/PrivateMutableImport.fs @@ -0,0 +1,12 @@ +module PrivateMutableImport + +open Fable.Core.JsInterop + +type ToastrOptions = + abstract Thing : string with get, set + +[] +module Toastr = + let mutable private theOptions: ToastrOptions = + //() + import "path" "toastr" diff --git a/tests/Integration/Integration/data/import/PrivateMutableImport.js.expected b/tests/Integration/Integration/data/import/PrivateMutableImport.js.expected new file mode 100644 index 000000000..3d4cd6c4e --- /dev/null +++ b/tests/Integration/Integration/data/import/PrivateMutableImport.js.expected @@ -0,0 +1,4 @@ +import { path } from "toastr"; + +export let Toastr_theOptions = path; + diff --git a/tests/Integration/Integration/data/import/import.fsproj b/tests/Integration/Integration/data/import/import.fsproj new file mode 100644 index 000000000..1a4c4915c --- /dev/null +++ b/tests/Integration/Integration/data/import/import.fsproj @@ -0,0 +1,17 @@ + + + + net8.0 + Major + true + + + + + + + + + + +