Elm-format: Should `\"` in triple-quoted strings be turned into `\\"` ?

Created on 24 Jul 2017  路  5Comments  路  Source: avh4/elm-format

elm-compiler has the possibly unexpected behavior that """ \" """ parses as the string " instead of \".

Should elm-format convert backslash-doublequote in triple-quoted strings to an escaped backslash followed by the double quote? This would mean elm-format and elm-make parse a different AST for the input, but would make it easier for people copying and pasting long strings (notably, JSON containing escaped double-quotes inside of strings) into Elm files.

discussion

Most helpful comment

I just want to add a datapoint - this has bitten us at work when trying to use JSON response mocks:

response : String
response =
    """{"data": "Escaped \"quotes\" in a string"}"""

which is valid JSON but elm-format transforms the inner string into "Escaped "quotes" in a string", which then can't be parsed by elm/json. So I'm not sure about other usecases (where \" :arrow_right: " is benefitial) but for this specific case it would be great to do \" :arrow_right: \\"...

All 5 comments

Hello, I'm new to elm/elm-format and the escaped quotes behavior inside triple quotes confuses me, is there anywhere I can read more about this?

For example:

json = """ { "foo": "b\\"a\\"r"} """  -- works
json = """ { "foo": "b\"a\"r"} """  -- doesn't work (also if elm-format is on, coverts to next line)
json = """ { "foo": "b"a"r"} """  -- doesn't work

https://github.com/elm-lang/core/issues/716 - possibly related

Currently in elm-compiler, in a triple-quoted string, " and \" both mean the same thing. If you want the resulting string to contain a backslash, you must use \\.

edit: comments relevant to #296

elm-format-0.18 0.5.2-alpha
breaks https://github.com/rtfeldman/elm-spa-example/blob/master/tests/RoutingTests.elm#L83
Replacing
"slug": \"""" ++ str ++ """"
by
"slug": """ " ++ str ++ " """
which elm refuses to compile

You are giving 2 arguments to something that is not a function!

81|             """
82|             { "description": null
83|>            , "slug": """ " ++ str ++ " """
...

edit: comments relevant to #296

@frawa see https://github.com/avh4/elm-format/issues/296 for the resolution of your issue (fixed in elm-format-0.6.1-alpha)

I just want to add a datapoint - this has bitten us at work when trying to use JSON response mocks:

response : String
response =
    """{"data": "Escaped \"quotes\" in a string"}"""

which is valid JSON but elm-format transforms the inner string into "Escaped "quotes" in a string", which then can't be parsed by elm/json. So I'm not sure about other usecases (where \" :arrow_right: " is benefitial) but for this specific case it would be great to do \" :arrow_right: \\"...

Was this page helpful?
0 / 5 - 0 ratings

Related issues

avh4 picture avh4  路  6Comments

avh4 picture avh4  路  4Comments

gabrielflorit picture gabrielflorit  路  7Comments

pdamoc picture pdamoc  路  8Comments

avh4 picture avh4  路  5Comments