Skip to content

Commit 7524fc3

Browse files
Improve SettingsHelper by making sure that its internal GetStyleCopSettings methods are only called by other classes
1 parent 77c498d commit 7524fc3

2 files changed

Lines changed: 12 additions & 11 deletions

File tree

StyleCop.Analyzers/StyleCop.Analyzers/Settings/SettingsHelper.cs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ internal static class SettingsHelper
2727

2828
private static SourceTextValueProvider<StyleCopSettings> SettingsValueProvider { get; } =
2929
new SourceTextValueProvider<StyleCopSettings>(
30-
text => GetStyleCopSettings(options: null, tree: null, SettingsFileName, text, DeserializationFailureBehavior.ReturnDefaultSettings));
30+
text => GetSettings(options: null, tree: null, SettingsFileName, text, DeserializationFailureBehavior.ReturnDefaultSettings));
3131

3232
/// <summary>
3333
/// Gets the StyleCop settings.
@@ -41,7 +41,7 @@ internal static class SettingsHelper
4141
/// <returns>A <see cref="StyleCopSettings"/> instance that represents the StyleCop settings for the given context.</returns>
4242
internal static StyleCopSettings GetStyleCopSettings(this SyntaxTreeAnalysisContext context, CancellationToken cancellationToken)
4343
{
44-
return GetStyleCopSettings(context.Options, context.Tree, cancellationToken);
44+
return GetSettings(context.Options, context.Tree, DeserializationFailureBehavior.ReturnDefaultSettings, cancellationToken);
4545
}
4646

4747
/// <summary>
@@ -56,7 +56,7 @@ internal static StyleCopSettings GetStyleCopSettings(this SyntaxTreeAnalysisCont
5656
/// <returns>A <see cref="StyleCopSettings"/> instance that represents the StyleCop settings for the given context.</returns>
5757
internal static StyleCopSettings GetStyleCopSettings(this SyntaxNodeAnalysisContext context, CancellationToken cancellationToken)
5858
{
59-
return GetStyleCopSettings(context.Options, context.Node.SyntaxTree, cancellationToken);
59+
return GetSettings(context.Options, context.Node.SyntaxTree, DeserializationFailureBehavior.ReturnDefaultSettings, cancellationToken);
6060
}
6161

6262
/// <summary>
@@ -72,21 +72,21 @@ internal static StyleCopSettings GetStyleCopSettings(this SyntaxNodeAnalysisCont
7272
/// <returns>A <see cref="StyleCopSettings"/> instance that represents the StyleCop settings for the given context.</returns>
7373
internal static StyleCopSettings GetStyleCopSettings(this AnalyzerOptions options, SyntaxTree tree, CancellationToken cancellationToken)
7474
{
75-
return GetStyleCopSettings(options, tree, DeserializationFailureBehavior.ReturnDefaultSettings, cancellationToken);
75+
return GetSettings(options, tree, DeserializationFailureBehavior.ReturnDefaultSettings, cancellationToken);
7676
}
7777

7878
/// <summary>
7979
/// Gets the StyleCop settings.
8080
/// </summary>
81-
/// <param name="options">The analyzer options that will be used to determine the StyleCop settings.</param>
81+
/// <param name="context">The context that will be used to determine the StyleCop settings.</param>
8282
/// <param name="tree">The syntax tree.</param>
8383
/// <param name="failureBehavior">The behavior of the method when a <see cref="JsonParseException"/> or
8484
/// <see cref="InvalidSettingsException"/> occurs while deserializing the settings file.</param>
8585
/// <param name="cancellationToken">The cancellation token that the operation will observe.</param>
8686
/// <returns>A <see cref="StyleCopSettings"/> instance that represents the StyleCop settings for the given context.</returns>
87-
internal static StyleCopSettings GetStyleCopSettings(this AnalyzerOptions options, SyntaxTree tree, DeserializationFailureBehavior failureBehavior, CancellationToken cancellationToken)
87+
internal static StyleCopSettings GetStyleCopSettings(this CompilationAnalysisContext context, SyntaxTree tree, DeserializationFailureBehavior failureBehavior, CancellationToken cancellationToken)
8888
{
89-
return GetStyleCopSettings(options, tree, options != null ? options.AdditionalFiles : ImmutableArray.Create<AdditionalText>(), failureBehavior, cancellationToken);
89+
return GetSettings(context.Options, tree, failureBehavior, cancellationToken);
9090
}
9191

9292
/// <summary>
@@ -108,14 +108,15 @@ internal static bool IsStyleCopSettingsFile(string path)
108108
|| string.Equals(fileName, AltSettingsFileName, StringComparison.OrdinalIgnoreCase);
109109
}
110110

111-
private static StyleCopSettings GetStyleCopSettings(AnalyzerOptions options, SyntaxTree tree, ImmutableArray<AdditionalText> additionalFiles, DeserializationFailureBehavior failureBehavior, CancellationToken cancellationToken)
111+
private static StyleCopSettings GetSettings(AnalyzerOptions options, SyntaxTree tree, DeserializationFailureBehavior failureBehavior, CancellationToken cancellationToken)
112112
{
113+
var additionalFiles = options != null ? options.AdditionalFiles : ImmutableArray.Create<AdditionalText>();
113114
foreach (var additionalFile in additionalFiles)
114115
{
115116
if (IsStyleCopSettingsFile(additionalFile.Path))
116117
{
117118
SourceText additionalTextContent = additionalFile.GetText(cancellationToken);
118-
return GetStyleCopSettings(options, tree, additionalFile.Path, additionalTextContent, failureBehavior);
119+
return GetSettings(options, tree, additionalFile.Path, additionalTextContent, failureBehavior);
119120
}
120121
}
121122

@@ -128,7 +129,7 @@ private static StyleCopSettings GetStyleCopSettings(AnalyzerOptions options, Syn
128129
return new StyleCopSettings();
129130
}
130131

131-
private static StyleCopSettings GetStyleCopSettings(AnalyzerOptions options, SyntaxTree tree, string path, SourceText text, DeserializationFailureBehavior failureBehavior)
132+
private static StyleCopSettings GetSettings(AnalyzerOptions options, SyntaxTree tree, string path, SourceText text, DeserializationFailureBehavior failureBehavior)
132133
{
133134
var analyzerConfigOptions = options.AnalyzerConfigOptionsProvider().GetOptions(tree);
134135

StyleCop.Analyzers/StyleCop.Analyzers/SpecialRules/SA0002InvalidSettingsFile.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ private static void HandleCompilation(CompilationAnalysisContext context)
5959

6060
try
6161
{
62-
SettingsHelper.GetStyleCopSettings(context.Options, firstSyntaxTree, DeserializationFailureBehavior.ThrowException, context.CancellationToken);
62+
context.GetStyleCopSettings(firstSyntaxTree, DeserializationFailureBehavior.ThrowException, context.CancellationToken);
6363
}
6464
catch (Exception ex) when (ex is JsonParseException || ex is InvalidSettingsException)
6565
{

0 commit comments

Comments
 (0)