Skip to content

Commit

Permalink
Add warning for any empty Fluent entries (#3121)
Browse files Browse the repository at this point in the history
  • Loading branch information
eemeli authored Feb 23, 2024
1 parent 2e9ded2 commit e007c62
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 4 deletions.
15 changes: 11 additions & 4 deletions pontoon/checks/libraries/pontoon_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,27 @@ def get_max_length(comment):

class IsEmptyVisitor(Visitor):
def __init__(self):
self.is_empty = True
self.is_empty = False
self.is_pattern_empty = True

def visit_Pattern(self, node):
self.is_pattern_empty = True
self.visit(node.elements)
if self.is_pattern_empty:
self.is_empty = True

def visit_Placeable(self, node):
if isinstance(node.expression, ast.Literal):
if node.expression.parse()["value"]:
self.is_empty = False
self.is_pattern_empty = False
elif isinstance(node.expression, ast.SelectExpression):
self.generic_visit(node.expression)
else:
self.is_empty = False
self.is_pattern_empty = False

def visit_TextElement(self, node):
if node.value:
self.is_empty = False
self.is_pattern_empty = False


def run_checks(entity, original, string):
Expand Down
50 changes: 50 additions & 0 deletions pontoon/checks/tests/test_pontoon_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,11 @@ def test_empty_translations(get_entity_mock):
get_entity_mock("ftl", string="key = value"), "", 'key = { "" }'
) == {"pndbWarnings": ["Empty translation"]}

assert (
run_checks(get_entity_mock("ftl", string="key = value"), "", 'key = { "x" }')
== {}
)

assert (
run_checks(
get_entity_mock("ftl", string="key =\n .attr = value"),
Expand All @@ -145,6 +150,51 @@ def test_empty_translations(get_entity_mock):
== {"pndbWarnings": ["Empty translation"]}
)

assert (
run_checks(
get_entity_mock("ftl", string="key =\n .attr = value"),
"",
"""key =
{ $var ->
[a] { "x" }
*[b] { "y" }
}
.attr = { "" }
""",
)
== {"pndbWarnings": ["Empty translation"]}
)

assert (
run_checks(
get_entity_mock("ftl", string="key =\n .attr = value"),
"",
"""key =
{ $var ->
[a] { "x" }
*[b] { "" }
}
.attr = { "y" }
""",
)
== {"pndbWarnings": ["Empty translation"]}
)

assert (
run_checks(
get_entity_mock("ftl", string="key =\n .attr = value"),
"",
"""key =
{ $var ->
[a] { "x" }
*[b] { "y" }
}
.attr = { "z" }
""",
)
== {}
)


def test_lang_newlines(get_entity_mock):
"""Newlines aren't allowed in lang files"""
Expand Down

0 comments on commit e007c62

Please sign in to comment.