Skip to content

Commit f7daa86

Browse files
authored
Merge pull request github#14318 from michaelnebel/csharp/readonlyfieldsinstubs
C#: Add readonly modifier to fields and constant values.
2 parents 539199b + 5e3ae0a commit f7daa86

File tree

68 files changed

+1126
-1087
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+1126
-1087
lines changed

csharp/extractor/Semmle.Extraction.CSharp.StubGenerator/StubVisitor.cs

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,35 @@ private static bool IsUnsafe(ITypeSymbol symbol) =>
296296
private static string EscapeIdentifier(string identifier) =>
297297
keywords.Contains(identifier) ? "@" + identifier : identifier;
298298

299+
private static bool TryGetConstantValue(IFieldSymbol symbol, out string value)
300+
{
301+
value = "";
302+
if (!symbol.HasConstantValue)
303+
{
304+
return false;
305+
}
306+
307+
var v = symbol.ConstantValue;
308+
switch (symbol.Type.SpecialType)
309+
{
310+
case SpecialType.System_Boolean:
311+
value = (bool)v ? "true" : "false";
312+
return true;
313+
case SpecialType.System_Byte:
314+
case SpecialType.System_SByte:
315+
case SpecialType.System_UInt16:
316+
case SpecialType.System_UInt32:
317+
case SpecialType.System_UInt64:
318+
case SpecialType.System_Int16:
319+
case SpecialType.System_Int32:
320+
case SpecialType.System_Int64:
321+
value = v.ToString()!;
322+
return true;
323+
default:
324+
return false;
325+
}
326+
}
327+
299328
public override void VisitField(IFieldSymbol symbol)
300329
{
301330
if (IsNotPublic(symbol.DeclaredAccessibility))
@@ -306,7 +335,14 @@ public override void VisitField(IFieldSymbol symbol)
306335
StubModifiers(symbol);
307336

308337
if (symbol.IsConst)
338+
{
309339
stubWriter.Write("const ");
340+
}
341+
342+
if (!symbol.IsConst && symbol.IsReadOnly)
343+
{
344+
stubWriter.Write("readonly ");
345+
}
310346

311347
if (IsUnsafe(symbol.Type))
312348
{
@@ -317,7 +353,10 @@ public override void VisitField(IFieldSymbol symbol)
317353
stubWriter.Write(" ");
318354
stubWriter.Write(EscapeIdentifier(symbol.Name));
319355
if (symbol.IsConst)
320-
stubWriter.Write(" = default");
356+
{
357+
var v = TryGetConstantValue(symbol, out var value) ? value : "default";
358+
stubWriter.Write($" = {v}");
359+
}
321360
stubWriter.WriteLine(";");
322361
}
323362

csharp/ql/test/resources/stubs/_frameworks/Microsoft.AspNetCore.App/Microsoft.AspNetCore.Antiforgery.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public class AntiforgeryOptions
1010
{
1111
public Microsoft.AspNetCore.Http.CookieBuilder Cookie { get => throw null; set { } }
1212
public AntiforgeryOptions() => throw null;
13-
public static string DefaultCookiePrefix;
13+
public static readonly string DefaultCookiePrefix;
1414
public string FormFieldName { get => throw null; set { } }
1515
public string HeaderName { get => throw null; set { } }
1616
public bool SuppressXFrameOptionsHeader { get => throw null; set { } }

csharp/ql/test/resources/stubs/_frameworks/Microsoft.AspNetCore.App/Microsoft.AspNetCore.Authentication.Cookies.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,19 @@ public class ChunkingCookieManager : Microsoft.AspNetCore.Authentication.Cookies
1313
public void AppendResponseCookie(Microsoft.AspNetCore.Http.HttpContext context, string key, string value, Microsoft.AspNetCore.Http.CookieOptions options) => throw null;
1414
public int? ChunkSize { get => throw null; set { } }
1515
public ChunkingCookieManager() => throw null;
16-
public const int DefaultChunkSize = default;
16+
public const int DefaultChunkSize = 4050;
1717
public void DeleteCookie(Microsoft.AspNetCore.Http.HttpContext context, string key, Microsoft.AspNetCore.Http.CookieOptions options) => throw null;
1818
public string GetRequestCookie(Microsoft.AspNetCore.Http.HttpContext context, string key) => throw null;
1919
public bool ThrowForPartialCookies { get => throw null; set { } }
2020
}
2121
public static class CookieAuthenticationDefaults
2222
{
23-
public static Microsoft.AspNetCore.Http.PathString AccessDeniedPath;
23+
public static readonly Microsoft.AspNetCore.Http.PathString AccessDeniedPath;
2424
public const string AuthenticationScheme = default;
25-
public static string CookiePrefix;
26-
public static Microsoft.AspNetCore.Http.PathString LoginPath;
27-
public static Microsoft.AspNetCore.Http.PathString LogoutPath;
28-
public static string ReturnUrlParameter;
25+
public static readonly string CookiePrefix;
26+
public static readonly Microsoft.AspNetCore.Http.PathString LoginPath;
27+
public static readonly Microsoft.AspNetCore.Http.PathString LogoutPath;
28+
public static readonly string ReturnUrlParameter;
2929
}
3030
public class CookieAuthenticationEvents
3131
{

csharp/ql/test/resources/stubs/_frameworks/Microsoft.AspNetCore.App/Microsoft.AspNetCore.Authentication.OAuth.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public class OAuthChallengeProperties : Microsoft.AspNetCore.Authentication.Auth
7474
public OAuthChallengeProperties(System.Collections.Generic.IDictionary<string, string> items) => throw null;
7575
public OAuthChallengeProperties(System.Collections.Generic.IDictionary<string, string> items, System.Collections.Generic.IDictionary<string, object> parameters) => throw null;
7676
public System.Collections.Generic.ICollection<string> Scope { get => throw null; set { } }
77-
public static string ScopeKey;
77+
public static readonly string ScopeKey;
7878
public virtual void SetScope(params string[] scopes) => throw null;
7979
}
8080
public class OAuthCodeExchangeContext
@@ -86,10 +86,10 @@ public class OAuthCodeExchangeContext
8686
}
8787
public static class OAuthConstants
8888
{
89-
public static string CodeChallengeKey;
90-
public static string CodeChallengeMethodKey;
91-
public static string CodeChallengeMethodS256;
92-
public static string CodeVerifierKey;
89+
public static readonly string CodeChallengeKey;
90+
public static readonly string CodeChallengeMethodKey;
91+
public static readonly string CodeChallengeMethodS256;
92+
public static readonly string CodeVerifierKey;
9393
}
9494
public class OAuthCreatingTicketContext : Microsoft.AspNetCore.Authentication.ResultContext<Microsoft.AspNetCore.Authentication.OAuth.OAuthOptions>
9595
{
@@ -107,7 +107,7 @@ public class OAuthCreatingTicketContext : Microsoft.AspNetCore.Authentication.Re
107107
}
108108
public static class OAuthDefaults
109109
{
110-
public static string DisplayName;
110+
public static readonly string DisplayName;
111111
}
112112
public class OAuthEvents : Microsoft.AspNetCore.Authentication.RemoteAuthenticationEvents
113113
{

csharp/ql/test/resources/stubs/_frameworks/Microsoft.AspNetCore.App/Microsoft.AspNetCore.Components.Forms.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,12 @@ public sealed class ValidationMessageStore
8181
public sealed class ValidationRequestedEventArgs : System.EventArgs
8282
{
8383
public ValidationRequestedEventArgs() => throw null;
84-
public static Microsoft.AspNetCore.Components.Forms.ValidationRequestedEventArgs Empty;
84+
public static readonly Microsoft.AspNetCore.Components.Forms.ValidationRequestedEventArgs Empty;
8585
}
8686
public sealed class ValidationStateChangedEventArgs : System.EventArgs
8787
{
8888
public ValidationStateChangedEventArgs() => throw null;
89-
public static Microsoft.AspNetCore.Components.Forms.ValidationStateChangedEventArgs Empty;
89+
public static readonly Microsoft.AspNetCore.Components.Forms.ValidationStateChangedEventArgs Empty;
9090
}
9191
}
9292
}

