Skip to content

Commit 0a6a186

Browse files
authored
Fix some code style issues (#5041)
* Accessibility modifiers required * Make field readonly * Remove redundant equality * Delegate invocation can be simplified
1 parent 1278e0b commit 0a6a186

File tree

20 files changed

+64
-94
lines changed

20 files changed

+64
-94
lines changed

Directory.Build.props

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,6 @@
6060
[IDE0029] Null check can be simplified
6161
[IDE0032] Use auto property
6262
[IDE0039] Use local function
63-
[IDE0040] Accessibility modifiers required
64-
[IDE0044] Make field readonly
6563
[IDE0045] 'if' statement can be simplified
6664
[IDE0046] 'if' statement can be simplified
6765
[IDE0055] Fix formatting
@@ -75,7 +73,6 @@
7573
[IDE0078] Use pattern matching
7674
[IDE0083] Use pattern matching
7775
[IDE0090] 'new' expression can be simplified
78-
[IDE0100] Remove redundant equality
7976
[IDE0130] Namespace does not match folder structure
8077
[IDE0160] Convert to block scoped namespace
8178
[IDE0200] Lambda expression can be removed
@@ -86,7 +83,6 @@
8683
[IDE0301] Collection initialization can be simplified
8784
[IDE0305] Collection initialization can be simplified
8885
[IDE0330] Use 'System.Threading.Lock'
89-
[IDE1005] Delegate invocation can be simplified
9086
[CA1200] Avoid using cref tags with a prefix
9187
[CA1304] The behavior of 'string.ToUpper()' could vary based on the current user's locale settings
9288
[CA1305] The behavior of 'int.ToString()' could vary based on the current user's locale settings
@@ -115,7 +111,7 @@
115111
116112
[SYSLIB0012] 'Assembly.CodeBase' is obsolete
117113
-->
118-
<NoWarn>$(NoWarn);IDE0005;IDE0008;IDE0011;IDE0017;IDE0019;IDE0021;IDE0022;IDE0025;IDE0027;IDE0028;IDE0029;IDE0032;IDE0039;IDE0040;IDE0044;IDE0045;IDE0046;IDE0055;IDE0056;IDE0057;IDE0059;IDE0060;IDE0061;IDE0063;IDE0074;IDE0078;IDE0083;IDE0090;IDE0100;IDE0130;IDE0160;IDE0200;IDE0260;IDE0270;IDE0290;IDE0300;IDE0305;IDE0301;IDE0330;IDE1005</NoWarn>
114+
<NoWarn>$(NoWarn);IDE0005;IDE0008;IDE0011;IDE0017;IDE0019;IDE0021;IDE0022;IDE0025;IDE0027;IDE0028;IDE0029;IDE0032;IDE0039;IDE0045;IDE0046;IDE0055;IDE0056;IDE0057;IDE0059;IDE0060;IDE0061;IDE0063;IDE0074;IDE0078;IDE0083;IDE0090;IDE0130;IDE0160;IDE0200;IDE0260;IDE0270;IDE0290;IDE0300;IDE0305;IDE0301;IDE0330</NoWarn>
119115
<NoWarn>$(NoWarn);CA1200;CA1304;CA1305;CA1310;CA1311;CA1507;CA1510;CA1514;CA1710;CA1716;CA1720;CA1725;CA1805;CA1827;CA1834;CA1845;CA1847;CA1854;CA1861;CA1862;CA1865;CA1866;CA1870;CA2249;CA2263;SYSLIB0012</NoWarn>
120116
</PropertyGroup>
121117

src/NSwag.AspNetCore.Launcher/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ internal sealed class Program
6060
["System.Text.Encodings.Web"] = new AssemblyLoadInfo(new Version(4, 0, 0)),
6161
};
6262

