From c587b0dfc11a01d42c0c5f9cea07d040f7a10788 Mon Sep 17 00:00:00 2001 From: Nick Snyder Date: Mon, 18 Jan 2021 11:54:34 -0800 Subject: [PATCH] fix escape sequences when extracting string literals (#244) --- v2/goi18n/extract_command.go | 7 ++++--- v2/goi18n/extract_command_test.go | 15 +++++++++++++++ 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/v2/goi18n/extract_command.go b/v2/goi18n/extract_command.go index 528d617c..67afab4a 100644 --- a/v2/goi18n/extract_command.go +++ b/v2/goi18n/extract_command.go @@ -9,6 +9,7 @@ import ( "io/ioutil" "os" "path/filepath" + "strconv" "strings" "github.com/nicksnyder/go-i18n/v2/i18n" @@ -240,9 +241,9 @@ func extractStringLiteral(expr ast.Expr) (string, bool) { if v.Kind != token.STRING { return "", false } - s := v.Value[1 : len(v.Value)-1] - if v.Value[0] == '"' { - s = strings.Replace(s, `\"`, `"`, -1) + s, err := strconv.Unquote(v.Value) + if err != nil { + return "", false } return s, true case *ast.BinaryExpr: diff --git a/v2/goi18n/extract_command_test.go b/v2/goi18n/extract_command_test.go index cab9061a..3cfe581b 100644 --- a/v2/goi18n/extract_command_test.go +++ b/v2/goi18n/extract_command_test.go @@ -33,6 +33,21 @@ func TestExtract(t *testing.T) { } `, }, + { + name: "escape newline", + fileName: "file.go", + file: `package main + + import "github.com/nicksnyder/go-i18n/v2/i18n" + + var hasnewline = &i18n.Message{ + ID: "hasnewline", + Other: "\nfoo\nbar\\", + } + `, + activeFile: []byte(`hasnewline = "\nfoo\nbar\\" +`), + }, { name: "escape", fileName: "file.go",