Export translation message IDs for shared presentation

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Storm DragonandClaude Opus 5.5 committed 2026-11-15 14:41:02 -05:00
1 parent 03896de84e
commit 8a61bfff04
11 files changed
+335 -19

No files matched your search

+168
View File
@@ -0,0 +1,168 @@
// Copyright (c) 2026 Storm Dragon
// SPDX-License-Identifier: GPL-3.0-or-later
// Package messagecheck verifies that a package's exported message ID list
// matches the English messages its source actually presents.
package messagecheck
import (
"go/ast"
"go/parser"
"go/token"
"io/fs"
"path/filepath"
"slices"
"strconv"
"strings"
"testing"
)
// Collect returns message IDs found in non-test Go files under root:
// literal arguments of Messages T/Format calls, literal arguments of Mark,
// and literal Label/Title fields of composite values not marked Literal.
func Collect(root string) ([]string, error) {
found := map[string]bool{}
err := filepath.WalkDir(root, func(path string, entry fs.DirEntry, err error) error {
if err != nil || entry.IsDir() || !strings.HasSuffix(path, ".go") || strings.HasSuffix(path, "_test.go") {
return err
}
file, err := parser.ParseFile(token.NewFileSet(), path, nil, 0)
if err != nil {
return err
}
// Elided element types, as in []Control{{...}}, inherit the outer element type.
elided := map[*ast.CompositeLit]string{}
ast.Inspect(file, func(node ast.Node) bool {
switch n := node.(type) {
case *ast.CallExpr:
if len(n.Args) > 0 && is_message_call(n.Fun) {
add(found, n.Args[0])
}
case *ast.CompositeLit:
typeName, ok := elided[n]
if n.Type != nil {
typeName = type_name(n.Type)
} else if !ok {
return true
}
for _, element := range n.Elts {
if pair, ok := element.(*ast.KeyValueExpr); ok {
element = pair.Value
}
if inner, ok := element.(*ast.CompositeLit); ok && inner.Type == nil {
elided[inner] = element_type_name(n.Type, typeName)
}
}
if !uiTypes[typeName] {
return true
}
fields := map[string]ast.Expr{}
for _, element := range n.Elts {
if pair, ok := element.(*ast.KeyValueExpr); ok {
if key, ok := pair.Key.(*ast.Ident); ok {
fields[key.Name] = pair.Value
}
}
}
if literal, ok := fields["Literal"].(*ast.Ident); ok && literal.Name == "true" {
return true
}
for _, name := range []string{"Label", "Title"} {
if value, ok := fields[name]; ok {
add(found, value)
}
}
}
return true
})
return nil
})
ids := make([]string, 0, len(found))
for id := range found {
ids = append(ids, id)
}
slices.Sort(ids)
return ids, err
}
// uiTypes are the presentation values whose Label and Title are message IDs.
var uiTypes = map[string]bool{"Form": true, "Tab": true, "Control": true, "Choice": true, "Option": true}
func type_name(expr ast.Expr) string {
switch t := expr.(type) {
case *ast.Ident:
return t.Name
case *ast.SelectorExpr:
return t.Sel.Name
case *ast.StarExpr:
return type_name(t.X)
}
return ""
}
// element_type_name names the type of elided values inside a composite literal.
func element_type_name(outer ast.Expr, inherited string) string {
switch t := outer.(type) {
case *ast.ArrayType:
return type_name(t.Elt)
case *ast.MapType:
return type_name(t.Value)
case nil:
return inherited
}
return ""
}
func is_message_call(fun ast.Expr) bool {
switch f := fun.(type) {
case *ast.Ident:
return f.Name == "Mark"
case *ast.SelectorExpr:
if f.Sel.Name == "Mark" {
return true
}
if f.Sel.Name != "T" && f.Sel.Name != "Format" {
return false
}
owner := f.X
if selector, ok := owner.(*ast.SelectorExpr); ok {
owner = selector.Sel
}
ident, ok := owner.(*ast.Ident)
return ok && strings.EqualFold(ident.Name, "messages")
}
return false
}
func add(found map[string]bool, expr ast.Expr) {
if literal, ok := expr.(*ast.BasicLit); ok && literal.Kind == token.STRING {
if text, err := strconv.Unquote(literal.Value); err == nil && text != "" {
found[text] = true
}
}
}
// Match fails t unless ids lists exactly the messages Collect finds under root.
func Match(t *testing.T, root string, ids []string) {
t.Helper()
collected, err := Collect(root)
if err != nil {
t.Fatal(err)
}
listed := map[string]bool{}
for _, id := range ids {
if id == "" || listed[id] {
t.Errorf("empty or duplicate message ID %q", id)
}
listed[id] = true
}
for _, id := range collected {
if !listed[id] {
t.Errorf("message %q is presented but missing from MessageIDs", id)
}
delete(listed, id)
}
for id := range listed {
t.Errorf("MessageIDs lists %q, which the source no longer presents", id)
}
}
@@ -0,0 +1,45 @@
// Copyright (c) 2026 Storm Dragon
// SPDX-License-Identifier: GPL-3.0-or-later
package messagecheck
import (
"os"
"path/filepath"
"slices"
"testing"
)
func TestCollectFindsOnlyPresentedMessages(t *testing.T) {
root := t.TempDir()
source := `package sample
func render(s session, choice Choice) {
s.messages.T("Translated")
s.messages.Format("Formatted {0}", "value")
m.Messages.T("Selector owner")
clock.Format("2006-01-02")
state := Mark("Marked")
_ = message.Mark("Qualified mark")
_ = Form{Title: "Title", Tabs: []Tab{{Label: "Elided tab", Controls: []Control{{Label: "Nested"}}}}}
_ = ui.Choice{Label: "Literal name", Literal: true}
_ = []*ui.Choice{{Label: "Pointer choice"}}
_ = Driver{Label: "Product name"}
_ = s.messages.T(state)
}
`
if err := os.WriteFile(filepath.Join(root, "sample.go"), []byte(source), 0o644); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(root, "sample_test.go"), []byte("package sample\n\nvar _ = Mark(\"Test only\")\n"), 0o644); err != nil {
t.Fatal(err)
}
got, err := Collect(root)
if err != nil {
t.Fatal(err)
}
want := []string{"Elided tab", "Formatted {0}", "Marked", "Nested", "Pointer choice", "Qualified mark", "Selector owner", "Title", "Translated"}
if !slices.Equal(got, want) {
t.Fatalf("got %q, want %q", got, want)
}
}