diff options
author | Pat Grasso <patgra123@gmail.com> | 2020-02-29 05:24:09 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-02-29 05:24:09 +0300 |
commit | 949485dac070df78fc9b06c9758facf6c41a6470 (patch) | |
tree | 868a3610d0f498851e2009630dda87c0cb890b8e | |
parent | 26334ab1db070c2801ba26550d7d6238f6c9da10 (diff) |
Add a nil pointer check before dereferencing an identifier's Obj (#211)
When walking the AST during extraction, if an identifier in a composite
literal is encountered that does not define in that file, the `Obj`
field of the `ast.Ident` will be nil.
Rather than panicking in this case, have the literal string extraction
function return false (no string found).
Also, add a test case representing this scenario.
-rw-r--r-- | v2/goi18n/extract_command.go | 3 | ||||
-rw-r--r-- | v2/goi18n/extract_command_test.go | 12 |
2 files changed, 15 insertions, 0 deletions
diff --git a/v2/goi18n/extract_command.go b/v2/goi18n/extract_command.go index 9f46160..cee9be7 100644 --- a/v2/goi18n/extract_command.go +++ b/v2/goi18n/extract_command.go @@ -259,6 +259,9 @@ func extractStringLiteral(expr ast.Expr) (string, bool) { } return x + y, true case *ast.Ident: + if v.Obj == nil { + return "", false + } switch z := v.Obj.Decl.(type) { case *ast.ValueSpec: s, ok := extractStringLiteral(z.Values[0]) diff --git a/v2/goi18n/extract_command_test.go b/v2/goi18n/extract_command_test.go index d5a7efd..cab9061 100644 --- a/v2/goi18n/extract_command_test.go +++ b/v2/goi18n/extract_command_test.go @@ -202,6 +202,18 @@ zero = "Zero translation" activeFile: []byte(`ConstantID = "ID is a constant" `), }, + { + name: "undefined identifier in composite lit", + fileName: "file.go", + file: `package main + + import "github.com/nicksnyder/go-i18n/v2/i18n" + + var m = &i18n.LocalizeConfig{ + Funcs: Funcs, + } + `, + }, } for _, test := range tests { |