Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions babel/messages/jslexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ def unquote_string(string: str) -> str:
assert string and string[0] == string[-1] and string[0] in '"\'`', (
'string provided is not properly delimited'
)
if string[0] == '`':
# ECMAScript normalizes physical CRLF and CR in template literals.
string = string.replace('\r\n', '\n').replace('\r', '\n')
string = line_join_re.sub('\\1', string[1:-1])
result: list[str] = []
add = result.append
Expand Down
9 changes: 9 additions & 0 deletions tests/messages/test_js_extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,3 +211,12 @@ def test_template_string_with_literal_braces() -> None:
)
messages = list(extract.extract("javascript", buf, {"gettext": None}, [], {"parse_template_string": True}))
assert messages == []


@pytest.mark.parametrize('line_ending', ['\n', '\r\n', '\r'])
def test_extract_normalizes_template_line_endings(line_ending):
source = f"_('escaped\\r\\n');{line_ending}_(`first{line_ending}second`);"
assert list(extract.extract('javascript', BytesIO(source.encode('utf-8')))) == [
(1, 'escaped\r\n', [], None),
(2, 'first\nsecond', [], None),
]