Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[dotnet] Modernize code style in the devtools source generator #15067

Merged

Conversation

RenderMichael
Copy link
Contributor

@RenderMichael RenderMichael commented Jan 13, 2025

User description

Thanks for contributing to Selenium!
A PR well described will help maintainers to quickly review and merge it

Before submitting your PR, please check our contributing guidelines.
Avoid large PRs, help reviewers by making them as simple and short as possible.

Description

Simplifies the code using modern C# features.

Motivation and Context

Splitting this off from some devtools source generator work I'm trying to figure out.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist

  • I have read the contributing document.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

PR Type

Enhancement


Description

  • Modernized C# code style using advanced language features.

  • Replaced constructors with inline property initializations.

  • Simplified foreach loops and improved type safety.

  • Removed redundant using directives for cleaner code.


Changes walkthrough 📝

Relevant files
Enhancement
10 files
CodeGenerationDefinitionTemplateSettings.cs
Simplified property initialization for template settings.
+33/-48 
CodeGenerationSettings.cs
Replaced constructor defaults with inline initializations.
+13/-25 
CodeGeneratorBase.cs
Simplified service provider initialization.                           
+9/-10   
DomainGenerator.cs
Improved type safety in `foreach` loops.                                 
+21/-19 
ProtocolGenerator.cs
Improved type safety and simplified `foreach` loops.         
+22/-18 
TemplatesManager.cs
Improved null checks and type safety in helpers.                 
+14/-17 
CommandDefinition.cs
Replaced constructor defaults with inline initializations.
+7/-15   
DomainDefinition.cs
Replaced constructor defaults with inline initializations.
+8/-17   
EventDefinition.cs
Replaced constructor defaults with inline initializations.
+5/-10   
ProtocolDefinition.cs
Replaced constructor defaults with inline initializations.
+5/-10   
Formatting
9 files
CodeGenerationTemplateSettings.cs
Removed redundant `using` directives.                                       
+2/-2     
CodeGeneratorContext.cs
Removed redundant `using` directives.                                       
+3/-3     
CommandGenerator.cs
Removed redundant `using` directives.                                       
+5/-5     
EventGenerator.cs
Removed redundant `using` directives.                                       
+5/-5     
ICodeGenerator.cs
Removed redundant `using` directives.                                       
+3/-3     
TypeGenerator.cs
Removed redundant `using` directives.                                       
+5/-5     
Utility.cs
Removed redundant `using` directives.                                       
+5/-6     
CommandLineOptions.cs
Fixed spacing in help text for output path.                           
+1/-4     
BooleanJsonConverter.cs
Removed redundant `using` directives.                                       
+4/-4     
Bug fix
1 files
IServiceProviderExtensions.cs
Fixed type in service registration for protocol generator.
+5/-6     
Additional files
5 files
IDefinition.cs +1/-1     
ProtocolDefinitionItem.cs +3/-3     
ProtocolVersionDefinition.cs +0/-2     
TypeDefinition.cs +7/-14   
Version.cs +4/-6     

💡 PR-Agent usage: Comment /help "your question" on any pull request to receive relevant information

Copy link
Contributor

PR Reviewer Guide 🔍

Here are some key observations to aid the review process:

⏱️ Estimated effort to review: 2 🔵🔵⚪⚪⚪
🧪 No relevant tests
🔒 No security concerns identified
⚡ Recommended focus areas for review

Null Reference

The CompareTo method does not handle the case where the other parameter is null, which could lead to a NullReferenceException when calling ToString() on a null object.

    return ToString().CompareTo(other.ToString());
}

Copy link
Contributor

PR Code Suggestions ✨

Explore these optional code suggestions:

CategorySuggestion                                                                                                                                    Score
Possible issue
Add dictionary key collision detection when merging generated code from multiple sources

The code uses a foreach loop with nested foreach loops to add items to a dictionary,
which could lead to key collisions if the same key appears in different generators.
Consider adding key collision checks or using a more robust merging strategy.

third_party/dotnet/devtools/src/generator/CodeGen/DomainGenerator.cs [26-29]

 foreach (KeyValuePair<string, string> x in typeGenerator.GenerateCode(type, context))
 {
+    if (result.ContainsKey(x.Key))
+    {
+        throw new InvalidOperationException($"Duplicate key detected: {x.Key}");
+    }
     result.Add(x.Key, x.Value);
 }
  • Apply this suggestion
Suggestion importance[1-10]: 8

Why: The suggestion addresses a potential bug where key collisions in the dictionary could silently overwrite data. Adding collision detection helps maintain data integrity and makes debugging easier.

8
General
Avoid unnecessary collection conversion to improve performance

The types.Values.ToList() conversion could be inefficient for large collections and
is unnecessary since the collection is only used for template data. Consider using
the Values directly.

third_party/dotnet/devtools/src/generator/CodeGen/ProtocolGenerator.cs [82]

-types = types.Values.ToList()
+types = types.Values
  • Apply this suggestion
Suggestion importance[1-10]: 3

Why: While valid, this optimization would have minimal impact since the collection is only used once for template data and is unlikely to be large enough for the conversion to cause performance issues.

3

@nvborisenko
Copy link
Member

Does it make sense to spend time on something what will be deprecated? I don't think so.

@RenderMichael RenderMichael merged commit f33ad97 into SeleniumHQ:trunk Jan 14, 2025
10 checks passed
@RenderMichael RenderMichael deleted the dotnet-generator-modernize branch January 14, 2025 02:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants