Skip to content

Commit

Permalink
Makes warnings and errors more accurate when specifying a source
Browse files Browse the repository at this point in the history
  • Loading branch information
NikolayPianikov committed Nov 3, 2023
1 parent 0ee775d commit 3e3e488
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 18 deletions.
30 changes: 15 additions & 15 deletions src/Pure.DI.Core/Core/ApiInvocationProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public void ProcessInvocation(
case nameof(IBinding.As):
if (invocation.ArgumentList.Arguments is [{ Expression: { } lifetimeExpression }])
{
metadataVisitor.VisitLifetime(new MdLifetime(semanticModel, invocation, semanticModel.GetRequiredConstantValue<Lifetime>(lifetimeExpression)));
metadataVisitor.VisitLifetime(new MdLifetime(semanticModel, invocation.ArgumentList, semanticModel.GetRequiredConstantValue<Lifetime>(lifetimeExpression)));
}

break;
Expand All @@ -88,8 +88,8 @@ public void ProcessInvocation(
case [{ Expression: { } publicCompositionType }]:
metadataVisitor.VisitSetup(
new MdSetup(
invocation,
CreateCompositionName(semanticModel.GetRequiredConstantValue<string>(publicCompositionType), @namespace),
invocation.ArgumentList,
CreateCompositionName(semanticModel.GetRequiredConstantValue<string>(publicCompositionType), @namespace, invocation.ArgumentList),
ImmutableArray<MdUsingDirectives>.Empty,
CompositionKind.Public,
GetSettings(invocation),
Expand All @@ -104,8 +104,8 @@ public void ProcessInvocation(
case [{ Expression: { } publicCompositionType }, { Expression: { } kindExpression }]:
metadataVisitor.VisitSetup(
new MdSetup(
invocation,
CreateCompositionName(semanticModel.GetRequiredConstantValue<string>(publicCompositionType), @namespace),
invocation.ArgumentList,
CreateCompositionName(semanticModel.GetRequiredConstantValue<string>(publicCompositionType), @namespace, invocation.ArgumentList),
ImmutableArray<MdUsingDirectives>.Empty,
semanticModel.GetRequiredConstantValue<CompositionKind>(kindExpression),
GetSettings(invocation),
Expand All @@ -127,15 +127,15 @@ public void ProcessInvocation(
case nameof(IConfiguration.DefaultLifetime):
if (invocation.ArgumentList.Arguments is [{ Expression: { } defaultLifetimeSyntax }])
{
metadataVisitor.VisitDefaultLifetime(new MdDefaultLifetime(new MdLifetime(semanticModel, invocation, semanticModel.GetRequiredConstantValue<Lifetime>(defaultLifetimeSyntax))));
metadataVisitor.VisitDefaultLifetime(new MdDefaultLifetime(new MdLifetime(semanticModel, invocation.ArgumentList, semanticModel.GetRequiredConstantValue<Lifetime>(defaultLifetimeSyntax))));
}

break;

case nameof(IConfiguration.DependsOn):
if (BuildConstantArgs<string>(semanticModel, invocation.ArgumentList.Arguments) is [..] compositionTypeNames)
{
metadataVisitor.VisitDependsOn(new MdDependsOn(semanticModel, invocation, compositionTypeNames.Select(i => CreateCompositionName(i, @namespace)).ToImmutableArray()));
metadataVisitor.VisitDependsOn(new MdDependsOn(semanticModel, invocation.ArgumentList, compositionTypeNames.Select(i => CreateCompositionName(i, @namespace, invocation.ArgumentList)).ToImmutableArray()));
}

break;
Expand All @@ -152,7 +152,7 @@ public void ProcessInvocation(
metadataVisitor.VisitContract(
new MdContract(
semanticModel,
invocation,
invocation.ArgumentList,
semanticModel.GetTypeSymbol<ITypeSymbol>(contractType, _cancellationToken),
BuildTags(semanticModel, invocation.ArgumentList.Arguments).ToImmutable()));
}
Expand Down Expand Up @@ -212,31 +212,31 @@ public void ProcessInvocation(
tag = new MdTag(0, semanticModel.GetConstantValue<object>(rootArgs[1].Expression));
}

metadataVisitor.VisitRoot(new MdRoot(invocation, semanticModel, rootSymbol, name, tag));
metadataVisitor.VisitRoot(new MdRoot(rootType, semanticModel, rootSymbol, name, tag));
}

break;

case nameof(IConfiguration.TypeAttribute):
if (genericName.TypeArgumentList.Arguments is [{ } typeAttributeType])
{
metadataVisitor.VisitTypeAttribute(new MdTypeAttribute(semanticModel, invocation, semanticModel.GetTypeSymbol<ITypeSymbol>(typeAttributeType, _cancellationToken), BuildConstantArgs<object>(semanticModel, invocation.ArgumentList.Arguments) is [int positionVal] ? positionVal : 0));
metadataVisitor.VisitTypeAttribute(new MdTypeAttribute(semanticModel, invocation.ArgumentList, semanticModel.GetTypeSymbol<ITypeSymbol>(typeAttributeType, _cancellationToken), BuildConstantArgs<object>(semanticModel, invocation.ArgumentList.Arguments) is [int positionVal] ? positionVal : 0));
}

break;

case nameof(IConfiguration.TagAttribute):
if (genericName.TypeArgumentList.Arguments is [{ } tagAttributeType])
{
metadataVisitor.VisitTagAttribute(new MdTagAttribute(semanticModel, invocation, semanticModel.GetTypeSymbol<ITypeSymbol>(tagAttributeType, _cancellationToken), BuildConstantArgs<object>(semanticModel, invocation.ArgumentList.Arguments) is [int positionVal] ? positionVal : 0));
metadataVisitor.VisitTagAttribute(new MdTagAttribute(semanticModel, invocation.ArgumentList, semanticModel.GetTypeSymbol<ITypeSymbol>(tagAttributeType, _cancellationToken), BuildConstantArgs<object>(semanticModel, invocation.ArgumentList.Arguments) is [int positionVal] ? positionVal : 0));
}

break;

case nameof(IConfiguration.OrdinalAttribute):
if (genericName.TypeArgumentList.Arguments is [{ } ordinalAttributeType])
{
metadataVisitor.VisitOrdinalAttribute(new MdOrdinalAttribute(semanticModel, invocation, semanticModel.GetTypeSymbol<ITypeSymbol>(ordinalAttributeType, _cancellationToken), BuildConstantArgs<object>(semanticModel, invocation.ArgumentList.Arguments) is [int positionVal] ? positionVal : 0));
metadataVisitor.VisitOrdinalAttribute(new MdOrdinalAttribute(semanticModel, invocation.ArgumentList, semanticModel.GetTypeSymbol<ITypeSymbol>(ordinalAttributeType, _cancellationToken), BuildConstantArgs<object>(semanticModel, invocation.ArgumentList.Arguments) is [int positionVal] ? positionVal : 0));
}

break;
Expand Down Expand Up @@ -267,7 +267,7 @@ private void VisitArg(

var argType = semanticModel.GetTypeSymbol<ITypeSymbol>(argTypeSyntax, _cancellationToken);
metadataVisitor.VisitContract(new MdContract(semanticModel, invocation, argType, tags.ToImmutableArray()));
metadataVisitor.VisitArg(new MdArg(semanticModel, invocation, argType, name, kind));
metadataVisitor.VisitArg(new MdArg(semanticModel, argTypeSyntax, argType, name, kind));
}
}

Expand Down Expand Up @@ -418,7 +418,7 @@ private static ImmutableArray<MdTag>.Builder BuildTags(SemanticModel semanticMod
return builder;
}

private static CompositionName CreateCompositionName(string name, string ns)
private static CompositionName CreateCompositionName(string name, string ns, SyntaxNode source)
{
string className;
string newNamespace;
Expand All @@ -439,6 +439,6 @@ private static CompositionName CreateCompositionName(string name, string ns)
newNamespace = ns;
}

return new CompositionName(className, newNamespace);
return new CompositionName(className, newNamespace, source);
}
}
2 changes: 1 addition & 1 deletion src/Pure.DI.Core/Core/MetadataBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ private IEnumerable<MdSetup> ResolveDependencies(
private void MergeSetups(IEnumerable<MdSetup> setups, out MdSetup mergedSetup, bool resolveDependsOn)
{
SyntaxNode? source = default;
var name = new CompositionName("Composition", "");
var name = new CompositionName("Composition", "", default);
var kind = CompositionKind.Global;
var settings = new Hints();
var bindingsBuilder = ImmutableArray.CreateBuilder<MdBinding>(64);
Expand Down
2 changes: 1 addition & 1 deletion src/Pure.DI.Core/Core/MetadataValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public bool Validate(in MdSetup setup)
&& (!SyntaxFacts.IsValidIdentifier(setup.Name.ClassName)
|| !IsValidOrEmptyIdentifier(setup.Name.Namespace.Replace('.', '_'))))
{
_logger.CompileError($"Invalid composition type name \"{setup.Name}\".", setup.Source.GetLocation(), LogId.ErrorInvalidMetadata);
_logger.CompileError($"Invalid composition type name \"{setup.Name}\".", (setup.Name.Source ?? setup.Source).GetLocation(), LogId.ErrorInvalidMetadata);
isValid = false;
}

Expand Down
12 changes: 11 additions & 1 deletion src/Pure.DI.Core/Core/Models/CompositionName.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
namespace Pure.DI.Core.Models;

public readonly record struct CompositionName(string ClassName, string Namespace)
public readonly record struct CompositionName(string ClassName, string Namespace, SyntaxNode? Source)
{
public bool Equals(CompositionName other) => ClassName == other.ClassName && Namespace == other.Namespace;

public override int GetHashCode()
{
unchecked
{
return (ClassName.GetHashCode() * 397) ^ Namespace.GetHashCode();
}
}

public string FullName =>
string.IsNullOrWhiteSpace(Namespace) ? ClassName : Namespace + "." + ClassName;

Expand Down

0 comments on commit 3e3e488

Please sign in to comment.