From 797aa61b1a363c52584744e9ed374cb1d9b83b3a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Marco=20Trevisan=20=28Trevi=C3=B1o=29?= <mail@3v1n0.net>
Date: Thu, 6 Sep 2018 22:59:35 +0200
Subject: [PATCH 1/2] javascript: add support for ES6 template strings

Strings can be defined using backtick (`) and can be used multi-line.

https://savannah.gnu.org/bugs/?50920
---
 gettext-tools/src/x-javascript.c          |  8 ++-
 gettext-tools/tests/Makefile.am           |  1 +
 gettext-tools/tests/xgettext-javascript-7 | 77 +++++++++++++++++++++++
 3 files changed, 83 insertions(+), 3 deletions(-)
 create mode 100755 gettext-tools/tests/xgettext-javascript-7

diff --git a/gettext-tools/src/x-javascript.c b/gettext-tools/src/x-javascript.c
index 2db00abe6..dd1912a48 100644
--- a/gettext-tools/src/x-javascript.c
+++ b/gettext-tools/src/x-javascript.c
@@ -731,7 +731,7 @@ enum token_type_ty
   token_type_regexp,            /* /.../ */
   token_type_operator,          /* - * / % . < > = ~ ! | & ? : ^ */
   token_type_equal,             /* = */
-  token_type_string,            /* "abc", 'abc' */
+  token_type_string,            /* "abc", 'abc', `abc` */
   token_type_keyword,           /* return, else */
   token_type_symbol,            /* symbol, number */
   token_type_other              /* misc. operator */
@@ -789,7 +789,7 @@ phase7_getuc (int quote_char)
       if (c == quote_char)
         return P7_STRING_END;
 
-      if (c == '\n')
+      if (quote_char != '`' && c == '\n')
         {
           phase2_ungetc (c);
           error_with_progname = false;
@@ -1194,7 +1194,9 @@ phase5_get (token_ty *tp)
             struct mixed_string_buffer *bp;
             int quote_char;
 
-            case '"': case '\'':
+            case '"':
+            case '\'':
+            case '`':
               quote_char = c;
               lexical_context = lc_string;
               /* Start accumulating the string.  */
diff --git a/gettext-tools/tests/Makefile.am b/gettext-tools/tests/Makefile.am
index 98d5c4b02..d65b807fc 100644
--- a/gettext-tools/tests/Makefile.am
+++ b/gettext-tools/tests/Makefile.am
@@ -112,6 +112,7 @@ TESTS = gettext-1 gettext-2 gettext-3 gettext-4 gettext-5 gettext-6 gettext-7 \
 	xgettext-lua-1 xgettext-lua-2 \
 	xgettext-javascript-1 xgettext-javascript-2 xgettext-javascript-3 \
 	xgettext-javascript-4 xgettext-javascript-5 xgettext-javascript-6 \
+	xgettext-javascript-7 \
 	xgettext-vala-1 \
 	xgettext-gsettings-1 \
 	xgettext-desktop-1 \
diff --git a/gettext-tools/tests/xgettext-javascript-7 b/gettext-tools/tests/xgettext-javascript-7
new file mode 100755
index 000000000..d0d4e320a
--- /dev/null
+++ b/gettext-tools/tests/xgettext-javascript-7
@@ -0,0 +1,77 @@
+#!/bin/sh
+. "${srcdir=.}/init.sh"; path_prepend_ . ../src
+
+# Test of JavaScript ES6 template strings support.
+
+cat <<\EOF > xg-js-7.js
+let s1 = `Template string`
+let t1 = _(`Expected "translation" string #1`);
+let t2 = _(`Expected 'translation' string #2`);
+let s2 = `Template \`escaped string\``;
+let t3 = _(`Expected \`translation\` string #3`);
+let s3 = `Template
+multi-line
+string`;
+let t4 = _(`Expected
+multi-line
+translation #4`)
+let s4 = `This is a` + `good
+template string ` +
+`joined ` + "with " + 'another '
+let t5 = _(`Expected ` + `multi
+line template string ` +
+"translation " + '#5')
+EOF
+
+: ${XGETTEXT=xgettext}
+${XGETTEXT} --add-comments --no-location -o xg-js-7.tmp xg-js-7.js 2>xg-js-7.err
+test $? = 0 || { cat xg-js-7.err; Exit 1; }
+# Don't simplify this to "grep ... < xg-js-7.tmp", otherwise OpenBSD 4.0 grep
+# only outputs "Binary file (standard input) matches".
+cat xg-js-7.tmp | grep -v 'POT-Creation-Date' | LC_ALL=C tr -d '\r' > xg-js-7.pot
+
+cat <<\EOF > xg-js-7.ok
+# SOME DESCRIPTIVE TITLE.
+# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
+# This file is distributed under the same license as the PACKAGE package.
+# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
+#
+#, fuzzy
+msgid ""
+msgstr ""
+"Project-Id-Version: PACKAGE VERSION\n"
+"Report-Msgid-Bugs-To: \n"
+"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
+"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
+"Language-Team: LANGUAGE <LL@li.org>\n"
+"Language: \n"
+"MIME-Version: 1.0\n"
+"Content-Type: text/plain; charset=CHARSET\n"
+"Content-Transfer-Encoding: 8bit\n"
+
+msgid "Expected \"translation\" string #1"
+msgstr ""
+
+msgid "Expected 'translation' string #2"
+msgstr ""
+
+msgid "Expected `translation` string #3"
+msgstr ""
+
+msgid ""
+"Expected\n"
+"multi-line\n"
+"translation #4"
+msgstr ""
+
+msgid ""
+"Expected multi\n"
+"line template string translation #5"
+msgstr ""
+EOF
+
+: ${DIFF=diff}
+${DIFF} xg-js-7.ok xg-js-7.pot
+result=$?
+
+exit $result
-- 
2.17.1

