Skip to content

Commit 96850c5

Browse files
authored
chore: Leverage semantic model when looking for documents (#157)
1 parent 36042b7 commit 96850c5

15 files changed

+58
-60
lines changed

src/Compiling/Program.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@
2020
await Console.Out.WriteLineAsync("Project mode");
2121
var compiler = serviceProvider.GetRequiredService<ProjectCompiler>();
2222
var result = await compiler.Compile(options.ToProjectCompilerOptions());
23-
return result.DocumentResults.Sum(r => r.Diagnostics.Count);
23+
return result.DocumentResults.Sum(r => r.Errors.Length);
2424
}
2525
else
2626
{
2727
await Console.Error.WriteLineAsync(
2828
"Directory mode is deprecated. Please use project mode by pointing source parameter to .csproj file or to folder with one .csproj file.");
2929
var compiler = serviceProvider.GetRequiredService<DirectoryCompiler>();
3030
var result = await compiler.Compile(options.ToDirectoryCompilerOptions());
31-
return result.DocumentResults.Sum(r => r.Diagnostics.Count);
31+
return result.DocumentResults.Sum(r => r.Errors.Length);
3232
}

src/Core/Compiling/CompilerUtils.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Diagnostics.CodeAnalysis;
55
using System.Xml.Linq;
66

7+
using Microsoft.Azure.ApiManagement.PolicyToolkit.Authoring;
78
using Microsoft.Azure.ApiManagement.PolicyToolkit.Compiling.Diagnostics;
89
using Microsoft.CodeAnalysis;
910
using Microsoft.CodeAnalysis.CSharp.Syntax;

src/Core/Compiling/DirectoryCompiler.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,20 +35,20 @@ public Task<DirectoryCompilerResult> Compile(DirectoryCompilerOptions options)
3535
syntaxTrees: [syntax],
3636
references: References);
3737

38-
var documents = syntax.GetRoot()
39-
.GetDocumentAttributedClasses();
38+
var semantics = compilation.GetSemanticModel(syntax);
39+
var documents = syntax.GetRoot().GetDocumentAttributedClasses(semantics);
4040

