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

Ide options #74053

Merged
merged 3 commits into from
Jun 29, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Convert PreferSystemHashCode and misc embedded language options to ed…
…itorconfig options
  • Loading branch information
tmat committed Jun 28, 2024
commit d29197083431a4b580eaedde2cfb6bd1c01abb20
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Editor.CSharp.UnitTests.Diagnostics;
using Microsoft.CodeAnalysis.Test.Utilities;
using Microsoft.CodeAnalysis.ValidateFormatString;
using Roslyn.Test.Utilities;
using Xunit;
using Xunit.Abstractions;
Expand Down Expand Up @@ -620,28 +621,6 @@ static void Main(string[] args)
""");
}

#if CODE_STYLE
// Option has no effect on CodeStyle layer CI execution as it is not an editorconfig option.
[Fact]
public async Task TestOption_Ignored()
{
var source = """
class Program
{
static void Main(string[] args)
{
string.Format("This [|{1}|] is my test", "teststring1");
}
}
""";
await TestDiagnosticInfoAsync(
source,
options: null,
diagnosticId: IDEDiagnosticIds.ValidateFormatStringDiagnosticID,
diagnosticSeverity: DiagnosticSeverity.Info,
diagnosticMessage: AnalyzersResources.Format_string_contains_invalid_placeholder);
}
#else
[Fact]
public async Task TestOption_Enabled()
{
Expand All @@ -654,12 +633,11 @@ static void Main(string[] args)
}
}
""";
var options = Option(IdeAnalyzerOptionsStorage.ReportInvalidPlaceholdersInStringDotFormatCalls, true);
var options = Option(FormatStringValidationOptionStorage.ReportInvalidPlaceholdersInStringDotFormatCalls, true);

