From 9b1d02140eda31c1ac4e9e650a8164afd3402791 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 a559da382..8349235b9 100644 --- a/babel/messages/extract.py +++ b/babel/messages/extract.py @@ -510,7 +510,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 @@ -527,6 +527,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)]