Skip to content

Commit

Permalink
Naming convention pass 4/n + Descriptor serialization (#137)
Browse files Browse the repository at this point in the history
  • Loading branch information
Marco Trivellato authored and GitHub Enterprise committed Nov 15, 2023
1 parent 8e8dcc1 commit c230dd9
Show file tree
Hide file tree
Showing 67 changed files with 2,147 additions and 2,092 deletions.
2,986 changes: 1,492 additions & 1,494 deletions Data/ApiDatabase.json

Large diffs are not rendered by default.

242 changes: 120 additions & 122 deletions Data/ProjectSettings.json

Large diffs are not rendered by default.

36 changes: 17 additions & 19 deletions Data/RoslynTextLookup.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
{
"Items": [
{
"id": "UDR0001",
"description": "This script contains static fields, but does not contain a method with the <b>[RuntimeInitializeOnLoadMethod]</b> attribute. If Domain Reload is disabled to facilitate faster entering/exiting Play Mode, the values in the static fields will not be reset.",
"solution": "Create a method with a <b>[RuntimeInitializeOnLoadMethod]</b> attribute and ensure that all static variables are assigned values in this method."
},
{
"id": "UDR0002",
"description": "This script contains one or more static fields and a method with the <b>[RuntimeInitializeOnLoadMethod]</b> attribute, but the method does not set the value of the field(s). If Domain Reload is disabled to facilitate faster entering/exiting Play Mode, the values in the static fields will not be reset.",
"solution": "Ensure that the method with a <b>[RuntimeInitializeOnLoadMethod]</b> attribute assigns values to all static variables declared in this class."
},
{
"id": "UDR0003",
"description": "This script contains one or more static events, but not all of the delegates are subscribed to the event are explicitly unsubscribed when leaving Play Mode. If Domain Reload is disabled to facilitate faster entering/exiting Play Mode, the event may end up with multiple duplicate registered delegates.",
"solution": "Unsubscribe callbacks from static events when they are no longer needed. This could be in a <b>MonoBehaviour.OnApplicationQuit()</b> or <b>MonoBehaviour.OnDestroy()</b> method, for example."
}
]
}
[
{
"id": "UDR0001",
"description": "This script contains static fields, but does not contain a method with the <b>[RuntimeInitializeOnLoadMethod]</b> attribute. If Domain Reload is disabled to facilitate faster entering/exiting Play Mode, the values in the static fields will not be reset.",
"solution": "Create a method with a <b>[RuntimeInitializeOnLoadMethod]</b> attribute and ensure that all static variables are assigned values in this method."
},
{
"id": "UDR0002",
"description": "This script contains one or more static fields and a method with the <b>[RuntimeInitializeOnLoadMethod]</b> attribute, but the method does not set the value of the field(s). If Domain Reload is disabled to facilitate faster entering/exiting Play Mode, the values in the static fields will not be reset.",
"solution": "Ensure that the method with a <b>[RuntimeInitializeOnLoadMethod]</b> attribute assigns values to all static variables declared in this class."
},
{
"id": "UDR0003",
"description": "This script contains one or more static events, but not all of the delegates are subscribed to the event are explicitly unsubscribed when leaving Play Mode. If Domain Reload is disabled to facilitate faster entering/exiting Play Mode, the event may end up with multiple duplicate registered delegates.",
"solution": "Unsubscribe callbacks from static events when they are no longer needed. This could be in a <b>MonoBehaviour.OnApplicationQuit()</b> or <b>MonoBehaviour.OnDestroy()</b> method, for example."
}
]
4 changes: 2 additions & 2 deletions Editor/Core/AnalysisContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ public bool IsDescriptorEnabled(Descriptor descriptor)
if (!descriptor.IsApplicable(Params))
return false;

var rule = Params.Rules.GetRule(descriptor.id);
var rule = Params.Rules.GetRule(descriptor.Id);
if (rule != null)
return rule.severity != Severity.None;

return descriptor.isEnabledByDefault;
return descriptor.IsEnabledByDefault;
}
}
}
6 changes: 3 additions & 3 deletions Editor/Core/Module.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,10 @@ public void RegisterDescriptor(Descriptor descriptor)
if (!descriptor.IsVersionCompatible())
return;

DescriptorLibrary.RegisterDescriptor(descriptor.id, descriptor);
DescriptorLibrary.RegisterDescriptor(descriptor.Id, descriptor);

if (!m_Ids.Add(descriptor.id))
throw new Exception("Duplicate descriptor with id: " + descriptor.id);
if (!m_Ids.Add(descriptor.Id))
throw new Exception("Duplicate descriptor with id: " + descriptor.Id);
}

