From 33a0f7a0dbdb80c31908bc2c4655ecaaeb86a37e Mon Sep 17 00:00:00 2001 From: Pranav K Date: Wed, 16 Sep 2015 22:06:25 -0700 Subject: [PATCH] Replace NotNullAttribute with thrown exceptions --- .../CodeAnalysisAttributeUtilities.cs | 9 +- .../CodeAnalysisSymbolBasedPropertyInfo.cs | 10 +- .../CodeAnalysisSymbolBasedTypeInfo.cs | 25 +++-- .../PrecompilationTagHelperTypeResolver.cs | 15 ++- .../project.json | 4 - .../TagHelpers/DefaultTagHelperContent.cs | 96 ++++++++++++++++--- .../TagHelpers/OutputElementHintAttribute.cs | 8 +- .../ReadOnlyTagHelperAttributeList.cs | 56 +++++++++-- .../TagHelpers/RuntimePropertyInfo.cs | 10 +- .../TagHelpers/RuntimeTypeInfo.cs | 9 +- .../TagHelpers/TagHelperAttribute.cs | 10 +- .../TagHelpers/TagHelperAttributeList.cs | 72 ++++++++++++-- .../TagHelpers/TagHelperContext.cs | 29 +++++- .../TagHelpers/TagHelperDescriptorFactory.cs | 15 ++- .../TagHelpers/TagHelperDescriptorResolver.cs | 8 +- .../TagHelperDesignTimeDescriptorFactory.cs | 17 +++- .../TagHelpers/TagHelperExecutionContext.cs | 71 +++++++++++--- .../TagHelpers/TagHelperOutput.cs | 9 +- .../TagHelpers/TagHelperRunner.cs | 9 +- .../TagHelpers/TagHelperScopeManager.cs | 35 ++++++- .../TagHelpers/TagHelperTypeResolver.cs | 15 ++- .../XMLDocumentationProvider.cs | 17 +++- .../project.json | 4 - .../TagHelperExecutionContextTest.cs | 8 +- 24 files changed, 451 insertions(+), 110 deletions(-) diff --git a/src/Microsoft.AspNet.Razor.Runtime.Precompilation/CodeAnalysisAttributeUtilities.cs b/src/Microsoft.AspNet.Razor.Runtime.Precompilation/CodeAnalysisAttributeUtilities.cs index 80cb142361..b27c691e53 100644 --- a/src/Microsoft.AspNet.Razor.Runtime.Precompilation/CodeAnalysisAttributeUtilities.cs +++ b/src/Microsoft.AspNet.Razor.Runtime.Precompilation/CodeAnalysisAttributeUtilities.cs @@ -1,4 +1,4 @@ -// Copyright (c) .NET Foundation. All rights reserved. +// 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; @@ -29,9 +29,14 @@ namespace Microsoft.AspNet.Razor.Runtime.Precompilation /// The type. /// The to find attributes on. /// - public static IEnumerable GetCustomAttributes([NotNull] ISymbol symbol) + public static IEnumerable GetCustomAttributes(ISymbol symbol) where TAttribute : Attribute { + if (symbol == null) + { + throw new ArgumentNullException(nameof(symbol)); + } + var attributes = symbol.GetAttributes(); if (attributes.Length > 0) { diff --git a/src/Microsoft.AspNet.Razor.Runtime.Precompilation/CodeAnalysisSymbolBasedPropertyInfo.cs b/src/Microsoft.AspNet.Razor.Runtime.Precompilation/CodeAnalysisSymbolBasedPropertyInfo.cs index df39e07492..f94571436a 100644 --- a/src/Microsoft.AspNet.Razor.Runtime.Precompilation/CodeAnalysisSymbolBasedPropertyInfo.cs +++ b/src/Microsoft.AspNet.Razor.Runtime.Precompilation/CodeAnalysisSymbolBasedPropertyInfo.cs @@ -1,4 +1,4 @@ -// Copyright (c) .NET Foundation. All rights reserved. +// 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; @@ -6,7 +6,6 @@ using System.Collections.Generic; using System.Diagnostics; using Microsoft.AspNet.Razor.Runtime.TagHelpers; using Microsoft.CodeAnalysis; -using Microsoft.Framework.Internal; namespace Microsoft.AspNet.Razor.Runtime.Precompilation { @@ -22,8 +21,13 @@ namespace Microsoft.AspNet.Razor.Runtime.Precompilation /// Initializes a new instance of . /// /// The . - public CodeAnalysisSymbolBasedPropertyInfo([NotNull] IPropertySymbol propertySymbol) + public CodeAnalysisSymbolBasedPropertyInfo(IPropertySymbol propertySymbol) { + if (propertySymbol == null) + { + throw new ArgumentNullException(nameof(propertySymbol)); + } + _propertySymbol = propertySymbol; PropertyType = new CodeAnalysisSymbolBasedTypeInfo(_propertySymbol.Type); } diff --git a/src/Microsoft.AspNet.Razor.Runtime.Precompilation/CodeAnalysisSymbolBasedTypeInfo.cs b/src/Microsoft.AspNet.Razor.Runtime.Precompilation/CodeAnalysisSymbolBasedTypeInfo.cs index 4d7d8ec776..3865b6e04b 100644 --- a/src/Microsoft.AspNet.Razor.Runtime.Precompilation/CodeAnalysisSymbolBasedTypeInfo.cs +++ b/src/Microsoft.AspNet.Razor.Runtime.Precompilation/CodeAnalysisSymbolBasedTypeInfo.cs @@ -1,4 +1,4 @@ -// Copyright (c) .NET Foundation. All rights reserved. +// 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; @@ -9,7 +9,6 @@ using System.Reflection; using System.Text; using Microsoft.AspNet.Razor.Runtime.TagHelpers; using Microsoft.CodeAnalysis; -using Microsoft.Framework.Internal; namespace Microsoft.AspNet.Razor.Runtime.Precompilation { @@ -35,8 +34,13 @@ namespace Microsoft.AspNet.Razor.Runtime.Precompilation /// Initializes a new instance of . /// /// The . - public CodeAnalysisSymbolBasedTypeInfo([NotNull] ITypeSymbol type) + public CodeAnalysisSymbolBasedTypeInfo(ITypeSymbol type) { + if (type == null) + { + throw new ArgumentNullException(nameof(type)); + } + _type = type; _underlyingType = UnwrapArrayType(type); } @@ -174,10 +178,10 @@ namespace Microsoft.AspNet.Razor.Runtime.Precompilation ITypeSymbol sourceTypeSymbol, System.Reflection.TypeInfo targetTypeInfo) { - return string.Equals( - targetTypeInfo.FullName, - GetFullName(sourceTypeSymbol), - StringComparison.Ordinal); + return string.Equals( + targetTypeInfo.FullName, + GetFullName(sourceTypeSymbol), + StringComparison.Ordinal); } /// @@ -185,8 +189,13 @@ namespace Microsoft.AspNet.Razor.Runtime.Precompilation /// /// The to generate the name for. /// The assembly qualified name. - public static string GetAssemblyQualifiedName([NotNull] ITypeSymbol symbol) + public static string GetAssemblyQualifiedName(ITypeSymbol symbol) { + if (symbol == null) + { + throw new ArgumentNullException(nameof(symbol)); + } + var builder = new StringBuilder(); GetAssemblyQualifiedName(builder, symbol); diff --git a/src/Microsoft.AspNet.Razor.Runtime.Precompilation/PrecompilationTagHelperTypeResolver.cs b/src/Microsoft.AspNet.Razor.Runtime.Precompilation/PrecompilationTagHelperTypeResolver.cs index eba15977fe..5622267dd0 100644 --- a/src/Microsoft.AspNet.Razor.Runtime.Precompilation/PrecompilationTagHelperTypeResolver.cs +++ b/src/Microsoft.AspNet.Razor.Runtime.Precompilation/PrecompilationTagHelperTypeResolver.cs @@ -6,7 +6,6 @@ using System.Collections.Generic; using System.Reflection; using Microsoft.AspNet.Razor.Runtime.TagHelpers; using Microsoft.CodeAnalysis; -using Microsoft.Framework.Internal; namespace Microsoft.AspNet.Razor.Runtime.Precompilation { @@ -24,14 +23,24 @@ namespace Microsoft.AspNet.Razor.Runtime.Precompilation /// Initializes a new instance of . /// /// The . - public PrecompilationTagHelperTypeResolver([NotNull] Compilation compilation) + public PrecompilationTagHelperTypeResolver(Compilation compilation) { + if (compilation == null) + { + throw new ArgumentNullException(nameof(compilation)); + } + _compilation = compilation; } /// - protected override IEnumerable GetTopLevelExportedTypes([NotNull] AssemblyName assemblyName) + protected override IEnumerable GetTopLevelExportedTypes(AssemblyName assemblyName) { + if (assemblyName == null) + { + throw new ArgumentNullException(nameof(assemblyName)); + } + lock (_assemblyLookupLock) { IEnumerable result; diff --git a/src/Microsoft.AspNet.Razor.Runtime.Precompilation/project.json b/src/Microsoft.AspNet.Razor.Runtime.Precompilation/project.json index 235b12f3f5..79d17c5352 100644 --- a/src/Microsoft.AspNet.Razor.Runtime.Precompilation/project.json +++ b/src/Microsoft.AspNet.Razor.Runtime.Precompilation/project.json @@ -9,10 +9,6 @@ "warningsAsErrors": true }, "dependencies": { - "Microsoft.Framework.NotNullAttribute.Sources": { - "version": "1.0.0-*", - "type": "build" - }, "Microsoft.Framework.PropertyActivator.Sources": { "version": "1.0.0-*", "type": "build" diff --git a/src/Microsoft.AspNet.Razor.Runtime/TagHelpers/DefaultTagHelperContent.cs b/src/Microsoft.AspNet.Razor.Runtime/TagHelpers/DefaultTagHelperContent.cs index 6b06b93a71..6f68a8b4ef 100644 --- a/src/Microsoft.AspNet.Razor.Runtime/TagHelpers/DefaultTagHelperContent.cs +++ b/src/Microsoft.AspNet.Razor.Runtime/TagHelpers/DefaultTagHelperContent.cs @@ -132,72 +132,132 @@ namespace Microsoft.AspNet.Razor.Runtime.TagHelpers } /// - public override TagHelperContent AppendFormat([NotNull] string format, object arg0) + public override TagHelperContent AppendFormat(string format, object arg0) { + if (format == null) + { + throw new ArgumentNullException(nameof(format)); + } + Buffer.Append(string.Format(format, arg0)); return this; } /// - public override TagHelperContent AppendFormat([NotNull] string format, object arg0, object arg1) + public override TagHelperContent AppendFormat(string format, object arg0, object arg1) { + if (format == null) + { + throw new ArgumentNullException(nameof(format)); + } + Buffer.Append(string.Format(format, arg0, arg1)); return this; } /// - public override TagHelperContent AppendFormat([NotNull] string format, object arg0, object arg1, object arg2) + public override TagHelperContent AppendFormat(string format, object arg0, object arg1, object arg2) { + if (format == null) + { + throw new ArgumentNullException(nameof(format)); + } + Buffer.Append(string.Format(format, arg0, arg1, arg2)); return this; } /// - public override TagHelperContent AppendFormat([NotNull] string format, params object[] args) + public override TagHelperContent AppendFormat(string format, params object[] args) { + if (format == null) + { + throw new ArgumentNullException(nameof(format)); + } + Buffer.Append(string.Format(format, args)); return this; } /// public override TagHelperContent AppendFormat( - [NotNull] IFormatProvider provider, - [NotNull] string format, + IFormatProvider provider, + string format, object arg0) { + if (provider == null) + { + throw new ArgumentNullException(nameof(provider)); + } + + if (format == null) + { + throw new ArgumentNullException(nameof(format)); + } + Buffer.Append(string.Format(provider, format, arg0)); return this; } /// public override TagHelperContent AppendFormat( - [NotNull] IFormatProvider provider, - [NotNull] string format, + IFormatProvider provider, + string format, object arg0, object arg1) { + if (provider == null) + { + throw new ArgumentNullException(nameof(provider)); + } + + if (format == null) + { + throw new ArgumentNullException(nameof(format)); + } + Buffer.Append(string.Format(provider, format, arg0, arg1)); return this; } /// public override TagHelperContent AppendFormat( - [NotNull] IFormatProvider provider, - [NotNull] string format, + IFormatProvider provider, + string format, object arg0, object arg1, object arg2) { + if (provider == null) + { + throw new ArgumentNullException(nameof(provider)); + } + + if (format == null) + { + throw new ArgumentNullException(nameof(format)); + } + Buffer.Append(string.Format(provider, format, arg0, arg1, arg2)); return this; } /// public override TagHelperContent AppendFormat( - [NotNull] IFormatProvider provider, - [NotNull] string format, + IFormatProvider provider, + string format, params object[] args) { + if (provider == null) + { + throw new ArgumentNullException(nameof(provider)); + } + + if (format == null) + { + throw new ArgumentNullException(nameof(format)); + } + Buffer.Append(string.Format(provider, format, args)); return this; } @@ -232,8 +292,18 @@ namespace Microsoft.AspNet.Razor.Runtime.TagHelpers } /// - public override void WriteTo([NotNull] TextWriter writer, [NotNull] IHtmlEncoder encoder) + public override void WriteTo(TextWriter writer, IHtmlEncoder encoder) { + if (writer == null) + { + throw new ArgumentNullException(nameof(writer)); + } + + if (encoder == null) + { + throw new ArgumentNullException(nameof(encoder)); + } + Buffer.WriteTo(writer, encoder); } diff --git a/src/Microsoft.AspNet.Razor.Runtime/TagHelpers/OutputElementHintAttribute.cs b/src/Microsoft.AspNet.Razor.Runtime/TagHelpers/OutputElementHintAttribute.cs index 62e814f74c..3ed807c6fd 100644 --- a/src/Microsoft.AspNet.Razor.Runtime/TagHelpers/OutputElementHintAttribute.cs +++ b/src/Microsoft.AspNet.Razor.Runtime/TagHelpers/OutputElementHintAttribute.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.Framework.Internal; namespace Microsoft.AspNet.Razor.Runtime.TagHelpers { @@ -18,8 +17,13 @@ namespace Microsoft.AspNet.Razor.Runtime.TagHelpers /// /// The HTML element the may output. /// - public OutputElementHintAttribute([NotNull] string outputElement) + public OutputElementHintAttribute(string outputElement) { + if (outputElement == null) + { + throw new ArgumentNullException(nameof(outputElement)); + } + OutputElement = outputElement; } diff --git a/src/Microsoft.AspNet.Razor.Runtime/TagHelpers/ReadOnlyTagHelperAttributeList.cs b/src/Microsoft.AspNet.Razor.Runtime/TagHelpers/ReadOnlyTagHelperAttributeList.cs index cb8904d48e..6cd5eebade 100644 --- a/src/Microsoft.AspNet.Razor.Runtime/TagHelpers/ReadOnlyTagHelperAttributeList.cs +++ b/src/Microsoft.AspNet.Razor.Runtime/TagHelpers/ReadOnlyTagHelperAttributeList.cs @@ -32,8 +32,13 @@ namespace Microsoft.AspNet.Razor.Runtime.TagHelpers /// . /// /// The collection to wrap. - public ReadOnlyTagHelperAttributeList([NotNull] IEnumerable attributes) + public ReadOnlyTagHelperAttributeList(IEnumerable attributes) { + if (attributes == null) + { + throw new ArgumentNullException(nameof(attributes)); + } + Attributes = new List(attributes); } @@ -64,10 +69,15 @@ namespace Microsoft.AspNet.Razor.Runtime.TagHelpers /// matching . /// /// is compared case-insensitively. - public TAttribute this[[NotNull] string name] + public TAttribute this[string name] { get { + if (name == null) + { + throw new ArgumentNullException(nameof(name)); + } + return Attributes.FirstOrDefault(attribute => NameEquals(name, attribute)); } } @@ -93,8 +103,13 @@ namespace Microsoft.AspNet.Razor.Runtime.TagHelpers /// /// s is compared case-insensitively. /// - public bool Contains([NotNull] TAttribute item) + public bool Contains(TAttribute item) { + if (item == null) + { + throw new ArgumentNullException(nameof(item)); + } + return Attributes.Contains(item); } @@ -109,8 +124,13 @@ namespace Microsoft.AspNet.Razor.Runtime.TagHelpers /// exists in the collection; otherwise, false. /// /// is compared case-insensitively. - public bool ContainsName([NotNull] string name) + public bool ContainsName(string name) { + if (name == null) + { + throw new ArgumentNullException(nameof(name)); + } + return Attributes.Any(attribute => NameEquals(name, attribute)); } @@ -124,8 +144,13 @@ namespace Microsoft.AspNet.Razor.Runtime.TagHelpers /// /// s is compared case-insensitively. /// - public int IndexOf([NotNull] TAttribute item) + public int IndexOf(TAttribute item) { + if (item == null) + { + throw new ArgumentNullException(nameof(item)); + } + return Attributes.IndexOf(item); } @@ -141,8 +166,13 @@ namespace Microsoft.AspNet.Razor.Runtime.TagHelpers /// true if a with the same /// exists in the collection; otherwise, false. /// is compared case-insensitively. - public bool TryGetAttribute([NotNull] string name, out TAttribute attribute) + public bool TryGetAttribute(string name, out TAttribute attribute) { + if (name == null) + { + throw new ArgumentNullException(nameof(name)); + } + attribute = Attributes.FirstOrDefault(attr => NameEquals(name, attr)); return attribute != null; @@ -160,8 +190,13 @@ namespace Microsoft.AspNet.Razor.Runtime.TagHelpers /// true if at least one with the same /// exists in the collection; otherwise, false. /// is compared case-insensitively. - public bool TryGetAttributes([NotNull] string name, out IEnumerable attributes) + public bool TryGetAttributes(string name, out IEnumerable attributes) { + if (name == null) + { + throw new ArgumentNullException(nameof(name)); + } + attributes = Attributes.Where(attribute => NameEquals(name, attribute)); return attributes.Any(); @@ -187,8 +222,13 @@ namespace Microsoft.AspNet.Razor.Runtime.TagHelpers /// The attribute to compare against. /// true if case-insensitively matches s /// . - protected static bool NameEquals(string name, [NotNull] TAttribute attribute) + protected static bool NameEquals(string name, TAttribute attribute) { + if (attribute == null) + { + throw new ArgumentNullException(nameof(attribute)); + } + return string.Equals(name, attribute.Name, StringComparison.OrdinalIgnoreCase); } } diff --git a/src/Microsoft.AspNet.Razor.Runtime/TagHelpers/RuntimePropertyInfo.cs b/src/Microsoft.AspNet.Razor.Runtime/TagHelpers/RuntimePropertyInfo.cs index b79ff696d7..a889ca1c81 100644 --- a/src/Microsoft.AspNet.Razor.Runtime/TagHelpers/RuntimePropertyInfo.cs +++ b/src/Microsoft.AspNet.Razor.Runtime/TagHelpers/RuntimePropertyInfo.cs @@ -1,11 +1,10 @@ -// Copyright (c) .NET Foundation. All rights reserved. +// 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.Collections.Generic; using System.Reflection; using Microsoft.AspNet.Razor.Runtime.TagHelpers; -using Microsoft.Framework.Internal; namespace Microsoft.AspNet.Razor.Runtime { @@ -18,8 +17,13 @@ namespace Microsoft.AspNet.Razor.Runtime /// Initializes a new instance of . /// /// The instance to adapt. - public RuntimePropertyInfo([NotNull] PropertyInfo propertyInfo) + public RuntimePropertyInfo(PropertyInfo propertyInfo) { + if (propertyInfo == null) + { + throw new ArgumentNullException(nameof(propertyInfo)); + } + Property = propertyInfo; } diff --git a/src/Microsoft.AspNet.Razor.Runtime/TagHelpers/RuntimeTypeInfo.cs b/src/Microsoft.AspNet.Razor.Runtime/TagHelpers/RuntimeTypeInfo.cs index c8148f1f19..c67d732d9b 100644 --- a/src/Microsoft.AspNet.Razor.Runtime/TagHelpers/RuntimeTypeInfo.cs +++ b/src/Microsoft.AspNet.Razor.Runtime/TagHelpers/RuntimeTypeInfo.cs @@ -1,4 +1,4 @@ -// Copyright (c) .NET Foundation. All rights reserved. +// 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; @@ -27,8 +27,13 @@ namespace Microsoft.AspNet.Razor.Runtime.TagHelpers /// Initializes a new instance of /// /// The instance to adapt. - public RuntimeTypeInfo([NotNull] TypeInfo typeInfo) + public RuntimeTypeInfo(TypeInfo typeInfo) { + if (typeInfo == null) + { + throw new ArgumentNullException(nameof(typeInfo)); + } + TypeInfo = typeInfo; } diff --git a/src/Microsoft.AspNet.Razor.Runtime/TagHelpers/TagHelperAttribute.cs b/src/Microsoft.AspNet.Razor.Runtime/TagHelpers/TagHelperAttribute.cs index f9fada7a01..8b38c978ec 100644 --- a/src/Microsoft.AspNet.Razor.Runtime/TagHelpers/TagHelperAttribute.cs +++ b/src/Microsoft.AspNet.Razor.Runtime/TagHelpers/TagHelperAttribute.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.Framework.Internal; namespace Microsoft.AspNet.Razor.Runtime.TagHelpers { @@ -25,9 +24,14 @@ namespace Microsoft.AspNet.Razor.Runtime.TagHelpers /// . /// /// A whose values should be copied. - public TagHelperAttribute([NotNull] IReadOnlyTagHelperAttribute attribute) - : this (attribute?.Name, attribute?.Value) + public TagHelperAttribute(IReadOnlyTagHelperAttribute attribute) + : this(attribute?.Name, attribute?.Value) { + if (attribute == null) + { + throw new ArgumentNullException(nameof(attribute)); + } + Minimized = attribute.Minimized; } diff --git a/src/Microsoft.AspNet.Razor.Runtime/TagHelpers/TagHelperAttributeList.cs b/src/Microsoft.AspNet.Razor.Runtime/TagHelpers/TagHelperAttributeList.cs index feaa5da34b..abfde82719 100644 --- a/src/Microsoft.AspNet.Razor.Runtime/TagHelpers/TagHelperAttributeList.cs +++ b/src/Microsoft.AspNet.Razor.Runtime/TagHelpers/TagHelperAttributeList.cs @@ -25,9 +25,13 @@ namespace Microsoft.AspNet.Razor.Runtime.TagHelpers /// . /// /// The collection to wrap. - public TagHelperAttributeList([NotNull] IEnumerable attributes) + public TagHelperAttributeList(IEnumerable attributes) : base(attributes) { + if (attributes == null) + { + throw new ArgumentNullException(nameof(attributes)); + } } /// @@ -40,9 +44,13 @@ namespace Microsoft.AspNet.Razor.Runtime.TagHelpers { return base[index]; } - [param: NotNull] set { + if (value == null) + { + throw new ArgumentNullException(nameof(value)); + } + if (value.Name == null) { throw new ArgumentException( @@ -83,15 +91,29 @@ namespace Microsoft.AspNet.Razor.Runtime.TagHelpers /// attributes["name"] = new TagHelperAttribute("name", "value"); /// /// - public new TagHelperAttribute this[[NotNull] string name] + public new TagHelperAttribute this[string name] { get { + if (name == null) + { + throw new ArgumentNullException(nameof(name)); + } + return base[name]; } - [param: NotNull] set { + if (name == null) + { + throw new ArgumentNullException(nameof(name)); + } + + if (value == null) + { + throw new ArgumentNullException(nameof(value)); + } + // Name will be null if user attempts to set the attribute via an implicit conversion: // output.Attributes["someName"] = "someValue" if (value.Name == null) @@ -152,8 +174,13 @@ namespace Microsoft.AspNet.Razor.Runtime.TagHelpers /// /// The of the attribute to add. /// The of the attribute to add. - public void Add([NotNull] string name, object value) + public void Add(string name, object value) { + if (name == null) + { + throw new ArgumentNullException(nameof(name)); + } + Attributes.Add(new TagHelperAttribute(name, value)); } @@ -161,8 +188,13 @@ namespace Microsoft.AspNet.Razor.Runtime.TagHelpers /// /// 's must not be null. /// - public void Add([NotNull] TagHelperAttribute attribute) + public void Add(TagHelperAttribute attribute) { + if (attribute == null) + { + throw new ArgumentNullException(nameof(attribute)); + } + if (attribute.Name == null) { throw new ArgumentException( @@ -179,8 +211,13 @@ namespace Microsoft.AspNet.Razor.Runtime.TagHelpers /// /// 's must not be null. /// - public void Insert(int index, [NotNull] TagHelperAttribute attribute) + public void Insert(int index, TagHelperAttribute attribute) { + if (attribute == null) + { + throw new ArgumentNullException(nameof(attribute)); + } + if (attribute.Name == null) { throw new ArgumentException( @@ -194,8 +231,13 @@ namespace Microsoft.AspNet.Razor.Runtime.TagHelpers } /// - public void CopyTo([NotNull] TagHelperAttribute[] array, int index) + public void CopyTo(TagHelperAttribute[] array, int index) { + if (array == null) + { + throw new ArgumentNullException(nameof(array)); + } + Attributes.CopyTo(array, index); } @@ -203,8 +245,13 @@ namespace Microsoft.AspNet.Razor.Runtime.TagHelpers /// /// s is compared case-insensitively. /// - public bool Remove([NotNull] TagHelperAttribute attribute) + public bool Remove(TagHelperAttribute attribute) { + if (attribute == null) + { + throw new ArgumentNullException(nameof(attribute)); + } + return Attributes.Remove(attribute); } @@ -225,8 +272,13 @@ namespace Microsoft.AspNet.Razor.Runtime.TagHelpers /// true if at least 1 was removed; otherwise, false. /// /// is compared case-insensitively. - public bool RemoveAll([NotNull] string name) + public bool RemoveAll(string name) { + if (name == null) + { + throw new ArgumentNullException(nameof(name)); + } + return Attributes.RemoveAll(attribute => NameEquals(name, attribute)) > 0; } diff --git a/src/Microsoft.AspNet.Razor.Runtime/TagHelpers/TagHelperContext.cs b/src/Microsoft.AspNet.Razor.Runtime/TagHelpers/TagHelperContext.cs index 3ee7ba85fc..96cdbc971b 100644 --- a/src/Microsoft.AspNet.Razor.Runtime/TagHelpers/TagHelperContext.cs +++ b/src/Microsoft.AspNet.Razor.Runtime/TagHelpers/TagHelperContext.cs @@ -5,7 +5,6 @@ using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; -using Microsoft.Framework.Internal; namespace Microsoft.AspNet.Razor.Runtime.TagHelpers { @@ -26,11 +25,31 @@ namespace Microsoft.AspNet.Razor.Runtime.TagHelpers /// A delegate used to execute and retrieve the rendered child content /// asynchronously. public TagHelperContext( - [NotNull] IEnumerable allAttributes, - [NotNull] IDictionary items, - [NotNull] string uniqueId, - [NotNull] Func> getChildContentAsync) + IEnumerable allAttributes, + IDictionary items, + string uniqueId, + Func> getChildContentAsync) { + if (allAttributes == null) + { + throw new ArgumentNullException(nameof(allAttributes)); + } + + if (items == null) + { + throw new ArgumentNullException(nameof(items)); + } + + if (uniqueId == null) + { + throw new ArgumentNullException(nameof(uniqueId)); + } + + if (getChildContentAsync == null) + { + throw new ArgumentNullException(nameof(getChildContentAsync)); + } + AllAttributes = new ReadOnlyTagHelperAttributeList( allAttributes.Select(attribute => new TagHelperAttribute(attribute.Name, attribute.Value))); Items = items; diff --git a/src/Microsoft.AspNet.Razor.Runtime/TagHelpers/TagHelperDescriptorFactory.cs b/src/Microsoft.AspNet.Razor.Runtime/TagHelpers/TagHelperDescriptorFactory.cs index 9b7cd6bf34..7a91562a44 100644 --- a/src/Microsoft.AspNet.Razor.Runtime/TagHelpers/TagHelperDescriptorFactory.cs +++ b/src/Microsoft.AspNet.Razor.Runtime/TagHelpers/TagHelperDescriptorFactory.cs @@ -8,7 +8,6 @@ using System.Linq; using System.Reflection; using System.Text.RegularExpressions; using Microsoft.AspNet.Razor.TagHelpers; -using Microsoft.Framework.Internal; namespace Microsoft.AspNet.Razor.Runtime.TagHelpers { @@ -54,10 +53,20 @@ namespace Microsoft.AspNet.Razor.Runtime.TagHelpers /// public static IEnumerable CreateDescriptors( string assemblyName, - [NotNull] ITypeInfo typeInfo, + ITypeInfo typeInfo, bool designTime, - [NotNull] ErrorSink errorSink) + ErrorSink errorSink) { + if (typeInfo == null) + { + throw new ArgumentNullException(nameof(typeInfo)); + } + + if (errorSink == null) + { + throw new ArgumentNullException(nameof(errorSink)); + } + if (ShouldSkipDescriptorCreation(designTime, typeInfo)) { return Enumerable.Empty(); diff --git a/src/Microsoft.AspNet.Razor.Runtime/TagHelpers/TagHelperDescriptorResolver.cs b/src/Microsoft.AspNet.Razor.Runtime/TagHelpers/TagHelperDescriptorResolver.cs index d1a292a20d..4a48651e89 100644 --- a/src/Microsoft.AspNet.Razor.Runtime/TagHelpers/TagHelperDescriptorResolver.cs +++ b/src/Microsoft.AspNet.Razor.Runtime/TagHelpers/TagHelperDescriptorResolver.cs @@ -8,7 +8,6 @@ using System.Linq; using System.Text.RegularExpressions; using Microsoft.AspNet.Razor.Parser; using Microsoft.AspNet.Razor.TagHelpers; -using Microsoft.Framework.Internal; namespace Microsoft.AspNet.Razor.Runtime.TagHelpers { @@ -52,8 +51,13 @@ namespace Microsoft.AspNet.Razor.Runtime.TagHelpers } /// - public IEnumerable Resolve([NotNull] TagHelperDescriptorResolutionContext context) + public IEnumerable Resolve(TagHelperDescriptorResolutionContext context) { + if (context == null) + { + throw new ArgumentNullException(nameof(context)); + } + var resolvedDescriptors = new HashSet(TagHelperDescriptorComparer.Default); // tagHelperPrefix directives do not affect which TagHelperDescriptors are added or removed from the final diff --git a/src/Microsoft.AspNet.Razor.Runtime/TagHelpers/TagHelperDesignTimeDescriptorFactory.cs b/src/Microsoft.AspNet.Razor.Runtime/TagHelpers/TagHelperDesignTimeDescriptorFactory.cs index b1f3d9d73d..22f7ce1d36 100644 --- a/src/Microsoft.AspNet.Razor.Runtime/TagHelpers/TagHelperDesignTimeDescriptorFactory.cs +++ b/src/Microsoft.AspNet.Razor.Runtime/TagHelpers/TagHelperDesignTimeDescriptorFactory.cs @@ -1,4 +1,4 @@ -// Copyright (c) .NET Foundation. All rights reserved. +// 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. #if !DNXCORE50 // Cannot accurately resolve the location of the documentation XML file in coreclr. @@ -9,7 +9,6 @@ using System.IO; using System.Linq; using System.Reflection; using Microsoft.AspNet.Razor.TagHelpers; -using Microsoft.Framework.Internal; namespace Microsoft.AspNet.Razor.Runtime.TagHelpers { @@ -27,8 +26,13 @@ namespace Microsoft.AspNet.Razor.Runtime.TagHelpers /// /// A that describes design time specific information /// for the given . - public static TagHelperDesignTimeDescriptor CreateDescriptor([NotNull] Type type) + public static TagHelperDesignTimeDescriptor CreateDescriptor(Type type) { + if (type == null) + { + throw new ArgumentNullException(nameof(type)); + } + var id = XmlDocumentationProvider.GetId(type); var documentationDescriptor = CreateDocumentationDescriptor(type.Assembly, id); @@ -62,8 +66,13 @@ namespace Microsoft.AspNet.Razor.Runtime.TagHelpers /// A that describes design time specific /// information for the given . public static TagHelperAttributeDesignTimeDescriptor CreateAttributeDescriptor( - [NotNull] PropertyInfo propertyInfo) + PropertyInfo propertyInfo) { + if (propertyInfo == null) + { + throw new ArgumentNullException(nameof(propertyInfo)); + } + var id = XmlDocumentationProvider.GetId(propertyInfo); var declaringAssembly = propertyInfo.DeclaringType.Assembly; var documentationDescriptor = CreateDocumentationDescriptor(declaringAssembly, id); diff --git a/src/Microsoft.AspNet.Razor.Runtime/TagHelpers/TagHelperExecutionContext.cs b/src/Microsoft.AspNet.Razor.Runtime/TagHelpers/TagHelperExecutionContext.cs index 363b35b4b0..9dcd7ed21d 100644 --- a/src/Microsoft.AspNet.Razor.Runtime/TagHelpers/TagHelperExecutionContext.cs +++ b/src/Microsoft.AspNet.Razor.Runtime/TagHelpers/TagHelperExecutionContext.cs @@ -4,7 +4,6 @@ using System; using System.Collections.Generic; using System.Threading.Tasks; -using Microsoft.Framework.Internal; namespace Microsoft.AspNet.Razor.Runtime.TagHelpers { @@ -45,14 +44,44 @@ namespace Microsoft.AspNet.Razor.Runtime.TagHelpers /// A delegate used to start a writing scope in a Razor page. /// A delegate used to end a writing scope in a Razor page. public TagHelperExecutionContext( - [NotNull] string tagName, + string tagName, TagMode tagMode, - [NotNull] IDictionary items, - [NotNull] string uniqueId, - [NotNull] Func executeChildContentAsync, - [NotNull] Action startTagHelperWritingScope, - [NotNull] Func endTagHelperWritingScope) + IDictionary items, + string uniqueId, + Func executeChildContentAsync, + Action startTagHelperWritingScope, + Func endTagHelperWritingScope) { + if (tagName == null) + { + throw new ArgumentNullException(nameof(tagName)); + } + + if (items == null) + { + throw new ArgumentNullException(nameof(items)); + } + + if (uniqueId == null) + { + throw new ArgumentNullException(nameof(uniqueId)); + } + + if (executeChildContentAsync == null) + { + throw new ArgumentNullException(nameof(executeChildContentAsync)); + } + + if (startTagHelperWritingScope == null) + { + throw new ArgumentNullException(nameof(startTagHelperWritingScope)); + } + + if (endTagHelperWritingScope == null) + { + throw new ArgumentNullException(nameof(endTagHelperWritingScope)); + } + _tagHelpers = new List(); _executeChildContentAsync = executeChildContentAsync; _startTagHelperWritingScope = startTagHelperWritingScope; @@ -127,8 +156,13 @@ namespace Microsoft.AspNet.Razor.Runtime.TagHelpers /// Tracks the given . /// /// The tag helper to track. - public void Add([NotNull] ITagHelper tagHelper) + public void Add(ITagHelper tagHelper) { + if (tagHelper == null) + { + throw new ArgumentNullException(nameof(tagHelper)); + } + _tagHelpers.Add(tagHelper); } @@ -136,8 +170,13 @@ namespace Microsoft.AspNet.Razor.Runtime.TagHelpers /// Tracks the minimized HTML attribute in and . /// /// The minimized HTML attribute name. - public void AddMinimizedHtmlAttribute([NotNull] string name) + public void AddMinimizedHtmlAttribute(string name) { + if (name == null) + { + throw new ArgumentNullException(nameof(name)); + } + HTMLAttributes.Add( new TagHelperAttribute { @@ -157,8 +196,13 @@ namespace Microsoft.AspNet.Razor.Runtime.TagHelpers /// /// The HTML attribute name. /// The HTML attribute value. - public void AddHtmlAttribute([NotNull] string name, object value) + public void AddHtmlAttribute(string name, object value) { + if (name == null) + { + throw new ArgumentNullException(nameof(name)); + } + HTMLAttributes.Add(name, value); AllAttributes.Add(name, value); } @@ -168,8 +212,13 @@ namespace Microsoft.AspNet.Razor.Runtime.TagHelpers /// /// The bound attribute name. /// The attribute value. - public void AddTagHelperAttribute([NotNull] string name, object value) + public void AddTagHelperAttribute(string name, object value) { + if (name == null) + { + throw new ArgumentNullException(nameof(name)); + } + AllAttributes.Add(name, value); } diff --git a/src/Microsoft.AspNet.Razor.Runtime/TagHelpers/TagHelperOutput.cs b/src/Microsoft.AspNet.Razor.Runtime/TagHelpers/TagHelperOutput.cs index bba143fce4..2bda595413 100644 --- a/src/Microsoft.AspNet.Razor.Runtime/TagHelpers/TagHelperOutput.cs +++ b/src/Microsoft.AspNet.Razor.Runtime/TagHelpers/TagHelperOutput.cs @@ -1,7 +1,7 @@ // 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 Microsoft.Framework.Internal; +using System; namespace Microsoft.AspNet.Razor.Runtime.TagHelpers { @@ -23,8 +23,13 @@ namespace Microsoft.AspNet.Razor.Runtime.TagHelpers /// The HTML attributes. public TagHelperOutput( string tagName, - [NotNull] TagHelperAttributeList attributes) + TagHelperAttributeList attributes) { + if (attributes == null) + { + throw new ArgumentNullException(nameof(attributes)); + } + TagName = tagName; Attributes = new TagHelperAttributeList(attributes); } diff --git a/src/Microsoft.AspNet.Razor.Runtime/TagHelpers/TagHelperRunner.cs b/src/Microsoft.AspNet.Razor.Runtime/TagHelpers/TagHelperRunner.cs index fdd5b40f53..101d6e41d0 100644 --- a/src/Microsoft.AspNet.Razor.Runtime/TagHelpers/TagHelperRunner.cs +++ b/src/Microsoft.AspNet.Razor.Runtime/TagHelpers/TagHelperRunner.cs @@ -1,9 +1,9 @@ // 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 System.Threading.Tasks; -using Microsoft.Framework.Internal; namespace Microsoft.AspNet.Razor.Runtime.TagHelpers { @@ -19,8 +19,13 @@ namespace Microsoft.AspNet.Razor.Runtime.TagHelpers /// /// Resulting from processing all of the /// 's s. - public async Task RunAsync([NotNull] TagHelperExecutionContext executionContext) + public async Task RunAsync(TagHelperExecutionContext executionContext) { + if (executionContext == null) + { + throw new ArgumentNullException(nameof(executionContext)); + } + var tagHelperContext = new TagHelperContext( executionContext.AllAttributes, executionContext.Items, diff --git a/src/Microsoft.AspNet.Razor.Runtime/TagHelpers/TagHelperScopeManager.cs b/src/Microsoft.AspNet.Razor.Runtime/TagHelpers/TagHelperScopeManager.cs index 4464b83fd9..4b6ebfe35b 100644 --- a/src/Microsoft.AspNet.Razor.Runtime/TagHelpers/TagHelperScopeManager.cs +++ b/src/Microsoft.AspNet.Razor.Runtime/TagHelpers/TagHelperScopeManager.cs @@ -34,13 +34,38 @@ namespace Microsoft.AspNet.Razor.Runtime.TagHelpers /// A delegate used to end a writing scope in a Razor page. /// A to use. public TagHelperExecutionContext Begin( - [NotNull] string tagName, + string tagName, TagMode tagMode, - [NotNull] string uniqueId, - [NotNull] Func executeChildContentAsync, - [NotNull] Action startTagHelperWritingScope, - [NotNull] Func endTagHelperWritingScope) + string uniqueId, + Func executeChildContentAsync, + Action startTagHelperWritingScope, + Func endTagHelperWritingScope) { + if (tagName == null) + { + throw new ArgumentNullException(nameof(tagName)); + } + + if (uniqueId == null) + { + throw new ArgumentNullException(nameof(uniqueId)); + } + + if (executeChildContentAsync == null) + { + throw new ArgumentNullException(nameof(executeChildContentAsync)); + } + + if (startTagHelperWritingScope == null) + { + throw new ArgumentNullException(nameof(startTagHelperWritingScope)); + } + + if (endTagHelperWritingScope == null) + { + throw new ArgumentNullException(nameof(endTagHelperWritingScope)); + } + IDictionary items; // If we're not wrapped by another TagHelper, then there will not be a parentExecutionContext. diff --git a/src/Microsoft.AspNet.Razor.Runtime/TagHelpers/TagHelperTypeResolver.cs b/src/Microsoft.AspNet.Razor.Runtime/TagHelpers/TagHelperTypeResolver.cs index 8a5ea0b43d..2e3bf850d9 100644 --- a/src/Microsoft.AspNet.Razor.Runtime/TagHelpers/TagHelperTypeResolver.cs +++ b/src/Microsoft.AspNet.Razor.Runtime/TagHelpers/TagHelperTypeResolver.cs @@ -5,7 +5,6 @@ using System; using System.Collections.Generic; using System.Linq; using System.Reflection; -using Microsoft.Framework.Internal; namespace Microsoft.AspNet.Razor.Runtime.TagHelpers { @@ -29,8 +28,13 @@ namespace Microsoft.AspNet.Razor.Runtime.TagHelpers public IEnumerable Resolve( string name, SourceLocation documentLocation, - [NotNull] ErrorSink errorSink) + ErrorSink errorSink) { + if (errorSink == null) + { + throw new ArgumentNullException(nameof(errorSink)); + } + if (string.IsNullOrEmpty(name)) { var errorLength = name == null ? 1 : Math.Max(name.Length, 1); @@ -71,8 +75,13 @@ namespace Microsoft.AspNet.Razor.Runtime.TagHelpers /// /// An of types exported from the given . /// - protected virtual IEnumerable GetTopLevelExportedTypes([NotNull] AssemblyName assemblyName) + protected virtual IEnumerable GetTopLevelExportedTypes(AssemblyName assemblyName) { + if (assemblyName == null) + { + throw new ArgumentNullException(nameof(assemblyName)); + } + var exportedTypeInfos = GetExportedTypes(assemblyName); return exportedTypeInfos diff --git a/src/Microsoft.AspNet.Razor.Runtime/XMLDocumentationProvider.cs b/src/Microsoft.AspNet.Razor.Runtime/XMLDocumentationProvider.cs index 7132cc1f80..c78a8e4e92 100644 --- a/src/Microsoft.AspNet.Razor.Runtime/XMLDocumentationProvider.cs +++ b/src/Microsoft.AspNet.Razor.Runtime/XMLDocumentationProvider.cs @@ -1,4 +1,4 @@ -// Copyright (c) .NET Foundation. All rights reserved. +// 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. #if !DNXCORE50 @@ -8,7 +8,6 @@ using System.Linq; using System.Reflection; using System.Text; using System.Xml.Linq; -using Microsoft.Framework.Internal; namespace Microsoft.AspNet.Razor.Runtime { @@ -76,8 +75,13 @@ namespace Microsoft.AspNet.Razor.Runtime /// /// The to get the identifier for. /// The identifier for the given . - public static string GetId([NotNull] Type type) + public static string GetId(Type type) { + if (type == null) + { + throw new ArgumentNullException(nameof(type)); + } + return $"T:{type.FullName}"; } @@ -86,8 +90,13 @@ namespace Microsoft.AspNet.Razor.Runtime /// /// The to get the identifier for. /// The identifier for the given . - public static string GetId([NotNull] PropertyInfo propertyInfo) + public static string GetId(PropertyInfo propertyInfo) { + if (propertyInfo == null) + { + throw new ArgumentNullException(nameof(propertyInfo)); + } + var declaringTypeInfo = propertyInfo.DeclaringType; return $"P:{declaringTypeInfo.FullName}.{propertyInfo.Name}"; } diff --git a/src/Microsoft.AspNet.Razor.Runtime/project.json b/src/Microsoft.AspNet.Razor.Runtime/project.json index 73e4a85112..4a35ed012a 100644 --- a/src/Microsoft.AspNet.Razor.Runtime/project.json +++ b/src/Microsoft.AspNet.Razor.Runtime/project.json @@ -19,10 +19,6 @@ "Microsoft.Framework.CopyOnWriteDictionary.Sources": { "type": "build", "version": "1.0.0-*" - }, - "Microsoft.Framework.NotNullAttribute.Sources": { - "type": "build", - "version": "1.0.0-*" } }, "frameworks": { diff --git a/test/Microsoft.AspNet.Razor.Runtime.Test/TagHelpers/TagHelperExecutionContextTest.cs b/test/Microsoft.AspNet.Razor.Runtime.Test/TagHelpers/TagHelperExecutionContextTest.cs index d8f0dcf94e..ac2759fa66 100644 --- a/test/Microsoft.AspNet.Razor.Runtime.Test/TagHelpers/TagHelperExecutionContextTest.cs +++ b/test/Microsoft.AspNet.Razor.Runtime.Test/TagHelpers/TagHelperExecutionContextTest.cs @@ -58,7 +58,7 @@ namespace Microsoft.AspNet.Razor.Runtime.TagHelpers var executionContext = new TagHelperExecutionContext( "p", tagMode: TagMode.StartTagAndEndTag, - items: null, + items: new Dictionary(), uniqueId: string.Empty, executeChildContentAsync: () => { @@ -91,7 +91,7 @@ namespace Microsoft.AspNet.Razor.Runtime.TagHelpers var executionContext = new TagHelperExecutionContext( "p", tagMode: TagMode.StartTagAndEndTag, - items: null, + items: new Dictionary(), uniqueId: string.Empty, executeChildContentAsync: () => { @@ -120,7 +120,7 @@ namespace Microsoft.AspNet.Razor.Runtime.TagHelpers var executionContext = new TagHelperExecutionContext( "p", tagMode: TagMode.StartTagAndEndTag, - items: null, + items: new Dictionary(), uniqueId: string.Empty, executeChildContentAsync: () => { return Task.FromResult(result: true); }, startTagHelperWritingScope: () => { }, @@ -145,7 +145,7 @@ namespace Microsoft.AspNet.Razor.Runtime.TagHelpers var executionContext = new TagHelperExecutionContext( "p", tagMode: TagMode.StartTagAndEndTag, - items: null, + items: new Dictionary(), uniqueId: string.Empty, executeChildContentAsync: () => {