Skip to content

Commit 1942e74

Browse files
committed
Extract static string when parse_template_string is set
The Javascript extractor currently does not extract static strings defined in template string quotes, if the `parse_template_string` option is enabled. This commit fixes that so static template strings are also extracted.
1 parent dc6908f commit 1942e74

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

babel/messages/extract.py

+10-2
Original file line numberDiff line numberDiff line change
@@ -785,7 +785,10 @@ def extract_javascript(
785785
and (not last_token or last_token.type != 'name' or last_token.value not in keywords)
786786
and token.type == 'template_string'
787787
):
788-
yield from parse_template_string(token.value, keywords, comment_tags, options, token.lineno)
788+
keyword = ""
789+
if function_stack and function_stack[-1].function_name in keywords:
790+
keyword = function_stack[-1].function_name
791+
yield from parse_template_string(token.value, keywords, comment_tags, options, token.lineno, keyword)
789792

790793
elif token.type == 'operator' and token.value == '(':
791794
if last_token.type == 'name':
@@ -904,6 +907,7 @@ def parse_template_string(
904907
comment_tags: Collection[str],
905908
options: _JSOptions,
906909
lineno: int = 1,
910+
keyword: str = "",
907911
) -> Generator[_ExtractionResult, None, None]:
908912
"""Parse JavaScript template string.
909913
@@ -925,10 +929,12 @@ def parse_template_string(
925929
inside_str = character
926930
elif inside_str == character and prev_character != r'\\':
927931
inside_str = False
928-
if level:
932+
if level or keyword:
929933
expression_contents += character
930934
if not inside_str:
931935
if character == '{' and prev_character == '$':
936+
if keyword:
937+
break
932938
level += 1
933939
elif level and character == '}':
934940
level -= 1
@@ -939,6 +945,8 @@ def parse_template_string(
939945
lineno += len(line_re.findall(expression_contents))
940946
expression_contents = ''
941947
prev_character = character
948+
if keyword:
949+
yield (lineno, keyword, expression_contents, [])
942950

943951

944952
_BUILTIN_EXTRACTORS = {

tests/messages/test_js_extract.py

+8
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,14 @@ def test_template_string_standard_usage():
144144

145145
assert messages == [(1, 'Very template, wow', [], None)]
146146

147+
def test_template_string_static_usage():
148+
buf = BytesIO(b"msg1 = gettext(`Very template, wow`)")
149+
messages = list(
150+
extract.extract('javascript', buf, {"gettext": None}, [], {'parse_template_string': True}),
151+
)
152+
153+
assert messages == [(1, 'Very template, wow', [], None)]
154+
147155

148156
def test_template_string_tag_usage():
149157
buf = BytesIO(b"function() { if(foo) msg1 = i18n`Tag template, wow`; }")

0 commit comments

Comments
 (0)