Browse Source

Extrapolate DerivesFromModuleBase() to an extension method

pull/906/head
Joe4evr 7 years ago
parent
commit
a81d776b2d
3 changed files with 25 additions and 28 deletions
  1. +1
    -16
      src/Discord.Net.Analyzers/GuildAccessAnalyzer.cs
  2. +24
    -0
      src/Discord.Net.Analyzers/SymbolExtensions.cs
  3. +0
    -12
      test/Discord.Net.Tests/AnalyzerTests/TestModule.cs

+ 1
- 16
src/Discord.Net.Analyzers/GuildAccessAnalyzer.cs View File

@@ -41,7 +41,7 @@ namespace Discord.Analyzers
// Bail out if the containing class doesn't derive from 'ModuleBase<T>'
var classNode = context.Node.FirstAncestorOrSelf<TypeDeclarationSyntax>();
var classSymbol = context.SemanticModel.GetDeclaredSymbol(classNode);
if (!DerivesFromModuleBase(classSymbol))
if (!classSymbol.DerivesFromModuleBase())
return;

// Bail out if the containing method isn't marked with '[Command]'
@@ -66,20 +66,5 @@ namespace Discord.Analyzers

private static readonly Func<AttributeData, bool> _attributeDataPredicate =
(a => a.AttributeClass.Name == nameof(RequireContextAttribute));

private static readonly string _moduleBaseName = typeof(ModuleBase<>).Name;

private static bool DerivesFromModuleBase(INamedTypeSymbol symbol)
{
var bType = symbol.BaseType;
while (bType != null)
{
if (bType.MetadataName == _moduleBaseName)
return true;

bType = bType.BaseType;
}
return false;
}
}
}

+ 24
- 0
src/Discord.Net.Analyzers/SymbolExtensions.cs View File

@@ -0,0 +1,24 @@
using System;
using Microsoft.CodeAnalysis;
using Discord.Commands;

namespace Discord.Analyzers
{
internal static class SymbolExtensions
{
private static readonly string _moduleBaseName = typeof(ModuleBase<>).Name;

public static bool DerivesFromModuleBase(this INamedTypeSymbol symbol)
{
var bType = symbol.BaseType;
while (bType != null)
{
if (bType.MetadataName == _moduleBaseName)
return true;

bType = bType.BaseType;
}
return false;
}
}
}

+ 0
- 12
test/Discord.Net.Tests/AnalyzerTests/TestModule.cs View File

@@ -1,12 +0,0 @@
using System;
using System.Threading.Tasks;
using Discord.Commands;

namespace Test
{
public class TestModule : ModuleBase<ICommandContext>
{
[Command("test"), RequireContext(ContextType.Group)]
public Task TestCmd() => ReplyAsync(Context.Guild.Name);
}
}

Loading…
Cancel
Save