fsprojects / fantomas

FSharp source code formatter
https://fsprojects.github.io/fantomas
Other
770 stars 191 forks source link

Fantomas strips quotes from quoted type-parameters #2875

Closed JohSand closed 1 year ago

JohSand commented 1 year ago

Issue created from fantomas-online

Code

let repro (t: '``QuotedWithIllegalChar<'T>``) = ()

Result

let repro (t: 'QuotedWithIllegalChar<'T>) = ()

Problem description

Sometimes it is useful to have type-parameters in quotes, allowing them to contain characters such as '<' and '>'. After formatting, this quotation is lost, leading to non-compiling code.

Extra information

Options

Fantomas main branch at 2023-05-05T09:52:15Z - 4093fa59701158aa447af923a84dc2bbcf416941

Default Fantomas configuration

Did you know that you can ignore files when formatting from fantomas-tool or the FAKE targets by using a .fantomasignore file?

nojaf commented 1 year ago

Hello, thank you for your interest in this project.

It appears we are not processing the ident text of a SynTypar in https://github.com/fsprojects/fantomas/blob/9298c26c6ba8ab92e7e88cf602654350a36d6572/src/Fantomas.Core/ASTTransformer.fs#L1976-L1985

Some check along these lines could do the trick:

let mkSynTypar (SynTypar(ident, req, _)) =
    let range =
        mkRange
            ident.idRange.FileName
            (Position.mkPos ident.idRange.StartLine (ident.idRange.StartColumn - 1))
            ident.idRange.End

    let identText =
        let width = ident.idRange.EndColumn - ident.idRange.StartColumn
        // 5 because of ^ or ' and `` on each side
        if ident.idText.Length + 5 = width then
            $"``{ident.idText}``"
        else
            ident.idText

    match req with
    | TyparStaticReq.None -> stn $"'{identText}" range
    | TyparStaticReq.HeadType -> stn $"^{identText}" range

Are you interested in submitting a PR for this?

JohSand commented 1 year ago

I'm willing to give it a try.