From 5b09b64982452bea56da65674700d0928ddfecc6 Mon Sep 17 00:00:00 2001 From: Aarni Koskela Date: Mon, 18 Jan 2016 19:07:17 +0200 Subject: [PATCH] JavaScript: support tags as keywords for template strings Refs #329 --- babel/messages/extract.py | 12 +++++++++++- tests/messages/test_js_extract.py | 9 +++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/babel/messages/extract.py b/babel/messages/extract.py index 2e5053759..3706df84d 100644 --- a/babel/messages/extract.py +++ b/babel/messages/extract.py @@ -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 diff --git a/tests/messages/test_js_extract.py b/tests/messages/test_js_extract.py index f81931900..35322ea16 100644 --- a/tests/messages/test_js_extract.py +++ b/tests/messages/test_js_extract.py @@ -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)]