-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Add ObjCFormatCheck for Objective-C format strings #18590
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
base: main
Are you sure you want to change the base?
Changes from all commits
0564fb1
2894743
7252ae1
6f90839
239480c
3e77511
1087b52
f4c077b
f170239
5b5e8ec
5245314
f693f69
a07730a
d2f800a
939315a
022f4d5
be7f7ff
f07a9ab
4ee0727
8812956
6d4c462
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -92,6 +92,20 @@ | |
| re.VERBOSE, | ||
| ) | ||
|
|
||
| OBJC_PRINTF_MATCH = re.compile( | ||
| r""" | ||
| %( # initial % | ||
| (?:(?P<ord>\d+)\$)? # variable order, like %1$@ | ||
| (?P<fullvar> | ||
| [ +#'-]* # flags | ||
| (?:\d+)? # width | ||
| (?:\.\d+)? # precision | ||
| (hh|h|l|ll)? # length formatting | ||
| (?P<type>[a-zA-Z@%]) # type (%s, %d, %@ etc.) | ||
| |) | ||
| )""", | ||
| re.VERBOSE, | ||
| ) | ||
| # index, width and precision can be '*', in which case their value | ||
| # will be read from the next element in the Args array | ||
| PASCAL_FORMAT_MATCH = re.compile( | ||
|
|
@@ -349,6 +363,11 @@ | |
| name_format_is_position_based, | ||
| extract_string_simple, | ||
| ), | ||
| "objc-format": ( | ||
| OBJC_PRINTF_MATCH, | ||
| c_format_is_position_based, | ||
| extract_string_simple, | ||
| ), | ||
| } | ||
|
|
||
|
|
||
|
|
@@ -517,11 +536,11 @@ | |
| errors: list[StrOrPromise] = [] | ||
|
|
||
| # Merge plurals | ||
| results: MissingExtraDict = defaultdict(list) | ||
| for result in checks: | ||
| if result: | ||
| for key, value in result.items(): | ||
| results[key].extend(value) | ||
| if results: | ||
| errors.extend(self.format_result(results)) | ||
| if errors: | ||
|
|
@@ -608,6 +627,15 @@ | |
| description = gettext_lazy("C format string does not match source.") | ||
|
|
||
|
|
||
| class ObjCFormatCheck(BasePrintfCheck): | ||
| """Check for Objective-C format string.""" | ||
|
|
||
| check_id = "objc_format" | ||
| name = gettext_lazy("Objective-C format") | ||
| description = gettext_lazy("Objective-C format string does not match source.") | ||
|
Comment on lines
+630
to
+635
|
||
| version_added = "5.17" | ||
|
|
||
|
|
||
| class PerlBraceFormatCheck(BaseFormatCheck): | ||
| """Check for Perl brace format string.""" | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -34,7 +34,7 @@ | |
| from weblate.checks.models import Check | ||
| from weblate.checks.qt import QtFormatCheck, QtPluralCheck | ||
| from weblate.checks.ruby import RubyFormatCheck | ||
| from weblate.checks.tests.test_checks import CheckTestCase, MockUnit | ||
| from weblate.checks.tests.test_checks import BaseFormatTest, CheckTestCase, MockUnit | ||
| from weblate.lang.models import Language | ||
| from weblate.trans.models import Component, Project, Translation, Unit | ||
| from weblate.trans.tests.test_views import FixtureTestCase | ||
|
|
@@ -1917,3 +1917,20 @@ | |
| ["Recognition {}% ({}/{})"], MockUnit(flags="python-brace-format") | ||
| ) | ||
| ) | ||
|
|
||
|
|
||
| class ObjCFormatTest(BaseFormatTest): | ||
| CHECK = "objc_format" | ||
|
|
||
| def test_format(self): | ||
|
|
||
| self.check_match("Hello %@", "Hello %@") | ||
| self.check_match("Order %1$@ %2$d", "Order %1$@ %2$d") | ||
| self.check_match("Value is %1$.2f", "Value is %1$.2f") | ||
|
|
||
| self.check_match("%1$@ %2$@", "%2$@ %1$@") | ||
|
|
||
| self.check_fail("Hello %@", "Hello %s") | ||
| self.check_fail("Total: %d", "Total: %@") | ||
| self.check_fail("Missing %@", "Missing") | ||
| self.check_fail("Hello", "Hello %@") | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This really should use existing attributes logic for the basic testing. Adding other tests is okay, but using existing fine-grained tests is better starting point. |
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This really should be in the generated file, but I've just realized that our documentation on this is outdated (see #18724).
make -C docs update-docsshould update it.