Skip to content

Commit

Permalink
Allow internal-only namespaces in @import directive
Browse files Browse the repository at this point in the history
  • Loading branch information
exyi committed Jan 25, 2024
1 parent cb8e675 commit e05f34f
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/Framework/Framework/Compilation/CompiledAssemblyCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -172,12 +172,13 @@ private HashSet<string> GetAllNamespaces()
foreach (var a in GetAllAssemblies())
{
string? lastNs = null; // namespaces come in batches, usually, so no need to hash it everytime when a quick compare says it's the same as last time
foreach (var type in a.ExportedTypes)
foreach (var type in a.GetLoadableTypes())
{
var ns = type.Namespace;
if (ns is null || lastNs == ns)
continue;
result.Add(ns);
result.Add(ns);
lastNs = ns;
}
}
return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
using System;
using System.Collections.Generic;

namespace DotVVM.Framework.Tests.Runtime.ControlTree.TestInternalOnlyNamespace
{
class InternalClass { }
}

namespace DotVVM.Framework.Tests.Runtime.ControlTree
{
[TestClass]
Expand Down Expand Up @@ -70,6 +75,46 @@ @viewModel object
Assert.AreEqual(typeof(List<int>).FullName, importDirective.Type.FullName);
}

[TestMethod]
public void ResolvedTree_ImportDirective_NamespaceDoesNotExist()
{
var root = ParseSource(@"
@viewModel object
@import This.Namespace.Does.Not.Exist
");
var importDirective = EnsureSingleResolvedImportDirective(root);

Assert.IsTrue(importDirective.HasError);
Assert.AreEqual("This.Namespace.Does.Not.Exist is unknown type or namespace.", importDirective.DothtmlNode.NodeErrors.Single());
}
[TestMethod]
public void ResolvedTree_ImportDirective_NamespaceDoesNotExist_Alias()
{
var root = ParseSource(@"
@viewModel object
@import testns = This.Namespace.Does.Not.Exist
");
var importDirective = EnsureSingleResolvedImportDirective(root);

Assert.IsTrue(importDirective.HasError);
Assert.AreEqual("This.Namespace.Does.Not.Exist is unknown type or namespace.", importDirective.DothtmlNode.NodeErrors.Single());
}

[TestMethod]
public void ResolvedTree_ImportDirective_InternalNamespace()
{
var root = ParseSource(@"
@viewModel object
@import DotVVM.Framework.Tests.Runtime.ControlTree.TestInternalOnlyNamespace
");
var importDirective = EnsureSingleResolvedImportDirective(root);

Assert.IsFalse(importDirective.HasError, message: importDirective.DothtmlNode.NodeErrors.SingleOrDefault());
Assert.IsNull(importDirective.Type);
Assert.IsTrue(importDirective.IsNamespace);
}


private static ResolvedImportDirective EnsureSingleResolvedImportDirective(ResolvedTreeRoot root) =>
root.Directives["import"]
.Single()
Expand Down

0 comments on commit e05f34f

Please sign in to comment.