4141
foreach (var document in documents)
4242
{
4343
IDocumentCompilationResult documentResult = compiler.Compile(compilation, document);
4444
result.DocumentResults.Add(documentResult);
4545

46-
foreach (var error in documentResult.Diagnostics)
46+
foreach (var error in documentResult.Errors)
4747
{
4848
Console.Error.WriteLine(error.ToString());
4949
}
5050

51-
var policyFileName = document.ExtractDocumentFileName();
51+
var policyFileName = document.ExtractDocumentFileName(semantics);
5252
var targetFile = FileUtils.WriteToFile(new FileUtils.Data()
5353
{
5454
Element = documentResult.Document,
Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,32 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
33

4+
using System.Collections.Immutable;
45
using System.Xml.Linq;
56

67
using Microsoft.CodeAnalysis;
78

89
namespace Microsoft.Azure.ApiManagement.PolicyToolkit.Compiling;
910

10-
public class DocumentCompilationContext(Compilation compilation, SyntaxNode syntaxRoot, XElement rootElement)
11+
public class DocumentCompilationContext(Compilation compilation, SyntaxNode syntaxRoot, XElement currentElement)
1112
: IDocumentCompilationContext, IDocumentCompilationResult
1213
{
13-
private readonly IList<Diagnostic> _diagnostics = new List<Diagnostic>();
14-
15-
public DocumentCompilationContext(DocumentCompilationContext parent, XElement rootElement) : this(
16-
parent.Compilation, parent.SyntaxRoot, rootElement)
14+
public DocumentCompilationContext(IDocumentCompilationContext parent, XElement currentElement)
15+
: this(parent.Compilation, parent.SyntaxRoot, currentElement)
1716
{
18-
_diagnostics = parent._diagnostics;
17+
RootElement = parent.RootElement;
18+
Diagnostics = parent.Diagnostics;
1919
}
2020

21-
public void AddPolicy(XNode element) => rootElement.Add(element);
22-
public void Report(Diagnostic diagnostic) => _diagnostics.Add(diagnostic);
21+
public void AddPolicy(XNode element) => CurrentElement.Add(element);
22+
public void Report(Diagnostic diagnostic) => Diagnostics.Add(diagnostic);
2323

2424
public Compilation Compilation { get; } = compilation;
2525
public SyntaxNode SyntaxRoot { get; } = syntaxRoot;
26+
public XElement RootElement { get; } = currentElement;
27+
public XElement CurrentElement { get; } = currentElement;
28+
public IList<Diagnostic> Diagnostics { get; } = new List<Diagnostic>();
2629

27-
public XElement Document => rootElement;
28-
public IReadOnlyList<Diagnostic> Diagnostics => _diagnostics.AsReadOnly();
30+
public XElement Document => CurrentElement;
31+
public ImmutableArray<Diagnostic> Errors => [..Diagnostics];
2932
}

src/Core/Compiling/IDocumentCompilationContext.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,8 @@ public interface IDocumentCompilationContext
1414

1515
Compilation Compilation { get; }
1616
SyntaxNode SyntaxRoot { get; }
17+
IList<Diagnostic> Diagnostics { get; }
18+
19+
XElement RootElement { get; }
20+
XElement CurrentElement { get; }
1721
}

src/Core/Compiling/IDocumentCompilationResult.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
33

4+
using System.Collections.Immutable;
45
using System.Xml.Linq;
56

67
using Microsoft.CodeAnalysis;
@@ -10,5 +11,5 @@ namespace Microsoft.Azure.ApiManagement.PolicyToolkit.Compiling;
1011
public interface IDocumentCompilationResult
1112
{
1213
XElement Document { get; }
13-
IReadOnlyList<Diagnostic> Diagnostics { get; }
14+
ImmutableArray<Diagnostic> Errors { get; }
1415
}

src/Core/Compiling/Policy/LimitConcurrencyCompiler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public void Handle(IDocumentCompilationContext context, InvocationExpressionSynt
8585
return;
8686
}
8787

88-
SubDocumentCompilationContext subContext = new(context, element);
88+
var subContext = new DocumentCompilationContext(context, element);
8989
_blockCompiler.Value.Compile(subContext, lambda.Block);
9090

9191
context.AddPolicy(element);

src/Core/Compiling/Policy/RetryCompiler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public void Handle(IDocumentCompilationContext context, InvocationExpressionSynt
9999
element.AddAttribute(config, nameof(RetryConfig.Delta), "delta");
100100
element.AddAttribute(config, nameof(RetryConfig.FirstFastRetry), "first-fast-retry");
101101

102-
SubDocumentCompilationContext subContext = new(context, element);
102+
var subContext = new DocumentCompilationContext(context, element);
103103
_blockCompiler.Value.Compile(subContext, lambda.Block);
104104

105105
context.AddPolicy(element);

src/Core/Compiling/Policy/WaitCompiler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public void Handle(IDocumentCompilationContext context, InvocationExpressionSynt
5959
element.Add(new XAttribute("for", value));
6060
}
6161

62-
SubDocumentCompilationContext subContext = new(context, element);
62+
var subContext = new DocumentCompilationContext(context, element);
6363
_blockCompiler.Value.Compile(subContext, lambda.Block);
6464

6565
context.AddPolicy(element);

src/Core/Compiling/ProjectCompiler.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,18 +53,19 @@ public async Task<ProjectCompilerResult> Compile(ProjectCompilerOptions options,
5353
{
5454
await Console.Out.WriteLineAsync($"File '{syntaxTree.FilePath}' processing");
5555
var root = await syntaxTree.GetRootAsync(cancellationToken);
56-
var documents = root.GetDocumentAttributedClasses();
56+
var semantics = compilation.GetSemanticModel(syntaxTree);
57+
var documents = root.GetDocumentAttributedClasses(semantics);
5758
foreach (var document in documents)
5859
{
5960
var documentResult = documentCompiler.Compile(compilation, document);
6061
result.DocumentResults.Add(documentResult);
6162

62-
foreach (var error in documentResult.Diagnostics)
63+
foreach (var error in documentResult.Errors)
6364
{
6465
await Console.Error.WriteLineAsync(error.ToString());
6566
}
6667

67-
var policyFileName = document.ExtractDocumentFileName();
68+
var policyFileName = document.ExtractDocumentFileName(semantics);
6869
var targetFile = FileUtils.WriteToFile(new FileUtils.Data()
6970
{
7071
Element = documentResult.Document,

0 commit comments

Comments
 (0)