Skip to content

Commit

Permalink
fix E103 to handle double braces (#136)
Browse files Browse the repository at this point in the history
  • Loading branch information
eviljeff committed Jun 22, 2022
1 parent c90b8cd commit cb9f9e5
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 11 deletions.
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"

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: }}"
assert len(msgs) == 0

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

0 comments on commit cb9f9e5

Please sign in to comment.