Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* SonarAnalyzer for .NET
* Copyright (C) 2015-2024 SonarSource SA
* mailto: contact AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

namespace SonarAnalyzer.CSharp.Styling.Common;

public abstract class StylingAnalyzer : SonarDiagnosticAnalyzer
{
protected DiagnosticDescriptor Rule { get; }

public sealed override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(Rule);

protected StylingAnalyzer(string id, string messageFormat, SourceScope scope = SourceScope.All) =>
Rule = DescriptorFactory.Create(id, messageFormat, scope);
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,13 @@
namespace SonarAnalyzer.Rules.CSharp.Styling;

[DiagnosticAnalyzer(LanguageNames.CSharp)]
public sealed class FileScopeNamespace : SonarDiagnosticAnalyzer // ToDo: https://github.com/SonarSource/sonar-dotnet/issues/9035 Extract into a usefull base class
public sealed class FileScopeNamespace : StylingAnalyzer
{
private static readonly DiagnosticDescriptor Rule = DescriptorFactory.Create("T0001", "Use file-scoped namespaces");
public FileScopeNamespace() : base("T0001", "Use file-scoped namespace.") { }

public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = ImmutableArray.Create(Rule); // ToDo: https://github.com/SonarSource/sonar-dotnet/issues/9035 Remove this

protected override void Initialize(SonarAnalysisContext context)
{
// ToDo: https://github.com/SonarSource/sonar-dotnet/issues/9035
if (context is null)
{
return; // This is a useless coverage test for now. It will be removed
}
}
protected override void Initialize(SonarAnalysisContext context) =>
// ToDo: Rework reporting
context.RegisterNodeAction(
c => c.ReportIssue(Diagnostic.Create(Rule, ((NamespaceDeclarationSyntax)c.Node).Name.GetLocation())),
SyntaxKind.NamespaceDeclaration);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
</ItemGroup>

<ItemGroup>
<!-- We need to update NuGet and JAR packaging after changing references -->
<!-- We need to update NuGet packaging after changing references -->
<ProjectReference Include="..\SonarAnalyzer.Common\SonarAnalyzer.Common.csproj" />
<ProjectReference Include="..\SonarAnalyzer.Common\SonarAnalyzer.CSharp.csproj" />
<ProjectReference Include="..\SonarAnalyzer.CSharp\SonarAnalyzer.CSharp.csproj" />
</ItemGroup>

<ItemGroup>
Expand Down
8 changes: 8 additions & 0 deletions analyzers/src/SonarAnalyzer.CSharp.Styling/packages.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -941,6 +941,14 @@
"Microsoft.Composition": "[1.0.27, )",
"System.Collections.Immutable": "[1.1.37, )"
}
},
"sonaranalyzer.csharp": {
"type": "Project",
"dependencies": {
"Microsoft.CodeAnalysis.CSharp.Workspaces": "[1.3.2, )",
"SonarAnalyzer": "[1.0.0, )",
"System.Collections.Immutable": "[1.1.37, )"
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,13 @@ namespace SonarAnalyzer.CSharp.Styling.Test.Rules;
[TestClass]
public class FileScopeNamespaceTest
{
private readonly VerifierBuilder builder = new VerifierBuilder<FileScopeNamespace>().WithOptions(ParseOptionsHelper.CSharpLatest).WithConcurrentAnalysis(false);

[TestMethod]
public void FileScopeNamespace() =>
new VerifierBuilder<FileScopeNamespace>().WithOptions(ParseOptionsHelper.CSharpLatest).AddPaths("FileScopeNamespace.cs").Verify();
builder.AddPaths("FileScopeNamespace.cs").Verify();

[TestMethod]
public void FileScopeNamespace_Compliant() =>
builder.AddPaths("FileScopeNamespace.Compliant.cs").VerifyNoIssueReported();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Outer; // Compliant

public class NotRelevant
{

}
Original file line number Diff line number Diff line change
@@ -1 +1,20 @@
// FIXME: Rewrite this file in https://github.com/SonarSource/sonar-dotnet/issues/9035

namespace Outer // Noncompliant {{Use file-scoped namespace.}}
// ^^^^^
{
public class NotRelevant
{

}

namespace Inner // Noncompliant, nested namespace should not be used in general
{

}
}


namespace // Error [CS1001] Identifier expected
{ // Noncompliant^-1#0

}
Original file line number Diff line number Diff line change
Expand Up @@ -552,11 +552,20 @@
"System.Collections.Immutable": "[1.1.37, )"
}
},
"sonaranalyzer.csharp": {
"type": "Project",
"dependencies": {
"Microsoft.CodeAnalysis.CSharp.Workspaces": "[1.3.2, )",
"SonarAnalyzer": "[1.0.0, )",
"System.Collections.Immutable": "[1.1.37, )"
}
},
"sonaranalyzer.csharp.styling": {
"type": "Project",
"dependencies": {
"Microsoft.CodeAnalysis.CSharp.Workspaces": "[1.3.2, )",
"SonarAnalyzer": "[1.0.0, )"
"SonarAnalyzer": "[1.0.0, )",
"SonarAnalyzer.CSharp": "[1.0.0, )"
}
},
"sonaranalyzer.testframework": {
Expand Down