public bool SupportsDescriptor(DescriptorID id)
Expand Down
77 changes: 28 additions & 49 deletions Editor/Diagnostic/Descriptor.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using System.Linq;
using System.Runtime.CompilerServices;
using Newtonsoft.Json;
using UnityEngine.Serialization;

namespace Unity.ProjectAuditor.Editor.Diagnostic
{
Expand All @@ -14,80 +14,62 @@ public sealed class Descriptor : IEquatable<Descriptor>
/// <summary>
/// An unique identifier for the diagnostic. IDs must have exactly 3 upper case characters, followed by 4 digits
/// </summary>
[JsonRequired]
public string id;
public string Id;

/// <summary>
/// Diagnostic title
/// </summary>
[JsonRequired]
public string title;
public string Title;

/// <summary>
/// Message used to describe a specific instance of the diagnostic
/// </summary>
[JsonIgnore]
public string messageFormat;
public string MessageFormat;

/// <summary>
/// Default severity of the diagnostic
/// </summary>
[JsonIgnore]
public Severity defaultSeverity;
public Severity DefaultSeverity;

/// <summary>
/// Returns true if the diagnostic is enabled by default.
/// </summary>
public bool isEnabledByDefault = true;
public bool IsEnabledByDefault = true;

/// <summary>
/// Affected areas
/// </summary>
[JsonRequired]
public string[] areas;
public string[] Areas;

/// <summary>
/// Affected platforms. If null, the diagnostic applies to all platforms
/// </summary>
[JsonProperty]
public string[] platforms;
public string[] Platforms;

/// <summary>
/// Description of the diagnostic
/// </summary>
[JsonRequired]
public string description;
public string Description;

/// <summary>
/// Recommendation to fix the diagnostic
/// </summary>
[JsonRequired]
public string solution;
public string Solution;

/// <summary>
/// Url to documentation
/// </summary>
[JsonIgnore]
public string documentationUrl;

[JsonProperty("documentationUrl")]
internal string documentationUrlForJson
{
get => string.IsNullOrEmpty(documentationUrl) ? null : documentationUrl;
set => documentationUrl = string.IsNullOrEmpty(value) ? String.Empty : value;
}
public string DocumentationUrl;

/// <summary>
/// Minimum Unity version this diagnostic applies to. If not specified, the diagnostic applies to all versions
/// </summary>
[JsonIgnore]
public string minimumVersion;
public string MinimumVersion;

/// <summary>
/// Maximum Unity version this diagnostic applies to. If not specified, the diagnostic applies to all versions
/// </summary>
[JsonIgnore]
public string maximumVersion;
public string MaximumVersion;

/// <summary>
/// Optional Auto-fixer
Expand All @@ -98,20 +80,17 @@ internal string documentationUrlForJson
/// <summary>
/// Name of the type (namespace and class/struct) of a known code API issue
/// </summary>
[JsonIgnore]
public string type;
public string Type;

/// <summary>
/// Name of the method of a known code API issue
/// </summary>
[JsonIgnore]
public string method;
public string Method;

/// <summary>
/// The evaluated value of a know code API issue
/// </summary>
[JsonIgnore]
public string value;
public string Value;

[JsonConstructor]
internal Descriptor()
Expand All @@ -129,16 +108,16 @@ internal Descriptor()
/// <param name="solution">Advice on how to resolve the issue.</param>
public Descriptor(string id, string title, string[] areas, string description, string solution)
{
this.id = id;
this.title = title;
this.areas = areas;
this.messageFormat = string.Empty;
this.description = description;
this.solution = solution;

type = string.Empty;
method = string.Empty;
defaultSeverity = Severity.Moderate;
Id = id;
Title = title;
Areas = areas;
MessageFormat = string.Empty;
Description = description;
Solution = solution;

Type = string.Empty;
Method = string.Empty;
DefaultSeverity = Severity.Moderate;
}

/// <summary>
Expand Down Expand Up @@ -174,7 +153,7 @@ public bool Equals(Descriptor other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return id == other.id;
return Id == other.Id;
}

/// <summary>Returns true if the Descriptor is equal to a given object, false otherwise.</summary>
Expand Down Expand Up @@ -202,7 +181,7 @@ internal void Fix(ProjectIssue issue)
/// <returns>The computed hash code.</returns>
public override int GetHashCode()
{
return id.GetHashCode();
return Id.GetHashCode();
}
}
}
32 changes: 16 additions & 16 deletions Editor/Diagnostic/DescriptorExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,32 @@ internal static class DescriptorExtensions
{
public static Area[] GetAreas(this Descriptor descriptor)
{
return descriptor.areas.Select(a => (Area)Enum.Parse(typeof(Area), a)).ToArray();
return descriptor.Areas.Select(a => (Area)Enum.Parse(typeof(Area), a)).ToArray();
}

public static string GetAreasSummary(this Descriptor descriptor)
{
return Formatting.CombineStrings(descriptor.areas);
return Formatting.CombineStrings(descriptor.Areas);
}

public static string GetPlatformsSummary(this Descriptor descriptor)
{
return (descriptor.platforms == null || descriptor.platforms.Length == 0) ? "Any" : Formatting.CombineStrings(descriptor.platforms);
return (descriptor.Platforms == null || descriptor.Platforms.Length == 0) ? "Any" : Formatting.CombineStrings(descriptor.Platforms);
}

public static string GetFullTypeName(this Descriptor descriptor)
{
return descriptor.type + "." + descriptor.method;
return descriptor.Type + "." + descriptor.Method;
}

/// <summary>
/// Check if the descriptor applies to the given platform
/// </summary>
public static bool IsPlatformCompatible(this Descriptor descriptor, BuildTarget buildTarget)
{
if (descriptor.platforms == null || descriptor.platforms.Length == 0)
if (descriptor.Platforms == null || descriptor.Platforms.Length == 0)
return true;
return descriptor.platforms.Contains(buildTarget.ToString());
return descriptor.Platforms.Contains(buildTarget.ToString());
}

public static bool IsApplicable(this Descriptor desc, AnalysisParams analysisParams)
Expand All @@ -49,7 +49,7 @@ public static bool IsApplicable(this Descriptor desc, AnalysisParams analysisPar
/// </summary>
public static bool IsPlatformSupported(this Descriptor desc)
{
var platforms = desc.platforms;
var platforms = desc.Platforms;
if (platforms == null)
return true;
foreach (var platform in platforms)
Expand All @@ -68,9 +68,9 @@ public static bool IsPlatformSupported(this Descriptor desc)
/// </summary>
public static bool IsPlatformSpecific(this Descriptor descriptor, BuildTarget buildTarget)
{
if (descriptor.platforms == null || descriptor.platforms.Length != 1)
if (descriptor.Platforms == null || descriptor.Platforms.Length != 1)
return false;
return descriptor.platforms[0].Equals(buildTarget.ToString());
return descriptor.Platforms[0].Equals(buildTarget.ToString());
}

static Version s_UnityVersion = (Version)null;
Expand All @@ -91,33 +91,33 @@ public static bool IsVersionCompatible(this Descriptor desc)
var minimumVersion = (Version)null;
var maximumVersion = (Version)null;

if (!string.IsNullOrEmpty(desc.minimumVersion))
if (!string.IsNullOrEmpty(desc.MinimumVersion))
{
try
{
minimumVersion = new Version(desc.minimumVersion);
minimumVersion = new Version(desc.MinimumVersion);
}
catch (Exception exception)
{
Debug.LogErrorFormat("Descriptor ({0}) minimumVersion ({1}) is invalid. Exception: {2}", desc.id, desc.minimumVersion, exception.Message);
Debug.LogErrorFormat("Descriptor ({0}) minimumVersion ({1}) is invalid. Exception: {2}", desc.Id, desc.MinimumVersion, exception.Message);
}
}

if (!string.IsNullOrEmpty(desc.maximumVersion))
if (!string.IsNullOrEmpty(desc.MaximumVersion))
{
try
{
maximumVersion = new Version(desc.maximumVersion);
maximumVersion = new Version(desc.MaximumVersion);
}
catch (Exception exception)
{
Debug.LogErrorFormat("Descriptor ({0}) maximumVersion ({1}) is invalid. Exception: {2}", desc.id, desc.maximumVersion, exception.Message);
Debug.LogErrorFormat("Descriptor ({0}) maximumVersion ({1}) is invalid. Exception: {2}", desc.Id, desc.MaximumVersion, exception.Message);
}
}

if (minimumVersion != null && maximumVersion != null && minimumVersion > maximumVersion)
{
Debug.LogErrorFormat("Descriptor ({0}) minimumVersion ({1}) is greater than maximumVersion ({2}).", desc.id, minimumVersion, maximumVersion);
Debug.LogErrorFormat("Descriptor ({0}) minimumVersion ({1}) is greater than maximumVersion ({2}).", desc.Id, minimumVersion, maximumVersion);
return false;
}

Expand Down
Loading

0 comments on commit c230dd9

Please sign in to comment.