.semgrep.yml 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. rules:
  2. - id: translation-no-new-style-formatting
  3. patterns:
  4. - pattern: $FUNC("$STRING_ID", ...)
  5. - metavariable-regex:
  6. metavariable: $FUNC
  7. regex: '_|gettext|gettext_lazy|ngettext|ngettext_lazy'
  8. - metavariable-regex:
  9. metavariable: $STRING_ID
  10. regex: ".*({(\\d*|[\\w_]*)}).*"
  11. message: |
  12. Do not use str.format style formatting for translations.
  13. Use printf style formatting with named placeholders instead.
  14. For example, do `_("Hello %(name)s") % {"name": "Wagtail"}`
  15. instead of `_("Hello {name}").format(name="Wagtail")`.
  16. See https://docs.wagtail.org/en/latest/contributing/translations.html#marking-strings-for-translation for more information.
  17. languages: [python, javascript, typescript]
  18. severity: ERROR
  19. - id: translation-no-f-strings
  20. patterns:
  21. - pattern: $FUNC(f"...", ...)
  22. - metavariable-regex:
  23. metavariable: $FUNC
  24. regex: '_|gettext|gettext_lazy|ngettext|ngettext_lazy'
  25. message: >
  26. Do not use formatted string literals for translations.
  27. Use printf style formatting with named placeholders instead.
  28. For example, do `_("Hello %(name)s") % {"name": "Wagtail"}`
  29. instead of `_(f"Hello {name}")`.
  30. See https://docs.wagtail.org/en/latest/contributing/translations.html#marking-strings-for-translation for more information.
  31. languages: [python]
  32. severity: ERROR
  33. - id: translation-no-anonymous-arguments
  34. patterns:
  35. - pattern: $FUNC("$STRING_ID", ...)
  36. - metavariable-regex:
  37. metavariable: $FUNC
  38. regex: '_|gettext|gettext_lazy|ngettext|ngettext_lazy'
  39. - metavariable-regex:
  40. metavariable: $STRING_ID
  41. regex: ".*%\\w.*"
  42. paths:
  43. exclude:
  44. - 'wagtail/test/numberformat.py'
  45. message: >
  46. Do not use anonymous placeholders for translations.
  47. Use printf style formatting with named placeholders instead.
  48. For example, do `_("Hello %(name)s") % {"name": "Wagtail"}`
  49. instead of `_("Hello %s") % "Wagtail"`.
  50. See https://docs.wagtail.org/en/latest/contributing/translations.html#marking-strings-for-translation for more information.
  51. languages: [python, javascript, typescript]
  52. severity: ERROR
  53. - id: translation-no-format-within-gettext-python
  54. patterns:
  55. - pattern: $FUNC("..." % ..., ...)
  56. - metavariable-regex:
  57. metavariable: $FUNC
  58. regex: '_|gettext|gettext_lazy|ngettext|ngettext_lazy'
  59. message: >
  60. Do not format string before translations
  61. or the interpolated value will be part of the key.
  62. Instead, interpolate after the call to gettext.
  63. For example, do `_("Hello %(name)s") % {"name": "Wagtail"}`
  64. instead of `_("Hello %(name)s" % {"name": "Wagtail"} )`.
  65. See https://docs.wagtail.org/en/latest/contributing/translations.html#marking-strings-for-translation for more information.
  66. languages: [python]
  67. severity: ERROR
  68. - id: translation-no-format-within-gettext-javascript
  69. patterns:
  70. - pattern: $FUNC("...".replace(...), ...)
  71. - metavariable-regex:
  72. metavariable: $FUNC
  73. regex: '_|gettext|gettext_lazy|ngettext|ngettext_lazy'
  74. message: >
  75. Do not format string before translations
  76. or the interpolated value will be part of the key.
  77. Instead, interpolate after the call to gettext.
  78. For example, do `_("Hello %(name)s") % {"name": "Wagtail"}`
  79. instead of `_("Hello %(name)s" % {"name": "Wagtail"} )`.
  80. See https://docs.wagtail.org/en/latest/contributing/translations.html#marking-strings-for-translation for more information.
  81. languages: [javascript, typescript]
  82. severity: ERROR