Skip to content

Commit

Permalink
Pranavs cool feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan Nowak committed May 23, 2019
1 parent ace1eb6 commit 6115c18
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 37 deletions.
70 changes: 36 additions & 34 deletions src/Components/Analyzers/src/ComponentParameterAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,56 +52,58 @@ public override void Initialize(AnalysisContext context)
}
}
if (properties.Count > 0)
if (properties.Count == 0)
{
context.RegisterSymbolEndAction(context =>
{
var captureUnmatchedValuesParameters = new List<IPropertySymbol>();
return;
}
context.RegisterSymbolEndAction(context =>
{
var captureUnmatchedValuesParameters = new List<IPropertySymbol>();
// Per-property validations
foreach (var property in properties)
{
if (property.SetMethod?.DeclaredAccessibility == Accessibility.Public)
{
if (property.SetMethod?.DeclaredAccessibility == Accessibility.Public)
{
context.ReportDiagnostic(Diagnostic.Create(
DiagnosticDescriptors.ComponentParametersShouldNotBePublic,
property.Locations[0],
property.ToDisplayString(SymbolDisplayFormat.CSharpErrorMessageFormat)));
}
context.ReportDiagnostic(Diagnostic.Create(
DiagnosticDescriptors.ComponentParametersShouldNotBePublic,
property.Locations[0],
property.ToDisplayString(SymbolDisplayFormat.CSharpErrorMessageFormat)));
}
if (ComponentFacts.IsParameterWithCaptureUnmatchedValues(symbols, property))
{
captureUnmatchedValuesParameters.Add(property);
if (ComponentFacts.IsParameterWithCaptureUnmatchedValues(symbols, property))
{
captureUnmatchedValuesParameters.Add(property);
// Check the type, we need to be able to assign a Dictionary<string, object>
var conversion = context.Compilation.ClassifyConversion(symbols.ParameterCaptureUnmatchedValuesRuntimeType, property.Type);
if (!conversion.Exists || conversion.IsExplicit)
{
context.ReportDiagnostic(Diagnostic.Create(
DiagnosticDescriptors.ComponentParameterCaptureUnmatchedValuesHasWrongType,
property.Locations[0],
property.ToDisplayString(SymbolDisplayFormat.CSharpErrorMessageFormat),
property.Type.ToDisplayString(SymbolDisplayFormat.CSharpErrorMessageFormat),
symbols.ParameterCaptureUnmatchedValuesRuntimeType.ToDisplayString(SymbolDisplayFormat.CSharpErrorMessageFormat)));
}
if (!conversion.Exists || conversion.IsExplicit)
{
context.ReportDiagnostic(Diagnostic.Create(
DiagnosticDescriptors.ComponentParameterCaptureUnmatchedValuesHasWrongType,
property.Locations[0],
property.ToDisplayString(SymbolDisplayFormat.CSharpErrorMessageFormat),
property.Type.ToDisplayString(SymbolDisplayFormat.CSharpErrorMessageFormat),
symbols.ParameterCaptureUnmatchedValuesRuntimeType.ToDisplayString(SymbolDisplayFormat.CSharpErrorMessageFormat)));
}
}
}
// Check if the type defines multiple CaptureUnmatchedValues parameters. Doing this outside the loop means we place the
// errors on the type.
if (captureUnmatchedValuesParameters.Count > 1)
{
context.ReportDiagnostic(Diagnostic.Create(
DiagnosticDescriptors.ComponentParameterCaptureUnmatchedValuesMustBeUnique,
context.Symbol.Locations[0],
type.ToDisplayString(SymbolDisplayFormat.CSharpErrorMessageFormat),
{
context.ReportDiagnostic(Diagnostic.Create(
DiagnosticDescriptors.ComponentParameterCaptureUnmatchedValuesMustBeUnique,
context.Symbol.Locations[0],
type.ToDisplayString(SymbolDisplayFormat.CSharpErrorMessageFormat),
Environment.NewLine,
string.Join(
Environment.NewLine,
string.Join(
Environment.NewLine,
captureUnmatchedValuesParameters.Select(p => p.ToDisplayString(SymbolDisplayFormat.CSharpErrorMessageFormat)).OrderBy(n => n))));
}
});
}
captureUnmatchedValuesParameters.Select(p => p.ToDisplayString(SymbolDisplayFormat.CSharpErrorMessageFormat)).OrderBy(n => n))));
}
});
}, SymbolKind.NamedType);
});
}
Expand Down
3 changes: 2 additions & 1 deletion src/Components/Analyzers/src/ComponentSymbols.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// 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 Microsoft.CodeAnalysis;

namespace Microsoft.AspNetCore.Components.Analyzers
Expand Down Expand Up @@ -29,7 +30,7 @@ public static bool TryCreate(Compilation compilation, out ComponentSymbols symbo
return false;
}

var dictionary = compilation.GetTypeByMetadataName(ComponentsApi.SystemCollectionsGenericDictionary);
var dictionary = compilation.GetTypeByMetadataName(typeof(Dictionary<,>).FullName);
var @string = compilation.GetSpecialType(SpecialType.System_String);
var @object = compilation.GetSpecialType(SpecialType.System_Object);
if (dictionary == null || @string == null || @object == null)
Expand Down
2 changes: 0 additions & 2 deletions src/Components/Analyzers/src/ComponentsApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ namespace Microsoft.AspNetCore.Components.Analyzers
// Keep these in sync with the actual definitions
internal static class ComponentsApi
{
public static readonly string SystemCollectionsGenericDictionary = "System.Collections.Generic.Dictionary`2";

public static readonly string AssemblyName = "Microsoft.AspNetCore.Components";

public static class ParameterAttribute
Expand Down

0 comments on commit 6115c18

Please sign in to comment.