From bc605c5df86a33ff415f627ab1aa8cca53780c81 Mon Sep 17 00:00:00 2001 From: James Westman Date: Tue, 21 Mar 2023 11:31:02 -0500 Subject: [PATCH] Reduce errors when a namespace is not found When the typelib for a namespace is not found, don't emit "namespace not imported" errors. Just emit the one error on the import statement. --- blueprintcompiler/gir.py | 3 ++- blueprintcompiler/language/imports.py | 8 ++++++++ blueprintcompiler/language/ui.py | 2 ++ tests/sample_errors/ns_not_found.blp | 6 ++++++ tests/sample_errors/ns_not_found.err | 1 + tests/test_samples.py | 1 + 6 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 tests/sample_errors/ns_not_found.blp create mode 100644 tests/sample_errors/ns_not_found.err diff --git a/blueprintcompiler/gir.py b/blueprintcompiler/gir.py index d795789..a8831af 100644 --- a/blueprintcompiler/gir.py +++ b/blueprintcompiler/gir.py @@ -741,6 +741,7 @@ class Repository(GirNode): class GirContext: def __init__(self): self.namespaces = {} + self.not_found_namespaces: T.Set[str] = set() def add_namespace(self, namespace: Namespace): other = self.namespaces.get(namespace.name) @@ -781,7 +782,7 @@ class GirContext: ns = ns or "Gtk" - if ns not in self.namespaces: + if ns not in self.namespaces and ns not in self.not_found_namespaces: raise CompileError( f"Namespace {ns} was not imported", did_you_mean=(ns, self.namespaces.keys()), diff --git a/blueprintcompiler/language/imports.py b/blueprintcompiler/language/imports.py index 224e0a3..e34901c 100644 --- a/blueprintcompiler/language/imports.py +++ b/blueprintcompiler/language/imports.py @@ -76,6 +76,14 @@ class Import(AstNode): UseNumberText("version").expected("a version number"), ) + @property + def namespace(self): + return self.tokens["namespace"] + + @property + def version(self): + return self.tokens["version"] + @validate("namespace", "version") def namespace_exists(self): gir.get_namespace(self.tokens["namespace"], self.tokens["version"]) diff --git a/blueprintcompiler/language/ui.py b/blueprintcompiler/language/ui.py index aeff186..8d27c0e 100644 --- a/blueprintcompiler/language/ui.py +++ b/blueprintcompiler/language/ui.py @@ -58,6 +58,8 @@ class UI(AstNode): try: if i.gir_namespace is not None: gir_ctx.add_namespace(i.gir_namespace) + else: + gir_ctx.not_found_namespaces.add(i.namespace) except CompileError as e: e.start = i.group.tokens["namespace"].start e.end = i.group.tokens["version"].end diff --git a/tests/sample_errors/ns_not_found.blp b/tests/sample_errors/ns_not_found.blp new file mode 100644 index 0000000..071d554 --- /dev/null +++ b/tests/sample_errors/ns_not_found.blp @@ -0,0 +1,6 @@ +using Gtk 4.0; +using NotARealNamespace 1; + +NotARealNamespace.Widget widget { + property: true; +} \ No newline at end of file diff --git a/tests/sample_errors/ns_not_found.err b/tests/sample_errors/ns_not_found.err new file mode 100644 index 0000000..4a6a111 --- /dev/null +++ b/tests/sample_errors/ns_not_found.err @@ -0,0 +1 @@ +2,7,19,Namespace NotARealNamespace-1 could not be found \ No newline at end of file diff --git a/tests/test_samples.py b/tests/test_samples.py index 29d2f14..45fb692 100644 --- a/tests/test_samples.py +++ b/tests/test_samples.py @@ -224,6 +224,7 @@ class TestSamples(unittest.TestCase): self.assert_sample_error("menu_no_id") self.assert_sample_error("menu_toplevel_attribute") self.assert_sample_error("no_import_version") + self.assert_sample_error("ns_not_found") self.assert_sample_error("ns_not_imported") self.assert_sample_error("not_a_class") self.assert_sample_error("object_dne")