fable-compiler / Fable

F# to JavaScript, TypeScript, Python, Rust and Dart Compiler
http://fable.io/
MIT License
2.93k stars 301 forks source link

System.FormattableString results in doubled '{' in JavaScript output #3889

Closed roboz0r closed 1 month ago

roboz0r commented 1 month ago

Description

When writing bindings referring to System.FormattableString the resulting JavaScript string includes {{ and }} where only single characters are expected. This results in broken styles in e.g. Fable.Lit.

Repro code

REPL

open System
open Fable.Core

[<Erase>]
type Binding() =
    [<Emit("css($0, $1)")>]
    static member inline private cssInner(strs: string[], [<ParamArray>] args: obj[]) : obj = jsNative

    static member inline css(s: FormattableString) =
        Binding.cssInner (s.GetStrings(), s.GetArguments())

module X = 
  let ex1 () =
    let color = "blue"
    Binding.css 
        $""":host {{
            color: {color};
        }}"""

  let ex2 () =
    Binding.css 
        $$""":host {
            color: blue;
        }"""

  let ex3 () =
    Binding.css 
        $":host {{
            color: blue;
        }}"

Expected and actual results

Actual

export function X_ex1() {
    const color = "blue";
    const s = fmt`:host {{
            color: ${color};
        }}`;
    return css(s.strs, ...s.args);
}

Expected

export function X_ex1() {
    const color = "blue";
    const s = fmt`:host {
            color: ${color};
        }`;
    return css(s.strs, ...s.args);
}

or

export function X_ex1() {
    const color = "blue";
    return css(`:host {
            color: ${color};
        }`);
}

Related information