Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,17 @@ public override SyntaxNode VisitTypeOfExpression(TypeOfExpressionSyntax node)
{
var symbol = node.Type.GetSymbolInfo(semanticModel);

// If symbol is null (e.g., type doesn't exist), fall back to base implementation
// to let the compiler report the error rather than crashing the generator
if (symbol is null)
{
return base.VisitTypeOfExpression(node) ?? node;
}

return SyntaxFactory
.TypeOfExpression(
SyntaxFactory.ParseTypeName(
symbol!.GloballyQualified())
symbol.GloballyQualified())
)
.WithoutTrivia();
}
Expand All @@ -159,6 +166,18 @@ public override SyntaxNode VisitTypeOfExpression(TypeOfExpressionSyntax node)
{
// nameof() syntax
var argumentList = (ArgumentListSyntax) childNodes[1];

// Check if there are any arguments before accessing
if (argumentList.Arguments.Count == 0)
{
// Invalid nameof() with no arguments - return empty string literal
// This prevents IndexOutOfRangeException and allows the compiler to report the error
return SyntaxFactory.LiteralExpression(
SyntaxKind.StringLiteralExpression,
SyntaxFactory.Literal(string.Empty)
);
}

var argumentExpression = argumentList.Arguments[0].Expression;

if (argumentExpression is IdentifierNameSyntax identifierNameSyntax)
Expand Down
Loading