feat(format): add runtime semantic-equality safety gate
This commit is contained in:
@@ -7,7 +7,6 @@ import (
|
||||
"testing"
|
||||
|
||||
"git.warky.dev/wdevs/pgtidy/pkg/config"
|
||||
"git.warky.dev/wdevs/pgtidy/pkg/lexer"
|
||||
"git.warky.dev/wdevs/pgtidy/pkg/parser"
|
||||
)
|
||||
|
||||
@@ -199,48 +198,7 @@ func TestCorpusIdempotentAndSafe(t *testing.T) {
|
||||
t.Logf("formatted %d corpus files (idempotent + semantically equal)", seen)
|
||||
}
|
||||
|
||||
// semanticallyEqual compares the non-trivia token streams of two sources,
|
||||
// treating unquoted identifiers/keywords case-insensitively and everything
|
||||
// else (strings, numbers, operators, punctuation) exactly. Dollar-quoted body
|
||||
// tokens are compared recursively so body whitespace normalization does not
|
||||
// trigger a false failure.
|
||||
// semanticallyEqual is a test-local alias for the exported safety check.
|
||||
func semanticallyEqual(a, b string) bool {
|
||||
ta := significant(a)
|
||||
tb := significant(b)
|
||||
if len(ta) != len(tb) {
|
||||
return false
|
||||
}
|
||||
for i := range ta {
|
||||
if ta[i].Kind != tb[i].Kind {
|
||||
return false
|
||||
}
|
||||
switch ta[i].Kind {
|
||||
case lexer.Ident:
|
||||
if !strings.EqualFold(ta[i].Text, tb[i].Text) {
|
||||
return false
|
||||
}
|
||||
case lexer.DollarString:
|
||||
_, innerA, _, okA := splitDollarQuote(ta[i].Text)
|
||||
_, innerB, _, okB := splitDollarQuote(tb[i].Text)
|
||||
if okA != okB || (okA && !semanticallyEqual(innerA, innerB)) {
|
||||
return false
|
||||
}
|
||||
default:
|
||||
if ta[i].Text != tb[i].Text {
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func significant(src string) []lexer.Token {
|
||||
var out []lexer.Token
|
||||
for _, t := range lexer.Lex(src) {
|
||||
if t.Kind == lexer.EOF || t.IsTrivia() {
|
||||
continue
|
||||
}
|
||||
out = append(out, t)
|
||||
}
|
||||
return out
|
||||
return SemanticallyEqual(a, b)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user