From 895ad896c72b933eff54dca994ae13de62b2fe2f Mon Sep 17 00:00:00 2001 From: Ryan Nowak Date: Mon, 1 Jul 2019 13:58:25 -0700 Subject: [PATCH] Fix: dotnet/aspnetcore-tooling#11197 automatic backing-field for @ref (dotnet/aspnetcore-tooling#707) This change makes @ref define a field for you by default. We didn't do this originally because we weren't sure if the compiler generating a field would always be what you want, and more importantly, we didn't have the expressiveness to add an opt-out. Now that we have directive attributes, it's easy to create an opt-out for the field generation. The special thing about it is that where you opt-out has to be static. For this reason the @bind:suppressField attribute cannot be used with a value, you either have the flag or not. Additionally, we don't support automatic field generation for generics, because usually we can't write the field name. If we want to in the future could make this work when the generic type arguments are specified. So in the case of generics, you have to opt-out, and you will get a diagnostic that tells you so.\n\nCommit migrated from https://github.com/dotnet/aspnetcore-tooling/commit/dadf9faf22e53912b79ecf8ac6d5d871b188d9fb --- .../CodeGeneration/CodeWriterExtensions.cs | 19 ++- .../CodeGeneration/DefaultDocumentWriter.cs | 2 +- .../src/ComponentResources.resx | 5 +- .../Components/ComponentDiagnosticFactory.cs | 26 +++ .../ComponentReferenceCaptureLoweringPass.cs | 101 ++++++++++-- ...faultRazorIntermediateNodeLoweringPhase.cs | 22 ++- .../DefaultTagHelperTargetExtension.cs | 17 +- .../FieldDeclarationIntermediateNode.cs | 2 + .../ReferenceCaptureIntermediateNode.cs | 5 +- .../src/RazorDiagnosticCollection.cs | 10 ++ .../CodeGeneration/CSharpCodeWriterTest.cs | 28 +++- .../ComponentCodeGenerationTestBase.cs | 153 ++++++++++++++---- .../TestComponent.codegen.cs | 14 +- .../Component_WithRef/TestComponent.ir.txt | 7 +- .../TestComponent.mappings.txt | 13 +- .../TestComponent.codegen.cs | 53 ++++++ .../TestComponent.ir.txt | 22 +++ .../TestComponent.mappings.txt | 16 ++ .../TestComponent.codegen.cs | 14 +- .../TestComponent.ir.txt | 9 +- .../TestComponent.mappings.txt | 13 +- .../Element_WithRef/TestComponent.codegen.cs | 14 +- .../Element_WithRef/TestComponent.ir.txt | 7 +- .../TestComponent.mappings.txt | 13 +- .../TestComponent.codegen.cs | 8 +- .../TestComponent.ir.txt | 9 +- .../TestComponent.mappings.txt | 14 +- .../TestComponent.codegen.cs | 43 +++++ .../TestComponent.ir.txt | 24 +++ .../TestComponent.mappings.txt | 16 ++ .../TestComponent.codegen.cs | 39 +++++ .../TestComponent.diagnostics.txt | 1 + .../TestComponent.ir.txt | 21 +++ .../TestComponent.mappings.txt | 5 + .../TestComponent.codegen.cs | 6 +- .../TestComponent.ir.txt | 14 +- .../TestComponent.mappings.txt | 12 +- .../TestComponent.codegen.cs | 0 .../TestComponent.ir.txt | 14 +- .../TestComponent.mappings.txt | 2 +- .../TestComponent.codegen.cs | 14 +- .../Component_WithRef/TestComponent.ir.txt | 3 +- .../TestComponent.mappings.txt | 13 +- .../TestComponent.codegen.cs | 41 +++++ .../TestComponent.ir.txt | 13 ++ .../TestComponent.mappings.txt | 16 ++ .../TestComponent.codegen.cs | 14 +- .../TestComponent.ir.txt | 3 +- .../TestComponent.mappings.txt | 13 +- .../Element_WithRef/TestComponent.codegen.cs | 14 +- .../Element_WithRef/TestComponent.ir.txt | 3 +- .../TestComponent.mappings.txt | 13 +- .../TestComponent.codegen.cs | 8 +- .../TestComponent.ir.txt | 5 +- .../TestComponent.mappings.txt | 12 +- .../TestComponent.codegen.cs | 42 +++++ .../TestComponent.ir.txt | 15 ++ .../TestComponent.mappings.txt | 16 ++ .../TestComponent.codegen.cs | 38 +++++ .../TestComponent.diagnostics.txt | 1 + .../TestComponent.ir.txt | 14 ++ .../TestComponent.mappings.txt | 5 + .../TestComponent.codegen.cs | 14 +- .../TestComponent.ir.txt | 3 +- .../TestComponent.mappings.txt | 13 +- .../TestComponent.codegen.cs | 50 ++++++ .../TestComponent.ir.txt | 17 ++ .../TestComponent.mappings.txt | 16 ++ .../TestComponent.codegen.cs | 5 + .../TestComponent.ir.txt | 1 + .../TestComponent.mappings.txt | 4 +- .../TestComponent.codegen.cs | 61 +++++++ .../TestComponent.ir.txt | 18 +++ .../TestComponent.mappings.txt | 16 ++ .../src/RefTagHelperDescriptorProvider.cs | 20 ++- .../RefTagHelperDescriptorProviderTest.cs | 11 +- 76 files changed, 1076 insertions(+), 302 deletions(-) create mode 100644 src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Component_WithRef_SuppressField/TestComponent.codegen.cs create mode 100644 src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Component_WithRef_SuppressField/TestComponent.ir.txt create mode 100644 src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Component_WithRef_SuppressField/TestComponent.mappings.txt create mode 100644 src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Element_WithRef_SuppressField/TestComponent.codegen.cs create mode 100644 src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Element_WithRef_SuppressField/TestComponent.ir.txt create mode 100644 src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Element_WithRef_SuppressField/TestComponent.mappings.txt create mode 100644 src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Element_WithRef_SuppressFieldWithValue_ResultsInDiagnostic/TestComponent.codegen.cs create mode 100644 src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Element_WithRef_SuppressFieldWithValue_ResultsInDiagnostic/TestComponent.diagnostics.txt create mode 100644 src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Element_WithRef_SuppressFieldWithValue_ResultsInDiagnostic/TestComponent.ir.txt create mode 100644 src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Element_WithRef_SuppressFieldWithValue_ResultsInDiagnostic/TestComponent.mappings.txt rename src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/{GenericComponent_WithComponentRef => GenericComponent_WithComponentRef_SuppressField}/TestComponent.codegen.cs (93%) rename src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/{GenericComponent_WithComponentRef => GenericComponent_WithComponentRef_SuppressField}/TestComponent.ir.txt (71%) rename src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/{GenericComponent_WithComponentRef => GenericComponent_WithComponentRef_SuppressField}/TestComponent.mappings.txt (57%) rename src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/{GenericComponent_WithComponentRef_TypeInference => GenericComponent_WithComponentRef_TypeInference_SuppressField}/TestComponent.codegen.cs (100%) rename src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/{GenericComponent_WithComponentRef_TypeInference => GenericComponent_WithComponentRef_TypeInference_SuppressField}/TestComponent.ir.txt (79%) rename src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/{GenericComponent_WithComponentRef_TypeInference => GenericComponent_WithComponentRef_TypeInference_SuppressField}/TestComponent.mappings.txt (87%) create mode 100644 src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/Component_WithRef_SuppressField/TestComponent.codegen.cs create mode 100644 src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/Component_WithRef_SuppressField/TestComponent.ir.txt create mode 100644 src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/Component_WithRef_SuppressField/TestComponent.mappings.txt create mode 100644 src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/Element_WithRef_SuppressField/TestComponent.codegen.cs create mode 100644 src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/Element_WithRef_SuppressField/TestComponent.ir.txt create mode 100644 src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/Element_WithRef_SuppressField/TestComponent.mappings.txt create mode 100644 src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/Element_WithRef_SuppressFieldWithValue_ResultsInDiagnostic/TestComponent.codegen.cs create mode 100644 src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/Element_WithRef_SuppressFieldWithValue_ResultsInDiagnostic/TestComponent.diagnostics.txt create mode 100644 src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/Element_WithRef_SuppressFieldWithValue_ResultsInDiagnostic/TestComponent.ir.txt create mode 100644 src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/Element_WithRef_SuppressFieldWithValue_ResultsInDiagnostic/TestComponent.mappings.txt create mode 100644 src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/GenericComponent_WithComponentRef_SuppressField/TestComponent.codegen.cs create mode 100644 src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/GenericComponent_WithComponentRef_SuppressField/TestComponent.ir.txt create mode 100644 src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/GenericComponent_WithComponentRef_SuppressField/TestComponent.mappings.txt create mode 100644 src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/GenericComponent_WithComponentRef_TypeInference_SuppressField/TestComponent.codegen.cs create mode 100644 src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/GenericComponent_WithComponentRef_TypeInference_SuppressField/TestComponent.ir.txt create mode 100644 src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/GenericComponent_WithComponentRef_TypeInference_SuppressField/TestComponent.mappings.txt diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/CodeGeneration/CodeWriterExtensions.cs b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/CodeGeneration/CodeWriterExtensions.cs index 9023e4d369..eee9f37927 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/CodeGeneration/CodeWriterExtensions.cs +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/CodeGeneration/CodeWriterExtensions.cs @@ -263,8 +263,13 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration string.Format(CultureInfo.InvariantCulture, InstanceMethodFormat, instanceName, methodName)); } - public static CodeWriter WriteField(this CodeWriter writer, IList modifiers, string typeName, string fieldName) + public static CodeWriter WriteField(this CodeWriter writer, IList suppressWarnings, IList modifiers, string typeName, string fieldName) { + if (suppressWarnings == null) + { + throw new ArgumentNullException(nameof(suppressWarnings)); + } + if (modifiers == null) { throw new ArgumentNullException(nameof(modifiers)); @@ -280,6 +285,12 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration throw new ArgumentNullException(nameof(fieldName)); } + for (var i = 0; i < suppressWarnings.Count; i++) + { + writer.Write("#pragma warning disable "); + writer.WriteLine(suppressWarnings[i]); + } + for (var i = 0; i < modifiers.Count; i++) { writer.Write(modifiers[i]); @@ -292,6 +303,12 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration writer.Write(";"); writer.WriteLine(); + for (var i = suppressWarnings.Count - 1; i >= 0; i--) + { + writer.Write("#pragma warning restore "); + writer.WriteLine(suppressWarnings[i]); + } + return writer; } diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/CodeGeneration/DefaultDocumentWriter.cs b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/CodeGeneration/DefaultDocumentWriter.cs index fcae6150cb..c5fb4de90e 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/CodeGeneration/DefaultDocumentWriter.cs +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/CodeGeneration/DefaultDocumentWriter.cs @@ -206,7 +206,7 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration public override void VisitFieldDeclaration(FieldDeclarationIntermediateNode node) { - Context.CodeWriter.WriteField(node.Modifiers, node.FieldType, node.FieldName); + Context.CodeWriter.WriteField(node.SuppressWarnings, node.Modifiers, node.FieldType, node.FieldName); } public override void VisitPropertyDeclaration(PropertyDeclarationIntermediateNode node) diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/ComponentResources.resx b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/ComponentResources.resx index 178f1bb59b..f932064455 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/ComponentResources.resx +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/ComponentResources.resx @@ -193,7 +193,10 @@ route template - Populates the specified field or property with a reference to the element or component. + Generates the specified field, and populates it during rendering with a reference to the element or component. + + + Suppresses the generation of a field by '@ref'. Specify '@ref:suppressField' to define your own property or field. Merges a collection of attributes into the current element or component. diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/Components/ComponentDiagnosticFactory.cs b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/Components/ComponentDiagnosticFactory.cs index 8eba2b1efb..07a2657a6c 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/Components/ComponentDiagnosticFactory.cs +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/Components/ComponentDiagnosticFactory.cs @@ -423,5 +423,31 @@ namespace Microsoft.AspNetCore.Razor.Language.Components { return RazorDiagnostic.Create(DuplicateComponentParameterDirective, source ?? SourceSpan.Undefined, attributeName, directiveAttributeName); } + + public static readonly RazorDiagnosticDescriptor RefSuppressFieldNotMinimized = + new RazorDiagnosticDescriptor( + $"{DiagnosticPrefix}10011", + () => + "The directive attribute '@ref:suppressField' must be used as a minimized attribute. Providing an attribute value like '@ref:suppressField=\"false\"' " + + "is not supported.", + RazorDiagnosticSeverity.Error); + + public static RazorDiagnostic Create_RefSuppressFieldNotMinimized(SourceSpan? source = null) + { + return RazorDiagnostic.Create(RefSuppressFieldNotMinimized, source ?? SourceSpan.Undefined); + } + + public static readonly RazorDiagnosticDescriptor RefSuppressFieldRequiredForGeneric = + new RazorDiagnosticDescriptor( + $"{DiagnosticPrefix}10012", + () => + "Using '@ref' on a generic-typed component requires manually defining a field to hold the component reference. Use '@ref:suppressField' to " + + "suppress field generation, and manually define a field inside '@code {{ }}' to contain the reference.", + RazorDiagnosticSeverity.Error); + + public static RazorDiagnostic Create_RefSuppressFieldRequiredForGeneric(SourceSpan? source = null) + { + return RazorDiagnostic.Create(RefSuppressFieldRequiredForGeneric, source ?? SourceSpan.Undefined); + } } } diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/Components/ComponentReferenceCaptureLoweringPass.cs b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/Components/ComponentReferenceCaptureLoweringPass.cs index b4d776aacf..6a24a46f80 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/Components/ComponentReferenceCaptureLoweringPass.cs +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/Components/ComponentReferenceCaptureLoweringPass.cs @@ -1,6 +1,8 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +using System; +using System.Linq; using Microsoft.AspNetCore.Razor.Language.Intermediate; namespace Microsoft.AspNetCore.Razor.Language.Components @@ -33,34 +35,109 @@ namespace Microsoft.AspNetCore.Razor.Language.Components if (node.TagHelper.IsRefTagHelper()) { - reference.Replace(RewriteUsage(@class, reference.Parent, node)); + // We've found an @ref directive attribute. + // + // If we can't get a nonempty identifier, do nothing because there will + // already be a diagnostic for empty values + var identifier = DetermineIdentifierToken(node); + if (identifier == null) + { + continue; + } + + var rewritten = RewriteUsage(reference.Parent, identifier); + reference.Replace(rewritten); + + // Now we need to check if the field generation has been suppressed. + // + // You have to suppress field generation for generic types because we don't know the + // type name to create the field. + var generateField = ShouldGenerateField(reference.Parent); + + // Insert the field with other fields, near the top of the class. + if (generateField) + { + var position = 0; + while (position < @class.Children.Count && @class.Children[i] is FieldDeclarationIntermediateNode) + { + position++; + } + + @class.Children.Insert(position, CreateField(rewritten.FieldTypeName, identifier)); + } } } } - private IntermediateNode RewriteUsage(ClassDeclarationIntermediateNode classNode, IntermediateNode parent, TagHelperDirectiveAttributeIntermediateNode node) + private ReferenceCaptureIntermediateNode RewriteUsage(IntermediateNode parent, IntermediateToken identifier) { - // If we can't get a nonempty attribute name, do nothing because there will - // already be a diagnostic for empty values - var identifierToken = DetermineIdentifierToken(node); - if (identifierToken == null) - { - return node; - } - // Determine whether this is an element capture or a component capture, and // if applicable the type name that will appear in the resulting capture code var componentTagHelper = (parent as ComponentIntermediateNode)?.Component; if (componentTagHelper != null) { - return new ReferenceCaptureIntermediateNode(identifierToken, componentTagHelper.GetTypeName()); + return new ReferenceCaptureIntermediateNode(identifier, componentTagHelper.GetTypeName()); } else { - return new ReferenceCaptureIntermediateNode(identifierToken); + return new ReferenceCaptureIntermediateNode(identifier); } } + private bool ShouldGenerateField(IntermediateNode parent) + { + var parameters = parent.FindDescendantNodes(); + for (var i = 0; i < parameters.Count; i++) + { + var parameter = parameters[i]; + if (parameter.TagHelper.IsRefTagHelper() && parameter.BoundAttributeParameter.Name == "suppressField") + { + if (parameter.HasDiagnostics) + { + parent.Diagnostics.AddRange(parameter.GetAllDiagnostics()); + } + + parent.Children.Remove(parameter); + + if (parameter.AttributeStructure == AttributeStructure.Minimized) + { + return false; + } + + // We do not support non-minimized attributes here because we can't allow the value to be dynamic. + // As a design/experience decision, we don't let you write @ref:suppressField="false" even though + // we could parse it. The rationale is that it's misleading, you type something that looks like code, + // but it's not really. + parent.Diagnostics.Add(ComponentDiagnosticFactory.Create_RefSuppressFieldNotMinimized(parameter.Source)); + } + } + + if (parent is ComponentIntermediateNode component && component.Component.IsGenericTypedComponent()) + { + // We cannot automatically generate a 'ref' field for generic components because we don't know + // how to write the type. + parent.Diagnostics.Add(ComponentDiagnosticFactory.Create_RefSuppressFieldRequiredForGeneric(parent.Source)); + return false; + } + + return true; + } + + private IntermediateNode CreateField(string fieldType, IntermediateToken identifier) + { + return new FieldDeclarationIntermediateNode() + { + FieldName = identifier.Content, + FieldType = fieldType, + Modifiers = { "private" }, + SuppressWarnings = + { + "0414", // Field is assigned by never used + "0169", // Field is never used + }, + }; + } + private IntermediateToken DetermineIdentifierToken(TagHelperDirectiveAttributeIntermediateNode attributeNode) { IntermediateToken foundToken = null; diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/DefaultRazorIntermediateNodeLoweringPhase.cs b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/DefaultRazorIntermediateNodeLoweringPhase.cs index 2e619dfb52..166a2912a4 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/DefaultRazorIntermediateNodeLoweringPhase.cs +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/DefaultRazorIntermediateNodeLoweringPhase.cs @@ -1744,14 +1744,6 @@ namespace Microsoft.AspNetCore.Razor.Language out var parameterMatch, out var associatedAttributeParameterDescriptor)) { - var expectsBooleanValue = associatedAttributeDescriptor.ExpectsBooleanValue(attributeName); - - if (!expectsBooleanValue) - { - // We do not allow minimized non-boolean bound attributes. - return; - } - // Directive attributes should start with '@' unless the descriptors are misconfigured. // In that case, we would have already logged an error. var actualAttributeName = attributeName.StartsWith("@") ? attributeName.Substring(1) : attributeName; @@ -1760,6 +1752,13 @@ namespace Microsoft.AspNetCore.Razor.Language if (parameterMatch && TagHelperMatchingConventions.TryGetBoundAttributeParameter(actualAttributeName, out var attributeNameWithoutParameter, out _)) { + var expectsBooleanValue = associatedAttributeParameterDescriptor.IsBooleanProperty; + if (!expectsBooleanValue) + { + // We do not allow minimized non-boolean bound attributes. + return; + } + attributeNode = new TagHelperDirectiveAttributeParameterIntermediateNode() { AttributeName = actualAttributeName, @@ -1775,6 +1774,13 @@ namespace Microsoft.AspNetCore.Razor.Language } else { + var expectsBooleanValue = associatedAttributeDescriptor.ExpectsBooleanValue(attributeName); + if (!expectsBooleanValue) + { + // We do not allow minimized non-boolean bound attributes. + return; + } + attributeNode = new TagHelperDirectiveAttributeIntermediateNode() { AttributeName = actualAttributeName, diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/Extensions/DefaultTagHelperTargetExtension.cs b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/Extensions/DefaultTagHelperTargetExtension.cs index c82b0e2135..959fb6c059 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/Extensions/DefaultTagHelperTargetExtension.cs +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/Extensions/DefaultTagHelperTargetExtension.cs @@ -8,12 +8,15 @@ using System.Linq; using System.Text; using Microsoft.AspNetCore.Razor.Language.CodeGeneration; using Microsoft.AspNetCore.Razor.Language.Intermediate; -using Microsoft.AspNetCore.Razor.Language.Legacy; namespace Microsoft.AspNetCore.Razor.Language.Extensions { internal sealed class DefaultTagHelperTargetExtension : IDefaultTagHelperTargetExtension { + private static readonly string[] FieldUnintializedModifiers = { "0649", }; + + private static readonly string[] FieldUnusedModifiers = { "0169", }; + private static readonly string[] PrivateModifiers = new string[] { "private" }; public string RunnerVariableName { get; set; } = "__tagHelperRunner"; @@ -476,9 +479,7 @@ namespace Microsoft.AspNetCore.Razor.Language.Extensions public void WriteTagHelperRuntime(CodeRenderingContext context, DefaultTagHelperRuntimeIntermediateNode node) { context.CodeWriter.WriteLine("#line hidden"); - context.CodeWriter.WriteLine("#pragma warning disable 0649"); - context.CodeWriter.WriteField(PrivateModifiers, ExecutionContextTypeName, ExecutionContextVariableName); - context.CodeWriter.WriteLine("#pragma warning restore 0649"); + context.CodeWriter.WriteField(FieldUnintializedModifiers, PrivateModifiers, ExecutionContextTypeName, ExecutionContextVariableName); context.CodeWriter .Write("private ") @@ -491,12 +492,8 @@ namespace Microsoft.AspNetCore.Razor.Language.Extensions if (!context.Options.DesignTime) { - // Need to disable the warning "X is never used." for the value buffer since - // whether it's used depends on how a TagHelper is used. - context.CodeWriter.WriteLine("#pragma warning disable 0169"); - context.CodeWriter.WriteField(PrivateModifiers, "string", StringValueBufferVariableName); - context.CodeWriter.WriteLine("#pragma warning restore 0169"); - + context.CodeWriter.WriteField(FieldUnusedModifiers, PrivateModifiers, "string", StringValueBufferVariableName); + var backedScopeManageVariableName = "__backed" + ScopeManagerVariableName; context.CodeWriter .Write("private ") diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/Intermediate/FieldDeclarationIntermediateNode.cs b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/Intermediate/FieldDeclarationIntermediateNode.cs index 0d6b731390..50cc1d10c6 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/Intermediate/FieldDeclarationIntermediateNode.cs +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/Intermediate/FieldDeclarationIntermediateNode.cs @@ -12,6 +12,8 @@ namespace Microsoft.AspNetCore.Razor.Language.Intermediate public IList Modifiers { get; } = new List(); + public IList SuppressWarnings { get; } = new List(); + public string FieldName { get; set; } public string FieldType { get; set; } diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/Intermediate/ReferenceCaptureIntermediateNode.cs b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/Intermediate/ReferenceCaptureIntermediateNode.cs index f70c7525a8..077bd0c1fa 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/Intermediate/ReferenceCaptureIntermediateNode.cs +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/Intermediate/ReferenceCaptureIntermediateNode.cs @@ -2,7 +2,6 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; -using Microsoft.AspNetCore.Razor.Language.CodeGeneration; using Microsoft.AspNetCore.Razor.Language.Components; namespace Microsoft.AspNetCore.Razor.Language.Intermediate @@ -35,7 +34,9 @@ namespace Microsoft.AspNetCore.Razor.Language.Intermediate public string ComponentCaptureTypeName { get; set; } - public string TypeName => $"global::System.Action<{(IsComponentCapture ? ComponentCaptureTypeName : "global::" + ComponentsApi.ElementRef.FullTypeName)}>"; + public string FieldTypeName => IsComponentCapture ? ComponentCaptureTypeName : "global::" + ComponentsApi.ElementRef.FullTypeName; + + public string TypeName => $"global::System.Action<{FieldTypeName}>"; public override void Accept(IntermediateNodeVisitor visitor) { diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/RazorDiagnosticCollection.cs b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/RazorDiagnosticCollection.cs index 71464ad304..3417ee59e8 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/RazorDiagnosticCollection.cs +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/RazorDiagnosticCollection.cs @@ -62,6 +62,16 @@ namespace Microsoft.AspNetCore.Razor.Language _inner.AddRange(items); } + public void AddRange(IEnumerable items) + { + if (items == null) + { + throw new ArgumentNullException(nameof(items)); + } + + _inner.AddRange(items); + } + public void Clear() { _inner.Clear(); diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/CodeGeneration/CSharpCodeWriterTest.cs b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/CodeGeneration/CSharpCodeWriterTest.cs index e0bfa57ca1..cb7973df37 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/CodeGeneration/CSharpCodeWriterTest.cs +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/CodeGeneration/CSharpCodeWriterTest.cs @@ -287,7 +287,7 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration var writer = new CodeWriter(); // Act - writer.WriteField(new[] { "private" }, "global::System.String", "_myString"); + writer.WriteField(Array.Empty(), new[] { "private" }, "global::System.String", "_myString"); // Assert var output = writer.GenerateCode(); @@ -301,13 +301,37 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration var writer = new CodeWriter(); // Act - writer.WriteField(new[] { "private", "readonly", "static" }, "global::System.String", "_myString"); + writer.WriteField(Array.Empty(), new[] { "private", "readonly", "static" }, "global::System.String", "_myString"); // Assert var output = writer.GenerateCode(); Assert.Equal("private readonly static global::System.String _myString;" + Environment.NewLine, output); } + [Fact] + public void WriteField_WithModifiersAndSupressions_WritesFieldDeclaration() + { + // Arrange + var writer = new CodeWriter(); + + // Act + writer.WriteField( + new[] { "0001", "0002", }, + new[] { "private", "readonly", "static" }, + "global::System.String", + "_myString"); + + // Assert + var output = writer.GenerateCode(); + Assert.Equal( + "#pragma warning disable 0001" + Environment.NewLine + + "#pragma warning disable 0002" + Environment.NewLine + + "private readonly static global::System.String _myString;" + Environment.NewLine + + "#pragma warning restore 0002" + Environment.NewLine + + "#pragma warning restore 0001" + Environment.NewLine, + output); + } + [Fact] public void WriteAutoPropertyDeclaration_WritesPropertyDeclaration() { diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/IntegrationTests/ComponentCodeGenerationTestBase.cs b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/IntegrationTests/ComponentCodeGenerationTestBase.cs index 5b17860cd3..a0f7aea1c4 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/IntegrationTests/ComponentCodeGenerationTestBase.cs +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/IntegrationTests/ComponentCodeGenerationTestBase.cs @@ -2994,7 +2994,7 @@ namespace Test } [Fact] - public void GenericComponent_WithComponentRef() + public void GenericComponent_WithComponentRef_CreatesDiagnostic() { // Arrange AdditionalSyntaxTrees.Add(Parse(@" @@ -3012,10 +3012,39 @@ namespace Test // Act var generated = CompileToCSharp(@" - @code { - private MyComponent _my; - public void Foo() { System.GC.KeepAlive(_my); } + MyComponent myInstance; + void DoStuff() { GC.KeepAlive(myInstance); } +} +", throwOnFailure: false); + + // Assert + var diagnostic = Assert.Single(generated.Diagnostics); + Assert.Same(diagnostic.Id, ComponentDiagnosticFactory.RefSuppressFieldRequiredForGeneric.Id); + } + + [Fact] + public void GenericComponent_WithComponentRef_SuppressField() + { + // Arrange + AdditionalSyntaxTrees.Add(Parse(@" +using Microsoft.AspNetCore.Components; + +namespace Test +{ + public class MyComponent : ComponentBase + { + [Parameter] public TItem Item { get; set; } + } +} +")); + + // Act + var generated = CompileToCSharp(@" + +@code { + MyComponent _my; + void DoStuff() { GC.KeepAlive(_my); } } "); @@ -3026,7 +3055,7 @@ namespace Test } [Fact] - public void GenericComponent_WithComponentRef_TypeInference() + public void GenericComponent_WithComponentRef_TypeInference_CreatesDiagnostic() { // Arrange AdditionalSyntaxTrees.Add(Parse(@" @@ -3045,6 +3074,38 @@ namespace Test var generated = CompileToCSharp(@" +@code { + private MyComponent _my; + public void Foo() { System.GC.KeepAlive(_my); } +} +", throwOnFailure: true); + + // Assert + var diagnostic = Assert.Single(generated.Diagnostics); + Assert.Same(diagnostic.Id, ComponentDiagnosticFactory.RefSuppressFieldRequiredForGeneric.Id); + + } + + [Fact] + public void GenericComponent_WithComponentRef_TypeInference_SuppressField() + { + // Arrange + AdditionalSyntaxTrees.Add(Parse(@" +using Microsoft.AspNetCore.Components; + +namespace Test +{ + public class MyComponent : ComponentBase + { + [Parameter] public TItem Item { get; set; } + } +} +")); + + // Act + var generated = CompileToCSharp(@" + + @code { private MyComponent _my; public void Foo() { System.GC.KeepAlive(_my); } @@ -3386,13 +3447,7 @@ namespace Test { // Arrange/Act var generated = CompileToCSharp(@" -Hello - -@code { - private Microsoft.AspNetCore.Components.ElementRef myElem; - public void Foo() { System.GC.KeepAlive(myElem); } -} -"); +Hello"); // Assert AssertDocumentNodeMatchesBaseline(generated.CodeDocument); @@ -3408,10 +3463,7 @@ namespace Test @code { - private ElementRef _element; - [Parameter] protected int Min { get; set; } - public void Foo() { System.GC.KeepAlive(_element); } } "); @@ -3421,6 +3473,35 @@ namespace Test CompileToAssembly(generated); } + [Fact] + public void Element_WithRef_SuppressField() + { + // Arrange/Act + var generated = CompileToCSharp(@" +Hello +@code { + ElementRef myElem; + void DoStuff() { GC.KeepAlive(myElem); } +}"); + + // Assert + AssertDocumentNodeMatchesBaseline(generated.CodeDocument); + AssertCSharpDocumentMatchesBaseline(generated.CodeDocument); + CompileToAssembly(generated); + } + + [Fact] + public void Element_WithRef_SuppressFieldWithValue_ResultsInDiagnostic() + { + // Arrange/Act + var generated = CompileToCSharp(@" +Hello", throwOnFailure: false); + + // Assert + var diagnostic = Assert.Single(generated.Diagnostics); + Assert.Same(diagnostic.Id, ComponentDiagnosticFactory.RefSuppressFieldNotMinimized.Id); + } + [Fact] public void Component_WithRef() { @@ -3438,13 +3519,7 @@ namespace Test // Arrange/Act var generated = CompileToCSharp(@" - - -@code { - private Test.MyComponent myInstance; - public void Foo() { System.GC.KeepAlive(myInstance); } -} -"); +"); // Assert AssertDocumentNodeMatchesBaseline(generated.CodeDocument); @@ -3472,11 +3547,6 @@ namespace Test Some further content - -@code { - private Test.MyComponent myInstance; - public void Foo() { System.GC.KeepAlive(myInstance); } -} "); // Assert @@ -3485,6 +3555,35 @@ namespace Test CompileToAssembly(generated); } + [Fact] + public void Component_WithRef_SuppressField() + { + // Arrange + AdditionalSyntaxTrees.Add(Parse(@" +using Microsoft.AspNetCore.Components; + +namespace Test +{ + public class MyComponent : ComponentBase + { + } +} +")); + + // Arrange/Act + var generated = CompileToCSharp(@" + +@code { + MyComponent myInstance; + void DoStuff() { GC.KeepAlive(myInstance); } +}"); + + // Assert + AssertDocumentNodeMatchesBaseline(generated.CodeDocument); + AssertCSharpDocumentMatchesBaseline(generated.CodeDocument); + CompileToAssembly(generated); + } + #endregion #region Templates diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Component_WithRef/TestComponent.codegen.cs b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Component_WithRef/TestComponent.codegen.cs index 598827ce99..0430bdb3a5 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Component_WithRef/TestComponent.codegen.cs +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Component_WithRef/TestComponent.codegen.cs @@ -10,6 +10,11 @@ namespace Test using Microsoft.AspNetCore.Components; public class TestComponent : Microsoft.AspNetCore.Components.ComponentBase { + #pragma warning disable 0414 + #pragma warning disable 0169 + private Test.MyComponent myInstance; + #pragma warning restore 0169 + #pragma warning restore 0414 #pragma warning disable 219 private void __RazorDirectiveTokenHelpers__() { } @@ -41,15 +46,6 @@ __o = typeof(MyComponent); #nullable disable } #pragma warning restore 1998 -#nullable restore -#line 3 "x:\dir\subdir\Test\TestComponent.cshtml" - - private Test.MyComponent myInstance; - public void Foo() { System.GC.KeepAlive(myInstance); } - -#line default -#line hidden -#nullable disable } } #pragma warning restore 1591 diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Component_WithRef/TestComponent.ir.txt b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Component_WithRef/TestComponent.ir.txt index 2e4013600f..25a4ad4d99 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Component_WithRef/TestComponent.ir.txt +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Component_WithRef/TestComponent.ir.txt @@ -6,6 +6,7 @@ Document - UsingDirective - (73:4,1 [28] ) - System.Threading.Tasks UsingDirective - (104:5,1 [37] ) - Microsoft.AspNetCore.Components ClassDeclaration - - public - TestComponent - Microsoft.AspNetCore.Components.ComponentBase - + FieldDeclaration - - private - Test.MyComponent - myInstance DesignTimeDirective - CSharpCode - IntermediateToken - - CSharp - #pragma warning disable 0414 @@ -22,9 +23,3 @@ Document - ComponentAttribute - - ParamAfter - AttributeStructure.DoubleQuotes HtmlContent - (64:0,64 [5] x:\dir\subdir\Test\TestComponent.cshtml) IntermediateToken - (64:0,64 [5] x:\dir\subdir\Test\TestComponent.cshtml) - Html - after - HtmlContent - (73:0,73 [4] x:\dir\subdir\Test\TestComponent.cshtml) - IntermediateToken - (73:0,73 [4] x:\dir\subdir\Test\TestComponent.cshtml) - Html - \n\n - HtmlContent - (189:5,1 [2] x:\dir\subdir\Test\TestComponent.cshtml) - IntermediateToken - (189:5,1 [2] x:\dir\subdir\Test\TestComponent.cshtml) - Html - \n - CSharpCode - (84:2,7 [104] x:\dir\subdir\Test\TestComponent.cshtml) - IntermediateToken - (84:2,7 [104] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - \n private Test.MyComponent myInstance;\n public void Foo() { System.GC.KeepAlive(myInstance); }\n diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Component_WithRef/TestComponent.mappings.txt b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Component_WithRef/TestComponent.mappings.txt index a0e550be07..8e2f5de343 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Component_WithRef/TestComponent.mappings.txt +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Component_WithRef/TestComponent.mappings.txt @@ -1,16 +1,5 @@ Source Location: (40:0,40 [10] x:\dir\subdir\Test\TestComponent.cshtml) |myInstance| -Generated Location: (1080:29,40 [10] ) +Generated Location: (1278:34,40 [10] ) |myInstance| -Source Location: (84:2,7 [104] x:\dir\subdir\Test\TestComponent.cshtml) -| - private Test.MyComponent myInstance; - public void Foo() { System.GC.KeepAlive(myInstance); } -| -Generated Location: (1445:45,7 [104] ) -| - private Test.MyComponent myInstance; - public void Foo() { System.GC.KeepAlive(myInstance); } -| - diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Component_WithRef_SuppressField/TestComponent.codegen.cs b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Component_WithRef_SuppressField/TestComponent.codegen.cs new file mode 100644 index 0000000000..afee5137ab --- /dev/null +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Component_WithRef_SuppressField/TestComponent.codegen.cs @@ -0,0 +1,53 @@ +// +#pragma warning disable 1591 +namespace Test +{ + #line hidden + using System; + using System.Collections.Generic; + using System.Linq; + using System.Threading.Tasks; + using Microsoft.AspNetCore.Components; + public class TestComponent : Microsoft.AspNetCore.Components.ComponentBase + { + #pragma warning disable 219 + private void __RazorDirectiveTokenHelpers__() { + } + #pragma warning restore 219 + #pragma warning disable 0414 + private static System.Object __o = null; + #pragma warning restore 0414 + #pragma warning disable 1998 + protected override void BuildRenderTree(Microsoft.AspNetCore.Components.RenderTree.RenderTreeBuilder builder) + { + builder.AddAttribute(-1, "ChildContent", (Microsoft.AspNetCore.Components.RenderFragment)((builder2) => { + } + )); +#nullable restore +#line 1 "x:\dir\subdir\Test\TestComponent.cshtml" + myInstance = default(Test.MyComponent); + +#line default +#line hidden +#nullable disable +#nullable restore +#line 1 "x:\dir\subdir\Test\TestComponent.cshtml" +__o = typeof(MyComponent); + +#line default +#line hidden +#nullable disable + } + #pragma warning restore 1998 +#nullable restore +#line 2 "x:\dir\subdir\Test\TestComponent.cshtml" + + MyComponent myInstance; + void DoStuff() { GC.KeepAlive(myInstance); } + +#line default +#line hidden +#nullable disable + } +} +#pragma warning restore 1591 diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Component_WithRef_SuppressField/TestComponent.ir.txt b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Component_WithRef_SuppressField/TestComponent.ir.txt new file mode 100644 index 0000000000..25bc61b8d8 --- /dev/null +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Component_WithRef_SuppressField/TestComponent.ir.txt @@ -0,0 +1,22 @@ +Document - + NamespaceDeclaration - - Test + UsingDirective - (3:1,1 [12] ) - System + UsingDirective - (18:2,1 [32] ) - System.Collections.Generic + UsingDirective - (53:3,1 [17] ) - System.Linq + UsingDirective - (73:4,1 [28] ) - System.Threading.Tasks + UsingDirective - (104:5,1 [37] ) - Microsoft.AspNetCore.Components + ClassDeclaration - - public - TestComponent - Microsoft.AspNetCore.Components.ComponentBase - + DesignTimeDirective - + CSharpCode - + IntermediateToken - - CSharp - #pragma warning disable 0414 + CSharpCode - + IntermediateToken - - CSharp - private static System.Object __o = null; + CSharpCode - + IntermediateToken - - CSharp - #pragma warning restore 0414 + MethodDeclaration - - protected override - void - BuildRenderTree + Component - (0:0,0 [52] x:\dir\subdir\Test\TestComponent.cshtml) - MyComponent + ReferenceCapture - (19:0,19 [10] x:\dir\subdir\Test\TestComponent.cshtml) - myInstance + HtmlContent - (52:0,52 [2] x:\dir\subdir\Test\TestComponent.cshtml) + IntermediateToken - (52:0,52 [2] x:\dir\subdir\Test\TestComponent.cshtml) - Html - \n + CSharpCode - (61:1,7 [81] x:\dir\subdir\Test\TestComponent.cshtml) + IntermediateToken - (61:1,7 [81] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - \n MyComponent myInstance;\n void DoStuff() { GC.KeepAlive(myInstance); }\n diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Component_WithRef_SuppressField/TestComponent.mappings.txt b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Component_WithRef_SuppressField/TestComponent.mappings.txt new file mode 100644 index 0000000000..c97795f34e --- /dev/null +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Component_WithRef_SuppressField/TestComponent.mappings.txt @@ -0,0 +1,16 @@ +Source Location: (19:0,19 [10] x:\dir\subdir\Test\TestComponent.cshtml) +|myInstance| +Generated Location: (1013:27,19 [10] ) +|myInstance| + +Source Location: (61:1,7 [81] x:\dir\subdir\Test\TestComponent.cshtml) +| + MyComponent myInstance; + void DoStuff() { GC.KeepAlive(myInstance); } +| +Generated Location: (1378:43,7 [81] ) +| + MyComponent myInstance; + void DoStuff() { GC.KeepAlive(myInstance); } +| + diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Component_WithRef_WithChildContent/TestComponent.codegen.cs b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Component_WithRef_WithChildContent/TestComponent.codegen.cs index 6befd8f44c..672c95d6cc 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Component_WithRef_WithChildContent/TestComponent.codegen.cs +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Component_WithRef_WithChildContent/TestComponent.codegen.cs @@ -10,6 +10,11 @@ namespace Test using Microsoft.AspNetCore.Components; public class TestComponent : Microsoft.AspNetCore.Components.ComponentBase { + #pragma warning disable 0414 + #pragma warning disable 0169 + private Test.MyComponent myInstance; + #pragma warning restore 0169 + #pragma warning restore 0414 #pragma warning disable 219 private void __RazorDirectiveTokenHelpers__() { } @@ -40,15 +45,6 @@ __o = typeof(MyComponent); #nullable disable } #pragma warning restore 1998 -#nullable restore -#line 5 "x:\dir\subdir\Test\TestComponent.cshtml" - - private Test.MyComponent myInstance; - public void Foo() { System.GC.KeepAlive(myInstance); } - -#line default -#line hidden -#nullable disable } } #pragma warning restore 1591 diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Component_WithRef_WithChildContent/TestComponent.ir.txt b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Component_WithRef_WithChildContent/TestComponent.ir.txt index a2aae2c90d..514feb7edb 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Component_WithRef_WithChildContent/TestComponent.ir.txt +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Component_WithRef_WithChildContent/TestComponent.ir.txt @@ -6,6 +6,7 @@ Document - UsingDirective - (73:4,1 [28] ) - System.Threading.Tasks UsingDirective - (104:5,1 [37] ) - Microsoft.AspNetCore.Components ClassDeclaration - - public - TestComponent - Microsoft.AspNetCore.Components.ComponentBase - + FieldDeclaration - - private - Test.MyComponent - myInstance DesignTimeDirective - CSharpCode - IntermediateToken - - CSharp - #pragma warning disable 0414 @@ -27,9 +28,5 @@ Document - ComponentAttribute - - SomeProp - AttributeStructure.DoubleQuotes HtmlContent - (41:0,41 [3] x:\dir\subdir\Test\TestComponent.cshtml) IntermediateToken - (41:0,41 [3] x:\dir\subdir\Test\TestComponent.cshtml) - Html - val - HtmlContent - (97:2,14 [4] x:\dir\subdir\Test\TestComponent.cshtml) - IntermediateToken - (97:2,14 [4] x:\dir\subdir\Test\TestComponent.cshtml) - Html - \n\n - HtmlContent - (213:7,1 [2] x:\dir\subdir\Test\TestComponent.cshtml) - IntermediateToken - (213:7,1 [2] x:\dir\subdir\Test\TestComponent.cshtml) - Html - \n - CSharpCode - (108:4,7 [104] x:\dir\subdir\Test\TestComponent.cshtml) - IntermediateToken - (108:4,7 [104] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - \n private Test.MyComponent myInstance;\n public void Foo() { System.GC.KeepAlive(myInstance); }\n + HtmlContent - (97:2,14 [2] x:\dir\subdir\Test\TestComponent.cshtml) + IntermediateToken - (97:2,14 [2] x:\dir\subdir\Test\TestComponent.cshtml) - Html - \n diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Component_WithRef_WithChildContent/TestComponent.mappings.txt b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Component_WithRef_WithChildContent/TestComponent.mappings.txt index f2715276d6..39cc0a17d0 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Component_WithRef_WithChildContent/TestComponent.mappings.txt +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Component_WithRef_WithChildContent/TestComponent.mappings.txt @@ -1,16 +1,5 @@ Source Location: (19:0,19 [10] x:\dir\subdir\Test\TestComponent.cshtml) |myInstance| -Generated Location: (1036:28,19 [10] ) +Generated Location: (1234:33,19 [10] ) |myInstance| -Source Location: (108:4,7 [104] x:\dir\subdir\Test\TestComponent.cshtml) -| - private Test.MyComponent myInstance; - public void Foo() { System.GC.KeepAlive(myInstance); } -| -Generated Location: (1401:44,7 [104] ) -| - private Test.MyComponent myInstance; - public void Foo() { System.GC.KeepAlive(myInstance); } -| - diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Element_WithRef/TestComponent.codegen.cs b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Element_WithRef/TestComponent.codegen.cs index c4ecda875b..c74b523370 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Element_WithRef/TestComponent.codegen.cs +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Element_WithRef/TestComponent.codegen.cs @@ -10,6 +10,11 @@ namespace Test using Microsoft.AspNetCore.Components; public class TestComponent : Microsoft.AspNetCore.Components.ComponentBase { + #pragma warning disable 0414 + #pragma warning disable 0169 + private global::Microsoft.AspNetCore.Components.ElementRef myElem; + #pragma warning restore 0169 + #pragma warning restore 0414 #pragma warning disable 219 private void __RazorDirectiveTokenHelpers__() { } @@ -29,15 +34,6 @@ namespace Test #nullable disable } #pragma warning restore 1998 -#nullable restore -#line 3 "x:\dir\subdir\Test\TestComponent.cshtml" - - private Microsoft.AspNetCore.Components.ElementRef myElem; - public void Foo() { System.GC.KeepAlive(myElem); } - -#line default -#line hidden -#nullable disable } } #pragma warning restore 1591 diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Element_WithRef/TestComponent.ir.txt b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Element_WithRef/TestComponent.ir.txt index d92f17dce9..d88a3bbec3 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Element_WithRef/TestComponent.ir.txt +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Element_WithRef/TestComponent.ir.txt @@ -6,6 +6,7 @@ Document - UsingDirective - (73:4,1 [28] ) - System.Threading.Tasks UsingDirective - (104:5,1 [37] ) - Microsoft.AspNetCore.Components ClassDeclaration - - public - TestComponent - Microsoft.AspNetCore.Components.ComponentBase - + FieldDeclaration - - private - global::Microsoft.AspNetCore.Components.ElementRef - myElem DesignTimeDirective - CSharpCode - IntermediateToken - - CSharp - #pragma warning disable 0414 @@ -24,9 +25,3 @@ Document - HtmlAttribute - - attributeafter=" - " HtmlAttributeValue - (61:0,61 [5] x:\dir\subdir\Test\TestComponent.cshtml) - IntermediateToken - (61:0,61 [5] x:\dir\subdir\Test\TestComponent.cshtml) - Html - after - HtmlContent - (80:0,80 [4] x:\dir\subdir\Test\TestComponent.cshtml) - IntermediateToken - (80:0,80 [4] x:\dir\subdir\Test\TestComponent.cshtml) - Html - \n\n - HtmlContent - (214:5,1 [2] x:\dir\subdir\Test\TestComponent.cshtml) - IntermediateToken - (214:5,1 [2] x:\dir\subdir\Test\TestComponent.cshtml) - Html - \n - CSharpCode - (91:2,7 [122] x:\dir\subdir\Test\TestComponent.cshtml) - IntermediateToken - (91:2,7 [122] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - \n private Microsoft.AspNetCore.Components.ElementRef myElem;\n public void Foo() { System.GC.KeepAlive(myElem); }\n diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Element_WithRef/TestComponent.mappings.txt b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Element_WithRef/TestComponent.mappings.txt index 75a139f95e..64a2a111d5 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Element_WithRef/TestComponent.mappings.txt +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Element_WithRef/TestComponent.mappings.txt @@ -1,16 +1,5 @@ Source Location: (37:0,37 [6] x:\dir\subdir\Test\TestComponent.cshtml) |myElem| -Generated Location: (880:24,37 [6] ) +Generated Location: (1108:29,37 [6] ) |myElem| -Source Location: (91:2,7 [122] x:\dir\subdir\Test\TestComponent.cshtml) -| - private Microsoft.AspNetCore.Components.ElementRef myElem; - public void Foo() { System.GC.KeepAlive(myElem); } -| -Generated Location: (1119:33,7 [122] ) -| - private Microsoft.AspNetCore.Components.ElementRef myElem; - public void Foo() { System.GC.KeepAlive(myElem); } -| - diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Element_WithRef_AndOtherAttributes/TestComponent.codegen.cs b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Element_WithRef_AndOtherAttributes/TestComponent.codegen.cs index dfe7b20a37..a65ff0c394 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Element_WithRef_AndOtherAttributes/TestComponent.codegen.cs +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Element_WithRef_AndOtherAttributes/TestComponent.codegen.cs @@ -10,6 +10,11 @@ namespace Test using Microsoft.AspNetCore.Components; public class TestComponent : Microsoft.AspNetCore.Components.ComponentBase { + #pragma warning disable 0414 + #pragma warning disable 0169 + private global::Microsoft.AspNetCore.Components.ElementRef _element; + #pragma warning restore 0169 + #pragma warning restore 0414 #pragma warning disable 219 private void __RazorDirectiveTokenHelpers__() { } @@ -41,10 +46,7 @@ namespace Test #nullable restore #line 3 "x:\dir\subdir\Test\TestComponent.cshtml" - private ElementRef _element; - [Parameter] protected int Min { get; set; } - public void Foo() { System.GC.KeepAlive(_element); } #line default diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Element_WithRef_AndOtherAttributes/TestComponent.ir.txt b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Element_WithRef_AndOtherAttributes/TestComponent.ir.txt index 2f79627e70..59d4ed931f 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Element_WithRef_AndOtherAttributes/TestComponent.ir.txt +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Element_WithRef_AndOtherAttributes/TestComponent.ir.txt @@ -6,6 +6,7 @@ Document - UsingDirective - (73:4,1 [28] ) - System.Threading.Tasks UsingDirective - (104:5,1 [37] ) - Microsoft.AspNetCore.Components ClassDeclaration - - public - TestComponent - Microsoft.AspNetCore.Components.ComponentBase - + FieldDeclaration - - private - global::Microsoft.AspNetCore.Components.ElementRef - _element DesignTimeDirective - CSharpCode - IntermediateToken - - CSharp - #pragma warning disable 0414 @@ -24,7 +25,7 @@ Document - ReferenceCapture - (49:0,49 [8] x:\dir\subdir\Test\TestComponent.cshtml) - _element HtmlContent - (61:0,61 [4] x:\dir\subdir\Test\TestComponent.cshtml) IntermediateToken - (61:0,61 [4] x:\dir\subdir\Test\TestComponent.cshtml) - Html - \n\n - HtmlContent - (234:7,5 [2] x:\dir\subdir\Test\TestComponent.cshtml) - IntermediateToken - (234:7,5 [2] x:\dir\subdir\Test\TestComponent.cshtml) - Html - \n - CSharpCode - (72:2,7 [161] x:\dir\subdir\Test\TestComponent.cshtml) - IntermediateToken - (72:2,7 [161] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - \n private ElementRef _element;\n\n [Parameter] protected int Min { get; set; }\n public void Foo() { System.GC.KeepAlive(_element); }\n + HtmlContent - (132:4,5 [2] x:\dir\subdir\Test\TestComponent.cshtml) + IntermediateToken - (132:4,5 [2] x:\dir\subdir\Test\TestComponent.cshtml) - Html - \n + CSharpCode - (72:2,7 [59] x:\dir\subdir\Test\TestComponent.cshtml) + IntermediateToken - (72:2,7 [59] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - \n [Parameter] protected int Min { get; set; }\n diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Element_WithRef_AndOtherAttributes/TestComponent.mappings.txt b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Element_WithRef_AndOtherAttributes/TestComponent.mappings.txt index b9dcf79899..13699c88b3 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Element_WithRef_AndOtherAttributes/TestComponent.mappings.txt +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Element_WithRef_AndOtherAttributes/TestComponent.mappings.txt @@ -1,25 +1,19 @@ Source Location: (37:0,37 [3] x:\dir\subdir\Test\TestComponent.cshtml) |Min| -Generated Location: (900:25,37 [3] ) +Generated Location: (1130:30,37 [3] ) |Min| Source Location: (49:0,49 [8] x:\dir\subdir\Test\TestComponent.cshtml) |_element| -Generated Location: (1089:33,49 [8] ) +Generated Location: (1319:38,49 [8] ) |_element| -Source Location: (72:2,7 [161] x:\dir\subdir\Test\TestComponent.cshtml) +Source Location: (72:2,7 [59] x:\dir\subdir\Test\TestComponent.cshtml) | - private ElementRef _element; - [Parameter] protected int Min { get; set; } - public void Foo() { System.GC.KeepAlive(_element); } | -Generated Location: (1330:42,7 [161] ) +Generated Location: (1560:47,7 [59] ) | - private ElementRef _element; - [Parameter] protected int Min { get; set; } - public void Foo() { System.GC.KeepAlive(_element); } | diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Element_WithRef_SuppressField/TestComponent.codegen.cs b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Element_WithRef_SuppressField/TestComponent.codegen.cs new file mode 100644 index 0000000000..c1596ddf4a --- /dev/null +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Element_WithRef_SuppressField/TestComponent.codegen.cs @@ -0,0 +1,43 @@ +// +#pragma warning disable 1591 +namespace Test +{ + #line hidden + using System; + using System.Collections.Generic; + using System.Linq; + using System.Threading.Tasks; + using Microsoft.AspNetCore.Components; + public class TestComponent : Microsoft.AspNetCore.Components.ComponentBase + { + #pragma warning disable 219 + private void __RazorDirectiveTokenHelpers__() { + } + #pragma warning restore 219 + #pragma warning disable 0414 + private static System.Object __o = null; + #pragma warning restore 0414 + #pragma warning disable 1998 + protected override void BuildRenderTree(Microsoft.AspNetCore.Components.RenderTree.RenderTreeBuilder builder) + { +#nullable restore +#line 1 "x:\dir\subdir\Test\TestComponent.cshtml" + myElem = default(Microsoft.AspNetCore.Components.ElementRef); + +#line default +#line hidden +#nullable disable + } + #pragma warning restore 1998 +#nullable restore +#line 2 "x:\dir\subdir\Test\TestComponent.cshtml" + + ElementRef myElem; + void DoStuff() { GC.KeepAlive(myElem); } + +#line default +#line hidden +#nullable disable + } +} +#pragma warning restore 1591 diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Element_WithRef_SuppressField/TestComponent.ir.txt b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Element_WithRef_SuppressField/TestComponent.ir.txt new file mode 100644 index 0000000000..5cb785f84e --- /dev/null +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Element_WithRef_SuppressField/TestComponent.ir.txt @@ -0,0 +1,24 @@ +Document - + NamespaceDeclaration - - Test + UsingDirective - (3:1,1 [12] ) - System + UsingDirective - (18:2,1 [32] ) - System.Collections.Generic + UsingDirective - (53:3,1 [17] ) - System.Linq + UsingDirective - (73:4,1 [28] ) - System.Threading.Tasks + UsingDirective - (104:5,1 [37] ) - Microsoft.AspNetCore.Components + ClassDeclaration - - public - TestComponent - Microsoft.AspNetCore.Components.ComponentBase - + DesignTimeDirective - + CSharpCode - + IntermediateToken - - CSharp - #pragma warning disable 0414 + CSharpCode - + IntermediateToken - - CSharp - private static System.Object __o = null; + CSharpCode - + IntermediateToken - - CSharp - #pragma warning restore 0414 + MethodDeclaration - - protected override - void - BuildRenderTree + MarkupElement - (0:0,0 [51] x:\dir\subdir\Test\TestComponent.cshtml) - elem + HtmlContent - (39:0,39 [5] x:\dir\subdir\Test\TestComponent.cshtml) + IntermediateToken - (39:0,39 [5] x:\dir\subdir\Test\TestComponent.cshtml) - Html - Hello + ReferenceCapture - (12:0,12 [6] x:\dir\subdir\Test\TestComponent.cshtml) - myElem + HtmlContent - (51:0,51 [2] x:\dir\subdir\Test\TestComponent.cshtml) + IntermediateToken - (51:0,51 [2] x:\dir\subdir\Test\TestComponent.cshtml) - Html - \n + CSharpCode - (60:1,7 [72] x:\dir\subdir\Test\TestComponent.cshtml) + IntermediateToken - (60:1,7 [72] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - \n ElementRef myElem;\n void DoStuff() { GC.KeepAlive(myElem); }\n diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Element_WithRef_SuppressField/TestComponent.mappings.txt b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Element_WithRef_SuppressField/TestComponent.mappings.txt new file mode 100644 index 0000000000..a4beb28484 --- /dev/null +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Element_WithRef_SuppressField/TestComponent.mappings.txt @@ -0,0 +1,16 @@ +Source Location: (12:0,12 [6] x:\dir\subdir\Test\TestComponent.cshtml) +|myElem| +Generated Location: (855:24,12 [6] ) +|myElem| + +Source Location: (60:1,7 [72] x:\dir\subdir\Test\TestComponent.cshtml) +| + ElementRef myElem; + void DoStuff() { GC.KeepAlive(myElem); } +| +Generated Location: (1094:33,7 [72] ) +| + ElementRef myElem; + void DoStuff() { GC.KeepAlive(myElem); } +| + diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Element_WithRef_SuppressFieldWithValue_ResultsInDiagnostic/TestComponent.codegen.cs b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Element_WithRef_SuppressFieldWithValue_ResultsInDiagnostic/TestComponent.codegen.cs new file mode 100644 index 0000000000..f98dd68089 --- /dev/null +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Element_WithRef_SuppressFieldWithValue_ResultsInDiagnostic/TestComponent.codegen.cs @@ -0,0 +1,39 @@ +// +#pragma warning disable 1591 +namespace Test +{ + #line hidden + using System; + using System.Collections.Generic; + using System.Linq; + using System.Threading.Tasks; + using Microsoft.AspNetCore.Components; + public class TestComponent : Microsoft.AspNetCore.Components.ComponentBase + { + #pragma warning disable 0414 + #pragma warning disable 0169 + private global::Microsoft.AspNetCore.Components.ElementRef myElem; + #pragma warning restore 0169 + #pragma warning restore 0414 + #pragma warning disable 219 + private void __RazorDirectiveTokenHelpers__() { + } + #pragma warning restore 219 + #pragma warning disable 0414 + private static System.Object __o = null; + #pragma warning restore 0414 + #pragma warning disable 1998 + protected override void BuildRenderTree(Microsoft.AspNetCore.Components.RenderTree.RenderTreeBuilder builder) + { +#nullable restore +#line 1 "x:\dir\subdir\Test\TestComponent.cshtml" + myElem = default(Microsoft.AspNetCore.Components.ElementRef); + +#line default +#line hidden +#nullable disable + } + #pragma warning restore 1998 + } +} +#pragma warning restore 1591 diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Element_WithRef_SuppressFieldWithValue_ResultsInDiagnostic/TestComponent.diagnostics.txt b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Element_WithRef_SuppressFieldWithValue_ResultsInDiagnostic/TestComponent.diagnostics.txt new file mode 100644 index 0000000000..bd82282e59 --- /dev/null +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Element_WithRef_SuppressFieldWithValue_ResultsInDiagnostic/TestComponent.diagnostics.txt @@ -0,0 +1 @@ +x:\dir\subdir\Test\TestComponent.cshtml(1,41): Error RZ10011: The directive attribute '@ref:suppressField' must be used as a minimized attribute. Providing an attribute value like '@ref:suppressField="false" is not supported. diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Element_WithRef_SuppressFieldWithValue_ResultsInDiagnostic/TestComponent.ir.txt b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Element_WithRef_SuppressFieldWithValue_ResultsInDiagnostic/TestComponent.ir.txt new file mode 100644 index 0000000000..69ddf6a8cd --- /dev/null +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Element_WithRef_SuppressFieldWithValue_ResultsInDiagnostic/TestComponent.ir.txt @@ -0,0 +1,21 @@ +Document - + NamespaceDeclaration - - Test + UsingDirective - (3:1,1 [12] ) - System + UsingDirective - (18:2,1 [32] ) - System.Collections.Generic + UsingDirective - (53:3,1 [17] ) - System.Linq + UsingDirective - (73:4,1 [28] ) - System.Threading.Tasks + UsingDirective - (104:5,1 [37] ) - Microsoft.AspNetCore.Components + ClassDeclaration - - public - TestComponent - Microsoft.AspNetCore.Components.ComponentBase - + FieldDeclaration - - private - global::Microsoft.AspNetCore.Components.ElementRef - myElem + DesignTimeDirective - + CSharpCode - + IntermediateToken - - CSharp - #pragma warning disable 0414 + CSharpCode - + IntermediateToken - - CSharp - private static System.Object __o = null; + CSharpCode - + IntermediateToken - - CSharp - #pragma warning restore 0414 + MethodDeclaration - - protected override - void - BuildRenderTree + MarkupElement - (0:0,0 [59] x:\dir\subdir\Test\TestComponent.cshtml) - elem + HtmlContent - (47:0,47 [5] x:\dir\subdir\Test\TestComponent.cshtml) + IntermediateToken - (47:0,47 [5] x:\dir\subdir\Test\TestComponent.cshtml) - Html - Hello + ReferenceCapture - (12:0,12 [6] x:\dir\subdir\Test\TestComponent.cshtml) - myElem diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Element_WithRef_SuppressFieldWithValue_ResultsInDiagnostic/TestComponent.mappings.txt b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Element_WithRef_SuppressFieldWithValue_ResultsInDiagnostic/TestComponent.mappings.txt new file mode 100644 index 0000000000..b77841ef4b --- /dev/null +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Element_WithRef_SuppressFieldWithValue_ResultsInDiagnostic/TestComponent.mappings.txt @@ -0,0 +1,5 @@ +Source Location: (12:0,12 [6] x:\dir\subdir\Test\TestComponent.cshtml) +|myElem| +Generated Location: (1083:29,12 [6] ) +|myElem| + diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/GenericComponent_WithComponentRef/TestComponent.codegen.cs b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/GenericComponent_WithComponentRef_SuppressField/TestComponent.codegen.cs similarity index 93% rename from src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/GenericComponent_WithComponentRef/TestComponent.codegen.cs rename to src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/GenericComponent_WithComponentRef_SuppressField/TestComponent.codegen.cs index dbf2201c83..56d704b4b6 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/GenericComponent_WithComponentRef/TestComponent.codegen.cs +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/GenericComponent_WithComponentRef_SuppressField/TestComponent.codegen.cs @@ -58,10 +58,10 @@ __o = typeof(MyComponent<>); } #pragma warning restore 1998 #nullable restore -#line 3 "x:\dir\subdir\Test\TestComponent.cshtml" +#line 2 "x:\dir\subdir\Test\TestComponent.cshtml" - private MyComponent _my; - public void Foo() { System.GC.KeepAlive(_my); } + MyComponent _my; + void DoStuff() { GC.KeepAlive(_my); } #line default #line hidden diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/GenericComponent_WithComponentRef/TestComponent.ir.txt b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/GenericComponent_WithComponentRef_SuppressField/TestComponent.ir.txt similarity index 71% rename from src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/GenericComponent_WithComponentRef/TestComponent.ir.txt rename to src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/GenericComponent_WithComponentRef_SuppressField/TestComponent.ir.txt index f634b69431..f5a1218aee 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/GenericComponent_WithComponentRef/TestComponent.ir.txt +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/GenericComponent_WithComponentRef_SuppressField/TestComponent.ir.txt @@ -14,15 +14,15 @@ Document - CSharpCode - IntermediateToken - - CSharp - #pragma warning restore 0414 MethodDeclaration - - protected override - void - BuildRenderTree - Component - (0:0,0 [45] x:\dir\subdir\Test\TestComponent.cshtml) - MyComponent + Component - (0:0,0 [64] x:\dir\subdir\Test\TestComponent.cshtml) - MyComponent ComponentTypeArgument - (19:0,19 [3] x:\dir\subdir\Test\TestComponent.cshtml) - TItem IntermediateToken - (19:0,19 [3] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - int ComponentAttribute - (29:0,29 [1] x:\dir\subdir\Test\TestComponent.cshtml) - Item - AttributeStructure.DoubleQuotes IntermediateToken - (29:0,29 [1] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - 3 ReferenceCapture - (38:0,38 [3] x:\dir\subdir\Test\TestComponent.cshtml) - _my - HtmlContent - (45:0,45 [4] x:\dir\subdir\Test\TestComponent.cshtml) - IntermediateToken - (45:0,45 [4] x:\dir\subdir\Test\TestComponent.cshtml) - Html - \n\n - HtmlContent - (147:5,1 [2] x:\dir\subdir\Test\TestComponent.cshtml) - IntermediateToken - (147:5,1 [2] x:\dir\subdir\Test\TestComponent.cshtml) - Html - \n - CSharpCode - (56:2,7 [90] x:\dir\subdir\Test\TestComponent.cshtml) - IntermediateToken - (56:2,7 [90] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - \n private MyComponent _my;\n public void Foo() { System.GC.KeepAlive(_my); }\n + HtmlContent - (64:0,64 [2] x:\dir\subdir\Test\TestComponent.cshtml) + IntermediateToken - (64:0,64 [2] x:\dir\subdir\Test\TestComponent.cshtml) - Html - \n + HtmlContent - (146:4,1 [2] x:\dir\subdir\Test\TestComponent.cshtml) + IntermediateToken - (146:4,1 [2] x:\dir\subdir\Test\TestComponent.cshtml) - Html - \n + CSharpCode - (73:1,7 [72] x:\dir\subdir\Test\TestComponent.cshtml) + IntermediateToken - (73:1,7 [72] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - \n MyComponent _my;\n void DoStuff() { GC.KeepAlive(_my); }\n diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/GenericComponent_WithComponentRef/TestComponent.mappings.txt b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/GenericComponent_WithComponentRef_SuppressField/TestComponent.mappings.txt similarity index 57% rename from src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/GenericComponent_WithComponentRef/TestComponent.mappings.txt rename to src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/GenericComponent_WithComponentRef_SuppressField/TestComponent.mappings.txt index 42acbfee06..a07e89c131 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/GenericComponent_WithComponentRef/TestComponent.mappings.txt +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/GenericComponent_WithComponentRef_SuppressField/TestComponent.mappings.txt @@ -13,14 +13,14 @@ Source Location: (38:0,38 [3] x:\dir\subdir\Test\TestComponent.cshtml) Generated Location: (1469:45,38 [3] ) |_my| -Source Location: (56:2,7 [90] x:\dir\subdir\Test\TestComponent.cshtml) +Source Location: (73:1,7 [72] x:\dir\subdir\Test\TestComponent.cshtml) | - private MyComponent _my; - public void Foo() { System.GC.KeepAlive(_my); } + MyComponent _my; + void DoStuff() { GC.KeepAlive(_my); } | -Generated Location: (1834:61,7 [90] ) +Generated Location: (1834:61,7 [72] ) | - private MyComponent _my; - public void Foo() { System.GC.KeepAlive(_my); } + MyComponent _my; + void DoStuff() { GC.KeepAlive(_my); } | diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/GenericComponent_WithComponentRef_TypeInference/TestComponent.codegen.cs b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/GenericComponent_WithComponentRef_TypeInference_SuppressField/TestComponent.codegen.cs similarity index 100% rename from src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/GenericComponent_WithComponentRef_TypeInference/TestComponent.codegen.cs rename to src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/GenericComponent_WithComponentRef_TypeInference_SuppressField/TestComponent.codegen.cs diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/GenericComponent_WithComponentRef_TypeInference/TestComponent.ir.txt b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/GenericComponent_WithComponentRef_TypeInference_SuppressField/TestComponent.ir.txt similarity index 79% rename from src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/GenericComponent_WithComponentRef_TypeInference/TestComponent.ir.txt rename to src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/GenericComponent_WithComponentRef_TypeInference_SuppressField/TestComponent.ir.txt index 2d876f125f..3f12dd56b1 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/GenericComponent_WithComponentRef_TypeInference/TestComponent.ir.txt +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/GenericComponent_WithComponentRef_TypeInference_SuppressField/TestComponent.ir.txt @@ -14,16 +14,16 @@ Document - CSharpCode - IntermediateToken - - CSharp - #pragma warning restore 0414 MethodDeclaration - - protected override - void - BuildRenderTree - Component - (0:0,0 [35] x:\dir\subdir\Test\TestComponent.cshtml) - MyComponent + Component - (0:0,0 [54] x:\dir\subdir\Test\TestComponent.cshtml) - MyComponent ComponentAttribute - (19:0,19 [1] x:\dir\subdir\Test\TestComponent.cshtml) - Item - AttributeStructure.DoubleQuotes IntermediateToken - (19:0,19 [1] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - 3 ReferenceCapture - (28:0,28 [3] x:\dir\subdir\Test\TestComponent.cshtml) - _my - HtmlContent - (35:0,35 [4] x:\dir\subdir\Test\TestComponent.cshtml) - IntermediateToken - (35:0,35 [4] x:\dir\subdir\Test\TestComponent.cshtml) - Html - \n\n - HtmlContent - (137:5,1 [2] x:\dir\subdir\Test\TestComponent.cshtml) - IntermediateToken - (137:5,1 [2] x:\dir\subdir\Test\TestComponent.cshtml) - Html - \n - CSharpCode - (46:2,7 [90] x:\dir\subdir\Test\TestComponent.cshtml) - IntermediateToken - (46:2,7 [90] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - \n private MyComponent _my;\n public void Foo() { System.GC.KeepAlive(_my); }\n + HtmlContent - (54:0,54 [4] x:\dir\subdir\Test\TestComponent.cshtml) + IntermediateToken - (54:0,54 [4] x:\dir\subdir\Test\TestComponent.cshtml) - Html - \n\n + HtmlContent - (156:5,1 [2] x:\dir\subdir\Test\TestComponent.cshtml) + IntermediateToken - (156:5,1 [2] x:\dir\subdir\Test\TestComponent.cshtml) - Html - \n + CSharpCode - (65:2,7 [90] x:\dir\subdir\Test\TestComponent.cshtml) + IntermediateToken - (65:2,7 [90] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - \n private MyComponent _my;\n public void Foo() { System.GC.KeepAlive(_my); }\n NamespaceDeclaration - - __Blazor.Test.TestComponent ClassDeclaration - - internal static - TypeInference - - ComponentTypeInferenceMethod - - __Blazor.Test.TestComponent.TypeInference - CreateMyComponent_0 diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/GenericComponent_WithComponentRef_TypeInference/TestComponent.mappings.txt b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/GenericComponent_WithComponentRef_TypeInference_SuppressField/TestComponent.mappings.txt similarity index 87% rename from src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/GenericComponent_WithComponentRef_TypeInference/TestComponent.mappings.txt rename to src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/GenericComponent_WithComponentRef_TypeInference_SuppressField/TestComponent.mappings.txt index 13e34b6bce..32c59dadb5 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/GenericComponent_WithComponentRef_TypeInference/TestComponent.mappings.txt +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/GenericComponent_WithComponentRef_TypeInference_SuppressField/TestComponent.mappings.txt @@ -8,7 +8,7 @@ Source Location: (28:0,28 [3] x:\dir\subdir\Test\TestComponent.cshtml) Generated Location: (1140:33,28 [3] ) |_my| -Source Location: (46:2,7 [90] x:\dir\subdir\Test\TestComponent.cshtml) +Source Location: (65:2,7 [90] x:\dir\subdir\Test\TestComponent.cshtml) | private MyComponent _my; public void Foo() { System.GC.KeepAlive(_my); } diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/Component_WithRef/TestComponent.codegen.cs b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/Component_WithRef/TestComponent.codegen.cs index 16a293d155..cd1d0ccc6f 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/Component_WithRef/TestComponent.codegen.cs +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/Component_WithRef/TestComponent.codegen.cs @@ -10,6 +10,11 @@ namespace Test using Microsoft.AspNetCore.Components; public class TestComponent : Microsoft.AspNetCore.Components.ComponentBase { + #pragma warning disable 0414 + #pragma warning disable 0169 + private Test.MyComponent myInstance; + #pragma warning restore 0169 + #pragma warning restore 0414 #pragma warning disable 1998 protected override void BuildRenderTree(Microsoft.AspNetCore.Components.RenderTree.RenderTreeBuilder builder) { @@ -29,15 +34,6 @@ namespace Test builder.CloseComponent(); } #pragma warning restore 1998 -#nullable restore -#line 3 "x:\dir\subdir\Test\TestComponent.cshtml" - - private Test.MyComponent myInstance; - public void Foo() { System.GC.KeepAlive(myInstance); } - -#line default -#line hidden -#nullable disable } } #pragma warning restore 1591 diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/Component_WithRef/TestComponent.ir.txt b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/Component_WithRef/TestComponent.ir.txt index c724647179..be89b1cae3 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/Component_WithRef/TestComponent.ir.txt +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/Component_WithRef/TestComponent.ir.txt @@ -6,6 +6,7 @@ Document - UsingDirective - (73:4,1 [30] ) - System.Threading.Tasks UsingDirective - (104:5,1 [39] ) - Microsoft.AspNetCore.Components ClassDeclaration - - public - TestComponent - Microsoft.AspNetCore.Components.ComponentBase - + FieldDeclaration - - private - Test.MyComponent - myInstance MethodDeclaration - - protected override - void - BuildRenderTree Component - (0:0,0 [73] x:\dir\subdir\Test\TestComponent.cshtml) - MyComponent ComponentAttribute - - ParamBefore - AttributeStructure.DoubleQuotes @@ -15,5 +16,3 @@ Document - ComponentAttribute - - ParamAfter - AttributeStructure.DoubleQuotes HtmlContent - (64:0,64 [5] x:\dir\subdir\Test\TestComponent.cshtml) IntermediateToken - (64:0,64 [5] x:\dir\subdir\Test\TestComponent.cshtml) - Html - after - CSharpCode - (84:2,7 [104] x:\dir\subdir\Test\TestComponent.cshtml) - IntermediateToken - (84:2,7 [104] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - \n private Test.MyComponent myInstance;\n public void Foo() { System.GC.KeepAlive(myInstance); }\n diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/Component_WithRef/TestComponent.mappings.txt b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/Component_WithRef/TestComponent.mappings.txt index cbf295b3fa..2eeb174cef 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/Component_WithRef/TestComponent.mappings.txt +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/Component_WithRef/TestComponent.mappings.txt @@ -1,16 +1,5 @@ Source Location: (40:0,40 [10] x:\dir\subdir\Test\TestComponent.cshtml) |myInstance| -Generated Location: (864:21,40 [10] ) +Generated Location: (1062:26,40 [10] ) |myInstance| -Source Location: (84:2,7 [104] x:\dir\subdir\Test\TestComponent.cshtml) -| - private Test.MyComponent myInstance; - public void Foo() { System.GC.KeepAlive(myInstance); } -| -Generated Location: (1151:33,7 [104] ) -| - private Test.MyComponent myInstance; - public void Foo() { System.GC.KeepAlive(myInstance); } -| - diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/Component_WithRef_SuppressField/TestComponent.codegen.cs b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/Component_WithRef_SuppressField/TestComponent.codegen.cs new file mode 100644 index 0000000000..4eeabde6c1 --- /dev/null +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/Component_WithRef_SuppressField/TestComponent.codegen.cs @@ -0,0 +1,41 @@ +// +#pragma warning disable 1591 +namespace Test +{ + #line hidden + using System; + using System.Collections.Generic; + using System.Linq; + using System.Threading.Tasks; + using Microsoft.AspNetCore.Components; + public class TestComponent : Microsoft.AspNetCore.Components.ComponentBase + { + #pragma warning disable 1998 + protected override void BuildRenderTree(Microsoft.AspNetCore.Components.RenderTree.RenderTreeBuilder builder) + { + builder.OpenComponent(0); + builder.AddComponentReferenceCapture(1, (__value) => { +#nullable restore +#line 1 "x:\dir\subdir\Test\TestComponent.cshtml" + myInstance = (Test.MyComponent)__value; + +#line default +#line hidden +#nullable disable + } + ); + builder.CloseComponent(); + } + #pragma warning restore 1998 +#nullable restore +#line 2 "x:\dir\subdir\Test\TestComponent.cshtml" + + MyComponent myInstance; + void DoStuff() { GC.KeepAlive(myInstance); } + +#line default +#line hidden +#nullable disable + } +} +#pragma warning restore 1591 diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/Component_WithRef_SuppressField/TestComponent.ir.txt b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/Component_WithRef_SuppressField/TestComponent.ir.txt new file mode 100644 index 0000000000..a39ce1d3c4 --- /dev/null +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/Component_WithRef_SuppressField/TestComponent.ir.txt @@ -0,0 +1,13 @@ +Document - + NamespaceDeclaration - - Test + UsingDirective - (3:1,1 [14] ) - System + UsingDirective - (18:2,1 [34] ) - System.Collections.Generic + UsingDirective - (53:3,1 [19] ) - System.Linq + UsingDirective - (73:4,1 [30] ) - System.Threading.Tasks + UsingDirective - (104:5,1 [39] ) - Microsoft.AspNetCore.Components + ClassDeclaration - - public - TestComponent - Microsoft.AspNetCore.Components.ComponentBase - + MethodDeclaration - - protected override - void - BuildRenderTree + Component - (0:0,0 [52] x:\dir\subdir\Test\TestComponent.cshtml) - MyComponent + ReferenceCapture - (19:0,19 [10] x:\dir\subdir\Test\TestComponent.cshtml) - myInstance + CSharpCode - (61:1,7 [81] x:\dir\subdir\Test\TestComponent.cshtml) + IntermediateToken - (61:1,7 [81] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - \n MyComponent myInstance;\n void DoStuff() { GC.KeepAlive(myInstance); }\n diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/Component_WithRef_SuppressField/TestComponent.mappings.txt b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/Component_WithRef_SuppressField/TestComponent.mappings.txt new file mode 100644 index 0000000000..b52149451f --- /dev/null +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/Component_WithRef_SuppressField/TestComponent.mappings.txt @@ -0,0 +1,16 @@ +Source Location: (19:0,19 [10] x:\dir\subdir\Test\TestComponent.cshtml) +|myInstance| +Generated Location: (719:19,19 [10] ) +|myInstance| + +Source Location: (61:1,7 [81] x:\dir\subdir\Test\TestComponent.cshtml) +| + MyComponent myInstance; + void DoStuff() { GC.KeepAlive(myInstance); } +| +Generated Location: (1006:31,7 [81] ) +| + MyComponent myInstance; + void DoStuff() { GC.KeepAlive(myInstance); } +| + diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/Component_WithRef_WithChildContent/TestComponent.codegen.cs b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/Component_WithRef_WithChildContent/TestComponent.codegen.cs index dadcb2273d..80988599b0 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/Component_WithRef_WithChildContent/TestComponent.codegen.cs +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/Component_WithRef_WithChildContent/TestComponent.codegen.cs @@ -10,6 +10,11 @@ namespace Test using Microsoft.AspNetCore.Components; public class TestComponent : Microsoft.AspNetCore.Components.ComponentBase { + #pragma warning disable 0414 + #pragma warning disable 0169 + private Test.MyComponent myInstance; + #pragma warning restore 0169 + #pragma warning restore 0414 #pragma warning disable 1998 protected override void BuildRenderTree(Microsoft.AspNetCore.Components.RenderTree.RenderTreeBuilder builder) { @@ -33,15 +38,6 @@ namespace Test builder.CloseComponent(); } #pragma warning restore 1998 -#nullable restore -#line 5 "x:\dir\subdir\Test\TestComponent.cshtml" - - private Test.MyComponent myInstance; - public void Foo() { System.GC.KeepAlive(myInstance); } - -#line default -#line hidden -#nullable disable } } #pragma warning restore 1591 diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/Component_WithRef_WithChildContent/TestComponent.ir.txt b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/Component_WithRef_WithChildContent/TestComponent.ir.txt index 1bc1814490..e99793984f 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/Component_WithRef_WithChildContent/TestComponent.ir.txt +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/Component_WithRef_WithChildContent/TestComponent.ir.txt @@ -6,6 +6,7 @@ Document - UsingDirective - (73:4,1 [30] ) - System.Threading.Tasks UsingDirective - (104:5,1 [39] ) - Microsoft.AspNetCore.Components ClassDeclaration - - public - TestComponent - Microsoft.AspNetCore.Components.ComponentBase - + FieldDeclaration - - private - Test.MyComponent - myInstance MethodDeclaration - - protected override - void - BuildRenderTree Component - (0:0,0 [97] x:\dir\subdir\Test\TestComponent.cshtml) - MyComponent ComponentChildContent - - ChildContent - context @@ -16,5 +17,3 @@ Document - ComponentAttribute - - SomeProp - AttributeStructure.DoubleQuotes HtmlContent - (41:0,41 [3] x:\dir\subdir\Test\TestComponent.cshtml) IntermediateToken - (41:0,41 [3] x:\dir\subdir\Test\TestComponent.cshtml) - Html - val - CSharpCode - (108:4,7 [104] x:\dir\subdir\Test\TestComponent.cshtml) - IntermediateToken - (108:4,7 [104] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - \n private Test.MyComponent myInstance;\n public void Foo() { System.GC.KeepAlive(myInstance); }\n diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/Component_WithRef_WithChildContent/TestComponent.mappings.txt b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/Component_WithRef_WithChildContent/TestComponent.mappings.txt index a77e00670e..06ea90766e 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/Component_WithRef_WithChildContent/TestComponent.mappings.txt +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/Component_WithRef_WithChildContent/TestComponent.mappings.txt @@ -1,16 +1,5 @@ Source Location: (19:0,19 [10] x:\dir\subdir\Test\TestComponent.cshtml) |myInstance| -Generated Location: (1069:27,19 [10] ) +Generated Location: (1267:32,19 [10] ) |myInstance| -Source Location: (108:4,7 [104] x:\dir\subdir\Test\TestComponent.cshtml) -| - private Test.MyComponent myInstance; - public void Foo() { System.GC.KeepAlive(myInstance); } -| -Generated Location: (1356:39,7 [104] ) -| - private Test.MyComponent myInstance; - public void Foo() { System.GC.KeepAlive(myInstance); } -| - diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/Element_WithRef/TestComponent.codegen.cs b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/Element_WithRef/TestComponent.codegen.cs index 9ff437c85b..1dacb96fe0 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/Element_WithRef/TestComponent.codegen.cs +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/Element_WithRef/TestComponent.codegen.cs @@ -10,6 +10,11 @@ namespace Test using Microsoft.AspNetCore.Components; public class TestComponent : Microsoft.AspNetCore.Components.ComponentBase { + #pragma warning disable 0414 + #pragma warning disable 0169 + private global::Microsoft.AspNetCore.Components.ElementRef myElem; + #pragma warning restore 0169 + #pragma warning restore 0414 #pragma warning disable 1998 protected override void BuildRenderTree(Microsoft.AspNetCore.Components.RenderTree.RenderTreeBuilder builder) { @@ -30,15 +35,6 @@ namespace Test builder.CloseElement(); } #pragma warning restore 1998 -#nullable restore -#line 3 "x:\dir\subdir\Test\TestComponent.cshtml" - - private Microsoft.AspNetCore.Components.ElementRef myElem; - public void Foo() { System.GC.KeepAlive(myElem); } - -#line default -#line hidden -#nullable disable } } #pragma warning restore 1591 diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/Element_WithRef/TestComponent.ir.txt b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/Element_WithRef/TestComponent.ir.txt index d9abf6f8b5..7acd3547a4 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/Element_WithRef/TestComponent.ir.txt +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/Element_WithRef/TestComponent.ir.txt @@ -6,6 +6,7 @@ Document - UsingDirective - (73:4,1 [30] ) - System.Threading.Tasks UsingDirective - (104:5,1 [39] ) - Microsoft.AspNetCore.Components ClassDeclaration - - public - TestComponent - Microsoft.AspNetCore.Components.ComponentBase - + FieldDeclaration - - private - global::Microsoft.AspNetCore.Components.ElementRef - myElem MethodDeclaration - - protected override - void - BuildRenderTree MarkupElement - (0:0,0 [80] x:\dir\subdir\Test\TestComponent.cshtml) - elem HtmlContent - (68:0,68 [5] x:\dir\subdir\Test\TestComponent.cshtml) @@ -17,5 +18,3 @@ Document - HtmlAttribute - - attributeafter=" - " HtmlAttributeValue - (61:0,61 [5] x:\dir\subdir\Test\TestComponent.cshtml) - IntermediateToken - (61:0,61 [5] x:\dir\subdir\Test\TestComponent.cshtml) - Html - after - CSharpCode - (91:2,7 [122] x:\dir\subdir\Test\TestComponent.cshtml) - IntermediateToken - (91:2,7 [122] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - \n private Microsoft.AspNetCore.Components.ElementRef myElem;\n public void Foo() { System.GC.KeepAlive(myElem); }\n diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/Element_WithRef/TestComponent.mappings.txt b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/Element_WithRef/TestComponent.mappings.txt index 6daf0a696b..7f14ab1c93 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/Element_WithRef/TestComponent.mappings.txt +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/Element_WithRef/TestComponent.mappings.txt @@ -1,16 +1,5 @@ Source Location: (37:0,37 [6] x:\dir\subdir\Test\TestComponent.cshtml) |myElem| -Generated Location: (855:21,37 [6] ) +Generated Location: (1083:26,37 [6] ) |myElem| -Source Location: (91:2,7 [122] x:\dir\subdir\Test\TestComponent.cshtml) -| - private Microsoft.AspNetCore.Components.ElementRef myElem; - public void Foo() { System.GC.KeepAlive(myElem); } -| -Generated Location: (1163:34,7 [122] ) -| - private Microsoft.AspNetCore.Components.ElementRef myElem; - public void Foo() { System.GC.KeepAlive(myElem); } -| - diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/Element_WithRef_AndOtherAttributes/TestComponent.codegen.cs b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/Element_WithRef_AndOtherAttributes/TestComponent.codegen.cs index 20fcb8a55f..d039bfcdce 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/Element_WithRef_AndOtherAttributes/TestComponent.codegen.cs +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/Element_WithRef_AndOtherAttributes/TestComponent.codegen.cs @@ -10,6 +10,11 @@ namespace Test using Microsoft.AspNetCore.Components; public class TestComponent : Microsoft.AspNetCore.Components.ComponentBase { + #pragma warning disable 0414 + #pragma warning disable 0169 + private global::Microsoft.AspNetCore.Components.ElementRef _element; + #pragma warning restore 0169 + #pragma warning restore 0414 #pragma warning disable 1998 protected override void BuildRenderTree(Microsoft.AspNetCore.Components.RenderTree.RenderTreeBuilder builder) { @@ -40,10 +45,7 @@ namespace Test #nullable restore #line 3 "x:\dir\subdir\Test\TestComponent.cshtml" - private ElementRef _element; - [Parameter] protected int Min { get; set; } - public void Foo() { System.GC.KeepAlive(_element); } #line default diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/Element_WithRef_AndOtherAttributes/TestComponent.ir.txt b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/Element_WithRef_AndOtherAttributes/TestComponent.ir.txt index 61dbd3d7d5..8006fc59eb 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/Element_WithRef_AndOtherAttributes/TestComponent.ir.txt +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/Element_WithRef_AndOtherAttributes/TestComponent.ir.txt @@ -6,6 +6,7 @@ Document - UsingDirective - (73:4,1 [30] ) - System.Threading.Tasks UsingDirective - (104:5,1 [39] ) - Microsoft.AspNetCore.Components ClassDeclaration - - public - TestComponent - Microsoft.AspNetCore.Components.ComponentBase - + FieldDeclaration - - private - global::Microsoft.AspNetCore.Components.ElementRef - _element MethodDeclaration - - protected override - void - BuildRenderTree MarkupElement - (0:0,0 [61] x:\dir\subdir\Test\TestComponent.cshtml) - input HtmlAttribute - - type=" - " @@ -15,5 +16,5 @@ Document - CSharpExpressionAttributeValue - (36:0,36 [4] x:\dir\subdir\Test\TestComponent.cshtml) - IntermediateToken - (37:0,37 [3] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - Min ReferenceCapture - (49:0,49 [8] x:\dir\subdir\Test\TestComponent.cshtml) - _element - CSharpCode - (72:2,7 [161] x:\dir\subdir\Test\TestComponent.cshtml) - IntermediateToken - (72:2,7 [161] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - \n private ElementRef _element;\n\n [Parameter] protected int Min { get; set; }\n public void Foo() { System.GC.KeepAlive(_element); }\n + CSharpCode - (72:2,7 [59] x:\dir\subdir\Test\TestComponent.cshtml) + IntermediateToken - (72:2,7 [59] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - \n [Parameter] protected int Min { get; set; }\n diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/Element_WithRef_AndOtherAttributes/TestComponent.mappings.txt b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/Element_WithRef_AndOtherAttributes/TestComponent.mappings.txt index c135b4409c..01521d24fe 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/Element_WithRef_AndOtherAttributes/TestComponent.mappings.txt +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/Element_WithRef_AndOtherAttributes/TestComponent.mappings.txt @@ -1,20 +1,14 @@ Source Location: (49:0,49 [8] x:\dir\subdir\Test\TestComponent.cshtml) |_element| -Generated Location: (1025:29,49 [8] ) +Generated Location: (1255:34,49 [8] ) |_element| -Source Location: (72:2,7 [161] x:\dir\subdir\Test\TestComponent.cshtml) +Source Location: (72:2,7 [59] x:\dir\subdir\Test\TestComponent.cshtml) | - private ElementRef _element; - [Parameter] protected int Min { get; set; } - public void Foo() { System.GC.KeepAlive(_element); } | -Generated Location: (1290:41,7 [161] ) +Generated Location: (1520:46,7 [59] ) | - private ElementRef _element; - [Parameter] protected int Min { get; set; } - public void Foo() { System.GC.KeepAlive(_element); } | diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/Element_WithRef_SuppressField/TestComponent.codegen.cs b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/Element_WithRef_SuppressField/TestComponent.codegen.cs new file mode 100644 index 0000000000..548594ab24 --- /dev/null +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/Element_WithRef_SuppressField/TestComponent.codegen.cs @@ -0,0 +1,42 @@ +// +#pragma warning disable 1591 +namespace Test +{ + #line hidden + using System; + using System.Collections.Generic; + using System.Linq; + using System.Threading.Tasks; + using Microsoft.AspNetCore.Components; + public class TestComponent : Microsoft.AspNetCore.Components.ComponentBase + { + #pragma warning disable 1998 + protected override void BuildRenderTree(Microsoft.AspNetCore.Components.RenderTree.RenderTreeBuilder builder) + { + builder.OpenElement(0, "elem"); + builder.AddElementReferenceCapture(1, (__value) => { +#nullable restore +#line 1 "x:\dir\subdir\Test\TestComponent.cshtml" + myElem = __value; + +#line default +#line hidden +#nullable disable + } + ); + builder.AddContent(2, "Hello"); + builder.CloseElement(); + } + #pragma warning restore 1998 +#nullable restore +#line 2 "x:\dir\subdir\Test\TestComponent.cshtml" + + ElementRef myElem; + void DoStuff() { GC.KeepAlive(myElem); } + +#line default +#line hidden +#nullable disable + } +} +#pragma warning restore 1591 diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/Element_WithRef_SuppressField/TestComponent.ir.txt b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/Element_WithRef_SuppressField/TestComponent.ir.txt new file mode 100644 index 0000000000..a92d1e0d9c --- /dev/null +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/Element_WithRef_SuppressField/TestComponent.ir.txt @@ -0,0 +1,15 @@ +Document - + NamespaceDeclaration - - Test + UsingDirective - (3:1,1 [14] ) - System + UsingDirective - (18:2,1 [34] ) - System.Collections.Generic + UsingDirective - (53:3,1 [19] ) - System.Linq + UsingDirective - (73:4,1 [30] ) - System.Threading.Tasks + UsingDirective - (104:5,1 [39] ) - Microsoft.AspNetCore.Components + ClassDeclaration - - public - TestComponent - Microsoft.AspNetCore.Components.ComponentBase - + MethodDeclaration - - protected override - void - BuildRenderTree + MarkupElement - (0:0,0 [51] x:\dir\subdir\Test\TestComponent.cshtml) - elem + HtmlContent - (39:0,39 [5] x:\dir\subdir\Test\TestComponent.cshtml) + IntermediateToken - (39:0,39 [5] x:\dir\subdir\Test\TestComponent.cshtml) - Html - Hello + ReferenceCapture - (12:0,12 [6] x:\dir\subdir\Test\TestComponent.cshtml) - myElem + CSharpCode - (60:1,7 [72] x:\dir\subdir\Test\TestComponent.cshtml) + IntermediateToken - (60:1,7 [72] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - \n ElementRef myElem;\n void DoStuff() { GC.KeepAlive(myElem); }\n diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/Element_WithRef_SuppressField/TestComponent.mappings.txt b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/Element_WithRef_SuppressField/TestComponent.mappings.txt new file mode 100644 index 0000000000..4b1c0546cf --- /dev/null +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/Element_WithRef_SuppressField/TestComponent.mappings.txt @@ -0,0 +1,16 @@ +Source Location: (12:0,12 [6] x:\dir\subdir\Test\TestComponent.cshtml) +|myElem| +Generated Location: (698:19,12 [6] ) +|myElem| + +Source Location: (60:1,7 [72] x:\dir\subdir\Test\TestComponent.cshtml) +| + ElementRef myElem; + void DoStuff() { GC.KeepAlive(myElem); } +| +Generated Location: (1006:32,7 [72] ) +| + ElementRef myElem; + void DoStuff() { GC.KeepAlive(myElem); } +| + diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/Element_WithRef_SuppressFieldWithValue_ResultsInDiagnostic/TestComponent.codegen.cs b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/Element_WithRef_SuppressFieldWithValue_ResultsInDiagnostic/TestComponent.codegen.cs new file mode 100644 index 0000000000..65d6d0d152 --- /dev/null +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/Element_WithRef_SuppressFieldWithValue_ResultsInDiagnostic/TestComponent.codegen.cs @@ -0,0 +1,38 @@ +// +#pragma warning disable 1591 +namespace Test +{ + #line hidden + using System; + using System.Collections.Generic; + using System.Linq; + using System.Threading.Tasks; + using Microsoft.AspNetCore.Components; + public class TestComponent : Microsoft.AspNetCore.Components.ComponentBase + { + #pragma warning disable 0414 + #pragma warning disable 0169 + private global::Microsoft.AspNetCore.Components.ElementRef myElem; + #pragma warning restore 0169 + #pragma warning restore 0414 + #pragma warning disable 1998 + protected override void BuildRenderTree(Microsoft.AspNetCore.Components.RenderTree.RenderTreeBuilder builder) + { + builder.OpenElement(0, "elem"); + builder.AddElementReferenceCapture(1, (__value) => { +#nullable restore +#line 1 "x:\dir\subdir\Test\TestComponent.cshtml" + myElem = __value; + +#line default +#line hidden +#nullable disable + } + ); + builder.AddContent(2, "Hello"); + builder.CloseElement(); + } + #pragma warning restore 1998 + } +} +#pragma warning restore 1591 diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/Element_WithRef_SuppressFieldWithValue_ResultsInDiagnostic/TestComponent.diagnostics.txt b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/Element_WithRef_SuppressFieldWithValue_ResultsInDiagnostic/TestComponent.diagnostics.txt new file mode 100644 index 0000000000..bd82282e59 --- /dev/null +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/Element_WithRef_SuppressFieldWithValue_ResultsInDiagnostic/TestComponent.diagnostics.txt @@ -0,0 +1 @@ +x:\dir\subdir\Test\TestComponent.cshtml(1,41): Error RZ10011: The directive attribute '@ref:suppressField' must be used as a minimized attribute. Providing an attribute value like '@ref:suppressField="false" is not supported. diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/Element_WithRef_SuppressFieldWithValue_ResultsInDiagnostic/TestComponent.ir.txt b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/Element_WithRef_SuppressFieldWithValue_ResultsInDiagnostic/TestComponent.ir.txt new file mode 100644 index 0000000000..b482efeff5 --- /dev/null +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/Element_WithRef_SuppressFieldWithValue_ResultsInDiagnostic/TestComponent.ir.txt @@ -0,0 +1,14 @@ +Document - + NamespaceDeclaration - - Test + UsingDirective - (3:1,1 [14] ) - System + UsingDirective - (18:2,1 [34] ) - System.Collections.Generic + UsingDirective - (53:3,1 [19] ) - System.Linq + UsingDirective - (73:4,1 [30] ) - System.Threading.Tasks + UsingDirective - (104:5,1 [39] ) - Microsoft.AspNetCore.Components + ClassDeclaration - - public - TestComponent - Microsoft.AspNetCore.Components.ComponentBase - + FieldDeclaration - - private - global::Microsoft.AspNetCore.Components.ElementRef - myElem + MethodDeclaration - - protected override - void - BuildRenderTree + MarkupElement - (0:0,0 [59] x:\dir\subdir\Test\TestComponent.cshtml) - elem + HtmlContent - (47:0,47 [5] x:\dir\subdir\Test\TestComponent.cshtml) + IntermediateToken - (47:0,47 [5] x:\dir\subdir\Test\TestComponent.cshtml) - Html - Hello + ReferenceCapture - (12:0,12 [6] x:\dir\subdir\Test\TestComponent.cshtml) - myElem diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/Element_WithRef_SuppressFieldWithValue_ResultsInDiagnostic/TestComponent.mappings.txt b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/Element_WithRef_SuppressFieldWithValue_ResultsInDiagnostic/TestComponent.mappings.txt new file mode 100644 index 0000000000..7fbcfcd168 --- /dev/null +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/Element_WithRef_SuppressFieldWithValue_ResultsInDiagnostic/TestComponent.mappings.txt @@ -0,0 +1,5 @@ +Source Location: (12:0,12 [6] x:\dir\subdir\Test\TestComponent.cshtml) +|myElem| +Generated Location: (926:24,12 [6] ) +|myElem| + diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/GenericComponent_WithComponentRef/TestComponent.codegen.cs b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/GenericComponent_WithComponentRef/TestComponent.codegen.cs index c6e0ae260c..b33b86132c 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/GenericComponent_WithComponentRef/TestComponent.codegen.cs +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/GenericComponent_WithComponentRef/TestComponent.codegen.cs @@ -10,6 +10,11 @@ namespace Test using Microsoft.AspNetCore.Components; public class TestComponent : Microsoft.AspNetCore.Components.ComponentBase { + #pragma warning disable 0414 + #pragma warning disable 0169 + private Test.MyComponent _my; + #pragma warning restore 0169 + #pragma warning restore 0414 #pragma warning disable 1998 protected override void BuildRenderTree(Microsoft.AspNetCore.Components.RenderTree.RenderTreeBuilder builder) { @@ -36,15 +41,6 @@ namespace Test builder.CloseComponent(); } #pragma warning restore 1998 -#nullable restore -#line 3 "x:\dir\subdir\Test\TestComponent.cshtml" - - private MyComponent _my; - public void Foo() { System.GC.KeepAlive(_my); } - -#line default -#line hidden -#nullable disable } } #pragma warning restore 1591 diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/GenericComponent_WithComponentRef/TestComponent.ir.txt b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/GenericComponent_WithComponentRef/TestComponent.ir.txt index 5dbefda847..22c8243ee5 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/GenericComponent_WithComponentRef/TestComponent.ir.txt +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/GenericComponent_WithComponentRef/TestComponent.ir.txt @@ -6,6 +6,7 @@ Document - UsingDirective - (73:4,1 [30] ) - System.Threading.Tasks UsingDirective - (104:5,1 [39] ) - Microsoft.AspNetCore.Components ClassDeclaration - - public - TestComponent - Microsoft.AspNetCore.Components.ComponentBase - + FieldDeclaration - - private - Test.MyComponent - _my MethodDeclaration - - protected override - void - BuildRenderTree Component - (0:0,0 [45] x:\dir\subdir\Test\TestComponent.cshtml) - MyComponent ComponentTypeArgument - (19:0,19 [3] x:\dir\subdir\Test\TestComponent.cshtml) - TItem @@ -13,5 +14,3 @@ Document - ComponentAttribute - (29:0,29 [1] x:\dir\subdir\Test\TestComponent.cshtml) - Item - AttributeStructure.DoubleQuotes IntermediateToken - (29:0,29 [1] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - 3 ReferenceCapture - (38:0,38 [3] x:\dir\subdir\Test\TestComponent.cshtml) - _my - CSharpCode - (56:2,7 [90] x:\dir\subdir\Test\TestComponent.cshtml) - IntermediateToken - (56:2,7 [90] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - \n private MyComponent _my;\n public void Foo() { System.GC.KeepAlive(_my); }\n diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/GenericComponent_WithComponentRef/TestComponent.mappings.txt b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/GenericComponent_WithComponentRef/TestComponent.mappings.txt index a830a6ef44..a259494405 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/GenericComponent_WithComponentRef/TestComponent.mappings.txt +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/GenericComponent_WithComponentRef/TestComponent.mappings.txt @@ -1,16 +1,5 @@ Source Location: (38:0,38 [3] x:\dir\subdir\Test\TestComponent.cshtml) |_my| -Generated Location: (1020:28,38 [3] ) +Generated Location: (1218:33,38 [3] ) |_my| -Source Location: (56:2,7 [90] x:\dir\subdir\Test\TestComponent.cshtml) -| - private MyComponent _my; - public void Foo() { System.GC.KeepAlive(_my); } -| -Generated Location: (1305:40,7 [90] ) -| - private MyComponent _my; - public void Foo() { System.GC.KeepAlive(_my); } -| - diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/GenericComponent_WithComponentRef_SuppressField/TestComponent.codegen.cs b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/GenericComponent_WithComponentRef_SuppressField/TestComponent.codegen.cs new file mode 100644 index 0000000000..91dc1326f1 --- /dev/null +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/GenericComponent_WithComponentRef_SuppressField/TestComponent.codegen.cs @@ -0,0 +1,50 @@ +// +#pragma warning disable 1591 +namespace Test +{ + #line hidden + using System; + using System.Collections.Generic; + using System.Linq; + using System.Threading.Tasks; + using Microsoft.AspNetCore.Components; + public class TestComponent : Microsoft.AspNetCore.Components.ComponentBase + { + #pragma warning disable 1998 + protected override void BuildRenderTree(Microsoft.AspNetCore.Components.RenderTree.RenderTreeBuilder builder) + { + builder.OpenComponent>(0); + builder.AddAttribute(1, "Item", Microsoft.AspNetCore.Components.RuntimeHelpers.TypeCheck( +#nullable restore +#line 1 "x:\dir\subdir\Test\TestComponent.cshtml" + 3 + +#line default +#line hidden +#nullable disable + )); + builder.AddComponentReferenceCapture(2, (__value) => { +#nullable restore +#line 1 "x:\dir\subdir\Test\TestComponent.cshtml" + _my = (Test.MyComponent)__value; + +#line default +#line hidden +#nullable disable + } + ); + builder.CloseComponent(); + } + #pragma warning restore 1998 +#nullable restore +#line 2 "x:\dir\subdir\Test\TestComponent.cshtml" + + MyComponent _my; + void DoStuff() { GC.KeepAlive(_my); } + +#line default +#line hidden +#nullable disable + } +} +#pragma warning restore 1591 diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/GenericComponent_WithComponentRef_SuppressField/TestComponent.ir.txt b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/GenericComponent_WithComponentRef_SuppressField/TestComponent.ir.txt new file mode 100644 index 0000000000..3ed36f4932 --- /dev/null +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/GenericComponent_WithComponentRef_SuppressField/TestComponent.ir.txt @@ -0,0 +1,17 @@ +Document - + NamespaceDeclaration - - Test + UsingDirective - (3:1,1 [14] ) - System + UsingDirective - (18:2,1 [34] ) - System.Collections.Generic + UsingDirective - (53:3,1 [19] ) - System.Linq + UsingDirective - (73:4,1 [30] ) - System.Threading.Tasks + UsingDirective - (104:5,1 [39] ) - Microsoft.AspNetCore.Components + ClassDeclaration - - public - TestComponent - Microsoft.AspNetCore.Components.ComponentBase - + MethodDeclaration - - protected override - void - BuildRenderTree + Component - (0:0,0 [64] x:\dir\subdir\Test\TestComponent.cshtml) - MyComponent + ComponentTypeArgument - (19:0,19 [3] x:\dir\subdir\Test\TestComponent.cshtml) - TItem + IntermediateToken - (19:0,19 [3] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - int + ComponentAttribute - (29:0,29 [1] x:\dir\subdir\Test\TestComponent.cshtml) - Item - AttributeStructure.DoubleQuotes + IntermediateToken - (29:0,29 [1] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - 3 + ReferenceCapture - (38:0,38 [3] x:\dir\subdir\Test\TestComponent.cshtml) - _my + CSharpCode - (73:1,7 [72] x:\dir\subdir\Test\TestComponent.cshtml) + IntermediateToken - (73:1,7 [72] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - \n MyComponent _my;\n void DoStuff() { GC.KeepAlive(_my); }\n diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/GenericComponent_WithComponentRef_SuppressField/TestComponent.mappings.txt b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/GenericComponent_WithComponentRef_SuppressField/TestComponent.mappings.txt new file mode 100644 index 0000000000..38b1b74663 --- /dev/null +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/GenericComponent_WithComponentRef_SuppressField/TestComponent.mappings.txt @@ -0,0 +1,16 @@ +Source Location: (38:0,38 [3] x:\dir\subdir\Test\TestComponent.cshtml) +|_my| +Generated Location: (1020:28,38 [3] ) +|_my| + +Source Location: (73:1,7 [72] x:\dir\subdir\Test\TestComponent.cshtml) +| + MyComponent _my; + void DoStuff() { GC.KeepAlive(_my); } +| +Generated Location: (1305:40,7 [72] ) +| + MyComponent _my; + void DoStuff() { GC.KeepAlive(_my); } +| + diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/GenericComponent_WithComponentRef_TypeInference/TestComponent.codegen.cs b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/GenericComponent_WithComponentRef_TypeInference/TestComponent.codegen.cs index 96d74f3a1b..523fdcd46c 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/GenericComponent_WithComponentRef_TypeInference/TestComponent.codegen.cs +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/GenericComponent_WithComponentRef_TypeInference/TestComponent.codegen.cs @@ -10,6 +10,11 @@ namespace Test using Microsoft.AspNetCore.Components; public class TestComponent : Microsoft.AspNetCore.Components.ComponentBase { + #pragma warning disable 0414 + #pragma warning disable 0169 + private Test.MyComponent _my; + #pragma warning restore 0169 + #pragma warning restore 0414 #pragma warning disable 1998 protected override void BuildRenderTree(Microsoft.AspNetCore.Components.RenderTree.RenderTreeBuilder builder) { diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/GenericComponent_WithComponentRef_TypeInference/TestComponent.ir.txt b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/GenericComponent_WithComponentRef_TypeInference/TestComponent.ir.txt index cde256a50b..ee14362fb9 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/GenericComponent_WithComponentRef_TypeInference/TestComponent.ir.txt +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/GenericComponent_WithComponentRef_TypeInference/TestComponent.ir.txt @@ -6,6 +6,7 @@ Document - UsingDirective - (73:4,1 [30] ) - System.Threading.Tasks UsingDirective - (104:5,1 [39] ) - Microsoft.AspNetCore.Components ClassDeclaration - - public - TestComponent - Microsoft.AspNetCore.Components.ComponentBase - + FieldDeclaration - - private - Test.MyComponent - _my MethodDeclaration - - protected override - void - BuildRenderTree Component - (0:0,0 [35] x:\dir\subdir\Test\TestComponent.cshtml) - MyComponent ComponentAttribute - (19:0,19 [1] x:\dir\subdir\Test\TestComponent.cshtml) - Item - AttributeStructure.DoubleQuotes diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/GenericComponent_WithComponentRef_TypeInference/TestComponent.mappings.txt b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/GenericComponent_WithComponentRef_TypeInference/TestComponent.mappings.txt index 636a07a580..7c936d6582 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/GenericComponent_WithComponentRef_TypeInference/TestComponent.mappings.txt +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/GenericComponent_WithComponentRef_TypeInference/TestComponent.mappings.txt @@ -1,6 +1,6 @@ Source Location: (28:0,28 [3] x:\dir\subdir\Test\TestComponent.cshtml) |_my| -Generated Location: (869:26,28 [3] ) +Generated Location: (1067:31,28 [3] ) |_my| Source Location: (46:2,7 [90] x:\dir\subdir\Test\TestComponent.cshtml) @@ -8,7 +8,7 @@ Source Location: (46:2,7 [90] x:\dir\subdir\Test\TestComponent.cshtml) private MyComponent _my; public void Foo() { System.GC.KeepAlive(_my); } | -Generated Location: (1092:37,7 [90] ) +Generated Location: (1290:42,7 [90] ) | private MyComponent _my; public void Foo() { System.GC.KeepAlive(_my); } diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/GenericComponent_WithComponentRef_TypeInference_SuppressField/TestComponent.codegen.cs b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/GenericComponent_WithComponentRef_TypeInference_SuppressField/TestComponent.codegen.cs new file mode 100644 index 0000000000..96d74f3a1b --- /dev/null +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/GenericComponent_WithComponentRef_TypeInference_SuppressField/TestComponent.codegen.cs @@ -0,0 +1,61 @@ +// +#pragma warning disable 1591 +namespace Test +{ + #line hidden + using System; + using System.Collections.Generic; + using System.Linq; + using System.Threading.Tasks; + using Microsoft.AspNetCore.Components; + public class TestComponent : Microsoft.AspNetCore.Components.ComponentBase + { + #pragma warning disable 1998 + protected override void BuildRenderTree(Microsoft.AspNetCore.Components.RenderTree.RenderTreeBuilder builder) + { + __Blazor.Test.TestComponent.TypeInference.CreateMyComponent_0(builder, 0, 1, +#nullable restore +#line 1 "x:\dir\subdir\Test\TestComponent.cshtml" + 3 + +#line default +#line hidden +#nullable disable + , 2, (__value) => { +#nullable restore +#line 1 "x:\dir\subdir\Test\TestComponent.cshtml" + _my = __value; + +#line default +#line hidden +#nullable disable + } + ); + } + #pragma warning restore 1998 +#nullable restore +#line 3 "x:\dir\subdir\Test\TestComponent.cshtml" + + private MyComponent _my; + public void Foo() { System.GC.KeepAlive(_my); } + +#line default +#line hidden +#nullable disable + } +} +namespace __Blazor.Test.TestComponent +{ + #line hidden + internal static class TypeInference + { + public static void CreateMyComponent_0(global::Microsoft.AspNetCore.Components.RenderTree.RenderTreeBuilder builder, int seq, int __seq0, TItem __arg0, int __seq1, global::System.Action> __arg1) + { + builder.OpenComponent>(seq); + builder.AddAttribute(__seq0, "Item", __arg0); + builder.AddComponentReferenceCapture(__seq1, (__value) => { __arg1((global::Test.MyComponent)__value); }); + builder.CloseComponent(); + } + } +} +#pragma warning restore 1591 diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/GenericComponent_WithComponentRef_TypeInference_SuppressField/TestComponent.ir.txt b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/GenericComponent_WithComponentRef_TypeInference_SuppressField/TestComponent.ir.txt new file mode 100644 index 0000000000..30bbe933db --- /dev/null +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/GenericComponent_WithComponentRef_TypeInference_SuppressField/TestComponent.ir.txt @@ -0,0 +1,18 @@ +Document - + NamespaceDeclaration - - Test + UsingDirective - (3:1,1 [14] ) - System + UsingDirective - (18:2,1 [34] ) - System.Collections.Generic + UsingDirective - (53:3,1 [19] ) - System.Linq + UsingDirective - (73:4,1 [30] ) - System.Threading.Tasks + UsingDirective - (104:5,1 [39] ) - Microsoft.AspNetCore.Components + ClassDeclaration - - public - TestComponent - Microsoft.AspNetCore.Components.ComponentBase - + MethodDeclaration - - protected override - void - BuildRenderTree + Component - (0:0,0 [54] x:\dir\subdir\Test\TestComponent.cshtml) - MyComponent + ComponentAttribute - (19:0,19 [1] x:\dir\subdir\Test\TestComponent.cshtml) - Item - AttributeStructure.DoubleQuotes + IntermediateToken - (19:0,19 [1] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - 3 + ReferenceCapture - (28:0,28 [3] x:\dir\subdir\Test\TestComponent.cshtml) - _my + CSharpCode - (65:2,7 [90] x:\dir\subdir\Test\TestComponent.cshtml) + IntermediateToken - (65:2,7 [90] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - \n private MyComponent _my;\n public void Foo() { System.GC.KeepAlive(_my); }\n + NamespaceDeclaration - - __Blazor.Test.TestComponent + ClassDeclaration - - internal static - TypeInference - - + ComponentTypeInferenceMethod - - __Blazor.Test.TestComponent.TypeInference - CreateMyComponent_0 diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/GenericComponent_WithComponentRef_TypeInference_SuppressField/TestComponent.mappings.txt b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/GenericComponent_WithComponentRef_TypeInference_SuppressField/TestComponent.mappings.txt new file mode 100644 index 0000000000..3a40923263 --- /dev/null +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/GenericComponent_WithComponentRef_TypeInference_SuppressField/TestComponent.mappings.txt @@ -0,0 +1,16 @@ +Source Location: (28:0,28 [3] x:\dir\subdir\Test\TestComponent.cshtml) +|_my| +Generated Location: (869:26,28 [3] ) +|_my| + +Source Location: (65:2,7 [90] x:\dir\subdir\Test\TestComponent.cshtml) +| + private MyComponent _my; + public void Foo() { System.GC.KeepAlive(_my); } +| +Generated Location: (1092:37,7 [90] ) +| + private MyComponent _my; + public void Foo() { System.GC.KeepAlive(_my); } +| + diff --git a/src/Razor/Microsoft.CodeAnalysis.Razor/src/RefTagHelperDescriptorProvider.cs b/src/Razor/Microsoft.CodeAnalysis.Razor/src/RefTagHelperDescriptorProvider.cs index ed1efdf453..ccc3ee7ad0 100644 --- a/src/Razor/Microsoft.CodeAnalysis.Razor/src/RefTagHelperDescriptorProvider.cs +++ b/src/Razor/Microsoft.CodeAnalysis.Razor/src/RefTagHelperDescriptorProvider.cs @@ -61,16 +61,24 @@ namespace Microsoft.CodeAnalysis.Razor }); }); - builder.BindAttribute(attribute => + builder.BindAttribute(@ref => { - attribute.Documentation = ComponentResources.RefTagHelper_Documentation; - attribute.Name = "@ref"; + @ref.Documentation = ComponentResources.RefTagHelper_Documentation; + @ref.Name = "@ref"; // WTE has a bug 15.7p1 where a Tag Helper without a display-name that looks like // a C# property will crash trying to create the tooltips. - attribute.SetPropertyName("Ref"); - attribute.TypeName = typeof(object).FullName; - attribute.Metadata[ComponentMetadata.Common.DirectiveAttribute] = bool.TrueString; + @ref.SetPropertyName("Ref"); + @ref.TypeName = typeof(object).FullName; + @ref.Metadata[ComponentMetadata.Common.DirectiveAttribute] = bool.TrueString; + + @ref.BindAttributeParameter(suppressField => + { + suppressField.Name = "suppressField"; + suppressField.Documentation = ComponentResources.RefTagHelper_SuppressField_Documentation; + suppressField.TypeName = typeof(bool).FullName; + suppressField.SetPropertyName("SuppressField"); + }); }); return builder.Build(); diff --git a/src/Razor/Microsoft.CodeAnalysis.Razor/test/RefTagHelperDescriptorProviderTest.cs b/src/Razor/Microsoft.CodeAnalysis.Razor/test/RefTagHelperDescriptorProviderTest.cs index 0948102d3f..ef4a3ed558 100644 --- a/src/Razor/Microsoft.CodeAnalysis.Razor/test/RefTagHelperDescriptorProviderTest.cs +++ b/src/Razor/Microsoft.CodeAnalysis.Razor/test/RefTagHelperDescriptorProviderTest.cs @@ -38,7 +38,7 @@ namespace Microsoft.CodeAnalysis.Razor Assert.False(item.IsComponentOrChildContentTagHelper()); Assert.Equal( - "Populates the specified field or property with a reference to the element or component.", + "Generates the specified field, and populates it during rendering with a reference to the element or component.", item.Documentation); Assert.Equal("Microsoft.AspNetCore.Components", item.AssemblyName); @@ -74,7 +74,7 @@ namespace Microsoft.CodeAnalysis.Razor Assert.False(attribute.IsIndexerStringProperty); Assert.Equal( - "Populates the specified field or property with a reference to the element or component.", + "Generates the specified field, and populates it during rendering with a reference to the element or component.", attribute.Documentation); Assert.Equal("@ref", attribute.Name); @@ -84,6 +84,13 @@ namespace Microsoft.CodeAnalysis.Razor Assert.False(attribute.IsStringProperty); Assert.False(attribute.IsBooleanProperty); Assert.False(attribute.IsEnum); + + var parameter = Assert.Single(attribute.BoundAttributeParameters); + Assert.Empty(parameter.Diagnostics); + Assert.Equal(":suppressField", parameter.DisplayName); + Assert.Equal("Suppresses the generation of a field by '@ref'. Specify '@ref:suppressField' to define your own property or field.", parameter.Documentation); + Assert.True(parameter.IsBooleanProperty); + Assert.Equal("suppressField", parameter.Name); } } }