]> git.ipfire.org Git - thirdparty/babel.git/commitdiff
JavaScript: support tags as keywords for template strings 332/head
authorAarni Koskela <akx@iki.fi>
Mon, 18 Jan 2016 17:07:17 +0000 (19:07 +0200)
committerAarni Koskela <akx@iki.fi>
Sun, 7 Feb 2016 11:39:17 +0000 (13:39 +0200)
Refs #329

babel/messages/extract.py
tests/messages/test_js_extract.py

index 2e5053759fd4e23441ec2de8379d3e2bd3d94855..3706df84dd24a92ec14d66e156c0506ccd1a689d 100644 (file)
@@ -511,7 +511,7 @@ def extract_javascript(fileobj, keywords, comment_tags, options):
                     * `template_string` -- set to false to disable ES6
                                            template string support.
     """
-    from babel.messages.jslexer import tokenize, unquote_string
+    from babel.messages.jslexer import Token, tokenize, unquote_string
     funcname = message_lineno = None
     messages = []
     last_argument = None
@@ -528,6 +528,16 @@ def extract_javascript(fileobj, keywords, comment_tags, options):
         template_string=options.get("template_string", True),
         dotted=dotted
     ):
+        if (  # Turn keyword`foo` expressions into keyword("foo") calls:
+            funcname and  # have a keyword...
+            (last_token and last_token.type == 'name') and  # we've seen nothing after the keyword...
+            token.type == 'template_string'  # this is a template string
+        ):
+            message_lineno = token.lineno
+            messages = [unquote_string(token.value)]
+            call_stack = 0
+            token = Token('operator', ')', token.lineno)
+
         if token.type == 'operator' and token.value == '(':
             if funcname:
                 message_lineno = token.lineno
index f819319002a5dcf9bd35814dddfea024bada8c1c..35322ea16b622158a875bfaa4537f9a7b0c132fd 100644 (file)
@@ -140,3 +140,12 @@ def test_template_string_standard_usage():
     )
 
     assert messages == [(1, 'Very template, wow', [], None)]
+
+
+def test_template_string_tag_usage():
+    buf = BytesIO(b"function() { if(foo) msg1 = i18n`Tag template, wow`; }")
+    messages = list(
+        extract.extract('javascript', buf, {"i18n": None}, [], {})
+    )
+
+    assert messages == [(1, 'Tag template, wow', [], None)]