Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix E103 to handle double braces #136

Merged
merged 1 commit into from Jun 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 23 additions & 4 deletions dennis/linter.py
@@ -1,4 +1,5 @@
import re
import uuid
from collections import namedtuple
from itertools import zip_longest

Expand Down Expand Up @@ -158,16 +159,25 @@ def lint(self, vartok, linted_entry):
return []

malformed_re = re.compile(r"(?:\{[^\}]+(?:\{|$))")
double_open = str(uuid.uuid4())
double_close = str(uuid.uuid4())

for trstr in linted_entry.strs:
if not trstr.msgstr_string:
continue

malformed = malformed_re.findall(trstr.msgstr_string)
malformed = malformed_re.findall(
trstr.msgstr_string.replace("{{", double_open).replace(
"}}", double_close
)
)
if not malformed:
continue

malformed = [item.strip() for item in malformed]
malformed = [
item.strip().replace(double_open, "{{").replace(double_close, "}}")
for item in malformed
]
msgs.append(
LintMessage(
ERROR,
Expand Down Expand Up @@ -195,16 +205,25 @@ def lint(self, vartok, linted_entry):
return []

malformed_re = re.compile(r"(?:(?:^|\})[^\{]*\})")
double_open = str(uuid.uuid4())
double_close = str(uuid.uuid4())

for trstr in linted_entry.strs:
if not trstr.msgstr_string:
continue

malformed = malformed_re.findall(trstr.msgstr_string)
malformed = malformed_re.findall(
trstr.msgstr_string.replace("{{", double_open).replace(
"}}", double_close
)
)
if not malformed:
continue

malformed = [item.strip() for item in malformed]
malformed = [
item.strip().replace(double_open, "{{").replace(double_close, "}}")
for item in malformed
]
msgs.append(
LintMessage(
ERROR,
Expand Down
23 changes: 16 additions & 7 deletions tests/test_linter.py
Expand Up @@ -283,6 +283,18 @@ def test_python_var_missing_right_curly_brace_two_vars(self):
assert msgs[0].code == "E102"
assert msgs[0].msg == 'missing right curly-brace: {0]" excede el tamano de {'

linted_entry = build_linted_entry(
"#: kitsune/questions/templates/questions/question_details.html:14\n"
'msgid "{q} | {product} Support Forum"\n'
'msgstr "{q} | {product}} foo bar"\n'
)

msgs = self.lintrule.lint(self.vartok, linted_entry)
assert len(msgs) == 1
assert msgs[0].kind == "err"
assert msgs[0].code == "E102"
assert msgs[0].msg == "missing right curly-brace: {product}} foo bar"
diox marked this conversation as resolved.
Show resolved Hide resolved

def test_varformat_empty(self):
vartok = VariableTokenizer([])

Expand Down Expand Up @@ -323,17 +335,14 @@ def test_python_var_missing_left_curly_brace(self):
assert msgs[0].code == "E103"
assert msgs[0].msg == "missing left curly-brace: } | product}"

def test_double_braces(self):
linted_entry = build_linted_entry(
"#: kitsune/questions/templates/questions/question_details.html:14\n"
'msgid "{q} | {product} Support Forum"\n'
'msgstr "{q} | {product}} foo bar"\n'
'msgid "This is {{literal}} brace, {0}, and {{another}}."\n'
'msgstr "This is {{literal}} brace, {0}, and {{another}}."\n'
)

msgs = self.lintrule.lint(self.vartok, linted_entry)
assert len(msgs) == 1
assert msgs[0].kind == "err"
assert msgs[0].code == "E103"
assert msgs[0].msg == "missing left curly-brace: }}"
diox marked this conversation as resolved.
Show resolved Hide resolved
assert len(msgs) == 0

def test_varformat_empty(self):
vartok = VariableTokenizer([])
Expand Down