[vala/staging] vala: Properly check GLib.Object naming convention for properties



commit 82846a3cdd18fb3e3b6ae307a50bdf83b6e21bcd
Author: Rico Tzschichholz <ricotz ubuntu com>
Date:   Sun Apr 18 21:02:21 2021 +0200

    vala: Properly check GLib.Object naming convention for properties

 tests/Makefile.am                                  |  2 ++
 tests/objects/property-class-invalid-name.test     |  8 +++++++
 tests/objects/property-interface-invalid-name.test |  8 +++++++
 vala/valaproperty.vala                             | 27 ++++++++++++++++++++++
 vala/valasemanticanalyzer.vala                     |  5 ----
 5 files changed, 45 insertions(+), 5 deletions(-)
---
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 5b2610dd6..fb1133555 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -480,6 +480,7 @@ TESTS = \
        objects/property-read-only-auto.vala \
        objects/property-read-only-member-write.test \
        objects/property-read-only-write.test \
+       objects/property-class-invalid-name.test \
        objects/property-construct-only.vala \
        objects/property-construct-only-write.test \
        objects/property-construct-only-write-after.test \
@@ -487,6 +488,7 @@ TESTS = \
        objects/property-delegate.vala \
        objects/property-delegate-owned.vala \
        objects/property-gboxed-nullable.vala \
+       objects/property-interface-invalid-name.test \
        objects/property-real-struct-assignment.vala \
        objects/property-real-struct-no-accessor.test \
        objects/property-simple-type-struct-nullable.vala \
diff --git a/tests/objects/property-class-invalid-name.test b/tests/objects/property-class-invalid-name.test
new file mode 100644
index 000000000..b1fa27966
--- /dev/null
+++ b/tests/objects/property-class-invalid-name.test
@@ -0,0 +1,8 @@
+Invalid Code
+
+class Foo : Object {
+       public string 1foo { get; set; }
+}
+
+void main () {
+}
diff --git a/tests/objects/property-interface-invalid-name.test 
b/tests/objects/property-interface-invalid-name.test
new file mode 100644
index 000000000..54c4292c5
--- /dev/null
+++ b/tests/objects/property-interface-invalid-name.test
@@ -0,0 +1,8 @@
+Invalid Code
+
+interface IFoo : Object {
+       public abstract string 1foo { get; set; }
+}
+
+void main () {
+}
diff --git a/vala/valaproperty.vala b/vala/valaproperty.vala
index f07f137e0..a917e20ad 100644
--- a/vala/valaproperty.vala
+++ b/vala/valaproperty.vala
@@ -413,6 +413,13 @@ public class Vala.Property : Symbol, Lockable {
 
                checked = true;
 
+               if (context.profile == Profile.GOBJECT && parent_symbol is ObjectTypeSymbol
+                   && ((ObjectTypeSymbol) parent_symbol).is_subtype_of (context.analyzer.object_type)
+                   && !is_valid_name (name)) {
+                       error = true;
+                       Report.error (source_reference, "`%s' is not valid property name in `GLib.Object' 
types", name);
+               }
+
                if (this_parameter != null) {
                        this_parameter.check (context);
                }
@@ -543,4 +550,24 @@ public class Vala.Property : Symbol, Lockable {
 
                return !error;
        }
+
+       // Ported from glib's "g_param_spec_is_valid_name"
+       static bool is_valid_name (string name) {
+               char* p;
+
+               if ((name[0] < 'A' || name[0] > 'Z')
+                   && (name[0] < 'a' || name[0] > 'z')) {
+                       return false;
+               }
+
+               for (p = name; *p != '\0'; p++) {
+                       char c = *p;
+                       if (c != '-' && c != '_' && (c < '0' || c > '9')
+                           && (c < 'A' || c > 'Z') && (c < 'a' || c > 'z')) {
+                               return false;
+                       }
+               }
+
+               return true;
+       }
 }
diff --git a/vala/valasemanticanalyzer.vala b/vala/valasemanticanalyzer.vala
index 9d2d7ea07..378c301bd 100644
--- a/vala/valasemanticanalyzer.vala
+++ b/vala/valasemanticanalyzer.vala
@@ -465,11 +465,6 @@ public class Vala.SemanticAnalyzer : CodeVisitor {
                        return false;
                }
 
-               if (!prop.name[0].isalpha ()) {
-                       // GObject requires properties to start with a letter
-                       return false;
-               }
-
                if (type_sym is Interface && !prop.is_abstract && !prop.external && !prop.external_package) {
                        // GObject does not support non-abstract interface properties,
                        // however we assume external properties always are GObject properties


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