Forked from thomaslevesque/CSharpErrorsAndWarnings.md
Created
September 25, 2020 10:22
-
-
Save ahmad-moussawi/c4920c86fe57ffa03fa58abcb663009a to your computer and use it in GitHub Desktop.
Revisions
-
thomaslevesque revised this gist
Jun 14, 2019 . No changes.There are no files selected for viewing
-
thomaslevesque revised this gist
Jun 14, 2019 . No changes.There are no files selected for viewing
-
thomaslevesque revised this gist
Jun 14, 2019 . 1 changed file with 377 additions and 189 deletions.There are no files selected for viewing
-
thomaslevesque revised this gist
Jun 14, 2019 . No changes.There are no files selected for viewing
-
thomaslevesque revised this gist
May 10, 2017 . 2 changed files with 0 additions and 151 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1237,4 +1237,3 @@ |CS9001|Error|Use of default literal is not valid in this context| |CS9002|Warning|Did you mean to use the default switch label (`default:`) rather than `case default:`? If you really mean to use the default literal, consider `case (default):` or another literal (`case 0:` or `case null:`) as appropriate.| |CS9003|Error|An expression of type '{0}' cannot be handled by a pattern of type '{1}' in C# {2}. Please use language version {3} or greater.| This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,150 +0,0 @@ -
thomaslevesque revised this gist
May 10, 2017 . 2 changed files with 1236 additions and 1236 deletions.There are no files selected for viewing
File renamed without changes. -
thomaslevesque created this gist
May 10, 2017 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,150 @@ static void Main() { var errorCodes = GetErrorCodes(); WriteMarkdownTable(errorCodes, Console.Out); } const string ErrorCodesUrl = "https://raw.githubusercontent.com/dotnet/roslyn/master/src/Compilers/CSharp/Portable/Errors/ErrorCode.cs"; const string ErrorResourcesUrl = "https://raw.githubusercontent.com/dotnet/roslyn/master/src/Compilers/CSharp/Portable/CSharpResources.resx"; private static IReadOnlyList<ErrorCode> GetErrorCodes() { using (var wc = new WebClient()) { var enumMembers = GetErrorCodeEnumMembers(wc); var dictionary = GetResourceDictionary(wc); var errorCodes = enumMembers .Select(m => ErrorCode.Parse( m, dictionary.TryGetValue(m.Identifier.ValueText, out var message) ? message : "")) .ToList(); return errorCodes; } } private static IEnumerable<EnumMemberDeclarationSyntax> GetErrorCodeEnumMembers(WebClient wc) { string errorCodesFileContent = wc.DownloadString(ErrorCodesUrl); var syntaxTree = CSharpSyntaxTree.ParseText(errorCodesFileContent); var root = syntaxTree.GetRoot(); var enumDeclaration = root.DescendantNodes() .OfType<EnumDeclarationSyntax>() .First(e => e.Identifier.ValueText == "ErrorCode"); return enumDeclaration.Members; } private static IReadOnlyDictionary<string, string> GetResourceDictionary(WebClient wc) { string resourcesFileContent = wc.DownloadString(ErrorResourcesUrl); var doc = XDocument.Parse(resourcesFileContent); var dictionary = doc.Root.Elements("data") .ToDictionary( e => e.Attribute("name").Value, e => e.Element("value").Value); return dictionary; } private static void WriteRuleSet(IEnumerable<ErrorCode> errorCodes, TextWriter writer) { var doc = new XDocument( new XElement("RuleSet", new XAttribute("Name", "C# Warnings as Errors"), new XAttribute("ToolsVersion", "15.0"), new XElement("Rules", new XAttribute("AnalyzerId", "Microsoft.CodeAnalysis.CSharp"), new XAttribute("RuleNamespace", "Microsoft.CodeAnalysis.CSharp"), from e in errorCodes where e.Level == DiagnosticLevel.Warning select new XElement("Rule", new XAttribute("Id", e.Code), new XAttribute("Action", "Error"))))); doc.Save(writer); } private static void WriteMarkdownTable(IEnumerable<ErrorCode> errorCodes, TextWriter writer) { writer.WriteLine("# All C# errors and warnings"); writer.WriteLine(); writer.WriteLine("*Parsed from the [Roslyn source code](https://github.com/dotnet/roslyn) using Roslyn.*"); writer.WriteLine(); writer.WriteLine("|Code|Level|Name|Message|"); writer.WriteLine("|----|-----|----|-------|"); foreach (var e in errorCodes) { if (e.Value == 0) continue; writer.WriteLine($"|{e.Code}|{e.Level}|{e.Name}|{e.Message}|"); } } class ErrorCode { public static ErrorCode Parse(EnumMemberDeclarationSyntax member, string message) { string name = member.Identifier.ValueText; if (name == "Void" || name == "Unknown") { return new ErrorCode(name, 0, DiagnosticLevel.Unknown, message); } else { return new ErrorCode( name.Substring(4), int.Parse(member.EqualsValue?.Value?.GetText()?.ToString() ?? "0"), ParseLevel(name.Substring(0, 3)), message); } } private ErrorCode(string name, int value, DiagnosticLevel level, string message) { Name = name; Value = value; Level = level; Message = message; } public string Name { get; } public int Value { get; } public string Code => $"CS{Value:D4}"; public DiagnosticLevel Level { get; } public string Message { get; } private static DiagnosticLevel ParseLevel(string level) { switch (level) { case "HDN": return DiagnosticLevel.Hidden; case "INF": return DiagnosticLevel.Info; case "WRN": return DiagnosticLevel.Warning; case "ERR": return DiagnosticLevel.Error; case "FTL": return DiagnosticLevel.Fatal; default: return DiagnosticLevel.Unknown; } } } enum DiagnosticLevel { Unknown, Hidden, Info, Warning, Error, Fatal }