-
Notifications
You must be signed in to change notification settings - Fork 0
Support analyzer property propagation #31
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 all 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
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,40 @@ | ||
| namespace FakeAnalyzers; | ||
|
|
||
| using System.Collections.Immutable; | ||
| using Microsoft.CodeAnalysis; | ||
| using Microsoft.CodeAnalysis.CSharp; | ||
| using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
| using Microsoft.CodeAnalysis.Diagnostics; | ||
|
|
||
| [DiagnosticAnalyzer(LanguageNames.CSharp)] | ||
| public class TestFlagAnalyzer : DiagnosticAnalyzer | ||
| { | ||
| public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => [DiagnosticDescriptors.TestFlagEnabled]; | ||
|
|
||
| public override void Initialize(AnalysisContext context) | ||
| { | ||
| context.EnableConcurrentExecution(); | ||
| context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); | ||
|
|
||
| context.RegisterCompilationStartAction(startContext => | ||
| { | ||
| if (!startContext.Options.AnalyzerConfigOptionsProvider.GlobalOptions.TryGetValue("build_property.TestFlag", out var value) || value != "enabled") | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| startContext.RegisterSyntaxNodeAction(AnalyzeClass, SyntaxKind.ClassDeclaration); | ||
| }); | ||
| } | ||
|
|
||
| static void AnalyzeClass(SyntaxNodeAnalysisContext context) | ||
| { | ||
| if (context.Node is not ClassDeclarationSyntax classDeclaration) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| var diagnostic = Diagnostic.Create(DiagnosticDescriptors.TestFlagEnabled, classDeclaration.Identifier.GetLocation(), classDeclaration.Identifier.Text); | ||
| context.ReportDiagnostic(diagnostic); | ||
| } | ||
| } |
27 changes: 27 additions & 0 deletions
27
src/Particular.AnalyzerTesting/AnalyzerConfigOptionsFactory.cs
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,27 @@ | ||
| namespace Particular.AnalyzerTesting; | ||
|
|
||
| using System.Collections.Generic; | ||
| using Microsoft.CodeAnalysis; | ||
| using Microsoft.CodeAnalysis.Diagnostics; | ||
|
|
||
| static class AnalyzerConfigOptionsFactory | ||
| { | ||
| public static AnalyzerConfigOptionsProvider CreateOptionsProvider(IReadOnlyDictionary<string, string> properties) | ||
| => new OptionsProvider(new DictionaryAnalyzerConfigOptions(properties)); | ||
|
|
||
| public static AnalyzerOptions CreateAnalyzerOptions(IReadOnlyDictionary<string, string> properties) | ||
| => new([], CreateOptionsProvider(properties)); | ||
|
|
||
| sealed class OptionsProvider(AnalyzerConfigOptions options) : AnalyzerConfigOptionsProvider | ||
| { | ||
| public override AnalyzerConfigOptions GetOptions(SyntaxTree tree) => options; | ||
| public override AnalyzerConfigOptions GetOptions(AdditionalText textFile) => options; | ||
| public override AnalyzerConfigOptions GlobalOptions => options; | ||
| } | ||
|
|
||
| sealed class DictionaryAnalyzerConfigOptions(IReadOnlyDictionary<string, string> properties) : AnalyzerConfigOptions | ||
| { | ||
| public override bool TryGetValue(string key, out string value) | ||
| => properties.TryGetValue(key, out value!); | ||
| } | ||
| } |
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
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,22 @@ | ||
| namespace Tests; | ||
|
|
||
| using System.Threading.Tasks; | ||
| using FakeAnalyzers; | ||
| using NUnit.Framework; | ||
| using Particular.AnalyzerTesting; | ||
|
|
||
| public class TestFlagAnalyzerTests | ||
| { | ||
| const string Code = """ | ||
| public class [|MyClass|] | ||
| { | ||
| } | ||
| """; | ||
|
|
||
| [Test] | ||
| public Task ReportsDiagnosticWhenPropertyIsEnabled() => | ||
| AnalyzerTest.ForAnalyzer<TestFlagAnalyzer>() | ||
| .WithProperty("build_property.TestFlag", "enabled") | ||
| .WithSource(Code) | ||
| .AssertDiagnostics(DiagnosticIds.TestFlagEnabled); | ||
| } |
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 |
|---|---|---|
| @@ -1,53 +1,67 @@ | ||
| namespace Tests.SourceGenerators; | ||
|
|
||
| using System.Linq; | ||
| using System.Threading.Tasks; | ||
| using FakeAnalyzers; | ||
| using NUnit.Framework; | ||
| using Particular.AnalyzerTesting; | ||
|
|
||
| public class BasicSourceGeneratorTest | ||
| { | ||
| const string Source = $$""" | ||
| using System; | ||
|
|
||
| [AttributeUsage(AttributeTargets.All)] | ||
| public class MarkerAttribute : Attribute { } | ||
|
|
||
| [Marker] | ||
| public class Hello | ||
| { | ||
| [Marker] | ||
| private string there = "foo"; | ||
|
|
||
| [Marker] | ||
| public DateTime Enjoy { get; set; } | ||
|
|
||
| public void Use() | ||
| { | ||
| _ = the; | ||
| _ = there; | ||
| } | ||
|
|
||
| [Marker] | ||
| private string the; | ||
|
|
||
| public void DoArguments([Marker] string test, [Marker] Hello results) | ||
| { | ||
| the = test; | ||
| } | ||
| } | ||
|
|
||
| """; | ||
|
|
||
| [Test] | ||
| public async Task BasicTest() | ||
| { | ||
| var source = $$""" | ||
| using System; | ||
|
|
||
| [AttributeUsage(AttributeTargets.All)] | ||
| public class MarkerAttribute : Attribute { } | ||
|
|
||
| [Marker] | ||
| public class Hello | ||
| { | ||
| [Marker] | ||
| private string there = "foo"; | ||
|
|
||
| [Marker] | ||
| public DateTime Enjoy { get; set; } | ||
|
|
||
| public void Use() | ||
| { | ||
| _ = the; | ||
| _ = there; | ||
| } | ||
|
|
||
| [Marker] | ||
| private string the; | ||
|
|
||
| public void DoArguments([Marker] string test, [Marker] Hello results) | ||
| { | ||
| the = test; | ||
| } | ||
| } | ||
|
|
||
| """; | ||
|
|
||
| SourceGeneratorTest.ForIncrementalGenerator<SimpleSourceGenerator>() | ||
| .WithSource(source) | ||
| .WithSource(Source) | ||
| .Run() | ||
| .Approve() | ||
| .ToConsole() | ||
| .AssertRunsAreEqual() | ||
| .OutputSteps(); | ||
| } | ||
| } | ||
|
|
||
| [Test] | ||
| public void AnalyzerSeesPropertyDuringSourceGeneratorRun() | ||
| { | ||
| var result = SourceGeneratorTest.ForIncrementalGenerator<SimpleSourceGenerator>() | ||
| .WithAnalyzer<TestFlagAnalyzer>() | ||
| .WithProperty("build_property.TestFlag", "enabled") | ||
| .SuppressCompilationErrors() | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this suppress needed?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes see #31 (review) I have confirmed that its still needed |
||
| .WithSource(Source) | ||
| .Run(); | ||
|
|
||
| Assert.That(result.AnalyzerDiagnostics.Select(diagnostic => diagnostic.Id), Contains.Item(DiagnosticIds.TestFlagEnabled)); | ||
| } | ||
| } | ||
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.