Skip to content

Commit

Permalink
Add diagnostics for unsupported expression.
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam Paquette committed Oct 9, 2023
1 parent 98f18ec commit c5c5eef
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 15 deletions.
6 changes: 6 additions & 0 deletions sample/Sample.Domain/ContactAggregate/TypelySpecification.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ internal class TypelySpecification : ITypelySpecification
{
public void Create(ITypelyBuilder builder)
{
const string myVar1 = "MyVar1";

Check failure on line 12 in sample/Sample.Domain/ContactAggregate/TypelySpecification.cs

View workflow job for this annotation

GitHub Actions / build (7.0)

The use of '"MyVar1"' is not allowed in a TypelySpecification.

Check failure on line 12 in sample/Sample.Domain/ContactAggregate/TypelySpecification.cs

View workflow job for this annotation

GitHub Actions / build (7.0)

The use of '"MyVar1"' is not allowed in a TypelySpecification.
string myVar2 = "MyVar2";

Check failure on line 13 in sample/Sample.Domain/ContactAggregate/TypelySpecification.cs

View workflow job for this annotation

GitHub Actions / build (7.0)

The use of '"MyVar2"' is not allowed in a TypelySpecification.

Check failure on line 13 in sample/Sample.Domain/ContactAggregate/TypelySpecification.cs

View workflow job for this annotation

GitHub Actions / build (7.0)

The use of '"MyVar2"' is not allowed in a TypelySpecification.

builder.OfString().For(myVar1);

Check failure on line 15 in sample/Sample.Domain/ContactAggregate/TypelySpecification.cs

View workflow job for this annotation

GitHub Actions / build (7.0)

'myVar1' is not allowed as a parameter of 'For' in the TypelySpecification. Instead use a string constant.

Check failure on line 15 in sample/Sample.Domain/ContactAggregate/TypelySpecification.cs

View workflow job for this annotation

GitHub Actions / build (7.0)

'myVar1' is not allowed as a parameter of 'For' in the TypelySpecification. Instead use a string constant.
builder.OfString().For(myVar2);

Check failure on line 16 in sample/Sample.Domain/ContactAggregate/TypelySpecification.cs

View workflow job for this annotation

GitHub Actions / build (7.0)

'myVar2' is not allowed as a parameter of 'For' in the TypelySpecification. Instead use a string constant.

Check failure on line 16 in sample/Sample.Domain/ContactAggregate/TypelySpecification.cs

View workflow job for this annotation

GitHub Actions / build (7.0)

'myVar2' is not allowed as a parameter of 'For' in the TypelySpecification. Instead use a string constant.

//Contact
builder.OfInt().For("ContactId").GreaterThan(0);

Expand Down
10 changes: 9 additions & 1 deletion src/Typely.Generators/Typely/DiagnosticDescriptors.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,15 @@ public static class DiagnosticDescriptors
public static DiagnosticDescriptor UnsupportedExpression { get; } = new(
id: "TYP0001",
title: "Unsupported expression",
messageFormat: "The use of '{0}' is not allowed in a TypelySpecification",
messageFormat: "The use of '{0}' is not allowed in a TypelySpecification.",
category: "Design",
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true);

public static DiagnosticDescriptor UnsupportedParameter { get; } = new(
id: "TYP0002",
title: "Unsupported parameter",
messageFormat: "'{0}' is not allowed as a parameter of '{1}' in the TypelySpecification. Instead use a string constant.",
category: "Design",
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true);
Expand Down
41 changes: 36 additions & 5 deletions src/Typely.Generators/Typely/Parsing/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -250,15 +250,13 @@ private static string GetNamespace(SyntaxNode classSyntax)
if (invocationExpressionSyntax.Expression is MemberAccessExpressionSyntax memberAccessExpressionSyntax)
{
var memberName = memberAccessExpressionSyntax.Name.Identifier.Text;
if (!SupportedMembers.All.Contains(memberName))
var argumentList = invocationExpressionSyntax.ArgumentList;

if (!IsExpressionValid(memberName, memberAccessExpressionSyntax, argumentList))
{
var diagnostic = Diagnostic.Create(DiagnosticDescriptors.UnsupportedExpression,
memberAccessExpressionSyntax.GetLocation(), memberAccessExpressionSyntax.Name.GetText());
diagnostics.Add(diagnostic);
return false;
}

var argumentList = invocationExpressionSyntax.ArgumentList;
parsed.Invocations.Insert(0, new ParsedInvocation(argumentList, memberName));

// ex: builder.OfInt().For("Vote").WithNamespace("UserAggregate").WithName()
Expand All @@ -277,8 +275,41 @@ private static string GetNamespace(SyntaxNode classSyntax)
{
parsed.Root = nameSyntax.Identifier.Text;
}
else
{
var diagnostic = Diagnostic.Create(DiagnosticDescriptors.UnsupportedExpression, syntaxNode.GetLocation(),
syntaxNode);
diagnostics.Add(diagnostic);
return false;
}

return true;

bool IsExpressionValid(string memberName, MemberAccessExpressionSyntax memberAccessExpressionSyntax,
ArgumentListSyntax argumentList)
{
if (!SupportedMembers.All.Contains(memberName))
{
var diagnostic = Diagnostic.Create(DiagnosticDescriptors.UnsupportedExpression,
memberAccessExpressionSyntax.GetLocation(),
memberAccessExpressionSyntax.Name.GetText());
diagnostics.Add(diagnostic);
return false;
}

if (argumentList.Arguments.Any() &&
argumentList.Arguments.First().Expression is IdentifierNameSyntax identifierNameSyntax)
{
var diagnostic = Diagnostic.Create(DiagnosticDescriptors.UnsupportedParameter,
memberAccessExpressionSyntax.GetLocation(),
identifierNameSyntax.Identifier.Text,
memberAccessExpressionSyntax.Name.GetText());
diagnostics.Add(diagnostic);
return false;
}

return true;
}
}

private struct ParsedStatementsResult
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,79 @@
Title: Unsupported expression,
Severity: Error,
WarningLevel: 0,
Location: {CurrentDirectory}../../..//Typely/Specifications/DiagnosticsSpecification.cs: (9,8)-(9,66),
MessageFormat: The use of '{0}' is not allowed in a TypelySpecification,
Message: The use of 'UnsupportedExtension' is not allowed in a TypelySpecification,
Location: {CurrentDirectory}../../..//Typely/Specifications/DiagnosticsSpecification.cs: (11,30)-(11,38),
MessageFormat: The use of '{0}' is not allowed in a TypelySpecification.,
Message: The use of '"MyVar1"' is not allowed in a TypelySpecification.,
Category: Design
},
{
Id: TYP0001,
Title: Unsupported expression,
Severity: Error,
WarningLevel: 0,
Location: {CurrentDirectory}../../..//Typely/Specifications/DiagnosticsSpecification.cs: (11,8)-(11,46),
MessageFormat: The use of '{0}' is not allowed in a TypelySpecification,
Message: The use of 'CreateTypeFromUnsupportedCall' is not allowed in a TypelySpecification,
Location: {CurrentDirectory}../../..//Typely/Specifications/DiagnosticsSpecification.cs: (12,24)-(12,32),
MessageFormat: The use of '{0}' is not allowed in a TypelySpecification.,
Message: The use of '"MyVar2"' is not allowed in a TypelySpecification.,
Category: Design
},
{
Id: TYP0002,
Title: Unsupported parameter,
Severity: Error,
WarningLevel: 0,
Location: {CurrentDirectory}../../..//Typely/Specifications/DiagnosticsSpecification.cs: (14,8)-(14,30),
MessageFormat: '{0}' is not allowed as a parameter of '{1}' in the TypelySpecification. Instead use a string constant.,
Message: 'myVar1' is not allowed as a parameter of 'For' in the TypelySpecification. Instead use a string constant.,
Category: Design
},
{
Id: TYP0002,
Title: Unsupported parameter,
Severity: Error,
WarningLevel: 0,
Location: {CurrentDirectory}../../..//Typely/Specifications/DiagnosticsSpecification.cs: (15,8)-(15,30),
MessageFormat: '{0}' is not allowed as a parameter of '{1}' in the TypelySpecification. Instead use a string constant.,
Message: 'myVar2' is not allowed as a parameter of 'For' in the TypelySpecification. Instead use a string constant.,
Category: Design
},
{
Id: TYP0002,
Title: Unsupported parameter,
Severity: Error,
WarningLevel: 0,
Location: {CurrentDirectory}../../..//Typely/Specifications/DiagnosticsSpecification.cs: (16,8)-(16,30),
MessageFormat: '{0}' is not allowed as a parameter of '{1}' in the TypelySpecification. Instead use a string constant.,
Message: 'TypeName' is not allowed as a parameter of 'For' in the TypelySpecification. Instead use a string constant.,
Category: Design
},
{
Id: TYP0001,
Title: Unsupported expression,
Severity: Error,
WarningLevel: 0,
Location: {CurrentDirectory}../../..//Typely/Specifications/DiagnosticsSpecification.cs: (17,8)-(17,66),
MessageFormat: The use of '{0}' is not allowed in a TypelySpecification.,
Message: The use of 'UnsupportedExtension' is not allowed in a TypelySpecification.,
Category: Design
},
{
Id: TYP0001,
Title: Unsupported expression,
Severity: Error,
WarningLevel: 0,
Location: {CurrentDirectory}../../..//Typely/Specifications/DiagnosticsSpecification.cs: (19,8)-(19,46),
MessageFormat: The use of '{0}' is not allowed in a TypelySpecification.,
Message: The use of 'CreateTypeFromUnsupportedCall' is not allowed in a TypelySpecification.,
Category: Design
},
{
Id: TYP0001,
Title: Unsupported expression,
Severity: Error,
WarningLevel: 0,
Location: {CurrentDirectory}../../..//Typely/Specifications/DiagnosticsSpecification.cs: (13,26)-(13,64),
MessageFormat: The use of '{0}' is not allowed in a TypelySpecification,
Message: The use of 'CreateTypeFromUnsupportedCall' is not allowed in a TypelySpecification,
Location: {CurrentDirectory}../../..//Typely/Specifications/DiagnosticsSpecification.cs: (21,26)-(21,64),
MessageFormat: The use of '{0}' is not allowed in a TypelySpecification.,
Message: The use of 'CreateTypeFromUnsupportedCall' is not allowed in a TypelySpecification.,
Category: Design
}
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,16 @@ namespace Typely.Generators.Tests.Typely.Specifications;

internal class DiagnosticsSpecification : ITypelySpecification
{
private const string TypeName = "MyType";

public void Create(ITypelyBuilder builder)
{
const string myVar1 = "MyVar1";
string myVar2 = "MyVar2";

builder.OfString().For(myVar1);
builder.OfString().For(myVar2);
builder.OfString().For(TypeName);
builder.OfString().For("Unsupported").UnsupportedExtension();

CreateTypeFromUnsupportedCall(builder);
Expand Down

0 comments on commit c5c5eef

Please sign in to comment.