Skip to content
Draft
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
Expand Up @@ -2,7 +2,6 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
Expand All @@ -29,8 +28,6 @@ public CSharpRemoveUnnecessaryImportsDiagnosticAnalyzer()
{
}

protected override GeneratedCodeAnalysisFlags GeneratedCodeAnalysisFlags => GeneratedCodeAnalysisFlags.Analyze | GeneratedCodeAnalysisFlags.ReportDiagnostics;

protected override ISyntaxFacts SyntaxFacts
=> CSharpSyntaxFacts.Instance;

Expand Down Expand Up @@ -67,16 +64,4 @@ protected override IEnumerable<TextSpan> GetFixableDiagnosticSpans(
}
}
}

protected override void AnalyzeSemanticModel(SemanticModelAnalysisContext context, SyntaxTree tree, CancellationToken cancellationToken)
{
// We've opted in to generated code analysis above, but we actually only want to analyze generated code for Razor
if (context.IsGeneratedCode &&
tree.FilePath.IndexOf("Microsoft.NET.Sdk.Razor.SourceGenerators.RazorSourceGenerator") == -1)
{
return;
}

base.AnalyzeSemanticModel(context, tree, cancellationToken);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.RemoveUnnecessaryImports;
using Microsoft.CodeAnalysis.Editor.UnitTests.CodeActions;
using Microsoft.CodeAnalysis.RemoveUnnecessaryImports;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Test.Utilities;
using Microsoft.CodeAnalysis.Testing;
Expand Down Expand Up @@ -194,54 +193,6 @@ static void Main(string[] args)
""",
}.RunAsync();

[Fact]
public Task TestRazorGeneratedCode()
=> new VerifyCS.Test
{
TestCode = """
// <auto-generated/>

using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
static void Main(string[] args)
{
List<int> d;
}
}
""",
SolutionTransforms =
{
(solution, projectId) =>
{
var razorGenerator = new Microsoft.NET.Sdk.Razor.SourceGenerators.RazorSourceGenerator((c) => c.AddSource("Component.razor.g.cs", """
using System;
using System.Collections.Generic;

public class C
{
public void M(List<int> items)
{
}
}
"""));

var project = solution.GetProject(projectId)!;
var updatedProject = project.AddAnalyzerReference(new TestGeneratorReference(razorGenerator));
return updatedProject.Solution;
}
},
ExpectedDiagnostics = {
// Microsoft.CodeAnalysis.Test.Utilities\Microsoft.NET.Sdk.Razor.SourceGenerators.RazorSourceGenerator\Component.razor.g.cs(1,1): hidden IDE0005_gen: Using directive is unnecessary.
VerifyCS.Diagnostic(RemoveUnnecessaryImportsConstants.IDE0005_gen).WithSpan(@"Microsoft.CodeAnalysis.Test.Utilities\Microsoft.NET.Sdk.Razor.SourceGenerators.RazorSourceGenerator\Component.razor.g.cs", 1, 1, 1, 14),
// Microsoft.CodeAnalysis.Test.Utilities\Microsoft.NET.Sdk.Razor.SourceGenerators.RazorSourceGenerator\Component.razor.g.cs(1,1): hidden RemoveUnnecessaryImportsFixable:
VerifyCS.Diagnostic(RemoveUnnecessaryImportsConstants.DiagnosticFixableId).WithSpan(@"Microsoft.CodeAnalysis.Test.Utilities\Microsoft.NET.Sdk.Razor.SourceGenerators.RazorSourceGenerator\Component.razor.g.cs", 1, 1, 2, 34),
}
}.RunAsync();

[Fact]
public Task TestGenericReferenceInTypeContext()
=> VerifyCS.VerifyCodeFixAsync(
Expand Down
2 changes: 1 addition & 1 deletion src/Analyzers/Core/Analyzers/Helpers/DiagnosticHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ static IEnumerable<string> GetEffectiveCustomTags(DiagnosticDescriptor descripto

// These diagnostics are hidden and not configurable, so help link can never be shown and is not applicable.
if (id == RemoveUnnecessaryImports.RemoveUnnecessaryImportsConstants.DiagnosticFixableId ||
id == RemoveUnnecessaryImports.RemoveUnnecessaryImportsConstants.IDE0005_gen)
id == "IDE0005_gen")
{
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,6 @@ private void AnalyzeSemanticModel(SemanticModelAnalysisContext context)
var tree = context.SemanticModel.SyntaxTree;
var cancellationToken = context.CancellationToken;

AnalyzeSemanticModel(context, tree, cancellationToken);
}

protected virtual void AnalyzeSemanticModel(SemanticModelAnalysisContext context, SyntaxTree tree, CancellationToken cancellationToken)
{
var unnecessaryImports = UnnecessaryImportsProvider.GetUnnecessaryImports(context.SemanticModel, context.FilterSpan, cancellationToken);
if (unnecessaryImports.Any())
{
Expand All @@ -102,7 +97,7 @@ protected virtual void AnalyzeSemanticModel(SemanticModelAnalysisContext context
// for us appropriately.
var mergedImports = MergeImports(unnecessaryImports);

var descriptor = context.IsGeneratedCode
var descriptor = GeneratedCodeUtilities.IsGeneratedCode(tree, IsRegularCommentOrDocComment, cancellationToken)
? _generatedCodeClassificationIdDescriptor
: _classificationIdDescriptor;
var contiguousSpans = GetContiguousSpans(mergedImports);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,4 @@ internal static class RemoveUnnecessaryImportsConstants
{
// NOTE: This is a trigger diagnostic, which doesn't show up in the ruleset editor and hence doesn't need a conventional IDE Diagnostic ID string.
public const string DiagnosticFixableId = "RemoveUnnecessaryImportsFixable";

public const string IDE0005_gen = "IDE0005_gen";
}
7 changes: 0 additions & 7 deletions src/Tools/ExternalAccess/Razor/Features/Cohost/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

using System;
using Microsoft.CodeAnalysis.LanguageServer;
using Microsoft.CodeAnalysis.RemoveUnnecessaryImports;

namespace Microsoft.CodeAnalysis.ExternalAccess.Razor.Cohost;

Expand All @@ -14,10 +13,4 @@ internal static class Constants

// These UI contexts are provided by Razor, so must match https://github.com/dotnet/razor/blob/main/src/Razor/src/Microsoft.VisualStudio.LanguageServices.Razor/RazorConstants.cs
public static readonly Guid RazorCohostingUIContext = new Guid("6d5b86dc-6b8a-483b-ae30-098a3c7d6774");

internal static class DiagnosticIds
{
public const string RemoveUnnecessaryImportsFixable = RemoveUnnecessaryImportsConstants.DiagnosticFixableId;
public const string IDE0005_gen = RemoveUnnecessaryImportsConstants.IDE0005_gen;
}
}
Loading