Patchwork [2,of,5,STABLE,V2] check-code: detect "% inside _()" when there are leading whitespaces

login
register
mail settings
Submitter Katsunori FUJIWARA
Date April 15, 2014, 6:12 p.m.
Message ID <4bd7437852c4736c0181.1397585550@juju>
Download mbox | patch
Permalink /patch/4377/
State Accepted
Commit e8ef59b351c3824c718a3b912c3f14a59587ae72
Headers show

Comments

Katsunori FUJIWARA - April 15, 2014, 6:12 p.m.
# HG changeset patch
# User FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
# Date 1397585100 -32400
#      Wed Apr 16 03:05:00 2014 +0900
# Branch stable
# Node ID 4bd7437852c4736c0181d272ee410669034cc906
# Parent  5ab1c796858b4eff91dd63a6710c288d278ee8c6
check-code: detect "% inside _()" when there are leading whitespaces

Before this patch, "contrib/check-code.py" can't detect "% inside _()"
correctly, when there are leading whitespaces before the format
string, like below:

    _(
      "format string %s" % v)

This patch adds regexp pattern "[ \t\n]*" before the pattern matching
against the format string.

"[\s\n]" can't be used in this purpose, because "\s" is automatically
replaced with "[ \t]" by "_preparepats()" and "\s" in "[]" causes
nested "[]" unexpectedly.

Patch

diff --git a/contrib/check-code.py b/contrib/check-code.py
--- a/contrib/check-code.py
+++ b/contrib/check-code.py
@@ -198,8 +198,8 @@ 
     (r'\s<>\s', '<> operator is not available in Python 3+, use !='),
     (r'^\s*\t', "don't use tabs"),
     (r'\S;\s*\n', "semicolon"),
-    (r'[^_]_\((?:"[^"]+"[ \t\n+]*)+%', "don't use % inside _()"),
-    (r"[^_]_\((?:'[^']+'[ \t\n+]*)+%", "don't use % inside _()"),
+    (r'[^_]_\([ \t\n]*(?:"[^"]+"[ \t\n+]*)+%', "don't use % inside _()"),
+    (r"[^_]_\([ \t\n]*(?:'[^']+'[ \t\n+]*)+%", "don't use % inside _()"),
     (r'(\w|\)),\w', "missing whitespace after ,"),
     (r'(\w|\))[+/*\-<>]\w', "missing whitespace in expression"),
     (r'^\s+(\w|\.)+=\w[^,()\n]*$', "missing whitespace in assignment"),
diff --git a/tests/test-check-code.t b/tests/test-check-code.t
--- a/tests/test-check-code.t
+++ b/tests/test-check-code.t
@@ -251,3 +251,32 @@ 
    warning: add two newlines after '.. note::'
   [1]
 
+  $ cat > ./map-inside-gettext.py <<EOF
+  > print _("map inside gettext %s" % v)
+  > 
+  > print _("concatenating " " by " " space %s" % v)
+  > print _("concatenating " + " by " + " '+' %s" % v)
+  > 
+  > print _("maping operation in different line %s"
+  >         % v)
+  > 
+  > print _(
+  >         "leading spaces inside of '(' %s" % v)
+  > EOF
+  $ "$check_code" ./map-inside-gettext.py
+  ./map-inside-gettext.py:1:
+   > print _("map inside gettext %s" % v)
+   don't use % inside _()
+  ./map-inside-gettext.py:3:
+   > print _("concatenating " " by " " space %s" % v)
+   don't use % inside _()
+  ./map-inside-gettext.py:4:
+   > print _("concatenating " + " by " + " '+' %s" % v)
+   don't use % inside _()
+  ./map-inside-gettext.py:6:
+   > print _("maping operation in different line %s"
+   don't use % inside _()
+  ./map-inside-gettext.py:9:
+   > print _(
+   don't use % inside _()
+  [1]