From b8c9a57c3912f3d3c0c5da6da34fbdd2a57d83c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20B=C3=B6hmke?= Date: Sun, 23 Feb 2020 10:00:43 +0100 Subject: [PATCH] catch unsupported function calls --- cli/xgotext/fixtures/main.go | 4 ++++ cli/xgotext/parser/golang.go | 16 ++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/cli/xgotext/fixtures/main.go b/cli/xgotext/fixtures/main.go index ffbf91a..19b0b0a 100644 --- a/cli/xgotext/fixtures/main.go +++ b/cli/xgotext/fixtures/main.go @@ -35,6 +35,10 @@ func main() { // same as before fmt.Println(gotext.Get("My text on 'domain-name' domain")) + // unsupported function call + trStr := "some string to translate" + fmt.Println(gotext.Get(trStr)) + // same with alias package name fmt.Println(alias.Get("alias call")) diff --git a/cli/xgotext/parser/golang.go b/cli/xgotext/parser/golang.go index 0c33d36..ec535dc 100644 --- a/cli/xgotext/parser/golang.go +++ b/cli/xgotext/parser/golang.go @@ -240,14 +240,30 @@ func (g *GoFile) parseGetter(def GetterDef, args []*ast.BasicLit, pos string) { domain, _ = strconv.Unquote(args[def.Domain].Value) } + // only handle function calls with strings as ID + if args[def.Id] == nil || args[def.Id].Kind != token.STRING { + log.Printf("ERR: Unsupported call at %s (ID not a string)", pos) + return + } + trans := Translation{ MsgId: args[def.Id].Value, SourceLocations: []string{pos}, } if def.Plural > 0 { + // plural ID must be a string + if args[def.Plural] == nil || args[def.Plural].Kind != token.STRING { + log.Printf("ERR: Unsupported call at %s (Plural not a string)", pos) + return + } trans.MsgIdPlural = args[def.Plural].Value } if def.Context > 0 { + // Context must be a string + if args[def.Context] == nil || args[def.Context].Kind != token.STRING { + log.Printf("ERR: Unsupported call at %s (Context not a string)", pos) + return + } trans.Context = args[def.Context].Value }