From 48f319659464aaf9553c046eeaf4972cf17d876c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrej=20=C4=8Ci=C5=BEm=C3=A1rik?= Date: Thu, 27 Apr 2023 10:56:39 +0200 Subject: [PATCH] Fix issue where property directive compiled without errors on a regular page --- .../Directives/BaseTypeDirectiveCompiler.cs | 15 ++++++--------- .../Directives/MarkupDirectiveCompilerPipeline.cs | 5 +++-- .../PropertyDeclarationDirectiveCompiler.cs | 9 ++++++++- 3 files changed, 17 insertions(+), 12 deletions(-) diff --git a/src/Framework/Framework/Compilation/Directives/BaseTypeDirectiveCompiler.cs b/src/Framework/Framework/Compilation/Directives/BaseTypeDirectiveCompiler.cs index 194d4867e2..868a1e4621 100644 --- a/src/Framework/Framework/Compilation/Directives/BaseTypeDirectiveCompiler.cs +++ b/src/Framework/Framework/Compilation/Directives/BaseTypeDirectiveCompiler.cs @@ -37,6 +37,7 @@ protected override IAbstractBaseTypeDirective Resolve(DothtmlDirectiveNode direc => TreeBuilder.BuildBaseTypeDirective(directiveNode, ParseDirective(directiveNode, p => p.ReadDirectiveTypeName()), imports); protected override ITypeDescriptor CreateArtefact(IReadOnlyList resolvedDirectives) { + var isMarkupControl = IsMarkupControl(); var wrapperType = GetDefaultWrapperType(); var baseControlDirective = resolvedDirectives.SingleOrDefault(); @@ -65,7 +66,7 @@ protected override ITypeDescriptor CreateArtefact(IReadOnlyList propertyDirectives ? new ResolvedTypeDescriptor(createdTypeInfo) : null; } - + /// /// Gets the default type of the wrapper for the view. /// 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() { diff --git a/src/Framework/Framework/Compilation/Directives/MarkupDirectiveCompilerPipeline.cs b/src/Framework/Framework/Compilation/Directives/MarkupDirectiveCompilerPipeline.cs index 742d08da85..d8b7e84a05 100644 --- a/src/Framework/Framework/Compilation/Directives/MarkupDirectiveCompilerPipeline.cs +++ b/src/Framework/Framework/Compilation/Directives/MarkupDirectiveCompilerPipeline.cs @@ -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); diff --git a/src/Framework/Framework/Compilation/Directives/PropertyDeclarationDirectiveCompiler.cs b/src/Framework/Framework/Compilation/Directives/PropertyDeclarationDirectiveCompiler.cs index 4f7e3777e7..8a0ad93241 100644 --- a/src/Framework/Framework/Compilation/Directives/PropertyDeclarationDirectiveCompiler.cs +++ b/src/Framework/Framework/Compilation/Directives/PropertyDeclarationDirectiveCompiler.cs @@ -17,18 +17,25 @@ public class PropertyDeclarationDirectiveCompiler : DirectiveCompiler imports; + private readonly bool isMarkupControl; public override string DirectiveName => ParserConstants.PropertyDeclarationDirective; - public PropertyDeclarationDirectiveCompiler(IReadOnlyDictionary> directiveNodesByName, IAbstractTreeBuilder treeBuilder, ITypeDescriptor controlWrapperType, ImmutableList imports) + public PropertyDeclarationDirectiveCompiler(IReadOnlyDictionary> directiveNodesByName, IAbstractTreeBuilder treeBuilder, bool isMarkupControl, ITypeDescriptor controlWrapperType, ImmutableList 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;