Skip to content
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

WIP Analyzer, just bare bones diagnostic #45433

Merged
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
1 change: 1 addition & 0 deletions src/Analyzers/CSharp/Analyzers/CSharpAnalyzers.projitems
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<Compile Include="$(MSBuildThisFileDirectory)AddAccessibilityModifiers\CSharpAddAccessibilityModifiersDiagnosticAnalyzer.cs" />
<Compile Include="$(MSBuildThisFileDirectory)AddBraces\CSharpAddBracesDiagnosticAnalyzer.cs" />
<Compile Include="$(MSBuildThisFileDirectory)AddRequiredParentheses\CSharpAddRequiredPatternParenthesesDiagnosticAnalyzer.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ConvertNameOf\CSharpConvertNameOfDiagnosticAnalyzer.cs" />
<Compile Include="$(MSBuildThisFileDirectory)RemoveUnnecessarySuppressions\CSharpRemoveUnnecessarySuppressionsDiagnosticAnalyzer.cs" />
<Compile Include="$(MSBuildThisFileDirectory)RemoveUnnecessaryParentheses\CSharpRemoveUnnecessaryPatternParenthesesDiagnosticAnalyzer.cs" />
<Compile Include="$(MSBuildThisFileDirectory)RemoveConfusingSuppression\CSharpRemoveConfusingSuppressionDiagnosticAnalyzer.cs" />
Expand Down
3 changes: 3 additions & 0 deletions src/Analyzers/CSharp/Analyzers/CSharpAnalyzersResources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -289,4 +289,7 @@
<data name="Suppression_operator_has_no_effect_and_can_be_misinterpreted" xml:space="preserve">
<value>Suppression operator has no effect and can be misinterpreted</value>
</data>
<data name="Convert_type_name_to_nameof" xml:space="preserve">
<value>Convert typeof to nameof</value>
</data>
</root>
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// Licensed to the .NET Foundation under one or more agreements.
// 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.Collections.Immutable;
using System.Linq;
using System.Linq.Expressions;
using System.Threading;
using Microsoft.CodeAnalysis.CodeStyle;
using Microsoft.CodeAnalysis.CSharp.CodeStyle;
using Microsoft.CodeAnalysis.CSharp.Extensions;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.PooledObjects;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Text;

