-
Notifications
You must be signed in to change notification settings - Fork 509
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2032 from sharwell/fix-2029
Implement SA0002 (InvalidSettingsFile)
- Loading branch information
Showing
13 changed files
with
295 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
StyleCop.Analyzers/StyleCop.Analyzers.Test/SpecialRules/SA0002UnitTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. | ||
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information. | ||
|
||
namespace StyleCop.Analyzers.Test.SpecialRules | ||
{ | ||
using System.Collections.Generic; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Analyzers.Settings; | ||
using Analyzers.SpecialRules; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
using TestHelper; | ||
using Xunit; | ||
|
||
/// <summary> | ||
/// Unit tests for <see cref="SA0002InvalidSettingsFile"/>. | ||
/// </summary> | ||
public class SA0002UnitTests : DiagnosticVerifier | ||
{ | ||
private const string TestCode = @" | ||
namespace NamespaceName { } | ||
"; | ||
|
||
private string settings; | ||
|
||
[Fact] | ||
public async Task TestMissingSettingsAsync() | ||
{ | ||
await this.VerifyCSharpDiagnosticAsync(TestCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false); | ||
} | ||
|
||
[Fact] | ||
public async Task TestValidSettingsAsync() | ||
{ | ||
this.settings = SettingsFileCodeFixProvider.DefaultSettingsFileContent; | ||
|
||
await this.VerifyCSharpDiagnosticAsync(TestCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false); | ||
} | ||
|
||
[Fact] | ||
public async Task TestInvalidSettingsAsync() | ||
{ | ||
this.settings = @" | ||
{ | ||
""$schema"": ""https://raw.githubusercontent.com/DotNetAnalyzers/StyleCopAnalyzers/master/StyleCop.Analyzers/StyleCop.Analyzers/Settings/stylecop.schema.json"" | ||
""settings"": { | ||
""documentationRules"": { | ||
""companyName"": ""ACME, Inc"", | ||
""copyrightText"": ""Copyright 2015 {companyName}. All rights reserved."" | ||
} | ||
} | ||
} | ||
"; | ||
|
||
// This diagnostic is reported without a location | ||
DiagnosticResult expected = this.CSharpDiagnostic(); | ||
|
||
await this.VerifyCSharpDiagnosticAsync(TestCode, expected, CancellationToken.None).ConfigureAwait(false); | ||
} | ||
|
||
/// <inheritdoc/> | ||
protected override string GetSettings() | ||
{ | ||
return this.settings; | ||
} | ||
|
||
/// <inheritdoc/> | ||
protected override IEnumerable<DiagnosticAnalyzer> GetCSharpDiagnosticAnalyzers() | ||
{ | ||
yield return new SA0002InvalidSettingsFile(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
StyleCop.Analyzers/StyleCop.Analyzers/Settings/DeserializationFailureBehavior.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. | ||
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information. | ||
|
||
namespace StyleCop.Analyzers | ||
{ | ||
using Newtonsoft.Json; | ||
using StyleCop.Analyzers.Settings.ObjectModel; | ||
|
||
/// <summary> | ||
/// Defines the behavior of various <see cref="SettingsHelper"/> methods in the event of a deserialization error. | ||
/// </summary> | ||
internal enum DeserializationFailureBehavior | ||
{ | ||
/// <summary> | ||
/// When deserialization fails, return a default <see cref="StyleCopSettings"/> instance. | ||
/// </summary> | ||
ReturnDefaultSettings, | ||
|
||
/// <summary> | ||
/// When deserialization fails, throw a <see cref="JsonException"/> containing details about the error. | ||
/// </summary> | ||
ThrowException | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
StyleCop.Analyzers/StyleCop.Analyzers/SpecialRules/SA0002InvalidSettingsFile.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. | ||
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information. | ||
|
||
namespace StyleCop.Analyzers.SpecialRules | ||
{ | ||
using System; | ||
using System.Collections.Immutable; | ||
using System.Globalization; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
using Newtonsoft.Json; | ||
|
||
/// <summary> | ||
/// The <em>stylecop.json</em> settings file could not be loaded due to a deserialization failure. | ||
/// </summary> | ||
[NoCodeFix("No automatic code fix is possible for general JSON syntax errors.")] | ||
[DiagnosticAnalyzer(LanguageNames.CSharp)] | ||
internal class SA0002InvalidSettingsFile : DiagnosticAnalyzer | ||
{ | ||
/// <summary> | ||
/// The ID for diagnostics produced by the <see cref="SA0002InvalidSettingsFile"/> analyzer. | ||
/// </summary> | ||
public const string DiagnosticId = "SA0002"; | ||
private const string HelpLink = "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SA0002.md"; | ||
private static readonly LocalizableString Title = new LocalizableResourceString(nameof(SpecialResources.SA0002Title), SpecialResources.ResourceManager, typeof(SpecialResources)); | ||
private static readonly LocalizableString MessageFormat = new LocalizableResourceString(nameof(SpecialResources.SA0002MessageFormat), SpecialResources.ResourceManager, typeof(SpecialResources)); | ||
private static readonly LocalizableString Description = new LocalizableResourceString(nameof(SpecialResources.SA0002Description), SpecialResources.ResourceManager, typeof(SpecialResources)); | ||
|
||
private static readonly DiagnosticDescriptor Descriptor = | ||
new DiagnosticDescriptor(DiagnosticId, Title, MessageFormat, AnalyzerCategory.SpecialRules, DiagnosticSeverity.Warning, AnalyzerConstants.EnabledByDefault, Description, HelpLink); | ||
|
||
private static readonly Action<CompilationAnalysisContext> CompilationAction = HandleCompilation; | ||
|
||
/// <inheritdoc/> | ||
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = | ||
ImmutableArray.Create(Descriptor); | ||
|
||
/// <inheritdoc/> | ||
public override void Initialize(AnalysisContext context) | ||
{ | ||
context.RegisterCompilationAction(CompilationAction); | ||
} | ||
|
||
private static void HandleCompilation(CompilationAnalysisContext context) | ||
{ | ||
try | ||
{ | ||
SettingsHelper.GetStyleCopSettings(context.Options, DeserializationFailureBehavior.ThrowException, context.CancellationToken); | ||
} | ||
catch (JsonException ex) | ||
{ | ||
string details = ex.Message; | ||
string completeDescription = string.Format(Description.ToString(CultureInfo.CurrentCulture), details); | ||
|
||
var completeDescriptor = new DiagnosticDescriptor(DiagnosticId, Title, MessageFormat, AnalyzerCategory.SpecialRules, DiagnosticSeverity.Warning, AnalyzerConstants.EnabledByDefault, completeDescription, HelpLink); | ||
context.ReportDiagnostic(Diagnostic.Create(completeDescriptor, Location.None)); | ||
} | ||
} | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
StyleCop.Analyzers/StyleCop.Analyzers/SpecialRules/SpecialResources.Designer.cs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.