[PATCH 1 of 6] check-code: replace quoted characters correctly

FUJIWARA Katsunori foozy at lares.dti.ne.jp
Tue May 31 12:15:35 UTC 2016


# HG changeset patch
# User FUJIWARA Katsunori <foozy at lares.dti.ne.jp>
# Date 1464695890 -32400
#      Tue May 31 20:58:10 2016 +0900
# Node ID 4dd530df474256a7011244132fe376eb5514c1c7
# Parent  e53f961ac75fb73b38b15a92a2a96f3d28206e64
check-code: replace quoted characters correctly

169cb9e47f8e tried to detect '.. note::' more exactly. But
implementation of it seems not correct, because:

  - fromc.find(c) returns -1 for other than "." and ":"
  - tochr[-1] returns "q" for such characters, but
  - expected result for them is "o"

This patch uses dict to manage replacement instead of replacing
str.find() by str.index(), for improvement/refactoring in subsequent
patches. Examination by fixedmap is placed just after examination for
' ' and '\n', because subsequent patch will integrate the latter into
the former.

This patch also changes regexp for 'string join across lines with no
space' rule, and adds detailed test for it, because 169cb9e47f8e did:

  - make repquote() distinguish "." (as "p") and ":" (as "q") from
    others (as "o"), but

  - not change this regexp without any reason (in commit log, at
    least), even though this regexp depends on what "o" means

This patch doesn't focuses on deciding whether "." and/or ":" should
be followed by whitespace or not in translatable messages.

diff --git a/contrib/check-code.py b/contrib/check-code.py
--- a/contrib/check-code.py
+++ b/contrib/check-code.py
@@ -51,22 +51,20 @@ def compilere(pat, multiline=False):
     return re.compile(pat)
 
 def repquote(m):
-    fromc = '.:'
-    tochr = 'pq'
+    fixedmap = {'.': 'p', ':': 'q'}
     def encodechr(i):
         if i > 255:
             return 'u'
         c = chr(i)
         if c in ' \n':
             return c
+        if c in fixedmap:
+            return fixedmap[c]
         if c.isalpha():
             return 'x'
         if c.isdigit():
             return 'n'
-        try:
-            return tochr[fromc.find(c)]
-        except (ValueError, IndexError):
-            return 'o'
+        return 'o'
     t = m.group('text')
     tt = ''.join(encodechr(i) for i in xrange(256))
     t = t.translate(tt)
@@ -248,7 +246,7 @@ pypats = [
     (r'^\s+(\w|\.)+=\w[^,()\n]*$', "missing whitespace in assignment"),
     (r'\w\s=\s\s+\w', "gratuitous whitespace after ="),
     (r'.{81}', "line too long"),
-    (r' x+[xo][\'"]\n\s+[\'"]x', 'string join across lines with no space'),
+    (r' x+[xpqo][\'"]\n\s+[\'"]x', 'string join across lines with no space'),
     (r'[^\n]\Z', "no trailing newline"),
     (r'(\S[ \t]+|^[ \t]+)\n', "trailing whitespace"),
 #    (r'^\s+[^_ \n][^_. \n]+_[^_\n]+\s*=',
diff --git a/tests/test-contrib-check-code.t b/tests/test-contrib-check-code.t
--- a/tests/test-contrib-check-code.t
+++ b/tests/test-contrib-check-code.t
@@ -248,3 +248,27 @@ web templates
    > {desc|escape}
    warning: follow desc keyword with either firstline or websub
   [1]
+
+'string join across lines with no space' detection
+
+  $ cat > stringjoin.py <<EOF
+  > foo = (' foo'
+  >        'bar foo.'
+  >        'bar foo:'
+  >        'bar foo@'
+  >        'bar')
+  > EOF
+  $ "$check_code" stringjoin.py
+  stringjoin.py:1:
+   > foo = (' foo'
+   string join across lines with no space
+  stringjoin.py:2:
+   >        'bar foo.'
+   string join across lines with no space
+  stringjoin.py:3:
+   >        'bar foo:'
+   string join across lines with no space
+  stringjoin.py:4:
+   >        'bar foo@'
+   string join across lines with no space
+  [1]


More information about the Mercurial-devel mailing list