Skip to content

Commit

Permalink
Add a nil pointer check before dereferencing an identifier's Obj (nic…
Browse files Browse the repository at this point in the history
…ksnyder#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.
  • Loading branch information
patgrasso authored Feb 29, 2020
1 parent 26334ab commit 949485d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
3 changes: 3 additions & 0 deletions v2/goi18n/extract_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down
12 changes: 12 additions & 0 deletions v2/goi18n/extract_command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 949485d

Please sign in to comment.