Skip to content

Commit d4dcec9

Browse files
authored
Enable Checking of Top Level Statement Files (#76)
1 parent e058af1 commit d4dcec9

File tree

6 files changed

+31
-5
lines changed

6 files changed

+31
-5
lines changed

source/NsDepCop.Analyzer/ParserAdapter/Roslyn/SyntaxNodeAnalyzer.cs

+6-1
Original file line numberDiff line numberDiff line change
@@ -197,10 +197,15 @@ protected virtual ITypeSymbol DetermineReferencedType(SyntaxNode node, SemanticM
197197
private static ITypeSymbol DetermineEnclosingType(SyntaxNode node, SemanticModel semanticModel)
198198
{
199199
// Find the type declaration that contains the current syntax node.
200-
var typeDeclarationSyntaxNode = node.Ancestors().FirstOrDefault(i => i.IsTypeDeclaration());
200+
var typeDeclarationSyntaxNode = node.Ancestors().FirstOrDefault(i => i.IsTypeDeclaration() || i is CompilationUnitSyntax);
201201
if (typeDeclarationSyntaxNode == null)
202202
return null;
203203

204+
if (typeDeclarationSyntaxNode is CompilationUnitSyntax)
205+
{
206+
return semanticModel.GetDeclaredSymbol(typeDeclarationSyntaxNode)?.ContainingType;
207+
}
208+
204209
// Determine the type of the type declaration that contains the current syntax node.
205210
return semanticModel.GetDeclaredSymbol(typeDeclarationSyntaxNode) as ITypeSymbol;
206211
}

source/NsDepCop.SourceTest/AnalyzerFeatureTests.cs

+10
Original file line numberDiff line numberDiff line change
@@ -96,5 +96,15 @@ public void AnalyzerFeature_VisibleMembersOfAllowedRule()
9696

9797
.Execute();
9898
}
99+
100+
[Fact]
101+
public void AnalyzerFeature_WithTopLevelStatement()
102+
{
103+
SourceTestSpecification.Create()
104+
105+
.ExpectInvalidSegment(1, 8, 12)
106+
107+
.Execute(Microsoft.CodeAnalysis.OutputKind.ConsoleApplication);
108+
}
99109
}
100110
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
System.Type field3;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<NsDepCopConfig>
3+
</NsDepCopConfig>

source/NsDepCop.SourceTest/NsDepCop.SourceTest.csproj

+7
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
<Compile Remove="AnalyzerFeature_VisibleMembersOfNamespace\AnalyzerFeature_VisibleMembersOfNamespace.cs">
4848

4949
</Compile>
50+
<Compile Remove="AnalyzerFeature_WithTopLevelStatement\AnalyzerFeature_WithTopLevelStatement.cs" />
5051
<Compile Remove="Cs7_IsExpressionWithPattern\Cs7_IsExpressionWithPattern.cs">
5152

5253
</Compile>
@@ -176,6 +177,9 @@
176177
<None Include="AnalyzerFeature_VisibleMembersOfNamespace\AnalyzerFeature_VisibleMembersOfNamespace.cs">
177178
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
178179
</None>
180+
<None Include="AnalyzerFeature_WithTopLevelStatement\AnalyzerFeature_WithTopLevelStatement.cs">
181+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
182+
</None>
179183
<None Include="Cs7_IsExpressionWithPattern\Cs7_IsExpressionWithPattern.cs">
180184
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
181185
</None>
@@ -317,6 +321,9 @@
317321
<None Update="AnalyzerFeature_VisibleMembersOfNamespace\config.nsdepcop">
318322
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
319323
</None>
324+
<None Update="AnalyzerFeature_WithTopLevelStatement\config.nsdepcop">
325+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
326+
</None>
320327
<None Update="Cs6_AliasQualifiedName\config.nsdepcop">
321328
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
322329
</None>

source/NsDepCop.SourceTest/SourceTestSpecification.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -45,24 +45,24 @@ public SourceTestSpecification ExpectInvalidSegment(int line, int startColumn, i
4545
return this;
4646
}
4747

48-
public void Execute()
48+
public void Execute(OutputKind? outputKind = null)
4949
{
5050
var sourceFilePaths = new[] {_name}.Select(GetTestFileFullPath).ToList();
5151
var referencedAssemblyPaths = GetReferencedAssemblyPaths().ToList();
5252

53-
ValidateCompilation(sourceFilePaths, referencedAssemblyPaths);
53+
ValidateCompilation(sourceFilePaths, referencedAssemblyPaths, outputKind ?? OutputKind.DynamicallyLinkedLibrary);
5454
AssertIllegalDependencies(sourceFilePaths, referencedAssemblyPaths);
5555
}
5656

5757
private static void DebugMessageHandler(string message) => Debug.WriteLine(message);
5858

59-
private void ValidateCompilation(IEnumerable<string> sourceFiles, IEnumerable<string> referencedAssemblies)
59+
private void ValidateCompilation(IEnumerable<string> sourceFiles, IEnumerable<string> referencedAssemblies, OutputKind outputKind = OutputKind.DynamicallyLinkedLibrary)
6060
{
6161
var compilation = CSharpCompilation.Create(
6262
"NsDepCopProject",
6363
sourceFiles.Select(i => CSharpSyntaxTree.ParseText(LoadFile(i), CSharpParseOptions)),
6464
referencedAssemblies.Select(i => MetadataReference.CreateFromFile(i)),
65-
new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary, allowUnsafe: true));
65+
new CSharpCompilationOptions(outputKind, allowUnsafe: true));
6666

6767
var errors = compilation.GetDiagnostics().Where(i => i.Severity == DiagnosticSeverity.Error).ToList();
6868
errors.Should().HaveCount(0);

0 commit comments

Comments
 (0)