diff --git a/src/Fable.Cli/CHANGELOG.md b/src/Fable.Cli/CHANGELOG.md index 7c43811e2f..ee78b58973 100644 --- a/src/Fable.Cli/CHANGELOG.md +++ b/src/Fable.Cli/CHANGELOG.md @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed +* [Python] Fix `String.IndexOf`/`LastIndexOf` with `StringComparison` argument emitting it as a start-index instead of a compile error (by @repo-assist) * [Beam] Fix `String.IndexOf`/`LastIndexOf` with `StringComparison` argument incorrectly treating the enum value as a start index ## 5.0.0-rc.6 - 2026-03-31 diff --git a/src/Fable.Compiler/CHANGELOG.md b/src/Fable.Compiler/CHANGELOG.md index dd09a9bb78..3de0f70abb 100644 --- a/src/Fable.Compiler/CHANGELOG.md +++ b/src/Fable.Compiler/CHANGELOG.md @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed +* [Python] Fix `String.IndexOf`/`LastIndexOf` with `StringComparison` argument emitting it as a start-index instead of a compile error (by @repo-assist) * [Beam] Fix `String.IndexOf`/`LastIndexOf` with `StringComparison` argument incorrectly treating the enum value as a start index ## 5.0.0-rc.12 - 2026-03-31 diff --git a/src/Fable.Transforms/Python/Replacements.fs b/src/Fable.Transforms/Python/Replacements.fs index ce8f64fb6d..311d173e62 100644 --- a/src/Fable.Transforms/Python/Replacements.fs +++ b/src/Fable.Transforms/Python/Replacements.fs @@ -1497,6 +1497,14 @@ let strings (com: ICompiler) (ctx: Context) r t (i: CallInfo) (thisArg: Expr opt Some c, args -> Helper.InstanceCall(c, methName, t, args, i.SignatureArgTypes, ?loc = r) |> Some | "IndexOf", Some c, _ -> + let args = + args + |> List.filter ( + function + | StringComparisonEnumValue -> false + | _ -> true + ) + match args with | [ ExprType Char ] | [ ExprType String ] @@ -1508,6 +1516,14 @@ let strings (com: ICompiler) (ctx: Context) r t (i: CallInfo) (thisArg: Expr opt |> addErrorAndReturnNull com ctx.InlinePath r |> Some | "LastIndexOf", Some c, _ -> + let args = + args + |> List.filter ( + function + | StringComparisonEnumValue -> false + | _ -> true + ) + match args with | [ ExprType Char ] | [ ExprType String ] -> Helper.InstanceCall(c, "rfind", t, args, i.SignatureArgTypes, ?loc = r) |> Some diff --git a/tests/Python/TestString.fs b/tests/Python/TestString.fs index e9f7dc4e36..925ddd707e 100644 --- a/tests/Python/TestString.fs +++ b/tests/Python/TestString.fs @@ -567,6 +567,31 @@ let ``test String.LastIndexOf works with offset`` () = "abcdbcebc".LastIndexOf("bc", 3) |> equal 1 +[] +let ``test String.IndexOf with StringComparison works`` () = + "abcdbc".IndexOf("b", StringComparison.Ordinal) + |> equal 1 + +[] +let ``test String.IndexOf char with StringComparison works`` () = + "abcdbc".IndexOf('b', StringComparison.Ordinal) + |> equal 1 + +[] +let ``test String.IndexOf with index and StringComparison works`` () = + "abcdbc".IndexOf("b", 3, StringComparison.Ordinal) + |> equal 4 + +[] +let ``test String.LastIndexOf with StringComparison works`` () = + "abcdbc".LastIndexOf("b", StringComparison.Ordinal) + |> equal 4 + +[] +let ``test String.LastIndexOf with index and StringComparison works`` () = + "abcdbc".LastIndexOf("b", 3, StringComparison.Ordinal) + |> equal 1 + [] let ``test String.IndexOfAny works`` () =