Skip to content

Commit

Permalink
Fixed interface indexers (#3831)
Browse files Browse the repository at this point in the history
  • Loading branch information
ncave authored May 29, 2024
1 parent 76a33dc commit 1a1854a
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 1 deletion.
4 changes: 4 additions & 0 deletions 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

### Fixed

* [TS] Fixed interface indexers (#3830) (by @ncave)

## 4.18.0 - 2024-05-23

### Fixed
Expand Down
2 changes: 1 addition & 1 deletion src/Fable.Transforms/Fable2Babel.fs
Original file line number Diff line number Diff line change
Expand Up @@ -3857,7 +3857,7 @@ module Util =
let methods =
methods
|> Array.map (fun info ->
let prop, isComputed = memberFromName info.DisplayName
let prop, isComputed = memberFromName info.CompiledName

let args =
info.CurriedParameterGroups
Expand Down
29 changes: 29 additions & 0 deletions tests/Js/Main/TypeTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,23 @@ type TestTypeAttached(a1, a2, a3) =
with get (i) = arr.[i]
and set (i) (v) = arr.[i] <- v

type ITestProps =
abstract Value1: float with get, set
abstract Value: int -> float with get, set
abstract Item: int -> float with get, set

type TestProps(arr: float[]) =
interface ITestProps with
member _.Value1
with get () = arr.[1]
and set (v) = arr.[1] <- v
member _.Value
with get (i) = arr.[i]
and set (i) (v) = arr.[i] <- v
member _.Item
with get (i) = arr.[i]
and set (i) (v) = arr.[i] <- v

type A = { thing: int } with
member x.show() = string x.thing
static member show (x: A) = "Static: " + (string x.thing)
Expand Down Expand Up @@ -672,6 +689,18 @@ let tests =
t[2] <- 33
t[2] |> equal 33

testCase "Interface Getters Setters and Indexers work" <| fun () ->
let t = TestProps([| 1; 2; 3 |]) :> ITestProps
t.Value1 |> equal 2
t.Value1 <- 22
t.Value1 |> equal 22
t.Value(0) |> equal 1
t.Value(0) <- 11
t.Value(0) |> equal 11
t[2] |> equal 3
t[2] <- 33
t[2] |> equal 33

testCase "Statically resolved instance calls work" <| fun () ->
let a = { thing = 5 }
let b = { label = "five" }
Expand Down
30 changes: 30 additions & 0 deletions tests/Python/TestType.fs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,23 @@ type TypeAttachedTest(a1, a2, a3) =
with get (i) = arr.[i]
and set (i) (v) = arr.[i] <- v

type ITestProps =
abstract Value1: float with get, set
abstract Value: int -> float with get, set
abstract Item: int -> float with get, set

type PropsTest(arr: float[]) =
interface ITestProps with
member _.Value1
with get () = arr.[1]
and set (v) = arr.[1] <- v
member _.Value
with get (i) = arr.[i]
and set (i) (v) = arr.[i] <- v
member _.Item
with get (i) = arr.[i]
and set (i) (v) = arr.[i] <- v

type A =
{ thing: int }
member x.show() = string x.thing
Expand Down Expand Up @@ -746,6 +763,19 @@ let ``test Attached Getters Setters and Indexers work`` () =
t.[2] <- 33
t.[2] |> equal 33

[<Fact>]
let ``test Interface Getters Setters and Indexers work`` () =
let t = PropsTest([| 1; 2; 3 |]) :> ITestProps
t.Value1 |> equal 2
t.Value1 <- 22
t.Value1 |> equal 22
t.Value(0) |> equal 1
t.Value(0) <- 11
t.Value(0) |> equal 11
t.[2] |> equal 3
t.[2] <- 33
t.[2] |> equal 33

[<Fact>]
let ``test Statically resolved instance calls work`` () =
let a = { thing = 5 }
Expand Down
30 changes: 30 additions & 0 deletions tests/Rust/tests/src/TypeTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,23 @@ type TestTypeAttached(a1, a2, a3) =
with get (i) = arr.[i]
and set (i) (v) = arr.[i] <- v

type ITestProps =
abstract Value1: float with get, set
abstract Value: int -> float with get, set
abstract Item: int -> float with get, set

type TestProps(arr: float[]) =
interface ITestProps with
member _.Value1
with get () = arr.[1]
and set (v) = arr.[1] <- v
member _.Value
with get (i) = arr.[i]
and set (i) (v) = arr.[i] <- v
member _.Item
with get (i) = arr.[i]
and set (i) (v) = arr.[i] <- v

type A = { thing: int } with
member x.show() = string x.thing
static member show (x: A) = "Static: " + (string x.thing)
Expand Down Expand Up @@ -667,6 +684,19 @@ let ``Attached Getters Setters and Indexers work`` () =
t[2] <- 33
t[2] |> equal 33

[<Fact>]
let ``Interface Getters Setters and Indexers work`` () =
let t = TestProps([| 1; 2; 3 |]) :> ITestProps
t.Value1 |> equal 2
t.Value1 <- 22
t.Value1 |> equal 22
t.Value(0) |> equal 1
t.Value(0) <- 11
t.Value(0) |> equal 11
t[2] |> equal 3
t[2] <- 33
t[2] |> equal 33

[<Fact>]
let ``Statically resolved instance calls work`` () =
let a = { thing = 5 }
Expand Down

0 comments on commit 1a1854a

Please sign in to comment.