csharp/ql/test/resources/stubs/_frameworks/Microsoft.AspNetCore.App/Microsoft.AspNetCore.Components.cs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -189,16 +189,16 @@ public abstract class ErrorBoundaryBase : Microsoft.AspNetCore.Components.Compon
189189
public struct EventCallback
190190
{
191191
public EventCallback(Microsoft.AspNetCore.Components.IHandleEvent receiver, System.MulticastDelegate @delegate) => throw null;
192-
public static Microsoft.AspNetCore.Components.EventCallback Empty;
193-
public static Microsoft.AspNetCore.Components.EventCallbackFactory Factory;
192+
public static readonly Microsoft.AspNetCore.Components.EventCallback Empty;
193+
public static readonly Microsoft.AspNetCore.Components.EventCallbackFactory Factory;
194194
public bool HasDelegate { get => throw null; }
195195
public System.Threading.Tasks.Task InvokeAsync(object arg) => throw null;
196196
public System.Threading.Tasks.Task InvokeAsync() => throw null;
197197
}
198198
public struct EventCallback<TValue>
199199
{
200200
public EventCallback(Microsoft.AspNetCore.Components.IHandleEvent receiver, System.MulticastDelegate @delegate) => throw null;
201-
public static Microsoft.AspNetCore.Components.EventCallback<TValue> Empty;
201+
public static readonly Microsoft.AspNetCore.Components.EventCallback<TValue> Empty;
202202
public bool HasDelegate { get => throw null; }
203203
public System.Threading.Tasks.Task InvokeAsync(TValue arg) => throw null;
204204
public System.Threading.Tasks.Task InvokeAsync() => throw null;
@@ -297,7 +297,7 @@ public static partial class EventCallbackFactoryEventArgsExtensions
297297
public struct EventCallbackWorkItem
298298
{
299299
public EventCallbackWorkItem(System.MulticastDelegate @delegate) => throw null;
300-
public static Microsoft.AspNetCore.Components.EventCallbackWorkItem Empty;
300+
public static readonly Microsoft.AspNetCore.Components.EventCallbackWorkItem Empty;
301301
public System.Threading.Tasks.Task InvokeAsync(object arg) => throw null;
302302
}
303303
public sealed class EventHandlerAttribute : System.Attribute
@@ -543,9 +543,9 @@ public struct ArrayBuilderSegment<T> : System.Collections.Generic.IEnumerable<T>
543543
}
544544
public struct ArrayRange<T>
545545
{
546-
public T[] Array;
546+
public readonly T[] Array;
547547
public Microsoft.AspNetCore.Components.RenderTree.ArrayRange<T> Clone() => throw null;
548-
public int Count;
548+
public readonly int Count;
549549
public ArrayRange(T[] array, int count) => throw null;
550550
}
551551
public class EventFieldInfo
@@ -585,16 +585,16 @@ public abstract class Renderer : System.IAsyncDisposable, System.IDisposable
585585
}
586586
public struct RenderTreeDiff
587587
{
588-
public int ComponentId;
589-
public Microsoft.AspNetCore.Components.RenderTree.ArrayBuilderSegment<Microsoft.AspNetCore.Components.RenderTree.RenderTreeEdit> Edits;
588+
public readonly int ComponentId;
589+
public readonly Microsoft.AspNetCore.Components.RenderTree.ArrayBuilderSegment<Microsoft.AspNetCore.Components.RenderTree.RenderTreeEdit> Edits;
590590
}
591591
public struct RenderTreeEdit
592592
{
593-
public int MoveToSiblingIndex;
594-
public int ReferenceFrameIndex;
595-
public string RemovedAttributeName;
596-
public int SiblingIndex;
597-
public Microsoft.AspNetCore.Components.RenderTree.RenderTreeEditType Type;
593+
public readonly int MoveToSiblingIndex;
594+
public readonly int ReferenceFrameIndex;
595+
public readonly string RemovedAttributeName;
596+
public readonly int SiblingIndex;
597+
public readonly Microsoft.AspNetCore.Components.RenderTree.RenderTreeEditType Type;
598598
}
599599
public enum RenderTreeEditType
600600
{

csharp/ql/test/resources/stubs/_frameworks/Microsoft.AspNetCore.App/Microsoft.AspNetCore.Cors.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,17 @@ namespace Infrastructure
3939
{
4040
public static class CorsConstants
4141
{
42-
public static string AccessControlAllowCredentials;
43-
public static string AccessControlAllowHeaders;
44-
public static string AccessControlAllowMethods;
45-
public static string AccessControlAllowOrigin;
46-
public static string AccessControlExposeHeaders;
47-
public static string AccessControlMaxAge;
48-
public static string AccessControlRequestHeaders;
49-
public static string AccessControlRequestMethod;
50-
public static string AnyOrigin;
51-
public static string Origin;
52-
public static string PreflightHttpMethod;
42+
public static readonly string AccessControlAllowCredentials;
43+
public static readonly string AccessControlAllowHeaders;
44+
public static readonly string AccessControlAllowMethods;
45+
public static readonly string AccessControlAllowOrigin;
46+
public static readonly string AccessControlExposeHeaders;
47+
public static readonly string AccessControlMaxAge;
48+
public static readonly string AccessControlRequestHeaders;
49+
public static readonly string AccessControlRequestMethod;
50+
public static readonly string AnyOrigin;
51+
public static readonly string Origin;
52+
public static readonly string PreflightHttpMethod;
5353
}
5454
public class CorsMiddleware
5555
{

csharp/ql/test/resources/stubs/_frameworks/Microsoft.AspNetCore.App/Microsoft.AspNetCore.Hosting.Abstractions.cs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ namespace Hosting
88
{
99
public static class EnvironmentName
1010
{
11-
public static string Development;
12-
public static string Production;
13-
public static string Staging;
11+
public static readonly string Development;
12+
public static readonly string Production;
13+
public static readonly string Staging;
1414
}
1515
public static partial class HostingAbstractionsWebHostBuilderExtensions
1616
{
@@ -106,21 +106,21 @@ public class WebHostBuilderContext
106106
}
107107
public static class WebHostDefaults
108108
{
109-
public static string ApplicationKey;
110-
public static string CaptureStartupErrorsKey;
111-
public static string ContentRootKey;
112-
public static string DetailedErrorsKey;
113-
public static string EnvironmentKey;
114-
public static string HostingStartupAssembliesKey;
115-
public static string HostingStartupExcludeAssembliesKey;
116-
public static string PreferHostingUrlsKey;
117-
public static string PreventHostingStartupKey;
118-
public static string ServerUrlsKey;
119-
public static string ShutdownTimeoutKey;
120-
public static string StartupAssemblyKey;
121-
public static string StaticWebAssetsKey;
122-
public static string SuppressStatusMessagesKey;
123-
public static string WebRootKey;
109+
public static readonly string ApplicationKey;
110+
public static readonly string CaptureStartupErrorsKey;
111+
public static readonly string ContentRootKey;
112+
public static readonly string DetailedErrorsKey;
113+
public static readonly string EnvironmentKey;
114+
public static readonly string HostingStartupAssembliesKey;
115+
public static readonly string HostingStartupExcludeAssembliesKey;
116+
public static readonly string PreferHostingUrlsKey;
117+
public static readonly string PreventHostingStartupKey;
118+
public static readonly string ServerUrlsKey;
119+
public static readonly string ShutdownTimeoutKey;
120+
public static readonly string StartupAssemblyKey;
121+
public static readonly string StaticWebAssetsKey;
122+
public static readonly string SuppressStatusMessagesKey;
123+
public static readonly string WebRootKey;
124124
}
125125
}
126126
}

csharp/ql/test/resources/stubs/_frameworks/Microsoft.AspNetCore.App/Microsoft.AspNetCore.Html.Abstractions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ public class HtmlFormattableString : Microsoft.AspNetCore.Html.IHtmlContent
4141
public class HtmlString : Microsoft.AspNetCore.Html.IHtmlContent
4242
{
4343
public HtmlString(string value) => throw null;
44-
public static Microsoft.AspNetCore.Html.HtmlString Empty;
45-
public static Microsoft.AspNetCore.Html.HtmlString NewLine;
44+
public static readonly Microsoft.AspNetCore.Html.HtmlString Empty;
45+
public static readonly Microsoft.AspNetCore.Html.HtmlString NewLine;
4646
public override string ToString() => throw null;
4747
public string Value { get => throw null; }
4848
public void WriteTo(System.IO.TextWriter writer, System.Text.Encodings.Web.HtmlEncoder encoder) => throw null;

0 commit comments

Comments
 (0)