[gobject-introspection/ebassi/issue-418: 2/3] scanner: Validate members of enumeration types




commit 16c533bd9ec42d43b7912d2061d37a8a42966826
Author: Emmanuele Bassi <ebassi gnome org>
Date:   Thu Feb 17 16:33:43 2022 +0000

    scanner: Validate members of enumeration types
    
    Members of enumeration types should not have a name that starts with
    a number, as that will inevitable break languages that do not allow
    identifiers to start with a numerical value.
    
    To avoid any grief, we change the name to begin with an underscore:
    
      GDK_2BUTTON_PRESS -> Gdk.EventType._2BUTTON_PRESS
      QMI_NAS_RADIO_INTERFACE_5GNR -> Qmi.NasRadioInterface._5GNR
    
    We also emit a warning when using strict validation mode, so we give
    a chance to projects to bail out before committing to a bad API.
    
    Fixes: #418

 giscanner/maintransformer.py | 12 ++++++++++++
 1 file changed, 12 insertions(+)
---
diff --git a/giscanner/maintransformer.py b/giscanner/maintransformer.py
index 2004d254..a5ee7de1 100644
--- a/giscanner/maintransformer.py
+++ b/giscanner/maintransformer.py
@@ -126,6 +126,8 @@ class MainTransformer(object):
             if isinstance(node, (ast.Class, ast.Interface)):
                 self._pair_class_virtuals(node)
                 self._pair_property_accessors(node)
+            if isinstance(node, (ast.Enum, ast.Bitfield)):
+                self._pass_member_numeric_name(node)
 
         # Some annotations need to be post function pairing
         self._namespace.walk(self._pass_read_annotations2)
@@ -1562,6 +1564,16 @@ method or constructor of some type."""
                     prop.getter = method.name
                     continue
 
+    def _pass_member_numeric_name(self, node):
+        """Validate the name of the members of enumeration types."""
+        for member in node.members:
+            if re.match(r"^[0-9]", member.name):
+                message.strict_node(member,
+                                    f"Member {member.name} for enumeration {node.name} starts "
+                                     "with a number",
+                                     context=node)
+                member.name = f"_{member.name}"
+
     def _pass3(self, node, chain):
         """Pass 3 is after we've loaded GType data and performed type
         closure."""


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]