From cb9f9e5cfbc530f4eddd1e0613e9f7f72dd3e17b Mon Sep 17 00:00:00 2001 From: Andrew Williamson Date: Wed, 22 Jun 2022 13:03:19 +0100 Subject: [PATCH] fix E103 to handle double braces (#136) --- dennis/linter.py | 27 +++++++++++++++++++++++---- tests/test_linter.py | 23 ++++++++++++++++------- 2 files changed, 39 insertions(+), 11 deletions(-) diff --git a/dennis/linter.py b/dennis/linter.py index 350b6db..4efa497 100644 --- a/dennis/linter.py +++ b/dennis/linter.py @@ -1,4 +1,5 @@ import re +import uuid from collections import namedtuple from itertools import zip_longest @@ -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, @@ -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, diff --git a/tests/test_linter.py b/tests/test_linter.py index b5f44c5..0d70192 100644 --- a/tests/test_linter.py +++ b/tests/test_linter.py @@ -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([]) @@ -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([])