63-
static int Main(string[] args)
63+
private static int Main(string[] args)
6464
{
6565
// Usage: NSwag.Console.AspNetCore [settingsFile] [toolsDirectory]
6666
if (args.Length < 2)

src/NSwag.CodeGeneration.CSharp/Models/CSharpFileTemplateModel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ public CSharpFileTemplateModel(
103103
/// <summary>Gets a value indicating whether [generate file response class].</summary>
104104
public bool GenerateFileResponseClass =>
105105
_settings.CSharpGeneratorSettings.ExcludedTypeNames?.Contains("FileResponse") != true &&
106-
_document.Operations.Any(o => o.Operation.ActualResponses.Any(r => r.Value.IsBinary(o.Operation) == true));
106+
_document.Operations.Any(o => o.Operation.ActualResponses.Any(r => r.Value.IsBinary(o.Operation)));
107107

108108
/// <summary>Gets or sets a value indicating whether to generate exception classes (default: true).</summary>
109109
public bool GenerateExceptionClasses => (_settings as CSharpClientGeneratorSettings)?.GenerateExceptionClasses == true;

src/NSwag.CodeGeneration.TypeScript/Models/TypeScriptParameterModel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public string TypePostfix
5050
{
5151
if (_settings.TypeScriptGeneratorSettings.SupportsStrictNullChecks)
5252
{
53-
return (IsNullable == true ? " | null" : "") + (IsRequired == false ? " | undefined" : "");
53+
return (IsNullable ? " | null" : "") + (!IsRequired ? " | undefined" : "");
5454
}
5555
else
5656
{

src/NSwag.CodeGeneration/Models/OperationModelBase.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public string UnwrappedResultType
111111
return "void";
112112
}
113113

114-
if (response.Value.IsBinary(_operation) == true)
114+
if (response.Value.IsBinary(_operation))
115115
{
116116
return _generator.GetBinaryResponseTypeName();
117117
}
@@ -334,7 +334,7 @@ protected virtual string ResolveParameterType(OpenApiParameter parameter)
334334
}
335335

336336
var typeNameHint = !schema.HasTypeNameTitle ? ConversionUtilities.ConvertToUpperCamelCase(parameter.Name, true) : null;
337-
var isNullable = parameter.IsRequired == false || parameter.IsNullable(_settings.CodeGeneratorSettings.SchemaType);
337+
var isNullable = !parameter.IsRequired || parameter.IsNullable(_settings.CodeGeneratorSettings.SchemaType);
338338
return _resolver.Resolve(schema, isNullable, typeNameHint);
339339
}
340340

src/NSwag.CodeGeneration/Models/ParameterModelBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ public IEnumerable<PropertyModel> CollectionPropertyNames
132132
public bool IsNullable => _parameter.IsNullable(_settings.SchemaType);
133133

134134
/// <summary>Gets a value indicating whether the parameter is optional (i.e. not required).</summary>
135-
public bool IsOptional => _parameter.IsRequired == false;
135+
public bool IsOptional => !_parameter.IsRequired;
136136

137137
/// <summary>Gets a value indicating whether the parameter has a description or is optional.</summary>
138138
public bool HasDescriptionOrIsOptional => HasDescription || !IsRequired;

src/NSwag.Commands/Commands/OutputCommandExtensions.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,20 @@ public static Task<bool> TryWriteFileOutputAsync(this IOutputCommand command, st
3030
if (!string.IsNullOrEmpty(path))
3131
{
3232
var directory = Path.GetDirectoryName(path);
33-
if (!string.IsNullOrEmpty(directory) && Directory.Exists(directory) == false)
33+
if (!string.IsNullOrEmpty(directory) && !Directory.Exists(directory))
3434
{
3535
Directory.CreateDirectory(directory);
3636
}
3737

3838
var data = generator();
3939

4040
data = data?.Replace("\r", "") ?? "";
41-
data = newLineBehavior == NewLineBehavior.Auto ? data.Replace("\n", Environment.NewLine) :
42-
newLineBehavior == NewLineBehavior.CRLF ? data.Replace("\n", "\r\n") : data;
41+
data = newLineBehavior switch
42+
{
43+
NewLineBehavior.Auto => data.Replace("\n", Environment.NewLine),
44+
NewLineBehavior.CRLF => data.Replace("\n", "\r\n"),
45+
_ => data
46+
};
4347

4448
if (!File.Exists(path) || File.ReadAllText(path) != data)
4549
{

src/NSwag.Commands/HostFactoryResolver.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,8 @@ private sealed class HostingListener : IObserver<DiagnosticListener>, IObserver<
172172

173173
private readonly TaskCompletionSource<object> _hostTcs = new TaskCompletionSource<object>();
174174
private IDisposable _disposable;
175-
private Action<object> _configure;
176-
private Action<Exception> _entrypointCompleted;
175+
private readonly Action<object> _configure;
176+
private readonly Action<Exception> _entrypointCompleted;
177177
private static readonly AsyncLocal<HostingListener> _currentListener = new AsyncLocal<HostingListener>();
178178

179179
public HostingListener(string[] args, MethodInfo entryPoint, TimeSpan waitTimeout, bool stopApplication, Action<object> configure, Action<Exception> entrypointCompleted)

src/NSwag.Core.Tests/DocumentLoadingTests.cs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class DocumentLoadingTests
1313
public async Task When_document_contains_readOnly_properties_then_they_are_correctly_loaded()
1414
{
1515
// Arrange
16-
var json = _sampleServiceCode;
16+
var json = SampleServiceCode;
1717

1818
// Act
1919
var document = await OpenApiDocument.FromJsonAsync(json);
@@ -33,7 +33,7 @@ public async Task When_document_contains_readOnly_properties_then_they_are_corre
3333
public async Task When_generating_operation_ids_then_missing_ids_are_generated()
3434
{
3535
// Arrange
36-
var json = _sampleServiceCode;
36+
var json = SampleServiceCode;
3737

3838
// Act
3939
var document = await OpenApiDocument.FromJsonAsync(json);
@@ -47,7 +47,7 @@ public async Task When_generating_operation_ids_then_missing_ids_are_generated()
4747
public async Task When_json_has_extension_data_then_it_is_loaded()
4848
{
4949
// Arrange
50-
var json = _jsonVendorExtensionData;
50+
var json = JsonVendorExtensionData;
5151

5252
// Act
5353
var document = await OpenApiDocument.FromJsonAsync(json);
@@ -68,7 +68,7 @@ public async Task When_locale_is_not_english_then_types_are_correctly_serialized
6868
CultureInfo.DefaultThreadCurrentCulture = ci;
6969

7070
// Act
71-
var json = _sampleServiceCode;
71+
var json = SampleServiceCode;
7272

7373
// Act
7474
var document = await OpenApiDocument.FromJsonAsync(json);
@@ -78,8 +78,7 @@ public async Task When_locale_is_not_english_then_types_are_correctly_serialized
7878
Assert.Equal(JsonObjectType.Integer, document.Definitions["Pet"].Properties["id"].Type);
7979
}
8080

81-
private string _sampleServiceCode =
82-
@"{
81+
private const string SampleServiceCode = @"{
8382
""swagger"": ""2.0"",
8483
""info"": {
8584
""version"": ""1.0.0"",
@@ -143,8 +142,7 @@ public async Task When_locale_is_not_english_then_types_are_correctly_serialized
143142
}
144143
}";
145144

146-
private string _jsonVendorExtensionData =
147-
@"{
145+
private const string JsonVendorExtensionData = @"{
148146
""swagger"": ""2.0"",
149147
""info"": {
150148
""title"": ""Swagger Test Sample"",

src/NSwag.Core/Collections/ObservableDictionary.cs

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -131,22 +131,14 @@ protected virtual void Insert(TKey key, TValue value, bool add)
131131
/// <param name="propertyName">Name of the property.</param>
132132
protected virtual void OnPropertyChanged(string propertyName)
133133
{
134-
var copy = PropertyChanged;
135-
if (copy != null)
136-
{
137-
copy(this, new PropertyChangedEventArgs(propertyName));
138-
}
134+
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
139135
}
140136

141137
/// <summary>Called when the collection has changed.</summary>
142138
protected void OnCollectionChanged()
143139
{
144140
OnPropertyChanged();
145-
var copy = CollectionChanged;
146-
if (copy != null)
147-
{
148-
copy(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
149-
}
141+
CollectionChanged?.Invoke(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
150142
}
151143

152144
/// <summary>Called when the collection has changed.</summary>
@@ -155,11 +147,7 @@ protected void OnCollectionChanged()
155147
protected void OnCollectionChanged(NotifyCollectionChangedAction action, KeyValuePair<TKey, TValue> changedItem)
156148
{
157149
OnPropertyChanged();
158-
var copy = CollectionChanged;
159-
if (copy != null)
160-
{
161-
copy(this, new NotifyCollectionChangedEventArgs(action, changedItem, 0));
162-
}
150+
CollectionChanged?.Invoke(this, new NotifyCollectionChangedEventArgs(action, changedItem, 0));
163151
}
164152

165153
/// <summary>Called when the collection has changed.</summary>
@@ -169,11 +157,7 @@ protected void OnCollectionChanged(NotifyCollectionChangedAction action, KeyValu
169157
protected void OnCollectionChanged(NotifyCollectionChangedAction action, KeyValuePair<TKey, TValue> newItem, KeyValuePair<TKey, TValue> oldItem)
170158
{
171159
OnPropertyChanged();
172-
var copy = CollectionChanged;
173-
if (copy != null)
174-
{
175-
copy(this, new NotifyCollectionChangedEventArgs(action, newItem, oldItem, 0));
176-
}
160+
CollectionChanged?.Invoke(this, new NotifyCollectionChangedEventArgs(action, newItem, oldItem, 0));
177161
}
178162

179163
/// <summary>Called when the collection has changed.</summary>
@@ -182,11 +166,7 @@ protected void OnCollectionChanged(NotifyCollectionChangedAction action, KeyValu
182166
protected void OnCollectionChanged(NotifyCollectionChangedAction action, IList newItems)
183167
{
184168
OnPropertyChanged();
185-
var copy = CollectionChanged;
186-
if (copy != null)
187-
{
188-
copy(this, new NotifyCollectionChangedEventArgs(action, newItems, 0));
189-
}
169+
CollectionChanged?.Invoke(this, new NotifyCollectionChangedEventArgs(action, newItems, 0));
190170
}
191171

192172
private void OnPropertyChanged()

0 commit comments

Comments
 (0)