-
-
Notifications
You must be signed in to change notification settings - Fork 0
Move PluginClassInfo & Add code regions #23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,9 +8,11 @@ | |
|
||
namespace Flow.Launcher.Localization.Analyzers.Localize | ||
{ | ||
[DiagnosticAnalyzer(LanguageNames.CSharp)] | ||
Check warning on line 11 in Flow.Launcher.Localization.Analyzers/Localize/ContextAvailabilityAnalyzer.cs
|
||
public class ContextAvailabilityAnalyzer : DiagnosticAnalyzer | ||
{ | ||
#region DiagnosticAnalyzer | ||
|
||
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create( | ||
AnalyzerDiagnostics.ContextIsAField, | ||
AnalyzerDiagnostics.ContextIsNotStatic, | ||
|
@@ -25,47 +27,55 @@ | |
context.RegisterSyntaxNodeAction(AnalyzeNode, SyntaxKind.ClassDeclaration); | ||
} | ||
|
||
#endregion | ||
|
||
#region Analyze Methods | ||
|
||
private static void AnalyzeNode(SyntaxNodeAnalysisContext context) | ||
{ | ||
var configOptions = context.Options.AnalyzerConfigOptionsProvider; | ||
var useDI = configOptions.GetFLLUseDependencyInjection(); | ||
|
||
// If we use dependency injection, we don't need to check for this context property | ||
if (useDI) return; | ||
if (useDI) | ||
{ | ||
// If we use dependency injection, we don't need to check for this context property | ||
return; | ||
} | ||
|
||
var classDeclaration = (ClassDeclarationSyntax)context.Node; | ||
var semanticModel = context.SemanticModel; | ||
var classSymbol = semanticModel.GetDeclaredSymbol(classDeclaration); | ||
|
||
if (!IsPluginEntryClass(classSymbol)) return; | ||
|
||
var contextProperty = classDeclaration.Members.OfType<PropertyDeclarationSyntax>() | ||
.Select(p => semanticModel.GetDeclaredSymbol(p)) | ||
.FirstOrDefault(p => p?.Type.Name is Constants.PluginContextTypeName); | ||
var pluginClassInfo = Helper.GetPluginClassInfo(classDeclaration, semanticModel, context.CancellationToken); | ||
if (pluginClassInfo == null) | ||
{ | ||
// Cannot find class that implements IPluginI18n | ||
return; | ||
} | ||
|
||
if (contextProperty != null) | ||
// Context property is found, check if it's a valid property | ||
if (pluginClassInfo.PropertyName != null) | ||
{ | ||
if (!contextProperty.IsStatic) | ||
if (!pluginClassInfo.IsStatic) | ||
{ | ||
context.ReportDiagnostic(Diagnostic.Create( | ||
AnalyzerDiagnostics.ContextIsNotStatic, | ||
contextProperty.DeclaringSyntaxReferences[0].GetSyntax().GetLocation() | ||
pluginClassInfo.CodeFixLocatioin | ||
)); | ||
return; | ||
} | ||
|
||
if (contextProperty.DeclaredAccessibility is Accessibility.Private || contextProperty.DeclaredAccessibility is Accessibility.Protected) | ||
if (pluginClassInfo.IsPrivate || pluginClassInfo.IsProtected) | ||
{ | ||
context.ReportDiagnostic(Diagnostic.Create( | ||
AnalyzerDiagnostics.ContextAccessIsTooRestrictive, | ||
contextProperty.DeclaringSyntaxReferences[0].GetSyntax().GetLocation() | ||
pluginClassInfo.CodeFixLocatioin | ||
)); | ||
return; | ||
} | ||
|
||
// If the context property is valid, we don't need to check for anything else | ||
return; | ||
} | ||
|
||
// Context property is not found, check if it's declared as a field | ||
var fieldDeclaration = classDeclaration.Members | ||
.OfType<FieldDeclarationSyntax>() | ||
.SelectMany(f => f.Declaration.Variables) | ||
|
@@ -75,7 +85,6 @@ | |
?.DeclaringSyntaxReferences[0] | ||
.GetSyntax() | ||
.FirstAncestorOrSelf<FieldDeclarationSyntax>(); | ||
|
||
if (parentSyntax != null) | ||
{ | ||
context.ReportDiagnostic(Diagnostic.Create( | ||
|
@@ -85,13 +94,13 @@ | |
return; | ||
} | ||
|
||
// Context property is not found, report an error | ||
context.ReportDiagnostic(Diagnostic.Create( | ||
AnalyzerDiagnostics.ContextIsNotDeclared, | ||
classDeclaration.Identifier.GetLocation() | ||
)); | ||
} | ||
|
||
private static bool IsPluginEntryClass(INamedTypeSymbol namedTypeSymbol) => | ||
namedTypeSymbol?.Interfaces.Any(i => i.Name == Constants.PluginInterfaceName) ?? false; | ||
#endregion | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
using Microsoft.CodeAnalysis; | ||
|
||
namespace Flow.Launcher.Localization.Shared | ||
{ | ||
public class PluginClassInfo | ||
{ | ||
public Location Location { get; } | ||
public string ClassName { get; } | ||
public string PropertyName { get; } | ||
public bool IsStatic { get; } | ||
public bool IsPrivate { get; } | ||
public bool IsProtected { get; } | ||
public Location CodeFixLocatioin { get; } | ||
|
||
public string ContextAccessor => $"{ClassName}.{PropertyName}"; | ||
public bool IsValid => PropertyName != null && IsStatic && (!IsPrivate) && (!IsProtected); | ||
|
||
public PluginClassInfo(Location location, string className, string propertyName, bool isStatic, bool isPrivate, bool isProtected, Location codeFixLocatioin) | ||
Jack251970 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
Location = location; | ||
ClassName = className; | ||
PropertyName = propertyName; | ||
IsStatic = isStatic; | ||
IsPrivate = isPrivate; | ||
IsProtected = isProtected; | ||
CodeFixLocatioin = codeFixLocatioin; | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.