:param options: a dictionary of additional options (optional)
Supported options are:
* `jsx` -- set to false to disable JSX/E4X support.
+ * `template_string` -- set to false to disable ES6
+ template string support.
"""
from babel.messages.jslexer import tokenize, unquote_string
funcname = message_lineno = None
for token in tokenize(
fileobj.read().decode(encoding),
jsx=options.get("jsx", True),
+ template_string=options.get("template_string", True),
dotted=dotted
):
if token.type == 'operator' and token.value == '(':
messages = []
call_stack = -1
- elif token.type == 'string':
+ elif token.type in ('string', 'template_string'):
new_value = unquote_string(token.value)
if concatenate_next:
last_argument = (last_argument or '') + new_value
)''')),
('jsx_tag', re.compile(r'<(?:/?)\w+.+?>', re.I)), # May be mangled in `get_rules`
('operator', re.compile(r'(%s)' % '|'.join(map(re.escape, operators)))),
+ ('template_string', re.compile(r'''`(?:[^`\\]*(?:\\.[^`\\]*)*)`''', re.UNICODE)),
('string', re.compile(r'''(?xs)(
'(?:[^'\\]*(?:\\.[^'\\]*)*)' |
"(?:[^"\\]*(?:\\.[^"\\]*)*)"
]
-def get_rules(jsx, dotted):
+def get_rules(jsx, dotted, template_string):
"""
Get a tokenization rule list given the passed syntax options.
for token_type, rule in _rules:
if not jsx and token_type and 'jsx' in token_type:
continue
+ if not template_string and token_type == 'template_string':
+ continue
if token_type == 'dotted_name':
if not dotted:
continue
def unquote_string(string):
"""Unquote a string with JavaScript rules. The string has to start with
- string delimiters (``'`` or ``"``.)
+ string delimiters (``'``, ``"`` or the back-tick/grave accent (for template strings).)
"""
- assert string and string[0] == string[-1] and string[0] in '"\'', \
+ assert string and string[0] == string[-1] and string[0] in '"\'`', \
'string provided is not properly delimited'
string = line_join_re.sub('\\1', string[1:-1])
result = []
return u''.join(result)
-def tokenize(source, jsx=True, dotted=True):
+def tokenize(source, jsx=True, dotted=True, template_string=True):
"""
Tokenize JavaScript/JSX source. Returns a generator of tokens.
:param jsx: Enable (limited) JSX parsing.
:param dotted: Read dotted names as single name token.
+ :param template_string: Support ES6 template strings
"""
may_divide = False
pos = 0
lineno = 1
end = len(source)
- rules = get_rules(jsx=jsx, dotted=dotted)
+ rules = get_rules(jsx=jsx, dotted=dotted, template_string=template_string)
while pos < end:
# handle regular rules first
)
assert messages == [(1, 'Insert coin to continue', [], None)]
+
+
+def test_template_string_standard_usage():
+ buf = BytesIO(b"msg1 = gettext(`Very template, wow`)")
+ messages = list(
+ extract.extract('javascript', buf, {"gettext": None}, [], {})
+ )
+
+ assert messages == [(1, 'Very template, wow', [], None)]