validation: Writable/construct-only properties

Add two new errors, one for non-writable properties and another for
binding construct-only properties.
This commit is contained in:
James Westman 2022-04-28 22:42:09 -05:00
parent 3b39e0d541
commit f78478bea1
5 changed files with 31 additions and 1 deletions

View file

@ -184,6 +184,14 @@ class Property(GirNode):
def signature(self): def signature(self):
return f"{self.type_name} {self.container.name}.{self.name}" return f"{self.type_name} {self.container.name}.{self.name}"
@property
def writable(self):
return self.xml["writable"] == "1"
@property
def construct_only(self):
return self.xml["construct-only"] == "1"
class Parameter(GirNode): class Parameter(GirNode):
def __init__(self, container: GirNode, xml: xml_reader.Element): def __init__(self, container: GirNode, xml: xml_reader.Element):

View file

@ -29,7 +29,7 @@ class Property(AstNode):
Statement( Statement(
UseIdent("name"), UseIdent("name"),
":", ":",
"bind", Keyword("bind"),
UseIdent("bind_source").expected("the ID of a source object to bind from"), UseIdent("bind_source").expected("the ID of a source object to bind from"),
".", ".",
UseIdent("bind_property").expected("a property name to bind from"), UseIdent("bind_property").expected("a property name to bind from"),
@ -85,6 +85,19 @@ class Property(AstNode):
did_you_mean=(self.tokens["name"], self.gir_class.properties.keys()) did_you_mean=(self.tokens["name"], self.gir_class.properties.keys())
) )
@validate("bind")
def property_bindable(self):
if self.tokens["bind"] and self.gir_property is not None and self.gir_property.construct_only:
raise CompileError(
f"{self.gir_property.full_name} can't be bound because it is construct-only",
hints=["construct-only properties may only be set to a static value"]
)
@validate("name")
def property_writable(self):
if self.gir_property is not None and not self.gir_property.writable:
raise CompileError(f"{self.gir_property.full_name} is not writable")
@validate() @validate()
def obj_property_type(self): def obj_property_type(self):

View file

@ -0,0 +1,6 @@
using Gtk 4.0;
ComboBox combo_box {
has-entry: bind combo_box.visible;
scale-factor: 2;
}

View file

@ -0,0 +1,2 @@
4,14,4,ComboBox.has-entry can't be bound because it is construct-only
5,3,12,Widget.scale-factor is not writable

View file

@ -190,6 +190,7 @@ class TestSamples(unittest.TestCase):
self.assert_sample_error("obj_in_string_list") self.assert_sample_error("obj_in_string_list")
self.assert_sample_error("obj_prop_type") self.assert_sample_error("obj_prop_type")
self.assert_sample_error("property_dne") self.assert_sample_error("property_dne")
self.assert_sample_error("read_only_properties")
self.assert_sample_error("signal_dne") self.assert_sample_error("signal_dne")
self.assert_sample_error("signal_object_dne") self.assert_sample_error("signal_object_dne")
self.assert_sample_error("size_group_non_widget") self.assert_sample_error("size_group_non_widget")