segmentio / golines

A golang formatter that fixes long lines
MIT License
903 stars 56 forks source link

Backslashes being removed from struct tag #92

Open thijsheijden opened 1 year ago

thijsheijden commented 1 year ago

I have the following struct tag: jsonschema:"title=Title,minLength=1,maxLength=40,pattern=^[^\\-<>%{}\\[\\]&#\\\\\\\"]+$", and upon running golines ${file} -w -m 120 using the Run on Save extension in VSCode, the tag is changed into jsonschema:"title=title=Title,maxLength=40,pattern=^[^\-<>%{}\[\]&#\\\"]+$", turning it into an invalid struct tag.

varfrog commented 1 year ago

To reproduce the bug, run golines on this file and notice how “pattern” changes.

package main

import "fmt"

type foo struct {
    Name string `jsonschema:"pattern=^[^\\-<>%{}\\[\\]&#\\\\\\\"]+$"`
}

func main() {
    // The below forces golines to rewrite the file and in doing so it changes the foo.Name field's "pattern"
    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa := 1
    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb := 1
    if aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa+bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb == 2 {
        fmt.Println("true")
    }
}

Even if the regular expression is invalid, the tool should not try to change it.