await TestDiagnosticInfoAsync(
source,
options: null,
globalOptions: options,
options: options,
diagnosticId: IDEDiagnosticIds.ValidateFormatStringDiagnosticID,
diagnosticSeverity: DiagnosticSeverity.Info,
diagnosticMessage: AnalyzersResources.Format_string_contains_invalid_placeholder);
Expand All @@ -677,11 +655,11 @@ static void Main(string[] args)
}
}
""";
var options = Option(IdeAnalyzerOptionsStorage.ReportInvalidPlaceholdersInStringDotFormatCalls, false);
var options = Option(FormatStringValidationOptionStorage.ReportInvalidPlaceholdersInStringDotFormatCalls, false);

await TestDiagnosticMissingAsync(source, new TestParameters(globalOptions: options));
await TestDiagnosticMissingAsync(source, new TestParameters(options: options));
}
#endif

[Fact]
public async Task OnePlaceholderOutOfBounds()
{
Expand Down
4 changes: 4 additions & 0 deletions src/Analyzers/Core/Analyzers/AnalyzerOptionsProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ public SyntaxFormattingOptions GetSyntaxFormattingOptions(ISyntaxFormatting form
public CodeStyleOption2<bool> PreferConditionalExpressionOverReturn => GetOption(CodeStyleOptions2.PreferConditionalExpressionOverReturn, FallbackCodeStyleOptions.PreferConditionalExpressionOverReturn);
public CodeStyleOption2<bool> PreferCompoundAssignment => GetOption(CodeStyleOptions2.PreferCompoundAssignment, FallbackCodeStyleOptions.PreferCompoundAssignment);
public CodeStyleOption2<bool> PreferSimplifiedInterpolation => GetOption(CodeStyleOptions2.PreferSimplifiedInterpolation, FallbackCodeStyleOptions.PreferSimplifiedInterpolation);
public CodeStyleOption2<bool> PreferSystemHashCode => GetOption(CodeStyleOptions2.PreferSystemHashCode);
public CodeStyleOption2<UnusedParametersPreference> UnusedParameters => GetOption(CodeStyleOptions2.UnusedParameters, FallbackCodeStyleOptions.UnusedParameters);
public CodeStyleOption2<AccessibilityModifiersRequired> RequireAccessibilityModifiers => GetOption(CodeStyleOptions2.AccessibilityModifiersRequired, FallbackCodeStyleOptions.AccessibilityModifiersRequired);
public CodeStyleOption2<bool> PreferReadonly => GetOption(CodeStyleOptions2.PreferReadonly, FallbackCodeStyleOptions.PreferReadonly);
Expand All @@ -96,6 +97,9 @@ public SyntaxFormattingOptions GetSyntaxFormattingOptions(ISyntaxFormatting form
private TValue GetOption<TValue>(Option2<TValue> option, TValue defaultValue)
=> _options.GetOption(option, defaultValue);

public TValue GetOption<TValue>(PerLanguageOption2<TValue> option)
=> _options.GetOption(option, _language);

private TValue GetOption<TValue>(PerLanguageOption2<TValue> option, TValue defaultValue)
=> _options.GetOption(option, _language, defaultValue);

Expand Down
1 change: 1 addition & 0 deletions src/Analyzers/Core/Analyzers/Analyzers.projitems
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@
<Compile Include="$(MSBuildThisFileDirectory)UseSystemHashCode\UseSystemHashCodeDiagnosticAnalyzer.cs" />
<Compile Include="$(MSBuildThisFileDirectory)UseThrowExpression\AbstractUseThrowExpressionDiagnosticAnalyzer.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ValidateFormatString\AbstractValidateFormatStringDiagnosticAnalyzer.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ValidateFormatString\FormatStringValidationOptionStorage.cs" />
</ItemGroup>
<ItemGroup Condition="'$(DefaultLanguageSourceExtension)' != '' AND '$(BuildingInsideVisualStudio)' != 'true'">
<ExpectedCompile Include="$(MSBuildThisFileDirectory)**\*$(DefaultLanguageSourceExtension)" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ private void AnalyzeOperationBlock(HashCodeAnalyzer analyzer, OperationBlockAnal
// We've got multiple members to hash, or multiple statements that can be reduced at this point.
Debug.Assert(elementCount >= 2 || statements.Length >= 2);

var option = context.Options.GetIdeOptions().PreferSystemHashCode;
var option = context.Options.GetAnalyzerOptions(operation.Syntax.SyntaxTree).PreferSystemHashCode;
if (!option.Value || ShouldSkipAnalysis(context, option.Notification))
return;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ private void AnalyzeNode(SyntaxNodeAnalysisContext context, INamedTypeSymbol for
return;
}

if (!context.GetIdeAnalyzerOptions().ReportInvalidPlaceholdersInStringDotFormatCalls)
if (!context.GetAnalyzerOptions().GetOption(FormatStringValidationOptionStorage.ReportInvalidPlaceholdersInStringDotFormatCalls))
{
return;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// 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 Microsoft.CodeAnalysis.Options;

namespace Microsoft.CodeAnalysis.ValidateFormatString;

internal static class FormatStringValidationOptionStorage
{
public static readonly PerLanguageOption2<bool> ReportInvalidPlaceholdersInStringDotFormatCalls = new(
"dotnet_unsupported_report_invalid_placeholders_in_string_dot_format_calls",
defaultValue: true,
isEditorConfigOption: true);

public static readonly ImmutableArray<IOption2> UnsupportedOptions = [ReportInvalidPlaceholdersInStringDotFormatCalls];
}
Original file line number Diff line number Diff line change
Expand Up @@ -1620,7 +1620,6 @@ End Class",
parseOptions:=Nothing,
compilationOptions:=Nothing,
options:=Nothing,
globalOptions:=Nothing,
"IDE0051",
DiagnosticSeverity.Info,
diagnosticMessage:=String.Format(AnalyzersResources.Private_member_0_is_unused, "C.New"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ internal override (DiagnosticAnalyzer, CodeFixProvider?) CreateDiagnosticProvide
=> (new CSharpJsonDiagnosticAnalyzer(), null);

private OptionsCollection OptionOn()
=> Option(IdeAnalyzerOptionsStorage.ReportInvalidJsonPatterns, true);
=> Option(JsonDetectionOptionsStorage.ReportInvalidJsonPatterns, true);

[Fact]
public async Task TestWarning1()
Expand All @@ -41,7 +41,7 @@ void Main()
}
}
""",
globalOptions: OptionOn(),
options: OptionOn(),
diagnosticId: AbstractJsonDiagnosticAnalyzer.DiagnosticId,
diagnosticSeverity: DiagnosticSeverity.Warning,
diagnosticMessage: string.Format(FeaturesResources.JSON_issue_0, FeaturesResources.Constructors_not_allowed));
Expand All @@ -59,7 +59,7 @@ void Main()
}
}
"""",
globalOptions: OptionOn(),
options: OptionOn(),
diagnosticId: AbstractJsonDiagnosticAnalyzer.DiagnosticId,
diagnosticSeverity: DiagnosticSeverity.Warning,
diagnosticMessage: string.Format(FeaturesResources.JSON_issue_0, FeaturesResources.Constructors_not_allowed));
Expand All @@ -77,7 +77,7 @@ void Main()
}
}
""",
globalOptions: OptionOn(),
options: OptionOn(),
diagnosticId: AbstractJsonDiagnosticAnalyzer.DiagnosticId,
diagnosticSeverity: DiagnosticSeverity.Warning,
diagnosticMessage: string.Format(FeaturesResources.JSON_issue_0,
Expand All @@ -104,7 +104,7 @@ void Main()
</Project>
</Workspace>
""",
globalOptions: OptionOn(),
options: OptionOn(),
diagnosticId: AbstractJsonDiagnosticAnalyzer.DiagnosticId,
diagnosticSeverity: DiagnosticSeverity.Warning,
diagnosticMessage: string.Format(FeaturesResources.JSON_issue_0,
Expand All @@ -131,7 +131,7 @@ void Main()
</Project>
</Workspace>
""",
globalOptions: OptionOn(),
options: OptionOn(),
diagnosticId: AbstractJsonDiagnosticAnalyzer.DiagnosticId,
diagnosticSeverity: DiagnosticSeverity.Warning,
diagnosticMessage: string.Format(FeaturesResources.JSON_issue_0,
Expand Down Expand Up @@ -202,7 +202,7 @@ void Main()
</Project>
</Workspace>
""",
globalOptions: OptionOn(),
options: OptionOn(),
diagnosticId: AbstractJsonDiagnosticAnalyzer.DiagnosticId,
diagnosticSeverity: DiagnosticSeverity.Warning,
diagnosticMessage: string.Format(FeaturesResources.JSON_issue_0,
Expand All @@ -229,7 +229,7 @@ void Main()
</Project>
</Workspace>
""",
globalOptions: OptionOn(),
options: OptionOn(),
diagnosticId: AbstractJsonDiagnosticAnalyzer.DiagnosticId,
diagnosticSeverity: DiagnosticSeverity.Warning,
diagnosticMessage: string.Format(FeaturesResources.JSON_issue_0,
Expand Down Expand Up @@ -304,7 +304,7 @@ void M([StringSyntax(StringSyntaxAttribute.Json)] string p)
</Document>
</Project>
</Workspace>",
globalOptions: OptionOn(),
options: OptionOn(),
diagnosticId: AbstractJsonDiagnosticAnalyzer.DiagnosticId,
diagnosticSeverity: DiagnosticSeverity.Warning,
diagnosticMessage: string.Format(FeaturesResources.JSON_issue_0,
Expand Down Expand Up @@ -335,7 +335,7 @@ void M([StringSyntax(StringSyntaxAttribute.Json)] string p, JsonDocumentOptions
</Document>
</Project>
</Workspace>",
globalOptions: OptionOn(),
options: OptionOn(),
diagnosticId: AbstractJsonDiagnosticAnalyzer.DiagnosticId,
diagnosticSeverity: DiagnosticSeverity.Warning,
diagnosticMessage: string.Format(FeaturesResources.JSON_issue_0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ internal override (DiagnosticAnalyzer, CodeFixProvider?) CreateDiagnosticProvide
=> (new CSharpRegexDiagnosticAnalyzer(), null);

private OptionsCollection OptionOn()
=> Option(IdeAnalyzerOptionsStorage.ReportInvalidRegexPatterns, true);
=> Option(RegexOptionsStorage.ReportInvalidRegexPatterns, true);

[Fact]
public async Task TestWarning1()
Expand All @@ -43,7 +43,7 @@ void Main()
}
}
""",
globalOptions: OptionOn(),
options: OptionOn(),
diagnosticId: AbstractRegexDiagnosticAnalyzer.DiagnosticId,
diagnosticSeverity: DiagnosticSeverity.Warning,
diagnosticMessage: string.Format(FeaturesResources.Regex_issue_0, FeaturesResources.Too_many_close_parens));
Expand All @@ -63,7 +63,7 @@ void Main()
}
}
""",
globalOptions: OptionOn(),
options: OptionOn(),
diagnosticId: AbstractRegexDiagnosticAnalyzer.DiagnosticId,
diagnosticSeverity: DiagnosticSeverity.Warning,
diagnosticMessage: string.Format(FeaturesResources.Regex_issue_0, FeaturesResources.Too_many_close_parens));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Microsoft.CodeAnalysis.CodeStyle;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.EmbeddedLanguages;
using Microsoft.CodeAnalysis.Options;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Simplification;

Expand Down Expand Up @@ -47,8 +48,8 @@ protected override void InitializeWorker(AnalysisContext context)

public void Analyze(SemanticModelAnalysisContext context)
{
if (!context.GetIdeAnalyzerOptions().DetectAndOfferEditorFeaturesForProbableJsonStrings
|| ShouldSkipAnalysis(context, notification: null))
if (!context.GetAnalyzerOptions().GetOption(JsonDetectionOptionsStorage.DetectAndOfferEditorFeaturesForProbableJsonStrings) ||
ShouldSkipAnalysis(context, notification: null))
{
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ protected override void InitializeWorker(AnalysisContext context)

public void Analyze(SemanticModelAnalysisContext context)
{
if (!context.GetIdeAnalyzerOptions().ReportInvalidJsonPatterns
|| ShouldSkipAnalysis(context, notification: null))
if (!context.GetAnalyzerOptions().GetOption(JsonDetectionOptionsStorage.ReportInvalidJsonPatterns) ||
ShouldSkipAnalysis(context, notification: null))
{
return;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// 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 Microsoft.CodeAnalysis.Options;

namespace Microsoft.CodeAnalysis.Features.EmbeddedLanguages.Json.LanguageServices;

internal static class JsonDetectionOptionsStorage
{
public static readonly PerLanguageOption2<bool> DetectAndOfferEditorFeaturesForProbableJsonStrings = new(
"dotnet_unsupported_detect_and_offer_editor_features_for_probable_json_strings",
defaultValue: true,
isEditorConfigOption: true);

public static PerLanguageOption2<bool> ReportInvalidJsonPatterns = new(
"dotnet_unsupported_report_invalid_json_patterns",
defaultValue: true,
isEditorConfigOption: true);

public static readonly ImmutableArray<IOption2> UnsupportedOptions = [DetectAndOfferEditorFeaturesForProbableJsonStrings, ReportInvalidJsonPatterns];
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public void Analyze(SemanticModelAnalysisContext context)
var semanticModel = context.SemanticModel;
var cancellationToken = context.CancellationToken;

var option = context.GetIdeAnalyzerOptions().ReportInvalidRegexPatterns;
var option = context.GetAnalyzerOptions().GetOption(RegexOptionsStorage.ReportInvalidRegexPatterns);
if (!option || ShouldSkipAnalysis(context, notification: null))
return;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// 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 Microsoft.CodeAnalysis.Options;

namespace Microsoft.CodeAnalysis.Features.EmbeddedLanguages.RegularExpressions.LanguageServices;

internal static class RegexOptionsStorage
{
public static PerLanguageOption2<bool> ReportInvalidRegexPatterns = new(
"dotnet_unsupported_report_invalid_regex_patterns",
defaultValue: true,
isEditorConfigOption: true);

public static readonly ImmutableArray<IOption2> UnsupportedOptions = [ReportInvalidRegexPatterns];
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@
using System.Linq;
using Microsoft.CodeAnalysis.CodeStyle;
using Microsoft.CodeAnalysis.Editing;
using Microsoft.CodeAnalysis.Features.EmbeddedLanguages.Json.LanguageServices;
using Microsoft.CodeAnalysis.Features.EmbeddedLanguages.RegularExpressions.LanguageServices;
using Microsoft.CodeAnalysis.Formatting;
using Microsoft.CodeAnalysis.Host.Mef;
using Microsoft.CodeAnalysis.PooledObjects;
using Microsoft.CodeAnalysis.ValidateFormatString;

namespace Microsoft.CodeAnalysis.Options;

Expand All @@ -32,7 +35,11 @@ internal sealed class EditorConfigOptionsEnumerator(

if (includeUnsupported)
{
yield return (WorkspacesResources.Core_EditorConfig_Options, FormattingOptions2.UndocumentedOptions);
// note: the feature string is ignored for unsupported options:
yield return ("unsupported", FormattingOptions2.UndocumentedOptions);
yield return ("unsupported", JsonDetectionOptionsStorage.UnsupportedOptions);
yield return ("unsupported", FormatStringValidationOptionStorage.UnsupportedOptions);
yield return ("unsupported", RegexOptionsStorage.UnsupportedOptions);
}

yield return (WorkspacesResources.dot_NET_Coding_Conventions, GenerationOptions.EditorConfigOptions.AddRange(CodeStyleOptions2.EditorConfigOptions));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,24 +191,22 @@ private protected async Task TestDiagnosticInfoAsync(
string diagnosticId,
DiagnosticSeverity diagnosticSeverity,
OptionsCollectionAlias options = null,
OptionsCollectionAlias globalOptions = null,
LocalizableString diagnosticMessage = null)
{
await TestDiagnosticInfoAsync(initialMarkup, parseOptions: null, compilationOptions: null, options, globalOptions, diagnosticId, diagnosticSeverity, diagnosticMessage);
await TestDiagnosticInfoAsync(initialMarkup, GetScriptOptions(), compilationOptions: null, options, globalOptions, diagnosticId, diagnosticSeverity, diagnosticMessage);
await TestDiagnosticInfoAsync(initialMarkup, parseOptions: null, compilationOptions: null, options, diagnosticId, diagnosticSeverity, diagnosticMessage);
await TestDiagnosticInfoAsync(initialMarkup, GetScriptOptions(), compilationOptions: null, options, diagnosticId, diagnosticSeverity, diagnosticMessage);
}

private protected async Task TestDiagnosticInfoAsync(
string initialMarkup,
ParseOptions parseOptions,
CompilationOptions compilationOptions,
OptionsCollectionAlias options,
OptionsCollectionAlias globalOptions,
string diagnosticId,
DiagnosticSeverity diagnosticSeverity,
LocalizableString diagnosticMessage = null)
{
var testOptions = new TestParameters(parseOptions, compilationOptions, options: options, globalOptions: globalOptions);
var testOptions = new TestParameters(parseOptions, compilationOptions, options: options);
using var workspace = CreateWorkspaceFromOptions(initialMarkup, testOptions);
var diagnostics = (await GetDiagnosticsAsync(workspace, testOptions)).ToImmutableArray();
diagnostics = diagnostics.WhereAsArray(d => d.Id == diagnosticId);
Expand Down
Loading