feat(format): add runtime semantic-equality safety gate
This commit is contained in:
@@ -63,6 +63,10 @@ func cmdFmt(args []string, stdin io.Reader, stdout, stderr io.Writer) int {
|
||||
return 2
|
||||
}
|
||||
out := format.File(parser.Parse(string(src)), st)
|
||||
if !format.SemanticallyEqual(string(src), out) {
|
||||
_, _ = fmt.Fprintln(stderr, "pgtidy: refusing to format stdin: formatter safety check failed (output would change code content)")
|
||||
return 2
|
||||
}
|
||||
switch {
|
||||
case check:
|
||||
if out != string(src) {
|
||||
@@ -86,6 +90,11 @@ func cmdFmt(args []string, stdin io.Reader, stdout, stderr io.Writer) int {
|
||||
continue
|
||||
}
|
||||
out := format.File(parser.Parse(string(src)), st)
|
||||
if !format.SemanticallyEqual(string(src), out) {
|
||||
_, _ = fmt.Fprintf(stderr, "pgtidy: refusing to format %s: formatter safety check failed (output would change code content)\n", path)
|
||||
exit = 2
|
||||
continue
|
||||
}
|
||||
changed := out != string(src)
|
||||
if changed {
|
||||
anyDiff = true
|
||||
|
||||
Reference in New Issue
Block a user