[Repo Assist] fix(js): handle .NET format specifiers in F# interpolated strings#4553
Draft
github-actions[bot] wants to merge 1 commit intomainfrom
Conversation
When an F# interpolated string uses a .NET format specifier
(e.g. `$"{x:N0}"` or `$"{n:#,#} items"`), the F# compiler
encodes the hole as `%P(N0)` in the PrintFormat format string
(with a non-empty specifier inside the parens).
The previous regex `%P\(\)` only matched empty parens, so `%P(N0)`
was never recognised as a hole. This caused `makeStringTemplateFrom`
to silently return a broken StringTemplate that included the literal
text `%P(N0)` with no interpolated value.
Fix:
- Update the regex in `makeStringTemplateFromWith` to
`%P\(([^)]*)\)` so it also captures non-empty .NET format
specifiers.
- Add a `handleDotNetSpec` callback alongside the existing
`handleFormatSpec` callback.
- In `makeStringTemplateFrom`, return None when a .NET spec is
encountered (correctly falls through to AllowingFormat).
- In `makeStringTemplateFromAllowingFormat`, wrap the value in
`String.format("{0:<spec>}", value)` so the specifier is
applied at runtime.
Closes #4046 (items 3 and 4)
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
41 tasks
24 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
🤖 This is an automated pull request from Repo Assist, an AI-powered assistant.
Closes #4046 (items 3 and 4 —
$"{x:N0}"and$"{n:#,#} items"producing literal%P(N0)/%P(#,#)output).Root cause
When an F# interpolated string uses a .NET format specifier such as
$"{x:N0}", the F# compiler encodes the hole in thePrintFormatformat string as%P(N0)— with the specifier inside the parentheses.The regex in
makeStringTemplateFromWith(Replacements.Util.fs) was%P\(\), which only matched empty parentheses. The pattern%P(N0)was never recognised as a hole, so:makeStringTemplateFromincorrectly built aStringTemplatethat contained the literal text%P(N0)with no interpolated value — a silent, wrong result.makeStringTemplateFromAllowingFormat(the second attempt) never got a chance to apply formatting because the first attempt returnedSome(brokenTemplate)instead ofNone.The end result was the literal string
%P(N0)appearing in the JavaScript output.Fix
src/Fable.Transforms/Replacements.Util.fs%P\(\)to%P\(([^)]*)\)to also capture the optional .NET specifier inside the parens (captured in Group 2).handleDotNetSpec: string -> Expr -> Expr optioncallback parameter to the privatemakeStringTemplateFromWith.makeStringTemplateFrompasses(fun _ _ -> None)forhandleDotNetSpec, so it correctly returnsNonewhen a .NET spec is present, falling through tomakeStringTemplateFromAllowingFormat.makeStringTemplateFromAllowingFormatpasses a callback that callsString.format("{0:<spec>}", value)— the same library function that already handles(1000).ToString("N0").tests/Js/Main/StringTests.fsN0,N2,F2standard numeric specifiers and the#,#custom pattern, including mixed strings like$"Count: {n:N0} items".Trade-offs
String.interpolatefor these cases (which also doesn't handle.NETspecs) — that is a pre-existing limitation and left for a separate fix. This PR only targets JavaScript/TypeScript.src/Fable.AST/orsrc/Fable.Core/.