namespace Microsoft.CodeAnalysis.CSharp.ConvertNameOf
{
/// <summary>
/// Finds code like typeof(someType).Name and determines whether it can be changed to nameof(someType), if yes then it offers a diagnostic
/// </summary>
[DiagnosticAnalyzer(LanguageNames.CSharp)]
internal class CSharpConvertNameOfDiagnosticAnalyzer : AbstractBuiltInCodeStyleDiagnosticAnalyzer
{
public CSharpConvertNameOfDiagnosticAnalyzer()
: base(IDEDiagnosticIds.ConvertNameOfDiagnosticId,
CSharpCodeStyleOptions.PreferBraces,
LanguageNames.CSharp,
new LocalizableResourceString(
nameof(CSharpAnalyzersResources.Convert_type_name_to_nameof), CSharpAnalyzersResources.ResourceManager, typeof(CSharpAnalyzersResources)))
{
}

protected override void InitializeWorker(AnalysisContext context)
{
context.RegisterSyntaxNodeAction(AnalyzeSyntaxAction, SyntaxKind.TypeOfExpression);
}

private void AnalyzeSyntaxAction(SyntaxNodeAnalysisContext syntaxContext)
{
//var options = syntaxContext.Options;
var syntaxTree = syntaxContext.Node.SyntaxTree;
//var cancellationToken = syntaxContext.CancellationToken;
var node = syntaxContext.Node;

// TODO: Any relevant style options?

// nameof was added in CSharp 6.0, so don't offer it for any languages after that time
if (((CSharpParseOptions)syntaxTree.Options).LanguageVersion < LanguageVersion.CSharp6)
{
return;
}

// TODO: Check for compiler errors on the typeof(someType).Name declaration

// TODO: Check that the current span is the case we're looking for

// TODO: Filter cases that don't work

// TODO: Create and report the right diagnostic
var location = Location.Create(syntaxTree, node.Span);
var additionalLocations = ImmutableArray.Create(node.GetLocation());

syntaxContext.ReportDiagnostic(DiagnosticHelper.Create(
Descriptor,
location,
ReportDiagnostic.Hidden,
additionalLocations,
properties: null));
}
// TODO: Overwrite GetAnalyzerCategory
public override DiagnosticAnalyzerCategory GetAnalyzerCategory()
=> DiagnosticAnalyzerCategory.SemanticSpanAnalysis;

// HELPERS GO HERE
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
<target state="translated">Převést příkaz switch na výraz</target>
<note />
</trans-unit>
<trans-unit id="Convert_type_name_to_nameof">
<source>Convert typeof to nameof</source>
<target state="new">Convert typeof to nameof</target>
<note />
</trans-unit>
<trans-unit id="Deconstruct_variable_declaration">
<source>Deconstruct variable declaration</source>
<target state="translated">Dekonstruovat deklaraci proměnné</target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
<target state="translated">Switch-Anweisung in Ausdruck konvertieren</target>
<note />
</trans-unit>
<trans-unit id="Convert_type_name_to_nameof">
<source>Convert typeof to nameof</source>
<target state="new">Convert typeof to nameof</target>
<note />
</trans-unit>
<trans-unit id="Deconstruct_variable_declaration">
<source>Deconstruct variable declaration</source>
<target state="translated">Variablendeklaration dekonstruieren</target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
<target state="translated">Convertir una instrucción switch en expresión</target>
<note />
</trans-unit>
<trans-unit id="Convert_type_name_to_nameof">
<source>Convert typeof to nameof</source>
<target state="new">Convert typeof to nameof</target>
<note />
</trans-unit>
<trans-unit id="Deconstruct_variable_declaration">
<source>Deconstruct variable declaration</source>
<target state="translated">Desconstruir la declaración de variable</target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
<target state="translated">Convertir l'instruction switch en expression</target>
<note />
</trans-unit>
<trans-unit id="Convert_type_name_to_nameof">
<source>Convert typeof to nameof</source>
<target state="new">Convert typeof to nameof</target>
<note />
</trans-unit>
<trans-unit id="Deconstruct_variable_declaration">
<source>Deconstruct variable declaration</source>
<target state="translated">Déconstruire la déclaration de variable</target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
<target state="translated">Converti l'istruzione switch in espressione</target>
<note />
</trans-unit>
<trans-unit id="Convert_type_name_to_nameof">
<source>Convert typeof to nameof</source>
<target state="new">Convert typeof to nameof</target>
<note />
</trans-unit>
<trans-unit id="Deconstruct_variable_declaration">
<source>Deconstruct variable declaration</source>
<target state="translated">Decostruisci la dichiarazione di variabile</target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
<target state="translated">switch ステートメントを式に変換します</target>
<note />
</trans-unit>
<trans-unit id="Convert_type_name_to_nameof">
<source>Convert typeof to nameof</source>
<target state="new">Convert typeof to nameof</target>
<note />
</trans-unit>
<trans-unit id="Deconstruct_variable_declaration">
<source>Deconstruct variable declaration</source>
<target state="translated">変数の宣言を分解</target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
<target state="translated">switch 문을 식으로 변환</target>
<note />
</trans-unit>
<trans-unit id="Convert_type_name_to_nameof">
<source>Convert typeof to nameof</source>
<target state="new">Convert typeof to nameof</target>
<note />
</trans-unit>
<trans-unit id="Deconstruct_variable_declaration">
<source>Deconstruct variable declaration</source>
<target state="translated">변수 선언 분해</target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
<target state="translated">Konwertuj instrukcję switch na wyrażenie</target>
<note />
</trans-unit>
<trans-unit id="Convert_type_name_to_nameof">
<source>Convert typeof to nameof</source>
<target state="new">Convert typeof to nameof</target>
<note />
</trans-unit>
<trans-unit id="Deconstruct_variable_declaration">
<source>Deconstruct variable declaration</source>
<target state="translated">Zdekonstruuj deklarację zmiennej</target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
<target state="translated">Converter a instrução switch em expressão</target>
<note />
</trans-unit>
<trans-unit id="Convert_type_name_to_nameof">
<source>Convert typeof to nameof</source>
<target state="new">Convert typeof to nameof</target>
<note />
</trans-unit>
<trans-unit id="Deconstruct_variable_declaration">
<source>Deconstruct variable declaration</source>
<target state="translated">Desconstruir declaração de variável</target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
<target state="translated">Преобразовать оператор switch в выражение</target>
<note />
</trans-unit>
<trans-unit id="Convert_type_name_to_nameof">
<source>Convert typeof to nameof</source>
<target state="new">Convert typeof to nameof</target>
<note />
</trans-unit>
<trans-unit id="Deconstruct_variable_declaration">
<source>Deconstruct variable declaration</source>
<target state="translated">Деконструировать объявление переменной</target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
<target state="translated">Switch deyimini ifadeye dönüştür</target>
<note />
</trans-unit>
<trans-unit id="Convert_type_name_to_nameof">
<source>Convert typeof to nameof</source>
<target state="new">Convert typeof to nameof</target>
<note />
</trans-unit>
<trans-unit id="Deconstruct_variable_declaration">
<source>Deconstruct variable declaration</source>
<target state="translated">Değişken bildirimini ayrıştır</target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
<target state="translated">将 switch 语句转换为表达式</target>
<note />
</trans-unit>
<trans-unit id="Convert_type_name_to_nameof">
<source>Convert typeof to nameof</source>
<target state="new">Convert typeof to nameof</target>
<note />
</trans-unit>
<trans-unit id="Deconstruct_variable_declaration">
<source>Deconstruct variable declaration</source>
<target state="translated">析构变量声明</target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
<target state="translated">將 switch 陳述式轉換為運算式</target>
<note />
</trans-unit>
<trans-unit id="Convert_type_name_to_nameof">
<source>Convert typeof to nameof</source>
<target state="new">Convert typeof to nameof</target>
<note />
</trans-unit>
<trans-unit id="Deconstruct_variable_declaration">
<source>Deconstruct variable declaration</source>
<target state="translated">解構變數宣告</target>
Expand Down