.semgrep.yml 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. message: >
  43. Do not use anonymous placeholders for translations.
  44. Use printf style formatting with named placeholders instead.
  45. For example, do `_("Hello %(name)s") % {"name": "Wagtail"}`
  46. instead of `_("Hello %s") % "Wagtail"`.
  47. See https://docs.wagtail.org/en/latest/contributing/translations.html#marking-strings-for-translation for more information.
  48. languages: [python, javascript, typescript]
  49. severity: ERROR
  50. - id: translation-no-format-within-gettext-python
  51. patterns:
  52. - pattern: $FUNC("..." % ..., ...)
  53. - metavariable-regex:
  54. metavariable: $FUNC
  55. regex: '_|gettext|gettext_lazy|ngettext|ngettext_lazy'
  56. message: >
  57. Do not format string before translations
  58. or the interpolated value will be part of the key.
  59. Instead, interpolate after the call to gettext.
  60. For example, do `_("Hello %(name)s") % {"name": "Wagtail"}`
  61. instead of `_("Hello %(name)s" % {"name": "Wagtail"} )`.
  62. See https://docs.wagtail.org/en/latest/contributing/translations.html#marking-strings-for-translation for more information.
  63. languages: [python]
  64. severity: ERROR
  65. - id: translation-no-format-within-gettext-javascript
  66. patterns:
  67. - pattern: $FUNC("...".replace(...), ...)
  68. - metavariable-regex:
  69. metavariable: $FUNC
  70. regex: '_|gettext|gettext_lazy|ngettext|ngettext_lazy'
  71. message: >
  72. Do not format string before translations
  73. or the interpolated value will be part of the key.
  74. Instead, interpolate after the call to gettext.
  75. For example, do `_("Hello %(name)s") % {"name": "Wagtail"}`
  76. instead of `_("Hello %(name)s" % {"name": "Wagtail"} )`.
  77. See https://docs.wagtail.org/en/latest/contributing/translations.html#marking-strings-for-translation for more information.
  78. languages: [javascript, typescript]
  79. severity: ERROR