Skip to content

Commit

Permalink
Extract static string when parse_template_string is set
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
dylankiss committed Jan 21, 2025
1 parent dc6908f commit 1942e74
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
12 changes: 10 additions & 2 deletions babel/messages/extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -785,7 +785,10 @@ def extract_javascript(
and (not last_token or last_token.type != 'name' or last_token.value not in keywords)
and token.type == 'template_string'
):
yield from parse_template_string(token.value, keywords, comment_tags, options, token.lineno)
keyword = ""
if function_stack and function_stack[-1].function_name in keywords:
keyword = function_stack[-1].function_name
yield from parse_template_string(token.value, keywords, comment_tags, options, token.lineno, keyword)

elif token.type == 'operator' and token.value == '(':
if last_token.type == 'name':
Expand Down Expand Up @@ -904,6 +907,7 @@ def parse_template_string(
comment_tags: Collection[str],
options: _JSOptions,
lineno: int = 1,
keyword: str = "",
) -> Generator[_ExtractionResult, None, None]:
"""Parse JavaScript template string.
Expand All @@ -925,10 +929,12 @@ def parse_template_string(
inside_str = character
elif inside_str == character and prev_character != r'\\':
inside_str = False
if level:
if level or keyword:
expression_contents += character
if not inside_str:
if character == '{' and prev_character == '$':
if keyword:
break

Check warning on line 937 in babel/messages/extract.py

View check run for this annotation

Codecov / codecov/patch

babel/messages/extract.py#L937

Added line #L937 was not covered by tests
level += 1
elif level and character == '}':
level -= 1
Expand All @@ -939,6 +945,8 @@ def parse_template_string(
lineno += len(line_re.findall(expression_contents))
expression_contents = ''
prev_character = character
if keyword:
yield (lineno, keyword, expression_contents, [])


_BUILTIN_EXTRACTORS = {
Expand Down
8 changes: 8 additions & 0 deletions tests/messages/test_js_extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,14 @@ def test_template_string_standard_usage():

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

def test_template_string_static_usage():
buf = BytesIO(b"msg1 = gettext(`Very template, wow`)")
messages = list(
extract.extract('javascript', buf, {"gettext": None}, [], {'parse_template_string': True}),
)

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


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

0 comments on commit 1942e74

Please sign in to comment.