Coverage for app/venv/lib/python3.14/site-packages/weblate/checks/ruby.py: 91%

11 statements  

« prev     ^ index     » next       coverage.py v7.15.2, created at 2026-10-07 07:15 +0000

1# Copyright © Michal Čihař <michal@weblate.org> 

2# 

3# SPDX-License-Identifier: GPL-3.0-or-later 

4 

5import re 

6 

7from django.utils.translation import gettext_lazy 

8 

9from weblate.checks.format import BaseFormatCheck 

10 

11RUBY_FORMAT_MATCH = re.compile( 

12 r""" 

13 %( # initial % 

14 (?: # classic printf style 

15 (?:(?P<ord>\d+)\$)? # variable order, like %1$s 

16 (?P<fullvar> 

17 [ +#*-]* # flags 

18 (?:\d+)? # width 

19 (?:\.\d+)? # precision 

20 (?P<type>[a-zA-Z%]) # type (%s, %d, etc.) 

21 ) 

22 )|(?: # template style 

23 (?P<t_fullvar> 

24 [ +#*-]* # flags 

25 (?:\d+)? # width 

26 (?:\.\d+)? # precision 

27 (?: 

28 (?: 

29 <(?P<t_field>[^> ]+)> # named printf reference 

30 (?P<t_type>[a-zA-Z]) # type (%s, %d, etc.) 

31 ) 

32 | 

33 (?:\{(?P<tt_field>[^} ]+)\}) # named reference (implicit %s) 

34 ) 

35 ) 

36 ) 

37 ) 

38 """, 

39 re.VERBOSE, 

40) 

41 

42 

43class RubyFormatCheck(BaseFormatCheck): 

44 """ 

45 Check for Ruby format string. 

46 

47 Ruby support various format strings (excluding string interpolation): 

48 - printf syntax: %s, %1$s 

49 - named printf syntax: %<variable>s 

50 - template style (implicit %s): %{variable} 

51 """ 

52 

53 check_id = "ruby_format" 

54 name = gettext_lazy("Ruby format") 

55 description = gettext_lazy("Ruby format string does not match source.") 

56 regexp = RUBY_FORMAT_MATCH 

57 

58 def is_position_based(self, string: str): 

59 return string != "%" and not re.search(r"[$<{]", string)