Skip to content

Commit

Permalink
Fix issue where property directive compiled without errors on a regul…
Browse files Browse the repository at this point in the history
…ar page
  • Loading branch information
acizmarik committed Apr 27, 2023
1 parent 954f93d commit 48f3196
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ protected override IAbstractBaseTypeDirective Resolve(DothtmlDirectiveNode direc
=> TreeBuilder.BuildBaseTypeDirective(directiveNode, ParseDirective(directiveNode, p => p.ReadDirectiveTypeName()), imports);

protected override ITypeDescriptor CreateArtefact(IReadOnlyList<IAbstractBaseTypeDirective> resolvedDirectives) {
var isMarkupControl = IsMarkupControl();
var wrapperType = GetDefaultWrapperType();

var baseControlDirective = resolvedDirectives.SingleOrDefault();
Expand Down Expand Up @@ -65,7 +66,7 @@ protected override ITypeDescriptor CreateArtefact(IReadOnlyList<IAbstractBaseTyp
}
}

if (DirectiveNodesByName.TryGetValue(ParserConstants.PropertyDeclarationDirective, out var propertyDirectives) && propertyDirectives.Any())
if (isMarkupControl && DirectiveNodesByName.TryGetValue(ParserConstants.PropertyDeclarationDirective, out var propertyDirectives) && propertyDirectives.Any())
{
wrapperType = CreateDynamicDeclaringType(wrapperType, propertyDirectives) ?? wrapperType;
}
Expand Down Expand Up @@ -107,19 +108,15 @@ IEnumerable<DothtmlDirectiveNode> propertyDirectives
? new ResolvedTypeDescriptor(createdTypeInfo)
: null;
}

/// <summary>
/// Gets the default type of the wrapper for the view.
/// </summary>
private ITypeDescriptor GetDefaultWrapperType()
{
if (fileName.EndsWith(".dotcontrol", StringComparison.Ordinal))
{
return new ResolvedTypeDescriptor(typeof(DotvvmMarkupControl));
}
=> new ResolvedTypeDescriptor(IsMarkupControl() ? typeof(DotvvmMarkupControl) : typeof(DotvvmView));

return new ResolvedTypeDescriptor(typeof(DotvvmView));
}
private bool IsMarkupControl()
=> fileName.EndsWith(".dotcontrol", StringComparison.Ordinal);

private static ModuleBuilder CreateDynamicMarkupControlAssembly()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,16 @@ public MarkupPageMetadata Compile(DothtmlRootNode dothtmlRoot, string fileName)
var baseType = baseTypeResult.Artefact;
resolvedDirectives.AddIfAny(baseTypeCompiler.DirectiveName, baseTypeResult.Directives);

var isMarkupControl = !baseType.IsEqualTo(ResolvedTypeDescriptor.Create(typeof(DotvvmView)));
var viewModuleDirectiveCompiler = new ViewModuleDirectiveCompiler(
directivesByName,
treeBuilder,
!baseType.IsEqualTo(ResolvedTypeDescriptor.Create(typeof(DotvvmView))),
isMarkupControl,
resourceRepository);
var viewModuleResult = viewModuleDirectiveCompiler.Compile();
resolvedDirectives.AddIfAny(viewModuleDirectiveCompiler.DirectiveName, viewModuleResult.Directives);

var propertyDirectiveCompiler = new PropertyDeclarationDirectiveCompiler(directivesByName, treeBuilder, baseType, imports);
var propertyDirectiveCompiler = new PropertyDeclarationDirectiveCompiler(directivesByName, treeBuilder, isMarkupControl, baseType, imports);
var propertyResult = propertyDirectiveCompiler.Compile();
resolvedDirectives.AddIfAny(propertyDirectiveCompiler.DirectiveName, propertyResult.Directives);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,25 @@ public class PropertyDeclarationDirectiveCompiler : DirectiveCompiler<IAbstractP
{
private readonly ITypeDescriptor controlWrapperType;
private readonly ImmutableList<NamespaceImport> imports;
private readonly bool isMarkupControl;

public override string DirectiveName => ParserConstants.PropertyDeclarationDirective;

public PropertyDeclarationDirectiveCompiler(IReadOnlyDictionary<string, IReadOnlyList<DothtmlDirectiveNode>> directiveNodesByName, IAbstractTreeBuilder treeBuilder, ITypeDescriptor controlWrapperType, ImmutableList<NamespaceImport> imports)
public PropertyDeclarationDirectiveCompiler(IReadOnlyDictionary<string, IReadOnlyList<DothtmlDirectiveNode>> directiveNodesByName, IAbstractTreeBuilder treeBuilder, bool isMarkupControl, ITypeDescriptor controlWrapperType, ImmutableList<NamespaceImport> imports)
: base(directiveNodesByName, treeBuilder)
{
this.isMarkupControl = isMarkupControl;
this.controlWrapperType = controlWrapperType;
this.imports = imports;
}

protected override IAbstractPropertyDeclarationDirective Resolve(DothtmlDirectiveNode directiveNode)
{
if (!isMarkupControl)
{
directiveNode.AddError("Can not use the @property directive outside of a markup control");
}

var valueSyntaxRoot = ParseDirective(directiveNode, p => p.ReadPropertyDirectiveValue());

var declaration = valueSyntaxRoot as PropertyDeclarationBindingParserNode;
Expand Down

0 comments on commit 48f3196

Please sign in to comment.